From 0840e2c9c67453bcc30e90f76c254910a80924d8 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Tue, 27 Aug 2024 10:03:57 +0200 Subject: [PATCH] Moved away from EmbeddedWidgetConnector Fixed lifetime management of mainwindow connector --- Linux/Cargo.lock | 36 +++++++------- Linux/ui-gtk/Cargo.toml | 2 +- .../src/devices/emulator_settings_ui.rs | 47 +++---------------- Linux/ui-gtk/src/devices/mod.rs | 4 +- .../src/devices/serial_min_settings_ui.rs | 2 +- Linux/ui-gtk/src/main.rs | 38 ++++++++++----- Linux/ui-gtk/src/mainwindow.rs | 2 +- Linux/ui-gtk/src/ui/mod.rs | 9 +--- Linux/ui-gtk/src/ui/uicomponent.rs | 19 +++++++- 9 files changed, 72 insertions(+), 87 deletions(-) diff --git a/Linux/Cargo.lock b/Linux/Cargo.lock index ff3167f..8bc721a 100644 --- a/Linux/Cargo.lock +++ b/Linux/Cargo.lock @@ -13,9 +13,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.14" +version = "0.6.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" dependencies = [ "anstyle", "anstyle-parse", @@ -28,33 +28,33 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" [[package]] name = "anstyle-parse" -version = "0.2.4" +version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" dependencies = [ "utf8parse", ] [[package]] name = "anstyle-query" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" dependencies = [ "windows-sys", ] [[package]] name = "anstyle-wincon" -version = "3.0.3" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" dependencies = [ "anstyle", "windows-sys", @@ -147,9 +147,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "colorchoice" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" [[package]] name = "core-foundation-sys" @@ -205,9 +205,9 @@ dependencies = [ [[package]] name = "env_filter" -version = "0.1.0" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" dependencies = [ "log", "regex", @@ -215,9 +215,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.11.3" +version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" dependencies = [ "anstream", "anstyle", @@ -666,9 +666,9 @@ dependencies = [ [[package]] name = "is_terminal_polyfill" -version = "1.70.0" +version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "itoa" diff --git a/Linux/ui-gtk/Cargo.toml b/Linux/ui-gtk/Cargo.toml index 093d067..d275d9d 100644 --- a/Linux/ui-gtk/Cargo.toml +++ b/Linux/ui-gtk/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" build = "build.rs" [dependencies] -env_logger = "0.11.3" +env_logger = "0.11.5" gtk = { version = "0.9.0", package = "gtk4", features = ["v4_14"] } log = "0.4.21" rust-i18n = "3.0.1" diff --git a/Linux/ui-gtk/src/devices/emulator_settings_ui.rs b/Linux/ui-gtk/src/devices/emulator_settings_ui.rs index b1ffeea..a9149fe 100644 --- a/Linux/ui-gtk/src/devices/emulator_settings_ui.rs +++ b/Linux/ui-gtk/src/devices/emulator_settings_ui.rs @@ -1,3 +1,6 @@ +use std::cell::RefCell; +use std::rc::Rc; + use massiveknob_backend::orchestrator::DeviceReference; use crate::ui::uicomponent::UiComponent; @@ -46,7 +49,7 @@ impl UiComponent for EmulatorSettingsUi } } - fn init(_root: &Self::Root, _state: &std::rc::Rc>) + fn init(_root: &Self::Root, _widgets: &Rc, _state: &Rc>) { } } @@ -54,48 +57,10 @@ impl UiComponent for EmulatorSettingsUi impl UiComponentState for EmulatorSettingsUi { - fn new(_init: EmulatorSettingsUiInit, _widgets: EmulatorSettingsUiWidgets) -> Self + fn new(_init: EmulatorSettingsUiInit) -> Self { Self { } } -} - - -/* -#[relm4::component(pub)] -impl SimpleComponent for EmulatorWindow -{ - type Init = (); - type Input = EmulatorWindowMessage; - type Output = (); - - view! - { - gtk::Window - { - set_title: Some(&t!("emulatorwindow.title")), - set_default_size: (300, 500) - } - } - - - fn init(_data: Self::Init, root: Self::Root, _sender: ComponentSender, ) -> ComponentParts - { - let model = EmulatorWindow {}; - let widgets = view_output!(); - - root.set_visible(true); - ComponentParts { model, widgets } - } - - - fn update(&mut self, msg: Self::Input, _sender: ComponentSender) - { - match msg - { - } - } -} - */ \ No newline at end of file +} \ No newline at end of file diff --git a/Linux/ui-gtk/src/devices/mod.rs b/Linux/ui-gtk/src/devices/mod.rs index 3add4dd..5cc5635 100644 --- a/Linux/ui-gtk/src/devices/mod.rs +++ b/Linux/ui-gtk/src/devices/mod.rs @@ -23,8 +23,8 @@ impl DeviceSettingsUiBuilder { match device.as_ref() { - Device::Emulator(_) => Box::new(EmulatorSettingsUi::builder().build(EmulatorSettingsUiInit { device })), - Device::SerialMin(_) => Box::new(SerialMinSettingsUi::builder().build(SerialMinSettingsUiInit { device })) + Device::Emulator(_) => Box::new(EmulatorSettingsUi::builder().build("EmulatorSettingsUi", EmulatorSettingsUiInit { device })), + Device::SerialMin(_) => Box::new(SerialMinSettingsUi::builder().build("SerialMinSettingsUi", SerialMinSettingsUiInit { device })) } } } \ No newline at end of file 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 b41119e..89b61df 100644 --- a/Linux/ui-gtk/src/devices/serial_min_settings_ui.rs +++ b/Linux/ui-gtk/src/devices/serial_min_settings_ui.rs @@ -153,7 +153,7 @@ 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; + let custom_port_visible = index_usize == self.ports.len(); widgets.custom_port_input.set_visible(custom_port_visible); } diff --git a/Linux/ui-gtk/src/main.rs b/Linux/ui-gtk/src/main.rs index d9b4a72..361ab1d 100644 --- a/Linux/ui-gtk/src/main.rs +++ b/Linux/ui-gtk/src/main.rs @@ -1,3 +1,5 @@ +use std::cell::RefCell; +use std::rc::Rc; use std::sync::Arc; use std::sync::Mutex; @@ -8,6 +10,7 @@ use mainwindow::MainWindow; use mainwindow::MainWindowInit; use massiveknob_backend::orchestrator::Orchestrator; use ui::uicomponent::UiComponent; +use ui::uicomponent::UiComponentConnector; const APP_ID: &str = "com.github.mvrens.massiveknob"; @@ -24,6 +27,7 @@ pub mod devices; pub mod mainwindow; + fn main() -> glib::ExitCode { env_logger::Builder::from_env(Env::default().default_filter_or("info")) @@ -31,23 +35,31 @@ fn main() -> glib::ExitCode .init(); + let mainwindow: Rc>>> = Rc::new(RefCell::new(None)); + + let app = gtk::Application::builder() .application_id(APP_ID) .build(); - app.connect_activate(activate); - app.run() -} - - -fn activate(app: >k::Application) -{ - let orchestrator = Arc::new(Mutex::new(Orchestrator::new())); - let mainwindow = MainWindow::builder().build(MainWindowInit { - app: app.clone(), - orchestrator: orchestrator.clone() - }); + let mainwindow = mainwindow.clone(); + app.connect_activate(move |app| + { + let orchestrator = Arc::new(Mutex::new(Orchestrator::new())); + let newmainwindow = MainWindow::builder().build("MainWindow", MainWindowInit + { + app: app.clone(), + orchestrator: orchestrator.clone() + }); - mainwindow.root.present(); + newmainwindow.root.present(); + mainwindow.borrow_mut().replace(newmainwindow); + }); + } + + let result = app.run(); + + log::debug!("So long and thanks for all the fish!"); + result } \ No newline at end of file diff --git a/Linux/ui-gtk/src/mainwindow.rs b/Linux/ui-gtk/src/mainwindow.rs index 0516580..c3d7519 100644 --- a/Linux/ui-gtk/src/mainwindow.rs +++ b/Linux/ui-gtk/src/mainwindow.rs @@ -118,7 +118,7 @@ impl UiComponent for MainWindow } let mut state = state.borrow_mut(); - state.update_active_device(&widgets, false); + state.update_active_device(widgets, false); } } diff --git a/Linux/ui-gtk/src/ui/mod.rs b/Linux/ui-gtk/src/ui/mod.rs index 39ff711..7018051 100644 --- a/Linux/ui-gtk/src/ui/mod.rs +++ b/Linux/ui-gtk/src/ui/mod.rs @@ -1,8 +1 @@ -pub mod uicomponent; - - -#[deprecated] -pub trait EmbeddedWidgetConnector -{ - fn root(&self) -> gtk::Widget; -} \ No newline at end of file +pub mod uicomponent; \ No newline at end of file diff --git a/Linux/ui-gtk/src/ui/uicomponent.rs b/Linux/ui-gtk/src/ui/uicomponent.rs index 5cd6baa..f71ad4a 100644 --- a/Linux/ui-gtk/src/ui/uicomponent.rs +++ b/Linux/ui-gtk/src/ui/uicomponent.rs @@ -35,9 +35,12 @@ pub trait UiComponentState } +#[must_use] pub struct UiComponentConnector { + pub name: String, pub root: C::Root, + pub widgets: Rc, pub state: Rc> } @@ -68,7 +71,7 @@ impl Default for UiComponentBuilder impl UiComponentBuilder { - pub fn build(&self, init: C::Init) -> UiComponentConnector + pub fn build(&self, name: &str, init: C::Init) -> UiComponentConnector { let root = C::build_root(&init); let widgets = Rc::new(C::build_widgets(&root, &init)); @@ -79,7 +82,9 @@ impl UiComponentBuilder UiComponentConnector:: { + name: String::from(name), root, + widgets, state } } @@ -90,7 +95,7 @@ impl From> for gtk::Widget { fn from(val: UiComponentConnector) -> Self { - val.root.into() + val.root.clone().into() } } @@ -101,4 +106,14 @@ impl UiComponentConnectorWidget for UiComponentConnector { self.root.clone().into() } +} + + +impl Drop for UiComponentConnector +{ + fn drop(&mut self) + { + self.root.unrealize(); + log::debug!("Dropped UiComponentConnector for {}", self.name.as_str()); + } } \ No newline at end of file