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
This commit is contained in:
Jeremy Soller 2024-02-13 10:34:13 -07:00
parent cc8033d74b
commit 3c5dcecf2b
2 changed files with 24 additions and 19 deletions

View file

@ -75,8 +75,8 @@ pub enum Message {
} }
#[derive(Default)] #[derive(Default)]
pub(crate) struct Cosmic<App> { pub struct Cosmic<App> {
pub(crate) app: App, pub app: App,
} }
impl<T: Application> IcedApplication for Cosmic<T> impl<T: Application> IcedApplication for Cosmic<T>
@ -228,7 +228,7 @@ where
#[cfg(any(feature = "multi-window", feature = "wayland"))] #[cfg(any(feature = "multi-window", feature = "wayland"))]
fn view(&self, id: window::Id) -> Element<Self::Message> { fn view(&self, id: window::Id) -> Element<Self::Message> {
if id != window::Id::MAIN { if id != self.app.main_window_id() {
return self.app.view_window(id).map(super::Message::App); return self.app.view_window(id).map(super::Message::App);
} }
@ -248,26 +248,26 @@ where
impl<T: Application> Cosmic<T> { impl<T: Application> Cosmic<T> {
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
pub fn close(&mut self) -> iced::Command<super::Message<T::Message>> { pub fn close(&mut self) -> iced::Command<super::Message<T::Message>> {
iced_sctk::commands::window::close_window(window::Id::MAIN) iced_sctk::commands::window::close_window(self.app.main_window_id())
} }
#[cfg(not(feature = "wayland"))] #[cfg(not(feature = "wayland"))]
#[allow(clippy::unused_self)] #[allow(clippy::unused_self)]
pub fn close(&mut self) -> iced::Command<super::Message<T::Message>> { pub fn close(&mut self) -> iced::Command<super::Message<T::Message>> {
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)] #[allow(clippy::too_many_lines)]
fn cosmic_update(&mut self, message: Message) -> iced::Command<super::Message<T::Message>> { fn cosmic_update(&mut self, message: Message) -> iced::Command<super::Message<T::Message>> {
match message { match message {
Message::WindowMaximized(id, maximized) => { Message::WindowMaximized(id, maximized) => {
if window::Id::MAIN == id { if self.app.main_window_id() == id {
self.app.core_mut().window.sharp_corners = maximized; self.app.core_mut().window.sharp_corners = maximized;
} }
} }
Message::WindowResize(id, width, height) => { 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_width(width);
self.app.core_mut().set_window_height(height); self.app.core_mut().set_window_height(height);
} }
@ -283,7 +283,7 @@ impl<T: Application> Cosmic<T> {
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
Message::WindowState(id, state) => { 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( self.app.core_mut().window.sharp_corners = state.intersects(
WindowState::MAXIMIZED WindowState::MAXIMIZED
| WindowState::FULLSCREEN | WindowState::FULLSCREEN
@ -298,7 +298,7 @@ impl<T: Application> Cosmic<T> {
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
Message::WmCapabilities(id, capabilities) => { Message::WmCapabilities(id, capabilities) => {
if window::Id::MAIN == id { if self.app.main_window_id() == id {
self.app.core_mut().window.show_maximize = self.app.core_mut().window.show_maximize =
capabilities.contains(WindowManagerCapabilities::MAXIMIZE); capabilities.contains(WindowManagerCapabilities::MAXIMIZE);
self.app.core_mut().window.show_minimize = self.app.core_mut().window.show_minimize =
@ -321,7 +321,7 @@ impl<T: Application> Cosmic<T> {
keyboard_nav::Message::Escape => return self.app.on_escape(), keyboard_nav::Message::Escape => return self.app.on_escape(),
keyboard_nav::Message::Search => return self.app.on_search(), 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) => { Message::ContextDrawer(show) => {
@ -329,11 +329,11 @@ impl<T: Application> Cosmic<T> {
return self.app.on_context_drawer(); 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) => { Message::NavBar(key) => {
self.app.core_mut().nav_bar_set_toggled_condensed(false); self.app.core_mut().nav_bar_set_toggled_condensed(false);
@ -403,7 +403,7 @@ impl<T: Application> Cosmic<T> {
Message::Activate(_token) => { Message::Activate(_token) => {
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
return iced_sctk::commands::activation::activate( return iced_sctk::commands::activation::activate(
iced::window::Id::MAIN, self.app.main_window_id(),
#[allow(clippy::used_underscore_binding)] #[allow(clippy::used_underscore_binding)]
_token, _token,
); );

View file

@ -433,6 +433,11 @@ where
Vec::new() 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 /// Allows overriding the default nav bar widget
fn nav_bar(&self) -> Option<Element<Message<Self::Message>>> { fn nav_bar(&self) -> Option<Element<Message<Self::Message>>> {
if !self.core().nav_bar_active() { if !self.core().nav_bar_active() {
@ -571,15 +576,15 @@ pub trait ApplicationExt: Application {
impl<App: Application> ApplicationExt for App { impl<App: Application> ApplicationExt for App {
fn drag(&mut self) -> iced::Command<Message<Self::Message>> { fn drag(&mut self) -> iced::Command<Message<Self::Message>> {
command::drag(Some(window::Id::MAIN)) command::drag(Some(self.main_window_id()))
} }
fn maximize(&mut self) -> iced::Command<Message<Self::Message>> { fn maximize(&mut self) -> iced::Command<Message<Self::Message>> {
command::maximize(Some(window::Id::MAIN), true) command::maximize(Some(self.main_window_id()), true)
} }
fn minimize(&mut self) -> iced::Command<Message<Self::Message>> { fn minimize(&mut self) -> iced::Command<Message<Self::Message>> {
command::minimize(Some(window::Id::MAIN)) command::minimize(Some(self.main_window_id()))
} }
#[cfg(any(feature = "multi-window", feature = "wayland"))] #[cfg(any(feature = "multi-window", feature = "wayland"))]
@ -606,7 +611,7 @@ impl<App: Application> ApplicationExt for App {
fn set_window_title(&mut self, title: String) -> iced::Command<Message<Self::Message>> { fn set_window_title(&mut self, title: String) -> iced::Command<Message<Self::Message>> {
self.core_mut() self.core_mut()
.title .title
.insert(window::Id::MAIN, title.clone()); .insert(self.main_window_id(), title.clone());
iced::Command::none() iced::Command::none()
} }
@ -658,7 +663,7 @@ impl<App: Application> ApplicationExt for App {
.push_maybe(if core.window.show_headerbar { .push_maybe(if core.window.show_headerbar {
Some({ Some({
let mut header = crate::widget::header_bar() let mut header = crate::widget::header_bar()
.window_id(window::Id::MAIN) .window_id(self.main_window_id())
.title(&core.window.header_title) .title(&core.window.header_title)
.on_drag(Message::Cosmic(cosmic::Message::Drag)) .on_drag(Message::Cosmic(cosmic::Message::Drag))
.on_close(Message::Cosmic(cosmic::Message::Close)) .on_close(Message::Cosmic(cosmic::Message::Close))