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
#[must_use]
pub fn main_window_id(&self) -> Option<window::Id> {
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<window::Id> {
self.main_window.replace(id)
pub fn set_main_window_id(&mut self, mut id: Option<window::Id>) -> Option<window::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.
#[cfg(feature = "applet")]
SuggestedBounds(Option<iced::Size>),
/// Window Created
WindowCreated(window::Id),
}
#[derive(Default)]
@ -92,7 +90,7 @@ where
T::Message: Send + 'static,
{
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>>) {
#[cfg(feature = "dbus-config")]
{
@ -100,7 +98,7 @@ where
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)
}
@ -166,9 +164,6 @@ 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::WindowCreated(id));
}
#[cfg(feature = "wayland")]
iced::Event::PlatformSpecific(iced::event::PlatformSpecific::Wayland(event)) => {
match event {
@ -660,13 +655,6 @@ impl<T: Application> Cosmic<T> {
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")]
Message::SuggestedBounds(b) => {
tracing::info!("Suggested bounds: {b:?}");

View file

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

View file

@ -399,7 +399,7 @@ pub fn run<App: Application>(flags: App::Flags) -> iced::Result {
.unwrap()
.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);
core.window.show_headerbar = false;
core.window.sharp_corners = true;
@ -427,7 +427,7 @@ pub fn run<App: Application>(flags: App::Flags) -> iced::Result {
.style(cosmic::Cosmic::style)
.theme(cosmic::Cosmic::theme)
.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]