1
0
mirror of synced 2024-11-22 07:43:50 +00:00

Even more refactoring

This commit is contained in:
Mark van Renswoude 2024-08-20 14:15:01 +02:00
parent 19a039f98d
commit a1695a3997
8 changed files with 86 additions and 96 deletions

22
Linux/Cargo.lock generated
View File

@ -732,7 +732,6 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serialport", "serialport",
"tracker",
"uuid", "uuid",
] ]
@ -746,7 +745,6 @@ dependencies = [
"massiveknob_backend", "massiveknob_backend",
"rust-i18n", "rust-i18n",
"serialport", "serialport",
"tracker",
] ]
[[package]] [[package]]
@ -1253,26 +1251,6 @@ dependencies = [
"winnow 0.6.18", "winnow 0.6.18",
] ]
[[package]]
name = "tracker"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce5c98457ff700aaeefcd4a4a492096e78a2af1dd8523c66e94a3adb0fdbd415"
dependencies = [
"tracker-macros",
]
[[package]]
name = "tracker-macros"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc19eb2373ccf3d1999967c26c3d44534ff71ae5d8b9dacf78f4b13132229e48"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "triomphe" name = "triomphe"
version = "0.1.12" version = "0.1.12"

View File

@ -13,7 +13,6 @@ rust-i18n = "3.0.1"
serde = { version = "1.0.203", features = ["derive"] } serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117" serde_json = "1.0.117"
serialport = { version = "4.4.0", default-features = false } serialport = { version = "4.4.0", default-features = false }
tracker = "0.2.2"
[dependencies.uuid] [dependencies.uuid]
version = "1.10.0" version = "1.10.0"

View File

@ -7,16 +7,10 @@ use crate::registry::RegistryItem;
use crate::ui::EmbeddedWidgetConnector; use crate::ui::EmbeddedWidgetConnector;
use crate::util::unique_id::UniqueId; use crate::util::unique_id::UniqueId;
#[tracker::track]
pub struct MainWindow pub struct MainWindow
{ {
#[do_not_track]
orchestrator: Orchestrator, orchestrator: Orchestrator,
#[do_not_track]
devices_sorted: Vec<SortedDevice>, devices_sorted: Vec<SortedDevice>,
#[no_eq]
device_settings_widget: Option<Box<dyn EmbeddedWidgetConnector>> device_settings_widget: Option<Box<dyn EmbeddedWidgetConnector>>
} }

View File

@ -10,7 +10,6 @@ gtk = { version = "0.9.0", package = "gtk4", features = ["v4_14"] }
log = "0.4.21" log = "0.4.21"
rust-i18n = "3.0.1" rust-i18n = "3.0.1"
serialport = { version = "4.4.0", default-features = false } serialport = { version = "4.4.0", default-features = false }
tracker = "0.2.2"
[dependencies.massiveknob_backend] [dependencies.massiveknob_backend]
path = "../backend" path = "../backend"

View File

@ -54,7 +54,7 @@ impl UiComponent for EmulatorSettingsUi
impl UiComponentState<EmulatorSettingsUi> for EmulatorSettingsUi impl UiComponentState<EmulatorSettingsUi> for EmulatorSettingsUi
{ {
fn new(_init: (), _widgets: EmulatorSettingsUiWidgets) -> Self fn new(_init: EmulatorSettingsUiInit, _widgets: EmulatorSettingsUiWidgets) -> Self
{ {
Self Self
{ {

View File

@ -1,3 +1,7 @@
use std::cell::RefCell;
use std::rc::Rc;
use gtk::glib;
use gtk::glib::clone; use gtk::glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::StringList; use gtk::StringList;
@ -6,14 +10,9 @@ use crate::ui::uicomponent::UiComponent;
use crate::ui::uicomponent::UiComponentState; use crate::ui::uicomponent::UiComponentState;
#[tracker::track]
pub struct SerialMinSettingsUi pub struct SerialMinSettingsUi
{ {
#[do_not_track] ports: Vec<String>
ports: Vec<String>,
custom_port: String,
custom_port_visible: bool
} }
@ -25,6 +24,7 @@ pub struct SerialMinSettingsUiInit
pub struct SerialMinSettingsUiWidgets pub struct SerialMinSettingsUiWidgets
{ {
port_select: gtk::DropDown,
custom_port_input: gtk::Entry custom_port_input: gtk::Entry
} }
@ -47,7 +47,7 @@ impl UiComponent for SerialMinSettingsUi
.build() .build()
} }
fn build_widgets(root: &Self::Root, init: &Self::Init) -> Self::Widgets fn build_widgets(root: &Self::Root, _init: &Self::Init) -> Self::Widgets
{ {
let port_label = gtk::Label::builder() let port_label = gtk::Label::builder()
.label(t!("serial_min.settings.port.label")) .label(t!("serial_min.settings.port.label"))
@ -57,33 +57,12 @@ impl UiComponent for SerialMinSettingsUi
root.append(&port_label); root.append(&port_label);
let port_model_vec: Vec<&str> = ports.iter().map(|p| p.as_str()).collect();
let port_model = StringList::new(&port_model_vec);
port_model.append(t!("serial_min.settings.port.custom").as_ref());
let port_select = gtk::DropDown::builder() let port_select = gtk::DropDown::builder()
.model(&port_model)
.build(); .build();
root.append(&port_select); root.append(&port_select);
let port_select_cloned = port_select.clone();
port_select.connect_selected_notify(clone!(
move |_|
{
let active_index = port_select_cloned.selected();
if active_index == gtk::ffi::GTK_INVALID_LIST_POSITION { return };
if let Ok(active_index_usize) = usize::try_from(active_index)
{
//sender.input(SerialMinSettingsWidgetMessage::PortChanged(active_index_usize));
}
}));
let custom_port_input = gtk::Entry::builder() let custom_port_input = gtk::Entry::builder()
.hexpand(true) .hexpand(true)
.placeholder_text(t!("serial_min.settings.custom_port_placeholder")) .placeholder_text(t!("serial_min.settings.custom_port_placeholder"))
@ -92,34 +71,68 @@ impl UiComponent for SerialMinSettingsUi
root.append(&custom_port_input); root.append(&custom_port_input);
let custom_port_input_cloned = custom_port_input.clone();
/*
custom_port_input.connect_changed(clone!(
@strong sender => move |_|
{
sender.input(SerialMinSettingsWidgetMessage::CustomPortChanged(String::from(custom_port_input_cloned.text().as_str())));
}
));
*/
Self::Widgets Self::Widgets
{ {
port_select,
custom_port_input custom_port_input
} }
} }
fn init(root: &Self::Root, state: &std::rc::Rc<std::cell::RefCell<Self::State>>) fn init(_root: &Self::Root, widgets: &Rc<Self::Widgets>, state: &Rc<RefCell<Self::State>>)
{ {
let port_model;
{
let state_borrowed = state.borrow();
.selected(ports.len().try_into().unwrap_or(gtk::ffi::GTK_INVALID_LIST_POSITION)) let port_model_vec: Vec<&str> = state_borrowed.ports.iter().map(|p| p.as_str()).collect();
port_model = StringList::new(&port_model_vec);
port_model.append(t!("serial_min.settings.port.custom").as_ref());
widgets.port_select.set_model(Some(&port_model));
widgets.port_select.set_selected(state_borrowed.ports.len().try_into().unwrap_or(gtk::ffi::GTK_INVALID_LIST_POSITION));
}
widgets.port_select.connect_selected_notify(clone!(
#[weak]
state,
#[weak]
widgets,
move |_|
{
let active_index = widgets.port_select.selected();
if active_index == gtk::ffi::GTK_INVALID_LIST_POSITION { return };
let state = state.borrow();
state.set_port(&widgets, active_index);
}));
/*
widgets.custom_port_input.connect_changed(clone!(
#[weak]
state,
#[weak(rename_to = custom_port_input)]
widgets.custom_port_input,
move |_|
{
let mut state = state.borrow_mut();
state.set_custom_port(custom_port_input.text().into());
}
));
*/
} }
} }
impl UiComponentState<SerialMinSettingsUi> for SerialMinSettingsUi impl UiComponentState<SerialMinSettingsUi> for SerialMinSettingsUi
{ {
fn new(_init: <SerialMinSettingsUi as UiComponent>::Init, _widgets: <SerialMinSettingsUi as UiComponent>::Widgets) -> Self fn new(_init: SerialMinSettingsUiInit) -> Self
{ {
let ports_list = serialport::available_ports().unwrap_or_default(); let ports_list = serialport::available_ports().unwrap_or_default();
let ports: Vec<String> = ports_list.iter().map(|p| p.port_name.clone()).collect(); let ports: Vec<String> = ports_list.iter().map(|p| p.port_name.clone()).collect();
@ -129,15 +142,22 @@ impl UiComponentState<SerialMinSettingsUi> for SerialMinSettingsUi
Self Self
{ {
ports, ports
custom_port: String::default(),
custom_port_visible: false,
tracker: 0
} }
} }
} }
impl SerialMinSettingsUi
{
fn set_port(&self, widgets: &Rc<SerialMinSettingsUiWidgets>, index: u32)
{
let Ok(index_usize) = usize::try_from(index) else { return };
let custom_port_visible = index_usize == self.ports.len() - 1;
widgets.custom_port_input.set_visible(custom_port_visible);
}
}
/* /*

View File

@ -11,6 +11,7 @@ use massiveknob_backend::util::unique_id::UniqueId;
use crate::devices::DeviceSettingsUiBuilder; use crate::devices::DeviceSettingsUiBuilder;
use crate::ui::uicomponent::UiComponent; use crate::ui::uicomponent::UiComponent;
use crate::ui::uicomponent::UiComponentConnectorWidget;
use crate::ui::uicomponent::UiComponentState; use crate::ui::uicomponent::UiComponentState;
@ -26,9 +27,8 @@ pub struct MainWindow
{ {
orchestrator: Arc<Mutex<Orchestrator>>, orchestrator: Arc<Mutex<Orchestrator>>,
devices_sorted: Vec<SortedDevice>, devices_sorted: Vec<SortedDevice>,
widgets: MainWindowWidgets,
device_settings_widget: Option<gtk::Widget> device_settings_widget: Option<Box<dyn UiComponentConnectorWidget>>
} }
@ -75,11 +75,11 @@ impl UiComponent for MainWindow
} }
fn init(_root: &Self::Root, state: &Rc<RefCell<Self::State>>) fn init(_root: &Self::Root, widgets: &Rc<Self::Widgets>, state: &Rc<RefCell<Self::State>>)
{ {
{ {
let state_borrowed = state.borrow(); let state_borrowed = state.borrow();
let devices_dropdown = state_borrowed.widgets.device.devices_dropdown.clone(); let devices_dropdown = widgets.device.devices_dropdown.clone();
let orchestrator = state_borrowed.orchestrator.lock().unwrap(); let orchestrator = state_borrowed.orchestrator.lock().unwrap();
let active_device_id = orchestrator.active_device_id(); let active_device_id = orchestrator.active_device_id();
@ -106,23 +106,26 @@ impl UiComponent for MainWindow
#[weak] #[weak]
state, state,
#[weak]
widgets,
move |_| move |_|
{ {
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
state.update_active_device(true); state.update_active_device(&widgets, true);
} }
)); ));
} }
let mut state = state.borrow_mut(); let mut state = state.borrow_mut();
state.update_active_device(false); state.update_active_device(&widgets, false);
} }
} }
impl UiComponentState<MainWindow> for MainWindow impl UiComponentState<MainWindow> for MainWindow
{ {
fn new(init: MainWindowInit, widgets: MainWindowWidgets) -> Self fn new(init: MainWindowInit) -> Self
{ {
let mut devices_sorted: Vec<SortedDevice>; let mut devices_sorted: Vec<SortedDevice>;
{ {
@ -144,7 +147,6 @@ impl UiComponentState<MainWindow> for MainWindow
{ {
orchestrator: init.orchestrator.clone(), orchestrator: init.orchestrator.clone(),
devices_sorted, devices_sorted,
widgets,
device_settings_widget: None device_settings_widget: None
} }
@ -204,11 +206,9 @@ impl MainWindow
} }
fn update_active_device(&mut self, set_active: bool) fn update_active_device(&mut self, widgets: &Rc<MainWindowWidgets>, set_active: bool)
{ {
log::info!("update_active_device"); let active_index = widgets.device.devices_dropdown.selected();
let active_index = self.widgets.device.devices_dropdown.selected();
if active_index == gtk::ffi::GTK_INVALID_LIST_POSITION { return }; if active_index == gtk::ffi::GTK_INVALID_LIST_POSITION { return };
let Ok(active_index_usize) = usize::try_from(active_index) else { return }; let Ok(active_index_usize) = usize::try_from(active_index) else { return };
@ -230,14 +230,14 @@ impl MainWindow
if let Some(prev_widget) = &self.device_settings_widget if let Some(prev_widget) = &self.device_settings_widget
{ {
self.widgets.device.settings_container.remove(prev_widget); widgets.device.settings_container.remove(&prev_widget.root());
} }
if let Some(device) = device if let Some(device) = device
{ {
let widget = DeviceSettingsUiBuilder::build(device.clone()); let widget = DeviceSettingsUiBuilder::build(device.clone());
self.widgets.device.settings_container.append(&widget); widgets.device.settings_container.append(&widget.root());
self.device_settings_widget = Some(widget); self.device_settings_widget = Some(widget);
} }
else else

View File

@ -25,13 +25,13 @@ pub trait UiComponent : Sized
fn build_root(init: &Self::Init) -> Self::Root; fn build_root(init: &Self::Init) -> Self::Root;
fn build_widgets(root: &Self::Root, init: &Self::Init) -> Self::Widgets; fn build_widgets(root: &Self::Root, init: &Self::Init) -> Self::Widgets;
fn init(root: &Self::Root, state: &Rc<RefCell<Self::State>>); fn init(root: &Self::Root, widgets: &Rc<Self::Widgets>, state: &Rc<RefCell<Self::State>>);
} }
pub trait UiComponentState<C: UiComponent> pub trait UiComponentState<C: UiComponent>
{ {
fn new(init: C::Init, widgets: C::Widgets) -> Self; fn new(init: C::Init) -> Self;
} }
@ -71,10 +71,10 @@ impl<C: UiComponent> UiComponentBuilder<C>
pub fn build(&self, init: C::Init) -> UiComponentConnector<C> pub fn build(&self, init: C::Init) -> UiComponentConnector<C>
{ {
let root = C::build_root(&init); let root = C::build_root(&init);
let widgets = C::build_widgets(&root, &init); let widgets = Rc::new(C::build_widgets(&root, &init));
let state = Rc::new(RefCell::new(C::State::new(init, widgets))); let state = Rc::new(RefCell::new(C::State::new(init)));
C::init(&root, &state); C::init(&root, &widgets, &state);
UiComponentConnector::<C> UiComponentConnector::<C>