From cf3ba4ca078bcc5238360a2ff448f80b482f451a Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 21 Oct 2024 17:32:16 -0400 Subject: [PATCH] refactor: allow resetting the main window id --- src/app/command.rs | 12 ++++++------ src/app/core.rs | 14 ++++++++------ src/app/cosmic.rs | 11 +++++++---- src/app/mod.rs | 12 ++++++------ src/applet/mod.rs | 4 ++-- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/app/command.rs b/src/app/command.rs index 5fd77fce..ff51a7d3 100644 --- a/src/app/command.rs +++ b/src/app/command.rs @@ -29,7 +29,7 @@ pub mod message { impl crate::app::Core { pub fn drag(&self, id: Option) -> iced::Task> { - let Some(id) = id.or(self.main_window.get().cloned()) else { + let Some(id) = id.or(self.main_window) else { return iced::Task::none(); }; crate::command::drag(id).map(Message::Cosmic) @@ -40,14 +40,14 @@ impl crate::app::Core { id: Option, maximized: bool, ) -> iced::Task> { - let Some(id) = id.or(self.main_window.get().cloned()) else { + let Some(id) = id.or(self.main_window) else { return iced::Task::none(); }; crate::command::maximize(id, maximized).map(Message::Cosmic) } pub fn minimize(&self, id: Option) -> iced::Task> { - let Some(id) = id.or(self.main_window.get().cloned()) else { + let Some(id) = id.or(self.main_window) else { return iced::Task::none(); }; crate::command::minimize(id).map(Message::Cosmic) @@ -62,7 +62,7 @@ impl crate::app::Core { id: Option, title: String, ) -> iced::Task> { - let Some(id) = id.or(self.main_window.get().cloned()) else { + let Some(id) = id.or(self.main_window) else { return iced::Task::none(); }; crate::command::set_title(id, title).map(Message::Cosmic) @@ -72,7 +72,7 @@ impl crate::app::Core { &self, id: Option, ) -> iced::Task> { - let Some(id) = id.or(self.main_window.get().cloned()) else { + let Some(id) = id.or(self.main_window) else { return iced::Task::none(); }; crate::command::set_windowed(id).map(Message::Cosmic) @@ -82,7 +82,7 @@ impl crate::app::Core { &self, id: Option, ) -> iced::Task> { - let Some(id) = id.or(self.main_window.get().cloned()) else { + let Some(id) = id.or(self.main_window) else { return iced::Task::none(); }; crate::command::toggle_maximize(id).map(Message::Cosmic) diff --git a/src/app/core.rs b/src/app/core.rs index f1e63d72..8ffe8ecd 100644 --- a/src/app/core.rs +++ b/src/app/core.rs @@ -94,7 +94,7 @@ pub struct Core { #[cfg(feature = "dbus-config")] pub(crate) settings_daemon: Option>, - pub(crate) main_window: OnceCell, + pub(crate) main_window: Option, pub(crate) exit_on_main_window_closed: bool, } @@ -152,7 +152,7 @@ impl Default for Core { portal_is_dark: None, portal_accent: None, portal_is_high_contrast: None, - main_window: OnceCell::new(), + main_window: None, exit_on_main_window_closed: true, } } @@ -373,9 +373,11 @@ impl Core { /// The [`Id`] of the main window pub fn main_window_id(&self) -> Option { - self.main_window - .get() - .filter(|id| iced::window::Id::NONE != **id) - .cloned() + self.main_window.filter(|id| iced::window::Id::NONE != *id) + } + + /// Reset the tracked main window to a new value + pub fn set_main_window_id(&mut self, id: window::Id) -> Option { + self.main_window.replace(id) } } diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index 295a0050..14dc96d0 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -79,7 +79,7 @@ pub enum Message { #[cfg(feature = "applet")] SuggestedBounds(Option), /// Window Created - MainWindowCreated(window::Id), + WindowCreated(window::Id), } #[derive(Default)] @@ -167,7 +167,7 @@ where iced::Event::Window(window::Event::Focused) => return Some(Message::Focus(id)), iced::Event::Window(window::Event::Unfocused) => return Some(Message::Unfocus(id)), iced::Event::Window(window::Event::Opened { .. }) => { - return Some(Message::MainWindowCreated(id)); + return Some(Message::WindowCreated(id)); } #[cfg(feature = "wayland")] iced::Event::PlatformSpecific(iced::event::PlatformSpecific::Wayland(event)) => { @@ -660,9 +660,12 @@ impl Cosmic { core.focused_window = None; } } - Message::MainWindowCreated(id) => { + Message::WindowCreated(id) => { let core = self.app.core_mut(); - _ = core.main_window.set(id); + // Set the main window if it was not set to something else + if core.main_window.is_none() { + core.main_window = Some(id); + } } #[cfg(feature = "applet")] Message::SuggestedBounds(b) => { diff --git a/src/app/mod.rs b/src/app/mod.rs index 2a7483fb..1cf419d2 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -91,7 +91,7 @@ pub(crate) fn iced_settings( THEME.lock().unwrap().set_theme(settings.theme.theme_type); if settings.no_main_window { - core.main_window.set(iced::window::Id::NONE).unwrap(); + core.main_window = Some(iced::window::Id::NONE); } let mut iced = iced::Settings::default(); @@ -136,7 +136,7 @@ pub(crate) fn iced_settings( /// Returns error on application failure. pub fn run(settings: Settings, flags: App::Flags) -> iced::Result { let default_font = settings.default_font; - let (settings, flags) = iced_settings::(settings, flags); + let (settings, mut flags) = iced_settings::(settings, flags); #[cfg(not(feature = "multi-window"))] { iced::application( @@ -159,9 +159,9 @@ pub fn run(settings: Settings, flags: App::Flags) -> iced::Res cosmic::Cosmic::update, cosmic::Cosmic::view, ); - if flags.0.main_window.get().is_none() { + if flags.0.main_window.is_none() { app = app.window(flags.2.clone()); - _ = flags.0.main_window.set(iced_core::window::Id::RESERVED); + flags.0.main_window = Some(iced_core::window::Id::RESERVED); } app.subscription(cosmic::Cosmic::subscription) .style(cosmic::Cosmic::style) @@ -395,9 +395,9 @@ where cosmic::Cosmic::update, cosmic::Cosmic::view, ); - if flags.0.main_window.get().is_none() { + if flags.0.main_window.is_none() { app = app.window(flags.2.clone()); - _ = flags.0.main_window.set(iced_core::window::Id::RESERVED); + _ = flags.0.main_window = Some(iced_core::window::Id::RESERVED); } app.subscription(cosmic::Cosmic::subscription) .style(cosmic::Cosmic::style) diff --git a/src/applet/mod.rs b/src/applet/mod.rs index 67054382..bff90489 100644 --- a/src/applet/mod.rs +++ b/src/applet/mod.rs @@ -419,9 +419,9 @@ pub fn run(flags: App::Flags) -> iced::Result { cosmic::Cosmic::update, cosmic::Cosmic::view, ); - if core.main_window.get().is_none() { + if core.main_window.is_none() { app = app.window(window_settings.clone()); - _ = core.main_window.set(iced_core::window::Id::RESERVED); + core.main_window = Some(iced_core::window::Id::RESERVED); } app.subscription(cosmic::Cosmic::subscription) .style(cosmic::Cosmic::style)