Skip to content

Commit

Permalink
Changes for multiple Application support
Browse files Browse the repository at this point in the history
- Add Application::main_window_id to allow ids other than MAIN
- Make Cosmic and Cosmic::app public to allow custom use
  • Loading branch information
jackpot51 committed Feb 13, 2024
1 parent cc8033d commit 3c5dcec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
28 changes: 14 additions & 14 deletions src/app/cosmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub enum Message {
}

#[derive(Default)]
pub(crate) struct Cosmic<App> {
pub(crate) app: App,
pub struct Cosmic<App> {
pub app: App,
}

impl<T: Application> IcedApplication for Cosmic<T>
Expand Down Expand Up @@ -228,7 +228,7 @@ where

#[cfg(any(feature = "multi-window", feature = "wayland"))]
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);
}

Expand All @@ -248,26 +248,26 @@ where
impl<T: Application> Cosmic<T> {
#[cfg(feature = "wayland")]
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"))]
#[allow(clippy::unused_self)]
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)]
fn cosmic_update(&mut self, message: Message) -> iced::Command<super::Message<T::Message>> {
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);
}
Expand All @@ -283,7 +283,7 @@ impl<T: Application> Cosmic<T> {

#[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
Expand All @@ -298,7 +298,7 @@ impl<T: Application> Cosmic<T> {

#[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 =
Expand All @@ -321,19 +321,19 @@ impl<T: Application> Cosmic<T> {
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) => {
self.app.core_mut().window.show_context = show;
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);
Expand Down Expand Up @@ -403,7 +403,7 @@ impl<T: Application> Cosmic<T> {
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,
);
Expand Down
15 changes: 10 additions & 5 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Element<Message<Self::Message>>> {
if !self.core().nav_bar_active() {
Expand Down Expand Up @@ -571,15 +576,15 @@ pub trait ApplicationExt: Application {

impl<App: Application> ApplicationExt for App {
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>> {
command::maximize(Some(window::Id::MAIN), true)
command::maximize(Some(self.main_window_id()), true)
}

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"))]
Expand All @@ -606,7 +611,7 @@ impl<App: Application> ApplicationExt for App {
fn set_window_title(&mut self, title: String) -> iced::Command<Message<Self::Message>> {
self.core_mut()
.title
.insert(window::Id::MAIN, title.clone());
.insert(self.main_window_id(), title.clone());
iced::Command::none()
}

Expand Down Expand Up @@ -658,7 +663,7 @@ impl<App: Application> 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))
Expand Down

0 comments on commit 3c5dcec

Please sign in to comment.