From 3c5dcecf2b771fb307f9083fac5de416b6fc7a94 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 13 Feb 2024 10:34:13 -0700 Subject: [PATCH] Changes for multiple Application support - Add Application::main_window_id to allow ids other than MAIN - Make Cosmic and Cosmic::app public to allow custom use --- src/app/cosmic.rs | 28 ++++++++++++++-------------- src/app/mod.rs | 15 ++++++++++----- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/app/cosmic.rs b/src/app/cosmic.rs index 8749f42cdbe..3cb302bdad0 100644 --- a/src/app/cosmic.rs +++ b/src/app/cosmic.rs @@ -75,8 +75,8 @@ pub enum Message { } #[derive(Default)] -pub(crate) struct Cosmic { - pub(crate) app: App, +pub struct Cosmic { + pub app: App, } impl IcedApplication for Cosmic @@ -228,7 +228,7 @@ where #[cfg(any(feature = "multi-window", feature = "wayland"))] fn view(&self, id: window::Id) -> Element { - if id != window::Id::MAIN { + if id != self.app.main_window_id() { return self.app.view_window(id).map(super::Message::App); } @@ -248,26 +248,26 @@ where impl Cosmic { #[cfg(feature = "wayland")] pub fn close(&mut self) -> iced::Command> { - iced_sctk::commands::window::close_window(window::Id::MAIN) + iced_sctk::commands::window::close_window(self.app.main_window_id()) } #[cfg(not(feature = "wayland"))] #[allow(clippy::unused_self)] pub fn close(&mut self) -> iced::Command> { - iced::Command::single(Action::Window(WindowAction::Close(window::Id::MAIN))) + iced::Command::single(Action::Window(WindowAction::Close(self.app.main_window_id()))) } #[allow(clippy::too_many_lines)] fn cosmic_update(&mut self, message: Message) -> iced::Command> { match message { Message::WindowMaximized(id, maximized) => { - if window::Id::MAIN == id { + if self.app.main_window_id() == id { self.app.core_mut().window.sharp_corners = maximized; } } Message::WindowResize(id, width, height) => { - if window::Id::MAIN == id { + if self.app.main_window_id() == id { self.app.core_mut().set_window_width(width); self.app.core_mut().set_window_height(height); } @@ -283,7 +283,7 @@ impl Cosmic { #[cfg(feature = "wayland")] Message::WindowState(id, state) => { - if window::Id::MAIN == id { + if self.app.main_window_id() == id { self.app.core_mut().window.sharp_corners = state.intersects( WindowState::MAXIMIZED | WindowState::FULLSCREEN @@ -298,7 +298,7 @@ impl Cosmic { #[cfg(feature = "wayland")] Message::WmCapabilities(id, capabilities) => { - if window::Id::MAIN == id { + if self.app.main_window_id() == id { self.app.core_mut().window.show_maximize = capabilities.contains(WindowManagerCapabilities::MAXIMIZE); self.app.core_mut().window.show_minimize = @@ -321,7 +321,7 @@ impl Cosmic { keyboard_nav::Message::Escape => return self.app.on_escape(), keyboard_nav::Message::Search => return self.app.on_search(), - keyboard_nav::Message::Fullscreen => return command::toggle_maximize(None), + keyboard_nav::Message::Fullscreen => return command::toggle_maximize(Some(self.app.main_window_id())), }, Message::ContextDrawer(show) => { @@ -329,11 +329,11 @@ impl Cosmic { return self.app.on_context_drawer(); } - Message::Drag => return command::drag(None), + Message::Drag => return command::drag(Some(self.app.main_window_id())), - Message::Minimize => return command::minimize(None), + Message::Minimize => return command::minimize(Some(self.app.main_window_id())), - Message::Maximize => return command::toggle_maximize(None), + Message::Maximize => return command::toggle_maximize(Some(self.app.main_window_id())), Message::NavBar(key) => { self.app.core_mut().nav_bar_set_toggled_condensed(false); @@ -403,7 +403,7 @@ impl Cosmic { Message::Activate(_token) => { #[cfg(feature = "wayland")] return iced_sctk::commands::activation::activate( - iced::window::Id::MAIN, + self.app.main_window_id(), #[allow(clippy::used_underscore_binding)] _token, ); diff --git a/src/app/mod.rs b/src/app/mod.rs index b985bb2f789..747a6f9f667 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -433,6 +433,11 @@ where Vec::new() } + /// Get the main [`window::Id`], which is [`window::Id::MAIN`] by default + fn main_window_id(&self) -> window::Id { + window::Id::MAIN + } + /// Allows overriding the default nav bar widget fn nav_bar(&self) -> Option>> { if !self.core().nav_bar_active() { @@ -571,15 +576,15 @@ pub trait ApplicationExt: Application { impl ApplicationExt for App { fn drag(&mut self) -> iced::Command> { - command::drag(Some(window::Id::MAIN)) + command::drag(Some(self.main_window_id())) } fn maximize(&mut self) -> iced::Command> { - command::maximize(Some(window::Id::MAIN), true) + command::maximize(Some(self.main_window_id()), true) } fn minimize(&mut self) -> iced::Command> { - command::minimize(Some(window::Id::MAIN)) + command::minimize(Some(self.main_window_id())) } #[cfg(any(feature = "multi-window", feature = "wayland"))] @@ -606,7 +611,7 @@ impl ApplicationExt for App { fn set_window_title(&mut self, title: String) -> iced::Command> { self.core_mut() .title - .insert(window::Id::MAIN, title.clone()); + .insert(self.main_window_id(), title.clone()); iced::Command::none() } @@ -658,7 +663,7 @@ impl ApplicationExt for App { .push_maybe(if core.window.show_headerbar { Some({ let mut header = crate::widget::header_bar() - .window_id(window::Id::MAIN) + .window_id(self.main_window_id()) .title(&core.window.header_title) .on_drag(Message::Cosmic(cosmic::Message::Drag)) .on_close(Message::Cosmic(cosmic::Message::Close))