From a1695a39976ed0ec7b6a926cf20792aacaf71c65 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Tue, 20 Aug 2024 14:15:01 +0200 Subject: [PATCH] Even more refactoring --- Linux/Cargo.lock | 22 ---- Linux/backend/Cargo.toml | 1 - Linux/backend/src/mainwindow.rs | 6 - Linux/ui-gtk/Cargo.toml | 3 +- .../src/devices/emulator_settings_ui.rs | 2 +- .../src/devices/serial_min_settings_ui.rs | 110 +++++++++++------- Linux/ui-gtk/src/mainwindow.rs | 28 ++--- Linux/ui-gtk/src/ui/uicomponent.rs | 10 +- 8 files changed, 86 insertions(+), 96 deletions(-) diff --git a/Linux/Cargo.lock b/Linux/Cargo.lock index d5f9bb5..ff3167f 100644 --- a/Linux/Cargo.lock +++ b/Linux/Cargo.lock @@ -732,7 +732,6 @@ dependencies = [ "serde", "serde_json", "serialport", - "tracker", "uuid", ] @@ -746,7 +745,6 @@ dependencies = [ "massiveknob_backend", "rust-i18n", "serialport", - "tracker", ] [[package]] @@ -1253,26 +1251,6 @@ dependencies = [ "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]] name = "triomphe" version = "0.1.12" diff --git a/Linux/backend/Cargo.toml b/Linux/backend/Cargo.toml index e48d3b5..02664ca 100644 --- a/Linux/backend/Cargo.toml +++ b/Linux/backend/Cargo.toml @@ -13,7 +13,6 @@ rust-i18n = "3.0.1" serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.117" serialport = { version = "4.4.0", default-features = false } -tracker = "0.2.2" [dependencies.uuid] version = "1.10.0" diff --git a/Linux/backend/src/mainwindow.rs b/Linux/backend/src/mainwindow.rs index b82847b..4b06819 100644 --- a/Linux/backend/src/mainwindow.rs +++ b/Linux/backend/src/mainwindow.rs @@ -7,16 +7,10 @@ use crate::registry::RegistryItem; use crate::ui::EmbeddedWidgetConnector; use crate::util::unique_id::UniqueId; -#[tracker::track] pub struct MainWindow { - #[do_not_track] orchestrator: Orchestrator, - - #[do_not_track] devices_sorted: Vec, - - #[no_eq] device_settings_widget: Option> } diff --git a/Linux/ui-gtk/Cargo.toml b/Linux/ui-gtk/Cargo.toml index 1d479de..093d067 100644 --- a/Linux/ui-gtk/Cargo.toml +++ b/Linux/ui-gtk/Cargo.toml @@ -10,7 +10,6 @@ gtk = { version = "0.9.0", package = "gtk4", features = ["v4_14"] } log = "0.4.21" rust-i18n = "3.0.1" serialport = { version = "4.4.0", default-features = false } -tracker = "0.2.2" [dependencies.massiveknob_backend] -path = "../backend" \ No newline at end of file +path = "../backend" diff --git a/Linux/ui-gtk/src/devices/emulator_settings_ui.rs b/Linux/ui-gtk/src/devices/emulator_settings_ui.rs index ced5591..b1ffeea 100644 --- a/Linux/ui-gtk/src/devices/emulator_settings_ui.rs +++ b/Linux/ui-gtk/src/devices/emulator_settings_ui.rs @@ -54,7 +54,7 @@ impl UiComponent for EmulatorSettingsUi impl UiComponentState for EmulatorSettingsUi { - fn new(_init: (), _widgets: EmulatorSettingsUiWidgets) -> Self + fn new(_init: EmulatorSettingsUiInit, _widgets: EmulatorSettingsUiWidgets) -> Self { Self { diff --git a/Linux/ui-gtk/src/devices/serial_min_settings_ui.rs b/Linux/ui-gtk/src/devices/serial_min_settings_ui.rs index 5990e78..b41119e 100644 --- a/Linux/ui-gtk/src/devices/serial_min_settings_ui.rs +++ b/Linux/ui-gtk/src/devices/serial_min_settings_ui.rs @@ -1,3 +1,7 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use gtk::glib; use gtk::glib::clone; use gtk::prelude::*; use gtk::StringList; @@ -6,14 +10,9 @@ use crate::ui::uicomponent::UiComponent; use crate::ui::uicomponent::UiComponentState; -#[tracker::track] pub struct SerialMinSettingsUi { - #[do_not_track] - ports: Vec, - - custom_port: String, - custom_port_visible: bool + ports: Vec } @@ -25,6 +24,7 @@ pub struct SerialMinSettingsUiInit pub struct SerialMinSettingsUiWidgets { + port_select: gtk::DropDown, custom_port_input: gtk::Entry } @@ -47,7 +47,7 @@ impl UiComponent for SerialMinSettingsUi .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() .label(t!("serial_min.settings.port.label")) @@ -57,33 +57,12 @@ impl UiComponent for SerialMinSettingsUi 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() - .model(&port_model) .build(); 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() .hexpand(true) .placeholder_text(t!("serial_min.settings.custom_port_placeholder")) @@ -92,34 +71,68 @@ impl UiComponent for SerialMinSettingsUi 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 { + port_select, custom_port_input } } - fn init(root: &Self::Root, state: &std::rc::Rc>) + fn init(_root: &Self::Root, widgets: &Rc, state: &Rc>) { + 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 for SerialMinSettingsUi { - fn new(_init: ::Init, _widgets: ::Widgets) -> Self + fn new(_init: SerialMinSettingsUiInit) -> Self { let ports_list = serialport::available_ports().unwrap_or_default(); let ports: Vec = ports_list.iter().map(|p| p.port_name.clone()).collect(); @@ -129,15 +142,22 @@ impl UiComponentState for SerialMinSettingsUi Self { - ports, - custom_port: String::default(), - custom_port_visible: false, - tracker: 0 + ports } } } +impl SerialMinSettingsUi +{ + fn set_port(&self, widgets: &Rc, 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); + } +} /* diff --git a/Linux/ui-gtk/src/mainwindow.rs b/Linux/ui-gtk/src/mainwindow.rs index 6435dcc..0516580 100644 --- a/Linux/ui-gtk/src/mainwindow.rs +++ b/Linux/ui-gtk/src/mainwindow.rs @@ -11,6 +11,7 @@ use massiveknob_backend::util::unique_id::UniqueId; use crate::devices::DeviceSettingsUiBuilder; use crate::ui::uicomponent::UiComponent; +use crate::ui::uicomponent::UiComponentConnectorWidget; use crate::ui::uicomponent::UiComponentState; @@ -26,9 +27,8 @@ pub struct MainWindow { orchestrator: Arc>, devices_sorted: Vec, - widgets: MainWindowWidgets, - device_settings_widget: Option + device_settings_widget: Option> } @@ -75,11 +75,11 @@ impl UiComponent for MainWindow } - fn init(_root: &Self::Root, state: &Rc>) + fn init(_root: &Self::Root, widgets: &Rc, state: &Rc>) { { 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 active_device_id = orchestrator.active_device_id(); @@ -106,23 +106,26 @@ impl UiComponent for MainWindow #[weak] state, + #[weak] + widgets, + move |_| { let mut state = state.borrow_mut(); - state.update_active_device(true); + state.update_active_device(&widgets, true); } )); } let mut state = state.borrow_mut(); - state.update_active_device(false); + state.update_active_device(&widgets, false); } } impl UiComponentState for MainWindow { - fn new(init: MainWindowInit, widgets: MainWindowWidgets) -> Self + fn new(init: MainWindowInit) -> Self { let mut devices_sorted: Vec; { @@ -144,7 +147,6 @@ impl UiComponentState for MainWindow { orchestrator: init.orchestrator.clone(), devices_sorted, - widgets, 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, set_active: bool) { - log::info!("update_active_device"); - - let active_index = self.widgets.device.devices_dropdown.selected(); + let active_index = widgets.device.devices_dropdown.selected(); if active_index == gtk::ffi::GTK_INVALID_LIST_POSITION { 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 { - self.widgets.device.settings_container.remove(prev_widget); + widgets.device.settings_container.remove(&prev_widget.root()); } if let Some(device) = device { 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); } else diff --git a/Linux/ui-gtk/src/ui/uicomponent.rs b/Linux/ui-gtk/src/ui/uicomponent.rs index fc1e3fb..5cd6baa 100644 --- a/Linux/ui-gtk/src/ui/uicomponent.rs +++ b/Linux/ui-gtk/src/ui/uicomponent.rs @@ -25,13 +25,13 @@ pub trait UiComponent : Sized fn build_root(init: &Self::Init) -> Self::Root; fn build_widgets(root: &Self::Root, init: &Self::Init) -> Self::Widgets; - fn init(root: &Self::Root, state: &Rc>); + fn init(root: &Self::Root, widgets: &Rc, state: &Rc>); } pub trait UiComponentState { - fn new(init: C::Init, widgets: C::Widgets) -> Self; + fn new(init: C::Init) -> Self; } @@ -71,10 +71,10 @@ impl UiComponentBuilder pub fn build(&self, init: C::Init) -> UiComponentConnector { let root = C::build_root(&init); - let widgets = C::build_widgets(&root, &init); - let state = Rc::new(RefCell::new(C::State::new(init, widgets))); + let widgets = Rc::new(C::build_widgets(&root, &init)); + let state = Rc::new(RefCell::new(C::State::new(init))); - C::init(&root, &state); + C::init(&root, &widgets, &state); UiComponentConnector::