refactor: allow resetting the main window id

This commit is contained in:
Ashley Wulber 2024-10-21 17:32:16 -04:00 committed by Ashley Wulber
parent b44fe2c0b6
commit cf3ba4ca07
5 changed files with 29 additions and 24 deletions

View file

@ -29,7 +29,7 @@ pub mod message {
impl crate::app::Core {
pub fn drag<M: Send + 'static>(&self, id: Option<window::Id>) -> iced::Task<Message<M>> {
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<window::Id>,
maximized: bool,
) -> iced::Task<Message<M>> {
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<M: Send + 'static>(&self, id: Option<window::Id>) -> iced::Task<Message<M>> {
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<window::Id>,
title: String,
) -> iced::Task<Message<M>> {
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<window::Id>,
) -> iced::Task<Message<M>> {
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<window::Id>,
) -> iced::Task<Message<M>> {
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)

View file

@ -94,7 +94,7 @@ pub struct Core {
#[cfg(feature = "dbus-config")]
pub(crate) settings_daemon: Option<cosmic_settings_daemon::CosmicSettingsDaemonProxy<'static>>,
pub(crate) main_window: OnceCell<window::Id>,
pub(crate) main_window: Option<window::Id>,
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<window::Id> {
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<window::Id> {
self.main_window.replace(id)
}
}

View file

@ -79,7 +79,7 @@ pub enum Message {
#[cfg(feature = "applet")]
SuggestedBounds(Option<iced::Size>),
/// 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<T: Application> Cosmic<T> {
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) => {

View file

@ -91,7 +91,7 @@ pub(crate) fn iced_settings<App: Application>(
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<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, flags) = iced_settings::<App>(settings, flags);
let (settings, mut flags) = iced_settings::<App>(settings, flags);
#[cfg(not(feature = "multi-window"))]
{
iced::application(
@ -159,9 +159,9 @@ pub fn run<App: Application>(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)

View file

@ -419,9 +419,9 @@ pub fn run<App: Application>(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)