Skip to content

Commit

Permalink
update to support winit multi-window
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Dec 7, 2023
1 parent 77e9a16 commit a734316
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 94 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/cosmic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions examples/cosmic/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {}

Expand Down
2 changes: 1 addition & 1 deletion iced
Submodule iced updated 71 files
+0 −1 .github/ISSUE_TEMPLATE/BUG-REPORT.yml
+3 −1 Cargo.toml
+2 −6 ECOSYSTEM.md
+3 −0 core/Cargo.toml
+42 −16 core/src/point.rs
+1 −1 core/src/widget/tree.rs
+6 −4 core/src/window.rs
+18 −4 core/src/window/event.rs
+21 −0 core/src/window/id.rs
+4 −2 core/src/window/position.rs
+44 −26 core/src/window/settings.rs
+0 −0 core/src/window/settings/linux.rs
+0 −0 core/src/window/settings/macos.rs
+0 −0 core/src/window/settings/other.rs
+0 −0 core/src/window/settings/wasm.rs
+0 −0 core/src/window/settings/windows.rs
+8 −4 examples/events/src/main.rs
+1 −1 examples/exit/src/main.rs
+6 −1 examples/integration/src/main.rs
+1 −1 examples/loading_spinners/src/circular.rs
+1 −1 examples/loading_spinners/src/linear.rs
+9 −0 examples/multi_window/Cargo.toml
+215 −0 examples/multi_window/src/main.rs
+5 −2 examples/screenshot/src/main.rs
+4 −27 examples/sctk_drag/src/main.rs
+0 −4 examples/sctk_lazy/src/main.rs
+5 −20 examples/sctk_session_lock/src/main.rs
+2 −6 examples/sctk_todos/src/main.rs
+5 −9 examples/solar_system/src/main.rs
+3 −1 examples/toast/src/main.rs
+7 −7 examples/todos/src/main.rs
+1 −1 examples/visible_bounds/src/main.rs
+4 −1 graphics/src/compositor.rs
+24 −24 renderer/src/compositor.rs
+1 −0 runtime/Cargo.toml
+3 −1 runtime/src/command/action.rs
+1 −1 runtime/src/command/platform_specific/wayland/layer_surface.rs
+1 −1 runtime/src/command/platform_specific/wayland/window.rs
+3 −0 runtime/src/lib.rs
+6 −0 runtime/src/multi_window.rs
+32 −0 runtime/src/multi_window/program.rs
+286 −0 runtime/src/multi_window/state.rs
+2 −2 runtime/src/program.rs
+1 −1 runtime/src/program/state.rs
+60 −38 runtime/src/window.rs
+87 −73 runtime/src/window/action.rs
+1 −1 sctk/Cargo.toml
+39 −70 sctk/src/application.rs
+8 −27 sctk/src/conversion.rs
+8 −14 sctk/src/sctk_event.rs
+1 −1 src/application.rs
+3 −0 src/lib.rs
+4 −0 src/multi_window.rs
+245 −0 src/multi_window/application.rs
+3 −9 src/settings.rs
+13 −29 src/wayland/mod.rs
+3 −22 src/wayland/sandbox.rs
+0 −6 src/window.rs
+0 −32 src/window/position.rs
+0 −1 tiny_skia/src/backend.rs
+15 −17 tiny_skia/src/window/compositor.rs
+11 −14 wgpu/src/window/compositor.rs
+1 −1 winit/Cargo.toml
+44 −65 winit/src/application.rs
+0 −101 winit/src/application/profiler.rs
+158 −45 winit/src/conversion.rs
+3 −6 winit/src/lib.rs
+1,326 −0 winit/src/multi_window.rs
+245 −0 winit/src/multi_window/state.rs
+156 −0 winit/src/multi_window/window_manager.rs
+3 −234 winit/src/settings.rs
29 changes: 17 additions & 12 deletions src/app/command.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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>> {
Expand All @@ -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)
}
7 changes: 5 additions & 2 deletions src/app/core.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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,

Expand All @@ -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()
Expand Down
60 changes: 36 additions & 24 deletions src/app/cosmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
{
Expand All @@ -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),
Expand All @@ -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 {

Check warning on line 117 in src/app/cosmic.rs

View workflow job for this annotation

GitHub Actions / examples (open-dialog)

unused variable: `id`

Check warning on line 117 in src/app/cosmic.rs

View workflow job for this annotation

GitHub Actions / tests (wayland)

unused variable: `id`
f64::from(self.app.core().scale_factor())
}

fn style(&self) -> <Self::Theme as iced_style::application::StyleSheet>::Style {
Expand Down Expand Up @@ -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 {

Check warning on line 206 in src/app/cosmic.rs

View workflow job for this annotation

GitHub Actions / examples (open-dialog)

unused variable: `id`

Check warning on line 206 in src/app/cosmic.rs

View workflow job for this annotation

GitHub Actions / tests (wayland)

unused variable: `id`
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);
}

Expand All @@ -208,7 +220,7 @@ where
}
}

#[cfg(not(feature = "wayland"))]
#[cfg(not(feature = "multi-window"))]
fn view(&self) -> Element<Self::Message> {
self.app.view_main()
}
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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 =
Expand All @@ -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) => {
Expand Down Expand Up @@ -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,
);
Expand Down
Loading

0 comments on commit a734316

Please sign in to comment.