update to support winit multi-window

This commit is contained in:
Ashley Wulber 2023-12-07 15:27:52 -05:00 committed by Ashley Wulber
parent 77e9a160c4
commit c66e4aafd0
13 changed files with 149 additions and 97 deletions

View file

@ -30,7 +30,10 @@ wayland = [
"iced/wayland",
"iced_sctk",
"cctk",
"multi-window",
]
# multi-window support
multi-window = ["iced_runtime/multi-window", "iced/multi-window", "iced_winit?/multi-window"]
# Render with wgpu
wgpu = ["iced/wgpu", "iced_wgpu"]
# X11 window support via winit

View file

@ -4,6 +4,7 @@
//! Application API example
use cosmic::app::{Command, Core, Settings};
use cosmic::iced_core::Size;
use cosmic::widget::nav_bar;
use cosmic::{executor, iced, ApplicationExt, Element};
@ -43,7 +44,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.default_icon_theme("Pop")
.default_text_size(16.0)
.scale_factor(1.0)
.size((1024, 768))
.size(Size::new(1024., 768.))
.theme(cosmic::Theme::dark());
cosmic::app::run::<App>(settings, input)?;

View file

@ -15,6 +15,6 @@ pub fn main() -> cosmic::iced::Result {
env_logger::init_from_env(env);
cosmic::icon_theme::set_default("Pop");
let mut settings = Settings::default();
settings.window.min_size = Some((600, 300));
settings.window.min_size = Some(cosmic::iced::Size::new(600., 300.));
Window::run(settings)
}

View file

@ -436,10 +436,10 @@ impl Application for Window {
Message::ToggleNavBarCondensed => {
self.nav_bar_toggled_condensed = !self.nav_bar_toggled_condensed
}
Message::Drag => return drag(),
Message::Close => return close(),
Message::Minimize => return minimize(true),
Message::Maximize => return toggle_maximize(),
Message::Drag => return drag(window::Id::MAIN),
Message::Close => return close(window::Id::MAIN),
Message::Minimize => return minimize(window::Id::MAIN, true),
Message::Maximize => return toggle_maximize(window::Id::MAIN),
Message::InputChanged => {}

View file

@ -16,7 +16,7 @@ use url::Url;
#[rustfmt::skip]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let settings = Settings::default()
.size((1024, 768));
.size(cosmic::iced::Size::new(1024.0, 768.0));
cosmic::app::run::<App>(settings, ())?;
@ -77,7 +77,10 @@ impl cosmic::Application for App {
};
app.set_header_title("Open a file".into());
let cmd = app.set_window_title("COSMIC OpenDialog Demo".into());
let cmd = app.set_window_title(
"COSMIC OpenDialog Demo".into(),
cosmic::iced::window::Id::MAIN,
);
(app, cmd)
}

2
iced

@ -1 +1 @@
Subproject commit 33b2fd967ada2d2c86eb1b57eb4997719774499e
Subproject commit 4521d6e584b1cddd00d8d1e7c20784cfd5bcdea9

View file

@ -1,6 +1,8 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use iced::window;
/// Asynchronous actions for COSMIC applications.
use super::Message;
@ -27,16 +29,16 @@ pub mod message {
}
}
pub fn drag<M: Send + 'static>() -> iced::Command<Message<M>> {
crate::command::drag().map(Message::Cosmic)
pub fn drag<M: Send + 'static>(id: Option<window::Id>) -> iced::Command<Message<M>> {
crate::command::drag(id).map(Message::Cosmic)
}
pub fn fullscreen<M: Send + 'static>() -> iced::Command<Message<M>> {
crate::command::fullscreen().map(Message::Cosmic)
pub fn fullscreen<M: Send + 'static>(id: Option<window::Id>) -> iced::Command<Message<M>> {
crate::command::fullscreen(id).map(Message::Cosmic)
}
pub fn minimize<M: Send + 'static>() -> iced::Command<Message<M>> {
crate::command::minimize().map(Message::Cosmic)
pub fn minimize<M: Send + 'static>(id: Option<window::Id>) -> iced::Command<Message<M>> {
crate::command::minimize(id).map(Message::Cosmic)
}
pub fn set_scaling_factor<M: Send + 'static>(factor: f32) -> iced::Command<Message<M>> {
@ -47,14 +49,17 @@ pub fn set_theme<M: Send + 'static>(theme: crate::Theme) -> iced::Command<Messag
message::cosmic(super::cosmic::Message::AppThemeChange(theme))
}
pub fn set_title<M: Send + 'static>(title: String) -> iced::Command<Message<M>> {
crate::command::set_title(title).map(Message::Cosmic)
pub fn set_title<M: Send + 'static>(
id: Option<window::Id>,
title: String,
) -> iced::Command<Message<M>> {
crate::command::set_title(id, title).map(Message::Cosmic)
}
pub fn set_windowed<M: Send + 'static>() -> iced::Command<Message<M>> {
crate::command::set_windowed().map(Message::Cosmic)
pub fn set_windowed<M: Send + 'static>(id: Option<window::Id>) -> iced::Command<Message<M>> {
crate::command::set_windowed(id).map(Message::Cosmic)
}
pub fn toggle_fullscreen<M: Send + 'static>() -> iced::Command<Message<M>> {
crate::command::toggle_fullscreen().map(Message::Cosmic)
pub fn toggle_fullscreen<M: Send + 'static>(id: Option<window::Id>) -> iced::Command<Message<M>> {
crate::command::toggle_fullscreen(id).map(Message::Cosmic)
}

View file

@ -1,8 +1,11 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use std::collections::HashMap;
use cosmic_config::CosmicConfigEntry;
use cosmic_theme::ThemeMode;
use iced_core::window::Id;
use crate::Theme;
@ -56,7 +59,7 @@ pub struct Core {
/// Theme mode
pub(super) system_theme_mode: ThemeMode,
pub(super) title: String,
pub(super) title: HashMap<Id, String>,
pub window: Window,
@ -78,7 +81,7 @@ impl Default for Core {
toggled_condensed: true,
},
scale_factor: 1.0,
title: String::new(),
title: HashMap::new(),
theme_sub_counter: 0,
system_theme: crate::theme::active(),
system_theme_mode: ThemeMode::config()

View file

@ -12,7 +12,13 @@ use cosmic_theme::ThemeMode;
use iced::event::wayland::{self, WindowEvent};
#[cfg(feature = "wayland")]
use iced::event::PlatformSpecific;
#[cfg(all(feature = "winit", feature = "multi-window"))]
use iced::multi_window::Application as IcedApplication;
#[cfg(feature = "wayland")]
use iced::wayland::Application as IcedApplication;
use iced::window;
#[cfg(not(feature = "multi-window"))]
use iced::Application as IcedApplication;
use iced_futures::event::listen_raw;
#[cfg(not(feature = "wayland"))]
use iced_runtime::command::Action;
@ -67,7 +73,7 @@ pub(crate) struct Cosmic<App> {
pub(crate) should_exit: bool,
}
impl<T: Application> iced::Application for Cosmic<T>
impl<T: Application> IcedApplication for Cosmic<T>
where
T::Message: Send + 'static,
{
@ -82,17 +88,16 @@ where
(Self::new(model), command)
}
#[cfg(feature = "wayland")]
fn close_requested(&self, id: window::Id) -> Self::Message {
self.app
.on_close_requested(id)
.map_or(super::Message::None, super::Message::App)
}
#[cfg(not(feature = "multi-window"))]
fn title(&self) -> String {
self.app.title().to_string()
}
#[cfg(feature = "multi-window")]
fn title(&self, id: window::Id) -> String {
self.app.title(id).to_string()
}
fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
match message {
super::Message::App(message) => self.app.update(message),
@ -103,13 +108,14 @@ where
}
}
#[cfg(not(feature = "multi-window"))]
fn scale_factor(&self) -> f64 {
f64::from(self.app.core().scale_factor())
}
#[cfg(feature = "wayland")]
fn should_exit(&self) -> bool {
self.should_exit || self.app.should_exit()
#[cfg(feature = "multi-window")]
fn scale_factor(&self, id: window::Id) -> f64 {
f64::from(self.app.core().scale_factor())
}
fn style(&self) -> <Self::Theme as iced_style::application::StyleSheet>::Style {
@ -191,13 +197,19 @@ where
])
}
#[cfg(not(feature = "multi-window"))]
fn theme(&self) -> Self::Theme {
crate::theme::active()
}
#[cfg(feature = "wayland")]
#[cfg(feature = "multi-window")]
fn theme(&self, id: window::Id) -> Self::Theme {
crate::theme::active()
}
#[cfg(feature = "multi-window")]
fn view(&self, id: window::Id) -> Element<Self::Message> {
if id != window::Id(0) {
if id != window::Id::MAIN {
return self.app.view_window(id).map(super::Message::App);
}
@ -208,7 +220,7 @@ where
}
}
#[cfg(not(feature = "wayland"))]
#[cfg(not(feature = "multi-window"))]
fn view(&self) -> Element<Self::Message> {
self.app.view_main()
}
@ -224,14 +236,14 @@ impl<T: Application> Cosmic<T> {
#[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))
iced::Command::single(Action::Window(WindowAction::Close(window::Id::MAIN)))
}
#[allow(clippy::too_many_lines)]
fn cosmic_update(&mut self, message: Message) -> iced::Command<super::Message<T::Message>> {
match message {
Message::WindowResize(id, width, height) => {
if window::Id(0) == id {
if window::Id::MAIN == id {
self.app.core_mut().set_window_width(width);
self.app.core_mut().set_window_height(height);
}
@ -241,7 +253,7 @@ impl<T: Application> Cosmic<T> {
#[cfg(feature = "wayland")]
Message::WindowState(id, state) => {
if window::Id(0) == id {
if window::Id::MAIN == id {
self.app.core_mut().window.sharp_corners = state.intersects(
WindowState::MAXIMIZED
| WindowState::FULLSCREEN
@ -256,7 +268,7 @@ impl<T: Application> Cosmic<T> {
#[cfg(feature = "wayland")]
Message::WmCapabilities(id, capabilities) => {
if window::Id(0) == id {
if window::Id::MAIN == id {
self.app.core_mut().window.can_fullscreen =
capabilities.contains(WindowManagerCapabilities::FULLSCREEN);
self.app.core_mut().window.show_maximize =
@ -281,25 +293,25 @@ 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_fullscreen(),
keyboard_nav::Message::Fullscreen => return command::toggle_fullscreen(None),
},
Message::ContextDrawer(show) => {
self.app.core_mut().window.show_context = show;
}
Message::Drag => return command::drag(),
Message::Drag => return command::drag(None),
Message::Minimize => return command::minimize(),
Message::Minimize => return command::minimize(None),
Message::Maximize => {
if self.app.core().window.sharp_corners {
self.app.core_mut().window.sharp_corners = false;
return command::set_windowed();
return command::set_windowed(None);
}
self.app.core_mut().window.sharp_corners = true;
return command::fullscreen();
return command::fullscreen(None);
}
Message::NavBar(key) => {
@ -370,7 +382,7 @@ impl<T: Application> Cosmic<T> {
Message::Activate(_token) => {
#[cfg(feature = "wayland")]
return iced_sctk::commands::activation::activate(
iced::window::Id::default(),
iced::window::Id::MAIN,
#[allow(clippy::used_underscore_binding)]
_token,
);

View file

@ -47,6 +47,9 @@ use crate::theme::THEME;
use crate::widget::{context_drawer, nav_bar};
use apply::Apply;
use iced::Subscription;
#[cfg(all(feature = "winit", feature = "multi-window"))]
use iced::{multi_window::Application as IcedApplication, window};
#[cfg(any(not(feature = "winit"), not(feature = "multi-window")))]
use iced::{window, Application as IcedApplication};
pub use message::Message;
use url::Url;
@ -70,8 +73,8 @@ pub(crate) fn iced_settings<App: Application>(
let mut core = Core::default();
core.debug = settings.debug;
core.set_scale_factor(settings.scale_factor);
core.set_window_width(settings.size.0);
core.set_window_height(settings.size.1);
core.set_window_width(settings.size.width as u32);
core.set_window_height(settings.size.height as u32);
THEME.with(move |t| {
let mut cosmic_theme = t.borrow_mut();
@ -98,7 +101,7 @@ pub(crate) fn iced_settings<App: Application>(
autosize: settings.autosize,
client_decorations: settings.client_decorations,
resizable: settings.resizable,
size: settings.size,
size: (settings.size.width as u32, settings.size.height as u32).into(),
size_limits: settings.size_limits,
title: None,
transparent: settings.transparent,
@ -428,11 +431,6 @@ where
/// Called before closing the application.
fn on_app_exit(&mut self) {}
#[cfg(feature = "wayland")]
fn should_exit(&self) -> bool {
false
}
/// Called when a window requests to be closed.
fn on_close_requested(&self, id: window::Id) -> Option<Self::Message> {
None
@ -471,7 +469,7 @@ where
/// Constructs views for other windows.
fn view_window(&self, id: window::Id) -> Element<Self::Message> {
panic!("no view for window {}", id.0);
panic!("no view for window {:?}", id);
}
/// Overrides the default style for applications
@ -499,10 +497,15 @@ pub trait ApplicationExt: Application {
/// Minimizes the window.
fn minimize(&mut self) -> iced::Command<Message<Self::Message>>;
/// Get the title of the main window.
#[cfg(not(feature = "multi-window"))]
fn title(&self) -> &str;
#[cfg(feature = "multi-window")]
/// Get the title of a window.
fn title(&self, id: window::Id) -> &str;
/// Set the context drawer title.
fn set_context_title(&mut self, title: String) {
self.core_mut().set_context_title(title);
@ -513,39 +516,60 @@ pub trait ApplicationExt: Application {
self.core_mut().set_header_title(title);
}
#[cfg(not(feature = "multi-window"))]
/// Set the title of the main window.
fn set_window_title(&mut self, title: String) -> iced::Command<Message<Self::Message>>;
#[cfg(feature = "multi-window")]
/// Set the title of a window.
fn set_window_title(
&mut self,
title: String,
id: window::Id,
) -> iced::Command<Message<Self::Message>>;
/// View template for the main window.
fn view_main(&self) -> Element<Message<Self::Message>>;
}
impl<App: Application> ApplicationExt for App {
fn drag(&mut self) -> iced::Command<Message<Self::Message>> {
command::drag()
command::drag(Some(window::Id::MAIN))
}
fn fullscreen(&mut self) -> iced::Command<Message<Self::Message>> {
command::fullscreen()
command::fullscreen(Some(window::Id::MAIN))
}
fn minimize(&mut self) -> iced::Command<Message<Self::Message>> {
command::minimize()
command::minimize(Some(window::Id::MAIN))
}
#[cfg(feature = "multi-window")]
fn title(&self, id: window::Id) -> &str {
self.core().title.get(&id).map(|s| s.as_str()).unwrap_or("")
}
#[cfg(not(feature = "multi-window"))]
fn title(&self) -> &str {
&self.core().title
&self.core().window.header_title
}
#[cfg(feature = "wayland")]
fn set_window_title(&mut self, title: String) -> iced::Command<Message<Self::Message>> {
self.core_mut().title = title.clone();
command::set_title(title)
#[cfg(feature = "multi-window")]
fn set_window_title(
&mut self,
title: String,
id: window::Id,
) -> iced::Command<Message<Self::Message>> {
self.core_mut().title.insert(id, title.clone());
command::set_title(Some(id), title)
}
#[cfg(not(feature = "wayland"))]
#[cfg(not(feature = "multi-window"))]
fn set_window_title(&mut self, title: String) -> iced::Command<Message<Self::Message>> {
self.core_mut().title = title.clone();
self.core_mut()
.title
.insert(window::Id::MAIN, title.clone());
iced::Command::none()
}

View file

@ -48,7 +48,7 @@ pub struct Settings {
pub(crate) scale_factor: f32,
/// Initial size of the window.
pub(crate) size: (u32, u32),
pub(crate) size: iced::Size,
/// Limitations of the window size
#[cfg(feature = "wayland")]
@ -91,7 +91,7 @@ impl Default for Settings {
.ok()
.and_then(|scale| scale.parse::<f32>().ok())
.unwrap_or(1.0),
size: (1024, 768),
size: iced::Size::new(1024.0, 768.0),
#[cfg(feature = "wayland")]
size_limits: Limits::NONE.min_height(1.0).min_width(1.0),
theme: crate::theme::system_preference(),

View file

@ -3,7 +3,6 @@
//! Create asynchronous actions to be performed in the background.
#[cfg(feature = "wayland")]
use iced::window;
use iced::Command;
use iced_core::window::Mode;
@ -33,45 +32,45 @@ pub fn message<M: Send + 'static>(message: M) -> Command<M> {
/// Initiates a window drag.
#[cfg(feature = "wayland")]
pub fn drag<M>() -> Command<M> {
iced_sctk::commands::window::start_drag_window(window::Id(0))
pub fn drag<M>(id: Option<window::Id>) -> Command<M> {
iced_sctk::commands::window::start_drag_window(id.unwrap_or(window::Id::MAIN))
}
/// Initiates a window drag.
#[cfg(not(feature = "wayland"))]
pub fn drag<M>() -> Command<M> {
iced_runtime::window::drag()
pub fn drag<M>(id: Option<window::Id>) -> Command<M> {
iced_runtime::window::drag(id.unwrap_or(window::Id::MAIN))
}
/// Fullscreens the window.
#[cfg(feature = "wayland")]
pub fn fullscreen<M>() -> Command<M> {
iced_sctk::commands::window::set_mode_window(window::Id(0), Mode::Fullscreen)
pub fn fullscreen<M>(id: Option<window::Id>) -> Command<M> {
iced_sctk::commands::window::set_mode_window(id.unwrap_or(window::Id::MAIN), Mode::Fullscreen)
}
/// Fullscreens the window.
#[cfg(not(feature = "wayland"))]
pub fn fullscreen<M>() -> Command<M> {
iced_runtime::window::change_mode(Mode::Fullscreen)
pub fn fullscreen<M>(id: Option<window::Id>) -> Command<M> {
iced_runtime::window::change_mode(id.unwrap_or(window::Id::MAIN), Mode::Fullscreen)
}
/// Minimizes the window.
#[cfg(feature = "wayland")]
pub fn minimize<M>() -> Command<M> {
iced_sctk::commands::window::set_mode_window(window::Id(0), Mode::Hidden)
pub fn minimize<M>(id: Option<window::Id>) -> Command<M> {
iced_sctk::commands::window::set_mode_window(id.unwrap_or(window::Id::MAIN), Mode::Hidden)
}
/// Minimizes the window.
#[cfg(not(feature = "wayland"))]
pub fn minimize<M>() -> Command<M> {
iced_runtime::window::minimize(true)
pub fn minimize<M>(id: Option<window::Id>) -> Command<M> {
iced_runtime::window::minimize(id.unwrap_or(window::Id::MAIN), true)
}
/// Sets the title of a window.
#[cfg(feature = "wayland")]
pub fn set_title<M>(title: String) -> Command<M> {
pub fn set_title<M>(id: Option<window::Id>, title: String) -> Command<M> {
window_action(WindowAction::Title {
id: window::Id(0),
id: id.unwrap_or(window::Id::MAIN),
title,
})
}
@ -79,32 +78,34 @@ pub fn set_title<M>(title: String) -> Command<M> {
/// Sets the title of a window.
#[cfg(not(feature = "wayland"))]
#[allow(unused_variables, clippy::needless_pass_by_value)]
pub fn set_title<M>(title: String) -> Command<M> {
pub fn set_title<M>(id: Option<window::Id>, title: String) -> Command<M> {
Command::none()
}
/// Sets the window mode to windowed.
#[cfg(feature = "wayland")]
pub fn set_windowed<M>() -> Command<M> {
iced_sctk::commands::window::set_mode_window(window::Id(0), Mode::Windowed)
pub fn set_windowed<M>(id: Option<window::Id>) -> Command<M> {
iced_sctk::commands::window::set_mode_window(id.unwrap_or(window::Id::MAIN), Mode::Windowed)
}
/// Sets the window mode to windowed.
#[cfg(not(feature = "wayland"))]
pub fn set_windowed<M>() -> Command<M> {
iced_runtime::window::change_mode(Mode::Windowed)
pub fn set_windowed<M>(id: Option<window::Id>) -> Command<M> {
iced_runtime::window::change_mode(id.unwrap_or(window::Id::MAIN), Mode::Windowed)
}
/// Toggles the windows' maximization state.
#[cfg(feature = "wayland")]
pub fn toggle_fullscreen<M>() -> Command<M> {
window_action(WindowAction::ToggleFullscreen { id: window::Id(0) })
pub fn toggle_fullscreen<M>(id: Option<window::Id>) -> Command<M> {
window_action(WindowAction::ToggleFullscreen {
id: id.unwrap_or(window::Id::MAIN),
})
}
/// Toggles the windows' maximization state.
#[cfg(not(feature = "wayland"))]
pub fn toggle_fullscreen<M>() -> Command<M> {
iced_runtime::window::toggle_maximize()
pub fn toggle_fullscreen<M>(id: Option<window::Id>) -> Command<M> {
iced_runtime::window::toggle_maximize(id.unwrap_or(window::Id::MAIN))
}
/// Creates a command to apply an action to a window.

View file

@ -31,10 +31,10 @@ where
.width(self.width)
.height(bounds.height - 8.0 - position.y);
let mut node =
self.content
.as_widget()
.layout(&mut self.tree, renderer, &limits);
let mut node = self
.content
.as_widget()
.layout(&mut self.tree, renderer, &limits);
let node_size = node.size();
node.move_to(Point {