refactor: allow resetting main window id to None

This commit is contained in:
Ashley Wulber 2024-10-21 18:25:25 -04:00 committed by Jeremy Soller
parent 953685a882
commit 07763aca8e
4 changed files with 17 additions and 27 deletions

View file

@ -372,12 +372,14 @@ impl Core {
} }
/// The [`Id`] of the main window /// The [`Id`] of the main window
#[must_use]
pub fn main_window_id(&self) -> Option<window::Id> { pub fn main_window_id(&self) -> Option<window::Id> {
self.main_window.filter(|id| iced::window::Id::NONE != *id) self.main_window.filter(|id| iced::window::Id::NONE != *id)
} }
/// Reset the tracked main window to a new value /// Reset the tracked main window to a new value
pub fn set_main_window_id(&mut self, id: window::Id) -> Option<window::Id> { pub fn set_main_window_id(&mut self, mut id: Option<window::Id>) -> Option<window::Id> {
self.main_window.replace(id) std::mem::swap(&mut self.main_window, &mut id);
id
} }
} }

View file

@ -78,8 +78,6 @@ pub enum Message {
/// Tracks updates to window suggested size. /// Tracks updates to window suggested size.
#[cfg(feature = "applet")] #[cfg(feature = "applet")]
SuggestedBounds(Option<iced::Size>), SuggestedBounds(Option<iced::Size>),
/// Window Created
WindowCreated(window::Id),
} }
#[derive(Default)] #[derive(Default)]
@ -92,7 +90,7 @@ where
T::Message: Send + 'static, T::Message: Send + 'static,
{ {
pub fn init( pub fn init(
(mut core, flags, window_settings): (Core, T::Flags, iced::window::Settings), (mut core, flags): (Core, T::Flags),
) -> (Self, iced::Task<super::Message<T::Message>>) { ) -> (Self, iced::Task<super::Message<T::Message>>) {
#[cfg(feature = "dbus-config")] #[cfg(feature = "dbus-config")]
{ {
@ -100,7 +98,7 @@ where
core.settings_daemon = block_on(cosmic_config::dbus::settings_daemon_proxy()).ok(); core.settings_daemon = block_on(cosmic_config::dbus::settings_daemon_proxy()).ok();
} }
let (model, mut command) = T::init(core, flags); let (model, command) = T::init(core, flags);
(Self::new(model), command) (Self::new(model), command)
} }
@ -166,9 +164,6 @@ where
} }
iced::Event::Window(window::Event::Focused) => return Some(Message::Focus(id)), 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::Unfocused) => return Some(Message::Unfocus(id)),
iced::Event::Window(window::Event::Opened { .. }) => {
return Some(Message::WindowCreated(id));
}
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
iced::Event::PlatformSpecific(iced::event::PlatformSpecific::Wayland(event)) => { iced::Event::PlatformSpecific(iced::event::PlatformSpecific::Wayland(event)) => {
match event { match event {
@ -660,13 +655,6 @@ impl<T: Application> Cosmic<T> {
core.focused_window = None; core.focused_window = None;
} }
} }
Message::WindowCreated(id) => {
let core = self.app.core_mut();
// 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")] #[cfg(feature = "applet")]
Message::SuggestedBounds(b) => { Message::SuggestedBounds(b) => {
tracing::info!("Suggested bounds: {b:?}"); tracing::info!("Suggested bounds: {b:?}");

View file

@ -72,7 +72,7 @@ use {
pub(crate) fn iced_settings<App: Application>( pub(crate) fn iced_settings<App: Application>(
settings: Settings, settings: Settings,
flags: App::Flags, flags: App::Flags,
) -> (iced::Settings, (Core, App::Flags, iced::window::Settings)) { ) -> (iced::Settings, (Core, App::Flags), iced::window::Settings) {
preload_fonts(); preload_fonts();
let mut core = Core::default(); let mut core = Core::default();
@ -126,7 +126,7 @@ pub(crate) fn iced_settings<App: Application>(
} }
window_settings.transparent = settings.transparent; window_settings.transparent = settings.transparent;
(iced, (core, flags, window_settings)) (iced, (core, flags), window_settings)
} }
/// Launch a COSMIC application with the given [`Settings`]. /// Launch a COSMIC application with the given [`Settings`].
@ -136,7 +136,7 @@ pub(crate) fn iced_settings<App: Application>(
/// Returns error on application failure. /// Returns error on application failure.
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result { pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result {
let default_font = settings.default_font; let default_font = settings.default_font;
let (settings, mut flags) = iced_settings::<App>(settings, flags); let (settings, mut flags, window_settings) = iced_settings::<App>(settings, flags);
#[cfg(not(feature = "multi-window"))] #[cfg(not(feature = "multi-window"))]
{ {
iced::application( iced::application(
@ -149,7 +149,7 @@ pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Res
.theme(cosmic::Cosmic::theme) .theme(cosmic::Cosmic::theme)
.window_size((500.0, 800.0)) .window_size((500.0, 800.0))
.settings(settings) .settings(settings)
.window(flags.2.clone()) .window(window_settings)
.run_with(move || cosmic::Cosmic::<App>::init(flags)) .run_with(move || cosmic::Cosmic::<App>::init(flags))
} }
#[cfg(feature = "multi-window")] #[cfg(feature = "multi-window")]
@ -160,7 +160,7 @@ pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Res
cosmic::Cosmic::view, cosmic::Cosmic::view,
); );
if flags.0.main_window.is_none() { if flags.0.main_window.is_none() {
app = app.window(flags.2.clone()); app = app.window(window_settings);
flags.0.main_window = Some(iced_core::window::Id::RESERVED); flags.0.main_window = Some(iced_core::window::Id::RESERVED);
} }
app.subscription(cosmic::Cosmic::subscription) app.subscription(cosmic::Cosmic::subscription)
@ -370,7 +370,7 @@ where
tracing::info!("Another instance is running"); tracing::info!("Another instance is running");
Ok(()) Ok(())
} else { } else {
let (settings, mut flags) = iced_settings::<App>(settings, flags); let (settings, mut flags, window_settings) = iced_settings::<App>(settings, flags);
flags.0.single_instance = true; flags.0.single_instance = true;
#[cfg(not(feature = "multi-window"))] #[cfg(not(feature = "multi-window"))]
@ -385,7 +385,7 @@ where
.theme(cosmic::Cosmic::theme) .theme(cosmic::Cosmic::theme)
.window_size((500.0, 800.0)) .window_size((500.0, 800.0))
.settings(settings) .settings(settings)
.window(flags.2.clone()) .window(window_settings)
.run_with(move || cosmic::Cosmic::<App>::init(flags)) .run_with(move || cosmic::Cosmic::<App>::init(flags))
} }
#[cfg(feature = "multi-window")] #[cfg(feature = "multi-window")]
@ -396,8 +396,8 @@ where
cosmic::Cosmic::view, cosmic::Cosmic::view,
); );
if flags.0.main_window.is_none() { if flags.0.main_window.is_none() {
app = app.window(flags.2.clone()); app = app.window(window_settings);
_ = flags.0.main_window = Some(iced_core::window::Id::RESERVED); flags.0.main_window = Some(iced_core::window::Id::RESERVED);
} }
app.subscription(cosmic::Cosmic::subscription) app.subscription(cosmic::Cosmic::subscription)
.style(cosmic::Cosmic::style) .style(cosmic::Cosmic::style)

View file

@ -399,7 +399,7 @@ pub fn run<App: Application>(flags: App::Flags) -> iced::Result {
.unwrap() .unwrap()
.set_theme(settings.theme.theme_type.clone()); .set_theme(settings.theme.theme_type.clone());
let (iced_settings, (mut core, flags, mut window_settings)) = let (iced_settings, (mut core, flags), mut window_settings) =
iced_settings::<App>(settings, flags); iced_settings::<App>(settings, flags);
core.window.show_headerbar = false; core.window.show_headerbar = false;
core.window.sharp_corners = true; core.window.sharp_corners = true;
@ -427,7 +427,7 @@ pub fn run<App: Application>(flags: App::Flags) -> iced::Result {
.style(cosmic::Cosmic::style) .style(cosmic::Cosmic::style)
.theme(cosmic::Cosmic::theme) .theme(cosmic::Cosmic::theme)
.settings(iced_settings) .settings(iced_settings)
.run_with(move || cosmic::Cosmic::<App>::init((core, flags, window_settings))) .run_with(move || cosmic::Cosmic::<App>::init((core, flags)))
} }
#[must_use] #[must_use]