refactor!: improve surface action handling and adjust libcosmic API

This commit is contained in:
Michael Aaron Murphy 2025-03-05 16:01:23 +01:00 • committed by Ashley Wulber
parent 2c10e4362a
commit adb08f9daf
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
23 changed files with 755 additions and 799 deletions

36
src/action.rs Normal file
View file

@ -0,0 +1,36 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::app;
pub const fn app<M>(message: M) -> Action<M> {
Action::App(message)
}
pub const fn cosmic<M>(message: app::Action) -> Action<M> {
Action::Cosmic(message)
}
pub const fn none<M>() -> Action<M> {
Action::None
}
#[derive(Clone, Debug)]
#[must_use]
pub enum Action<M> {
/// Messages from the application, for the application.
App(M),
/// Internal messages to be handled by libcosmic.
Cosmic(app::Action),
#[cfg(feature = "single-instance")]
/// Dbus activation messages
DbusActivation(app::DbusActivationMessage),
/// Do nothing
None,
}
impl<M> From<M> for Action<M> {
fn from(value: M) -> Self {
Self::App(value)
}
}

75
src/app/action.rs Normal file
View file

@ -0,0 +1,75 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use crate::surface;
use crate::theme::Theme;
use crate::widget::nav_bar;
use crate::{config::CosmicTk, keyboard_nav};
#[cfg(feature = "wayland")]
use cctk::sctk::reexports::csd_frame::{WindowManagerCapabilities, WindowState};
use cosmic_theme::ThemeMode;
#[cfg(not(any(feature = "multi-window", feature = "wayland")))]
use iced::Application as IcedApplication;
/// A message managed internally by COSMIC.
#[derive(Clone, Debug)]
pub enum Action {
/// Activate the application
Activate(String),
/// Application requests theme change.
AppThemeChange(Theme),
/// Requests to close the window.
Close,
/// Closes or shows the context drawer.
ContextDrawer(bool),
/// Requests to drag the window.
Drag,
/// Window focus changed
Focus(iced::window::Id),
/// Keyboard shortcuts managed by libcosmic.
KeyboardNav(keyboard_nav::Action),
/// Requests to maximize the window.
Maximize,
/// Requests to minimize the window.
Minimize,
/// Activates a navigation element from the nav bar.
NavBar(nav_bar::Id),
/// Activates a context menu for an item from the nav bar.
NavBarContext(nav_bar::Id),
/// Set scaling factor
ScaleFactor(f32),
/// Show the window menu
ShowWindowMenu,
/// Tracks updates to window suggested size.
#[cfg(feature = "applet")]
SuggestedBounds(Option<iced::Size>),
/// Internal surface message
Surface(surface::Action),
/// Notifies that a surface was closed.
/// Any data relating to the surface should be cleaned up.
SurfaceClosed(iced::window::Id),
/// Notification of system theme changes.
SystemThemeChange(Vec<&'static str>, Theme),
/// Notification of system theme mode changes.
SystemThemeModeChange(Vec<&'static str>, ThemeMode),
/// Toggles visibility of the nav bar.
ToggleNavBar,
/// Toggles the condensed status of the nav bar.
ToggleNavBarCondensed,
/// Toolkit configuration update
ToolkitConfig(CosmicTk),
/// Window focus lost
Unfocus(iced::window::Id),
/// Updates the window maximized state
WindowMaximized(iced::window::Id, bool),
/// Updates the tracked window geometry.
WindowResize(iced::window::Id, f32, f32),
/// Tracks updates to window state.
#[cfg(feature = "wayland")]
WindowState(iced::window::Id, WindowState),
/// Capabilities the window manager supports
#[cfg(feature = "wayland")]
WmCapabilities(iced::window::Id, WindowManagerCapabilities),
#[cfg(feature = "xdg-portal")]
DesktopSettings(crate::theme::portal::Desktop),
}

View file

@ -1,94 +0,0 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use iced::window;
/// Asynchronous actions for COSMIC applications.
use super::Message;
/// Commands for COSMIC applications.
pub type Task<M> = iced::Task<Message<M>>;
/// Creates a task which yields a [`crate::app::Message`].
pub fn message<M: Send + 'static>(message: Message<M>) -> Task<M> {
crate::task::message(message)
}
/// Convenience methods for building message-based commands.
pub mod message {
/// Creates a task which yields an application message.
pub fn app<M: Send + 'static>(message: M) -> crate::app::Task<M> {
super::message(super::Message::App(message))
}
/// Creates a task which yields a cosmic message.
pub fn cosmic<M: Send + 'static>(message: crate::app::cosmic::Message) -> crate::app::Task<M> {
super::message(super::Message::Cosmic(message))
}
}
impl crate::app::Core {
pub fn drag<M: Send + 'static>(&self, id: Option<window::Id>) -> iced::Task<Message<M>> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::task::drag(id).map(Message::Cosmic)
}
pub fn maximize<M: Send + 'static>(
&self,
id: Option<window::Id>,
maximized: bool,
) -> iced::Task<Message<M>> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::task::maximize(id, maximized).map(Message::Cosmic)
}
pub fn minimize<M: Send + 'static>(&self, id: Option<window::Id>) -> iced::Task<Message<M>> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::task::minimize(id).map(Message::Cosmic)
}
pub fn set_scaling_factor<M: Send + 'static>(&self, factor: f32) -> iced::Task<Message<M>> {
message::cosmic(super::cosmic::Message::ScaleFactor(factor))
}
pub fn set_title<M: Send + 'static>(
&self,
id: Option<window::Id>,
title: String,
) -> iced::Task<Message<M>> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::task::set_title(id, title).map(Message::Cosmic)
}
pub fn set_windowed<M: Send + 'static>(
&self,
id: Option<window::Id>,
) -> iced::Task<Message<M>> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::task::set_windowed(id).map(Message::Cosmic)
}
pub fn toggle_maximize<M: Send + 'static>(
&self,
id: Option<window::Id>,
) -> iced::Task<Message<M>> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::task::toggle_maximize(id).map(Message::Cosmic)
}
}
pub fn set_theme<M: Send + 'static>(theme: crate::Theme) -> iced::Task<Message<M>> {
message::cosmic(super::cosmic::Message::AppThemeChange(theme))
}

View file

@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//
use std::borrow::Cow;
use crate::Element;

View file

@ -5,12 +5,9 @@ use std::borrow::Borrow;
use std::collections::HashMap;
use std::sync::Arc;
use super::{Application, ApplicationExt, Core, Subscription};
use crate::config::CosmicTk;
use crate::surface_message::{MessageWrapper, SurfaceMessage};
use super::{Action, Application, ApplicationExt, Subscription};
use crate::theme::{Theme, ThemeType, THEME};
use crate::widget::nav_bar;
use crate::{keyboard_nav, Element};
use crate::{keyboard_nav, Core, Element};
#[cfg(feature = "wayland")]
use cctk::sctk::reexports::csd_frame::{WindowManagerCapabilities, WindowState};
use cosmic_theme::ThemeMode;
@ -22,83 +19,23 @@ use iced::{window, Task};
use iced_futures::event::listen_with;
use palette::color_difference::EuclideanDistance;
/// A message managed internally by COSMIC.
#[derive(Clone, Debug)]
pub enum Message {
/// Application requests theme change.
AppThemeChange(Theme),
/// Requests to close the window.
Close,
/// Closes or shows the context drawer.
ContextDrawer(bool),
/// Requests to drag the window.
Drag,
/// Keyboard shortcuts managed by libcosmic.
KeyboardNav(keyboard_nav::Message),
/// Requests to maximize the window.
Maximize,
/// Requests to minimize the window.
Minimize,
/// Activates a navigation element from the nav bar.
NavBar(nav_bar::Id),
/// Activates a context menu for an item from the nav bar.
NavBarContext(nav_bar::Id),
/// Set scaling factor
ScaleFactor(f32),
/// Notification of system theme changes.
SystemThemeChange(Vec<&'static str>, Theme),
/// Notification of system theme mode changes.
SystemThemeModeChange(Vec<&'static str>, ThemeMode),
/// Toggles visibility of the nav bar.
ToggleNavBar,
/// Toggles the condensed status of the nav bar.
ToggleNavBarCondensed,
/// Toolkit configuration update
ToolkitConfig(CosmicTk),
/// Updates the window maximized state
WindowMaximized(window::Id, bool),
/// Updates the tracked window geometry.
WindowResize(window::Id, f32, f32),
/// Tracks updates to window state.
#[cfg(feature = "wayland")]
WindowState(window::Id, WindowState),
/// Capabilities the window manager supports
#[cfg(feature = "wayland")]
WmCapabilities(window::Id, WindowManagerCapabilities),
/// Notifies that a surface was closed.
/// Any data relating to the surface should be cleaned up.
SurfaceClosed(window::Id),
/// Activate the application
Activate(String),
ShowWindowMenu,
#[cfg(feature = "xdg-portal")]
DesktopSettings(crate::theme::portal::Desktop),
/// Window focus changed
Focus(window::Id),
/// Window focus lost
Unfocus(window::Id),
/// Tracks updates to window suggested size.
#[cfg(feature = "applet")]
SuggestedBounds(Option<iced::Size>),
}
#[derive(Default)]
pub struct Cosmic<App: Application> {
pub app: App,
#[cfg(feature = "wayland")]
pub surface_views: HashMap<
window::Id,
Box<dyn for<'a> Fn(&'a App) -> Element<'a, super::Message<App::Message>>>,
Box<dyn for<'a> Fn(&'a App) -> Element<'a, crate::Action<App::Message>>>,
>,
}
impl<T: Application> Cosmic<T>
where
T::Message: Send + 'static + Into<crate::surface_message::MessageWrapper<T::Message>>,
T::Message: Send + 'static,
{
pub fn init(
(mut core, flags): (Core, T::Flags),
) -> (Self, iced::Task<super::Message<T::Message>>) {
) -> (Self, iced::Task<crate::Action<T::Message>>) {
#[cfg(feature = "dbus-config")]
{
use iced_futures::futures::executor::block_on;
@ -122,11 +59,11 @@ where
pub fn surface_update(
&mut self,
_surface_message: SurfaceMessage,
) -> iced::Task<super::Message<T::Message>> {
_surface_message: crate::surface::Action,
) -> iced::Task<crate::Action<T::Message>> {
#[cfg(feature = "wayland")]
match _surface_message {
SurfaceMessage::AppSubsurface(settings, view) => {
crate::surface::Action::AppSubsurface(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings)
.ok()
.and_then(|s| s.downcast::<Box<dyn Fn(&mut T) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
@ -136,7 +73,7 @@ where
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::Message>>
dyn for<'a> Fn(&'a T) -> Element<'a, crate::Action<T::Message>>
+ Send
+ Sync,
>>() {
@ -154,7 +91,7 @@ where
iced_winit::commands::subsurface::get_subsurface(settings(&mut self.app))
}
}
SurfaceMessage::Subsurface(settings, view) => {
crate::surface::Action::Subsurface(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings)
.ok()
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync>>().ok()) else {
@ -164,7 +101,7 @@ where
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn() -> Element<'static, super::Message<T::Message>> + Send + Sync,
dyn Fn() -> Element<'static, crate::Action<T::Message>> + Send + Sync,
>>() {
Ok(v) => Some(v),
Err(err) => {
@ -180,7 +117,7 @@ where
iced_winit::commands::subsurface::get_subsurface(settings())
}
}
SurfaceMessage::AppPopup(settings, view) => {
crate::surface::Action::AppPopup(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings)
.ok()
.and_then(|s| s.downcast::<Box<dyn Fn(&mut T) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else {
@ -190,7 +127,7 @@ where
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn for<'a> Fn(&'a T) -> Element<'a, super::Message<T::Message>>
dyn for<'a> Fn(&'a T) -> Element<'a, crate::Action<T::Message>>
+ Send
+ Sync,
>>() {
@ -209,12 +146,14 @@ where
}
}
#[cfg(feature = "wayland")]
SurfaceMessage::DestroyPopup(id) => iced_winit::commands::popup::destroy_popup(id),
crate::surface::Action::DestroyPopup(id) => {
iced_winit::commands::popup::destroy_popup(id)
}
#[cfg(feature = "wayland")]
SurfaceMessage::DestroySubsurface(id) => {
crate::surface::Action::DestroySubsurface(id) => {
iced_winit::commands::subsurface::destroy_subsurface(id)
}
SurfaceMessage::ResponsiveMenuBar {
crate::surface::Action::ResponsiveMenuBar {
menu_bar,
limits,
size,
@ -223,7 +162,7 @@ where
core.menu_bars.insert(menu_bar, (limits, size));
iced::Task::none()
}
SurfaceMessage::Popup(settings, view) => {
crate::surface::Action::Popup(settings, view) => {
let Some(settings) = std::sync::Arc::try_unwrap(settings)
.ok()
.and_then(|s| s.downcast::<Box<dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings + Send + Sync>>().ok()) else {
@ -233,7 +172,7 @@ where
if let Some(view) = view.and_then(|view| {
match std::sync::Arc::try_unwrap(view).ok()?.downcast::<Box<
dyn Fn() -> Element<'static, super::Message<T::Message>> + Send + Sync,
dyn Fn() -> Element<'static, crate::Action<T::Message>> + Send + Sync,
>>() {
Ok(v) => Some(v),
Err(err) => {
@ -249,8 +188,10 @@ where
iced_winit::commands::popup::get_popup(settings())
}
}
SurfaceMessage::Ignore => iced::Task::none(),
SurfaceMessage::Task(f) => f().map(|sm| super::Message::Surface(sm)),
crate::surface::Action::Ignore => iced::Task::none(),
crate::surface::Action::Task(f) => {
f().map(|sm| crate::Action::Cosmic(Action::Surface(sm)))
}
}
#[cfg(not(feature = "wayland"))]
@ -259,19 +200,14 @@ where
pub fn update(
&mut self,
message: super::Message<T::Message>,
) -> iced::Task<super::Message<T::Message>> {
message: crate::Action<T::Message>,
) -> iced::Task<crate::Action<T::Message>> {
let message = match message {
super::Message::Surface(surface_message) => self.surface_update(surface_message),
super::Message::App(message) => match Into::<MessageWrapper<T::Message>>::into(message)
{
MessageWrapper::Surface(surface_message) => self.surface_update(surface_message),
MessageWrapper::Message(message) => self.app.update(message),
},
super::Message::Cosmic(message) => self.cosmic_update(message),
super::Message::None => iced::Task::none(),
crate::Action::App(message) => self.app.update(message),
crate::Action::Cosmic(message) => self.cosmic_update(message),
crate::Action::None => iced::Task::none(),
#[cfg(feature = "single-instance")]
super::Message::DbusActivation(message) => self.app.dbus_activation(message),
crate::Action::DbusActivation(message) => self.app.dbus_activation(message),
};
#[cfg(target_env = "gnu")]
@ -307,29 +243,29 @@ where
}
#[allow(clippy::too_many_lines)]
pub fn subscription(&self) -> Subscription<super::Message<T::Message>> {
pub fn subscription(&self) -> Subscription<crate::Action<T::Message>> {
let window_events = listen_with(|event, _, id| {
match event {
iced::Event::Window(window::Event::Resized(iced::Size { width, height })) => {
return Some(Message::WindowResize(id, width, height));
return Some(Action::WindowResize(id, width, height));
}
iced::Event::Window(window::Event::Closed) => {
return Some(Message::SurfaceClosed(id));
return Some(Action::SurfaceClosed(id));
}
iced::Event::Window(window::Event::Focused) => return Some(Message::Focus(id)),
iced::Event::Window(window::Event::Unfocused) => return Some(Message::Unfocus(id)),
iced::Event::Window(window::Event::Focused) => return Some(Action::Focus(id)),
iced::Event::Window(window::Event::Unfocused) => return Some(Action::Unfocus(id)),
#[cfg(feature = "wayland")]
iced::Event::PlatformSpecific(iced::event::PlatformSpecific::Wayland(event)) => {
match event {
wayland::Event::Popup(wayland::PopupEvent::Done, _, id)
| wayland::Event::Layer(wayland::LayerEvent::Done, _, id) => {
return Some(Message::SurfaceClosed(id));
return Some(Action::SurfaceClosed(id));
}
#[cfg(feature = "applet")]
wayland::Event::Window(
iced::event::wayland::WindowEvent::SuggestedBounds(b),
) => {
return Some(Message::SuggestedBounds(b));
return Some(Action::SuggestedBounds(b));
}
_ => (),
}
@ -341,7 +277,7 @@ where
});
let mut subscriptions = vec![
self.app.subscription().map(super::Message::App),
self.app.subscription().map(crate::Action::App),
self.app
.core()
.watch_config::<crate::config::CosmicTk>(crate::config::ID)
@ -354,7 +290,7 @@ where
tracing::error!(?why, "cosmic toolkit config update error");
}
super::Message::Cosmic(Message::ToolkitConfig(update.config))
crate::Action::Cosmic(Action::ToolkitConfig(update.config))
}),
self.app
.core()
@ -381,12 +317,12 @@ where
{
tracing::error!(?why, "cosmic theme config update error");
}
Message::SystemThemeChange(
Action::SystemThemeChange(
update.keys,
crate::theme::Theme::system(Arc::new(update.config)),
)
})
.map(super::Message::Cosmic),
.map(crate::Action::Cosmic),
self.app
.core()
.watch_config::<ThemeMode>(cosmic_theme::THEME_MODE_ID)
@ -398,21 +334,21 @@ where
{
tracing::error!(?error, "error reading system theme mode update");
}
Message::SystemThemeModeChange(update.keys, update.config)
Action::SystemThemeModeChange(update.keys, update.config)
})
.map(super::Message::Cosmic),
window_events.map(super::Message::Cosmic),
.map(crate::Action::Cosmic),
window_events.map(crate::Action::Cosmic),
#[cfg(feature = "xdg-portal")]
crate::theme::portal::desktop_settings()
.map(Message::DesktopSettings)
.map(super::Message::Cosmic),
.map(Action::DesktopSettings)
.map(crate::Action::Cosmic),
];
if self.app.core().keyboard_nav {
subscriptions.push(
keyboard_nav::subscription()
.map(Message::KeyboardNav)
.map(super::Message::Cosmic),
.map(Action::KeyboardNav)
.map(crate::Action::Cosmic),
);
}
@ -435,7 +371,7 @@ where
}
#[cfg(feature = "multi-window")]
pub fn view(&self, id: window::Id) -> Element<super::Message<T::Message>> {
pub fn view(&self, id: window::Id) -> Element<crate::Action<T::Message>> {
#[cfg(feature = "wayland")]
if let Some(v) = self.surface_views.get(&id) {
return v(&self.app);
@ -446,13 +382,13 @@ where
.main_window_id()
.is_some_and(|main_id| main_id == id)
{
return self.app.view_window(id).map(super::Message::App);
return self.app.view_window(id).map(crate::Action::App);
}
let view = if self.app.core().window.use_template {
self.app.view_main()
} else {
self.app.view().map(super::Message::App)
self.app.view().map(crate::Action::App)
};
#[cfg(target_env = "gnu")]
@ -462,7 +398,7 @@ where
}
#[cfg(not(feature = "multi-window"))]
pub fn view(&self) -> Element<super::Message<T::Message>> {
pub fn view(&self) -> Element<crate::Action<T::Message>> {
let view = self.app.view_main();
#[cfg(target_env = "gnu")]
@ -474,7 +410,7 @@ where
impl<T: Application> Cosmic<T> {
#[allow(clippy::unused_self)]
pub fn close(&mut self) -> iced::Task<super::Message<T::Message>> {
pub fn close(&mut self) -> iced::Task<crate::Action<T::Message>> {
if let Some(id) = self.app.core().main_window_id() {
iced::window::close(id)
} else {
@ -483,9 +419,9 @@ impl<T: Application> Cosmic<T> {
}
#[allow(clippy::too_many_lines)]
fn cosmic_update(&mut self, message: Message) -> iced::Task<super::Message<T::Message>> {
fn cosmic_update(&mut self, message: Action) -> iced::Task<crate::Action<T::Message>> {
match message {
Message::WindowMaximized(id, maximized) => {
Action::WindowMaximized(id, maximized) => {
if self
.app
.core()
@ -496,7 +432,7 @@ impl<T: Application> Cosmic<T> {
}
}
Message::WindowResize(id, width, height) => {
Action::WindowResize(id, width, height) => {
if self
.app
.core()
@ -511,12 +447,12 @@ impl<T: Application> Cosmic<T> {
//TODO: more efficient test of maximized (winit has no event for maximize if set by the OS)
return iced::window::get_maximized(id).map(move |maximized| {
super::Message::Cosmic(Message::WindowMaximized(id, maximized))
crate::Action::Cosmic(Action::WindowMaximized(id, maximized))
});
}
#[cfg(feature = "wayland")]
Message::WindowState(id, state) => {
Action::WindowState(id, state) => {
if self
.app
.core()
@ -536,7 +472,7 @@ impl<T: Application> Cosmic<T> {
}
#[cfg(feature = "wayland")]
Message::WmCapabilities(id, capabilities) => {
Action::WmCapabilities(id, capabilities) => {
if self
.app
.core()
@ -552,49 +488,49 @@ impl<T: Application> Cosmic<T> {
}
}
Message::KeyboardNav(message) => match message {
keyboard_nav::Message::FocusNext => {
return iced::widget::focus_next().map(super::Message::Cosmic)
Action::KeyboardNav(message) => match message {
keyboard_nav::Action::FocusNext => {
return iced::widget::focus_next().map(crate::Action::Cosmic)
}
keyboard_nav::Message::FocusPrevious => {
return iced::widget::focus_previous().map(super::Message::Cosmic)
keyboard_nav::Action::FocusPrevious => {
return iced::widget::focus_previous().map(crate::Action::Cosmic)
}
keyboard_nav::Message::Escape => return self.app.on_escape(),
keyboard_nav::Message::Search => return self.app.on_search(),
keyboard_nav::Action::Escape => return self.app.on_escape(),
keyboard_nav::Action::Search => return self.app.on_search(),
keyboard_nav::Message::Fullscreen => return self.app.core().toggle_maximize(None),
keyboard_nav::Action::Fullscreen => return self.app.core().toggle_maximize(None),
},
Message::ContextDrawer(show) => {
Action::ContextDrawer(show) => {
self.app.core_mut().set_show_context(show);
return self.app.on_context_drawer();
}
Message::Drag => return self.app.core().drag(None),
Action::Drag => return self.app.core().drag(None),
Message::Minimize => return self.app.core().minimize(None),
Action::Minimize => return self.app.core().minimize(None),
Message::Maximize => return self.app.core().toggle_maximize(None),
Action::Maximize => return self.app.core().toggle_maximize(None),
Message::NavBar(key) => {
Action::NavBar(key) => {
self.app.core_mut().nav_bar_set_toggled_condensed(false);
return self.app.on_nav_select(key);
}
Message::NavBarContext(key) => {
Action::NavBarContext(key) => {
self.app.core_mut().nav_bar_set_context(key);
return self.app.on_nav_context(key);
}
Message::ToggleNavBar => {
Action::ToggleNavBar => {
self.app.core_mut().nav_bar_toggle();
}
Message::ToggleNavBarCondensed => {
Action::ToggleNavBarCondensed => {
self.app.core_mut().nav_bar_toggle_condensed();
}
Message::AppThemeChange(mut theme) => {
Action::AppThemeChange(mut theme) => {
if let ThemeType::System { theme: _, .. } = theme.theme_type {
self.app.core_mut().theme_sub_counter += 1;
@ -610,7 +546,7 @@ impl<T: Application> Cosmic<T> {
THEME.lock().unwrap().set_theme(theme.theme_type);
}
Message::SystemThemeChange(keys, theme) => {
Action::SystemThemeChange(keys, theme) => {
let cur_is_dark = THEME.lock().unwrap().theme_type.is_dark();
// Ignore updates if the current theme mode does not match.
if cur_is_dark != theme.cosmic().is_dark {
@ -648,17 +584,17 @@ impl<T: Application> Cosmic<T> {
return cmd;
}
Message::ScaleFactor(factor) => {
Action::ScaleFactor(factor) => {
self.app.core_mut().set_scale_factor(factor);
}
Message::Close => {
Action::Close => {
return match self.app.on_app_exit() {
Some(message) => self.app.update(message),
None => self.close(),
};
}
Message::SystemThemeModeChange(keys, mode) => {
Action::SystemThemeModeChange(keys, mode) => {
if !keys.contains(&"is_dark") {
return iced::Task::none();
}
@ -710,7 +646,7 @@ impl<T: Application> Cosmic<T> {
}
return Task::batch(cmds);
}
Message::Activate(_token) =>
Action::Activate(_token) =>
{
#[cfg(feature = "wayland")]
if let Some(id) = self.app.core().main_window_id() {
@ -721,7 +657,10 @@ impl<T: Application> Cosmic<T> {
);
}
}
Message::SurfaceClosed(id) => {
Action::Surface(action) => return self.surface_update(action),
Action::SurfaceClosed(id) => {
let mut ret = if let Some(msg) = self.app.on_close_requested(id) {
self.app.update(msg)
} else {
@ -731,17 +670,19 @@ impl<T: Application> Cosmic<T> {
if core.exit_on_main_window_closed
&& core.main_window_id().is_some_and(|m_id| id == m_id)
{
ret = Task::batch(vec![iced::exit::<super::Message<T::Message>>()]);
ret = Task::batch(vec![iced::exit::<crate::Action<T::Message>>()]);
}
return ret;
}
Message::ShowWindowMenu => {
Action::ShowWindowMenu => {
if let Some(id) = self.app.core().main_window_id() {
return iced::window::show_system_menu(id);
}
}
#[cfg(feature = "xdg-portal")]
Message::DesktopSettings(crate::theme::portal::Desktop::ColorScheme(s)) => {
Action::DesktopSettings(crate::theme::portal::Desktop::ColorScheme(s)) => {
use ashpd::desktop::settings::ColorScheme;
if match THEME.lock().unwrap().theme_type {
ThemeType::System {
@ -781,7 +722,7 @@ impl<T: Application> Cosmic<T> {
}
}
#[cfg(feature = "xdg-portal")]
Message::DesktopSettings(crate::theme::portal::Desktop::Accent(c)) => {
Action::DesktopSettings(crate::theme::portal::Desktop::Accent(c)) => {
use palette::Srgba;
let c = Srgba::new(c.red() as f32, c.green() as f32, c.blue() as f32, 1.0);
let core = self.app.core_mut();
@ -810,11 +751,11 @@ impl<T: Application> Cosmic<T> {
}
}
#[cfg(feature = "xdg-portal")]
Message::DesktopSettings(crate::theme::portal::Desktop::Contrast(_)) => {
Action::DesktopSettings(crate::theme::portal::Desktop::Contrast(_)) => {
// TODO when high contrast is integrated in settings and all custom themes
}
Message::ToolkitConfig(config) => {
Action::ToolkitConfig(config) => {
// Change the icon theme if not defined by the application.
if !self.app.core().icon_theme_override
&& crate::icon_theme::default() != config.icon_theme
@ -825,18 +766,18 @@ impl<T: Application> Cosmic<T> {
*crate::config::COSMIC_TK.write().unwrap() = config;
}
Message::Focus(f) => {
Action::Focus(f) => {
self.app.core_mut().focused_window = Some(f);
}
Message::Unfocus(id) => {
Action::Unfocus(id) => {
let core = self.app.core_mut();
if core.focused_window.as_ref().is_some_and(|cur| *cur == id) {
core.focused_window = None;
}
}
#[cfg(feature = "applet")]
Message::SuggestedBounds(b) => {
Action::SuggestedBounds(b) => {
tracing::info!("Suggested bounds: {b:?}");
let core = self.app.core_mut();
core.applet.suggested_bounds = b;
@ -863,9 +804,9 @@ impl<App: Application> Cosmic<App> {
&mut self,
settings: iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings,
view: Box<
dyn for<'a> Fn(&'a App) -> Element<'a, super::Message<App::Message>> + Send + Sync,
dyn for<'a> Fn(&'a App) -> Element<'a, crate::Action<App::Message>> + Send + Sync,
>,
) -> Task<super::Message<App::Message>> {
) -> Task<crate::Action<App::Message>> {
use iced_winit::commands::subsurface::get_subsurface;
self.surface_views.insert(settings.id, view);
@ -878,9 +819,9 @@ impl<App: Application> Cosmic<App> {
&mut self,
settings: iced_runtime::platform_specific::wayland::popup::SctkPopupSettings,
view: Box<
dyn for<'a> Fn(&'a App) -> Element<'a, super::Message<App::Message>> + Send + Sync,
dyn for<'a> Fn(&'a App) -> Element<'a, crate::Action<App::Message>> + Send + Sync,
>,
) -> Task<super::Message<App::Message>> {
) -> Task<crate::Action<App::Message>> {
use iced_winit::commands::popup::get_popup;
self.surface_views.insert(settings.id, view);

View file

@ -6,243 +6,27 @@
//! Check out our [application](https://github.com/pop-os/libcosmic/tree/master/examples/application)
//! example in our repository.
pub mod command;
mod action;
pub use action::Action;
use cosmic_config::CosmicConfigEntry;
pub mod context_drawer;
mod core;
pub mod cosmic;
#[cfg(all(feature = "winit", feature = "multi-window"))]
pub(crate) mod multi_window;
pub mod settings;
pub mod message {
use crate::surface_message::SurfaceMessage;
pub type Task<M> = iced::Task<crate::Action<M>>;
#[derive(Clone, Debug)]
#[must_use]
pub enum Message<M> {
/// Messages from the application, for the application.
App(M),
/// Internal messages to be handled by libcosmic.
Cosmic(super::cosmic::Message),
#[cfg(feature = "single-instance")]
/// Dbus activation messages
DbusActivation(super::DbusActivationMessage),
/// Do nothing
None,
/// Internal surface message
Surface(SurfaceMessage),
}
pub const fn app<M>(message: M) -> Message<M> {
Message::App(message)
}
pub const fn cosmic<M>(message: super::cosmic::Message) -> Message<M> {
Message::Cosmic(message)
}
pub const fn none<M>() -> Message<M> {
Message::None
}
/// Used to produce a destroy popup message from within a widget.
#[cfg(feature = "wayland")]
#[must_use]
pub fn destroy_popup<Message>(id: iced_core::window::Id) -> Message
where
Message: From<SurfaceMessage> + 'static,
{
Message::from(SurfaceMessage::DestroyPopup(id))
}
#[cfg(feature = "wayland")]
#[must_use]
pub fn destroy_subsurface<M: From<SurfaceMessage>>(id: iced_core::window::Id) -> M {
let surface_msg = SurfaceMessage::DestroySubsurface(id);
M::from(surface_msg)
}
#[cfg(feature = "wayland")]
#[must_use]
pub fn app_popup<App: super::Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
view: Option<
Box<
dyn for<'a> Fn(&'a App) -> crate::Element<'a, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
>,
>,
) -> App::Message
where
App::Message:
Into<crate::surface_message::MessageWrapper<App::Message>> + From<SurfaceMessage>,
{
use std::{any::Any, sync::Arc};
use crate::surface_message::SurfaceMessage;
let boxed: Box<
dyn Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
App::Message::from(SurfaceMessage::AppPopup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
))
}
/// Used to create a subsurface message from within a widget.
#[cfg(feature = "wayland")]
#[must_use]
pub fn simple_subsurface<Message, V>(
settings: impl Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
view: Option<
Box<
dyn Fn() -> crate::Element<'static, super::Message<Message>>
+ Send
+ Sync
+ 'static,
>,
>,
) -> SurfaceMessage
where
Message: From<SurfaceMessage> + 'static,
V:,
{
use std::{any::Any, sync::Arc};
use crate::surface_message::SurfaceMessage;
let boxed: Box<
dyn Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
SurfaceMessage::Subsurface(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
)
}
/// Used to create a popup message from within a widget.
#[cfg(feature = "wayland")]
#[must_use]
pub fn simple_popup<Message, V>(
settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
view: Option<
impl Fn() -> crate::Element<'static, crate::app::Message<Message>> + Send + Sync + 'static,
>,
) -> SurfaceMessage
where
Message: From<SurfaceMessage> + 'static,
V:,
{
use std::{any::Any, sync::Arc};
use crate::surface_message::SurfaceMessage;
let boxed: Box<
dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
SurfaceMessage::Popup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
dyn Fn() -> crate::Element<'static, super::Message<Message>>
+ Send
+ Sync
+ 'static,
> = Box::new(view);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Arc::new(boxed)
}),
)
}
#[cfg(feature = "wayland")]
#[must_use]
pub fn subsurface<App: super::Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings + Send + Sync + 'static,
// XXX Boxed trait object is required for less cumbersome type inference, but we box it anyways.
view: Option<
Box<
dyn for<'a> Fn(&'a App) -> crate::Element<'a, super::Message<App::Message>>
+ Send
+ Sync
+ 'static,
>,
>,
) -> App::Message
where
App::Message:
Into<crate::surface_message::MessageWrapper<App::Message>> + From<SurfaceMessage>,
{
use crate::surface_message::SurfaceMessage;
use std::{any::Any, sync::Arc};
let boxed: Box<
dyn Fn(
&mut App,
)
-> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
App::Message::from(SurfaceMessage::AppSubsurface(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
))
}
impl<M> From<M> for Message<M> {
fn from(value: M) -> Self {
Self::App(value)
}
}
}
use std::borrow::Cow;
pub use self::command::Task;
pub use self::core::Core;
pub use self::settings::Settings;
use crate::prelude::*;
use crate::surface_message::SurfaceMessage;
use crate::theme::THEME;
use crate::widget::{container, horizontal_space, id_container, menu, nav_bar, popover};
pub use crate::Core;
use apply::Apply;
use context_drawer::ContextDrawer;
use iced::window;
use iced::{Length, Subscription};
pub use message::Message;
pub use settings::Settings;
use std::borrow::Cow;
use url::Url;
#[cfg(feature = "single-instance")]
use {
@ -319,10 +103,7 @@ pub(crate) fn iced_settings<App: Application>(
/// # Errors
///
/// Returns error on application failure.
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result
where
App::Message: Into<crate::surface_message::MessageWrapper<App::Message>>,
{
pub fn run<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result {
#[cfg(target_env = "gnu")]
if let Some(threshold) = settings.default_mmap_threshold {
crate::malloc::limit_mmap_threshold(threshold);
@ -510,11 +291,7 @@ impl DbusActivation {
pub fn run_single_instance<App: Application>(settings: Settings, flags: App::Flags) -> iced::Result
where
App::Flags: CosmicFlags,
App::Message: Clone
+ std::fmt::Debug
+ Send
+ Into<crate::surface_message::MessageWrapper<App::Message>>
+ 'static,
App::Message: Clone + std::fmt::Debug + Send + 'static,
{
let activation_token = std::env::var("XDG_ACTIVATION_TOKEN").ok();
@ -682,7 +459,7 @@ where
}
/// Allows overriding the default nav bar widget.
fn nav_bar(&self) -> Option<Element<Message<Self::Message>>> {
fn nav_bar(&self) -> Option<Element<crate::Action<Self::Message>>> {
if !self.core().nav_bar_active() {
return None;
}
@ -690,8 +467,8 @@ where
let nav_model = self.nav_model()?;
let mut nav =
crate::widget::nav_bar(nav_model, |id| Message::Cosmic(cosmic::Message::NavBar(id)))
.on_context(|id| Message::Cosmic(cosmic::Message::NavBarContext(id)))
crate::widget::nav_bar(nav_model, |id| crate::Action::Cosmic(Action::NavBar(id)))
.on_context(|id| crate::Action::Cosmic(Action::NavBarContext(id)))
.context_menu(self.nav_context_menu(self.core().nav_bar_context()))
.into_container()
// XXX both must be shrink to avoid flex layout from ignoring it
@ -706,7 +483,10 @@ where
}
/// Shows a context menu for the active nav bar item.
fn nav_context_menu(&self, id: nav_bar::Id) -> Option<Vec<menu::Tree<Message<Self::Message>>>> {
fn nav_context_menu(
&self,
id: nav_bar::Id,
) -> Option<Vec<menu::Tree<crate::Action<Self::Message>>>> {
None
}
@ -839,7 +619,21 @@ pub trait ApplicationExt: Application {
fn set_window_title(&mut self, title: String, id: window::Id) -> Task<Self::Message>;
/// View template for the main window.
fn view_main(&self) -> Element<Message<Self::Message>>;
fn view_main(&self) -> Element<crate::Action<Self::Message>>;
fn watch_config<T: CosmicConfigEntry + Send + Sync + Default + 'static + Clone + PartialEq>(
&self,
id: &'static str,
) -> iced::Subscription<cosmic_config::Update<T>> {
self.core().watch_config(id)
}
fn watch_state<T: CosmicConfigEntry + Send + Sync + Default + 'static + Clone + PartialEq>(
&self,
id: &'static str,
) -> iced::Subscription<cosmic_config::Update<T>> {
self.core().watch_state(id)
}
}
impl<App: Application> ApplicationExt for App {
@ -886,7 +680,7 @@ impl<App: Application> ApplicationExt for App {
#[allow(clippy::too_many_lines)]
/// Creates the view for the main window.
fn view_main(&self) -> Element<Message<Self::Message>> {
fn view_main(&self) -> Element<crate::Action<Self::Message>> {
let core = self.core();
let is_condensed = core.is_condensed();
// TODO: More granularity might be needed for different resize border
@ -953,13 +747,13 @@ impl<App: Application> ApplicationExt for App {
[0, 0, 0, 0]
})
.apply(Element::from)
.map(Message::App),
.map(crate::Action::App),
);
} else {
//TODO: container and padding are temporary, until
//the `resize_border` is moved to not cover window content
widgets.push(
container(main_content.map(Message::App))
container(main_content.map(crate::Action::App))
.padding(main_content_padding)
.into(),
);
@ -969,7 +763,7 @@ impl<App: Application> ApplicationExt for App {
//TODO: container and padding are temporary, until
//the `resize_border` is moved to not cover window content
widgets.push(
container(main_content.map(Message::App))
container(main_content.map(crate::Action::App))
.padding(main_content_padding)
.into(),
);
@ -985,7 +779,7 @@ impl<App: Application> ApplicationExt for App {
context_width,
)
.apply(Element::from)
.map(Message::App)
.map(crate::Action::App)
.apply(container)
.width(context_width)
.apply(|drawer| {
@ -1015,7 +809,7 @@ impl<App: Application> ApplicationExt for App {
.push(content_row)
.push_maybe(
self.footer()
.map(|footer| container(footer.map(Message::App)).padding([0, 8, 8, 8])),
.map(|footer| container(footer.map(crate::Action::App)).padding([0, 8, 8, 8])),
);
let content: Element<_> = if core.window.content_container {
content_col
@ -1035,45 +829,45 @@ impl<App: Application> ApplicationExt for App {
let mut header = crate::widget::header_bar()
.focused(focused)
.title(&core.window.header_title)
.on_drag(Message::Cosmic(cosmic::Message::Drag))
.on_right_click(Message::Cosmic(cosmic::Message::ShowWindowMenu))
.on_double_click(Message::Cosmic(cosmic::Message::Maximize));
.on_drag(crate::Action::Cosmic(Action::Drag))
.on_right_click(crate::Action::Cosmic(Action::ShowWindowMenu))
.on_double_click(crate::Action::Cosmic(Action::Maximize));
if self.nav_model().is_some() {
let toggle = crate::widget::nav_bar_toggle()
.active(core.nav_bar_active())
.selected(focused)
.on_toggle(if is_condensed {
Message::Cosmic(cosmic::Message::ToggleNavBarCondensed)
crate::Action::Cosmic(Action::ToggleNavBarCondensed)
} else {
Message::Cosmic(cosmic::Message::ToggleNavBar)
crate::Action::Cosmic(Action::ToggleNavBar)
});
header = header.start(toggle);
}
if core.window.show_close {
header = header.on_close(Message::Cosmic(cosmic::Message::Close));
header = header.on_close(crate::Action::Cosmic(Action::Close));
}
if core.window.show_maximize && crate::config::show_maximize() {
header = header.on_maximize(Message::Cosmic(cosmic::Message::Maximize));
header = header.on_maximize(crate::Action::Cosmic(Action::Maximize));
}
if core.window.show_minimize && crate::config::show_minimize() {
header = header.on_minimize(Message::Cosmic(cosmic::Message::Minimize));
header = header.on_minimize(crate::Action::Cosmic(Action::Minimize));
}
for element in self.header_start() {
header = header.start(element.map(Message::App));
header = header.start(element.map(crate::Action::App));
}
for element in self.header_center() {
header = header.center(element.map(Message::App));
header = header.center(element.map(crate::Action::App));
}
for element in self.header_end() {
header = header.end(element.map(Message::App));
header = header.end(element.map(crate::Action::App));
}
if content_container {
@ -1133,7 +927,7 @@ impl<App: Application> ApplicationExt for App {
.dialog()
.map(|w| Element::from(id_container(w, iced_core::id::Id::new("COSMIC_dialog"))))
{
popover = popover.popup(dialog.map(Message::App));
popover = popover.popup(dialog.map(crate::Action::App));
}
let view_element: Element<_> = popover.into();
@ -1142,7 +936,8 @@ impl<App: Application> ApplicationExt for App {
}
#[cfg(feature = "single-instance")]
fn single_instance_subscription<App: ApplicationExt>() -> Subscription<Message<App::Message>> {
fn single_instance_subscription<App: ApplicationExt>() -> Subscription<crate::Action<App::Message>>
{
use iced_futures::futures::StreamExt;
iced_futures::Subscription::run_with_id(
TypeId::of::<DbusActivation>(),
@ -1187,13 +982,13 @@ fn single_instance_subscription<App: ApplicationExt>() -> Subscription<Message<A
while let Some(mut msg) = rx.next().await {
if let Some(token) = msg.activation_token.take() {
if let Err(err) = output
.send(Message::Cosmic(cosmic::Message::Activate(token)))
.send(crate::Action::Cosmic(Action::Activate(token)))
.await
{
tracing::error!(?err, "Failed to send message");
}
}
if let Err(err) = output.send(Message::DbusActivation(msg)).await {
if let Err(err) = output.send(crate::Action::DbusActivation(msg)).await {
tracing::error!(?err, "Failed to send message");
}
}

View file

@ -1,3 +1,6 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Create and run daemons that run in the background.
//! Copied from iced 0.13, but adds optional initial window

View file

@ -11,7 +11,6 @@ use crate::{
window, Color, Length, Limits, Rectangle,
},
iced_widget,
surface_message::SurfaceMessage,
theme::{self, system_dark, system_light, Button, THEME},
widget::{
self,
@ -220,21 +219,19 @@ impl Context {
)
}
pub fn applet_tooltip<
'a,
Message: 'static + From<SurfaceMessage> + Into<crate::surface_message::MessageWrapper<Message>>,
>(
pub fn applet_tooltip<'a, Message: 'static>(
&self,
content: impl Into<Element<'a, Message>>,
tooltip: impl Into<Cow<'static, str>>,
has_popup: bool,
) -> crate::widget::wayland::tooltip::widget::Tooltip<'a, Message, Message> {
on_surface_action: impl Fn(crate::surface::Action) -> Message + 'static,
) -> crate::widget::wayland::tooltip::widget::Tooltip<'a, Message> {
let window_id = *TOOLTIP_WINDOW_ID;
let subsurface_id = TOOLTIP_ID.clone();
let anchor = self.anchor;
let tooltip = tooltip.into();
crate::widget::wayland::tooltip::widget::Tooltip::<'a, Message, Message>::new(
crate::widget::wayland::tooltip::widget::Tooltip::<'a, Message>::new(
content,
(!has_popup).then_some(move |bounds: Rectangle| {
let window_id = window_id;
@ -273,14 +270,15 @@ impl Context {
}
}),
move || {
Element::<'static, crate::app::Message<Message>>::from(autosize::autosize(
Element::from(autosize::autosize(
layer_container(crate::widget::text(tooltip.clone()))
.layer(crate::cosmic_theme::Layer::Background)
.padding(4.),
subsurface_id.clone(),
))
},
crate::app::message::destroy_popup::<Message>(window_id),
on_surface_action(crate::surface::action::destroy_popup(window_id)),
on_surface_action,
)
.delay(Duration::from_millis(100))
}
@ -443,10 +441,7 @@ impl Context {
/// # Errors
///
/// Returns error on application failure.
pub fn run<App: Application>(flags: App::Flags) -> iced::Result
where
App::Message: Into<crate::surface_message::MessageWrapper<App::Message>>,
{
pub fn run<App: Application>(flags: App::Flags) -> iced::Result {
let helper = Context::default();
let mut settings = helper.window_settings();

43
src/command.rs Normal file
View file

@ -0,0 +1,43 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use iced::window;
/// Initiates a window drag.
pub fn drag<M>(id: window::Id) -> iced::Task<crate::Action<M>> {
iced_runtime::window::drag(id)
}
/// Maximizes the window.
pub fn maximize<M>(id: window::Id, maximized: bool) -> iced::Task<crate::Action<M>> {
iced_runtime::window::maximize(id, maximized)
}
/// Minimizes the window.
pub fn minimize<M>(id: window::Id) -> iced::Task<crate::Action<M>> {
iced_runtime::window::minimize(id, true)
}
/// Sets the title of a window.
#[allow(unused_variables, clippy::needless_pass_by_value)]
pub fn set_title<M>(id: window::Id, title: String) -> iced::Task<crate::Action<M>> {
iced::Task::none()
}
pub fn set_scaling_factor<M: Send + 'static>(factor: f32) -> iced::Task<crate::Action<M>> {
iced::Task::done(crate::app::Action::ScaleFactor(factor)).map(crate::Action::Cosmic)
}
pub fn set_theme<M: Send + 'static>(theme: crate::Theme) -> iced::Task<crate::Action<M>> {
iced::Task::done(crate::app::Action::AppThemeChange(theme)).map(crate::Action::Cosmic)
}
/// Sets the window mode to windowed.
pub fn set_windowed<M>(id: window::Id) -> iced::Task<crate::Action<M>> {
iced_runtime::window::change_mode(id, window::Mode::Windowed)
}
/// Toggles the windows' maximize state.
pub fn toggle_maximize<M>(id: window::Id) -> iced::Task<crate::Action<M>> {
iced_runtime::window::toggle_maximize(id)
}

View file

@ -3,15 +3,11 @@
use std::collections::HashMap;
use crate::widget::{
button, icon,
menu::{self},
nav_bar, responsive_container,
};
use crate::widget::nav_bar;
use cosmic_config::CosmicConfigEntry;
use cosmic_theme::ThemeMode;
use iced::{window, Limits, Size};
use iced_core::{window::Id, Element};
use iced_core::window::Id;
use palette::Srgba;
use slotmap::Key;
@ -250,6 +246,10 @@ impl Core {
}
}
pub fn main_window_is(&self, id: iced::window::Id) -> bool {
self.main_window_id().is_some_and(|main_id| main_id == id)
}
/// Whether the nav panel is visible or not
#[must_use]
pub fn nav_bar_active(&self) -> bool {
@ -382,75 +382,57 @@ impl Core {
id
}
#[cfg(feature = "surface-message")]
/// # Panics
///
/// Will panic if the menu bar collapses without tracking the size
pub fn responsive_menu_bar<
'a,
Message: Clone
+ From<crate::surface_message::SurfaceMessage>
+ Into<crate::surface_message::MessageWrapper<Message>>
+ 'static,
A: menu::Action<Message = Message>,
>(
&self,
key_binds: &HashMap<menu::KeyBind, A>,
id: crate::widget::Id,
trees: Vec<(
std::borrow::Cow<'static, str>,
Vec<menu::Item<A, std::borrow::Cow<'static, str>>>,
)>,
) -> crate::Element<'a, Message> {
use crate::widget::id_container;
pub fn drag<M: Send + 'static>(&self, id: Option<window::Id>) -> crate::app::Task<M> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::command::drag(id)
}
let menu_bar_size = self.menu_bars.get(&id);
#[allow(clippy::if_not_else)]
if !menu_bar_size.is_some_and(|(limits, size)| {
let max_size = limits.max();
max_size.width < size.width
}) {
crate::Element::from(responsive_container::responsive_container(
id_container(
menu::bar(
trees
.into_iter()
.map(|mt| {
menu::Tree::<_>::with_children(
menu::root(mt.0),
menu::items(key_binds, mt.1),
)
})
.collect(),
),
crate::widget::Id::new(format!("menu_bar_expanded_{id}")),
),
id,
))
} else {
crate::Element::from(
responsive_container::responsive_container(
id_container(
menu::bar(vec![menu::Tree::<_>::with_children(
Element::from(
button::icon(icon::from_name("open-menu-symbolic"))
.padding([4, 12])
.class(crate::theme::Button::MenuRoot),
),
menu::items(
key_binds,
trees
.into_iter()
.map(|mt| menu::Item::Folder(mt.0, mt.1))
.collect(),
),
)]),
crate::widget::Id::new(format!("menu_bar_collapsed_{id}")),
),
id,
)
.size(menu_bar_size.unwrap().1),
)
}
pub fn maximize<M: Send + 'static>(
&self,
id: Option<window::Id>,
maximized: bool,
) -> crate::app::Task<M> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::command::maximize(id, maximized)
}
pub fn minimize<M: Send + 'static>(&self, id: Option<window::Id>) -> crate::app::Task<M> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::command::minimize(id)
}
pub fn set_title<M: Send + 'static>(
&self,
id: Option<window::Id>,
title: String,
) -> crate::app::Task<M> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::command::set_title(id, title)
}
pub fn set_windowed<M: Send + 'static>(&self, id: Option<window::Id>) -> crate::app::Task<M> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::command::set_windowed(id)
}
pub fn toggle_maximize<M: Send + 'static>(
&self,
id: Option<window::Id>,
) -> crate::app::Task<M> {
let Some(id) = id.or(self.main_window) else {
return iced::Task::none();
};
crate::command::toggle_maximize(id)
}
}

View file

@ -8,7 +8,7 @@ use iced_core::keyboard::key::Named;
use iced_futures::event::listen_raw;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Message {
pub enum Action {
Escape,
FocusNext,
FocusPrevious,
@ -16,7 +16,7 @@ pub enum Message {
Search,
}
pub fn subscription() -> Subscription<Message> {
pub fn subscription() -> Subscription<Action> {
listen_raw(|event, status, _| {
if event::Status::Ignored != status {
return None;
@ -30,18 +30,18 @@ pub fn subscription() -> Subscription<Message> {
}) => match key {
Named::Tab if !modifiers.control() => {
return Some(if modifiers.shift() {
Message::FocusPrevious
Action::FocusPrevious
} else {
Message::FocusNext
Action::FocusNext
});
}
Named::Escape => {
return Some(Message::Escape);
return Some(Action::Escape);
}
Named::F11 => {
return Some(Message::Fullscreen);
return Some(Action::Fullscreen);
}
_ => (),
@ -51,7 +51,7 @@ pub fn subscription() -> Subscription<Message> {
modifiers,
..
}) if c == "f" && modifiers.control() => {
return Some(Message::Search);
return Some(Action::Search);
}
_ => (),

View file

@ -9,19 +9,31 @@ pub mod prelude {
pub use crate::ext::*;
#[cfg(feature = "winit")]
pub use crate::ApplicationExt;
pub use crate::{Also, Apply, Element, Renderer, Theme};
pub use crate::{Also, Apply, Element, Renderer, Task, Theme};
}
pub use apply::{Also, Apply};
/// Actions are managed internally by the cosmic runtime.
pub mod action;
pub use action::Action;
#[cfg(feature = "winit")]
pub mod app;
#[cfg(feature = "winit")]
#[doc(inline)]
pub use app::{Application, ApplicationExt};
#[cfg(feature = "applet")]
pub mod applet;
pub mod command;
/// State which is managed by the cosmic runtime.
pub mod core;
#[doc(inline)]
pub use core::Core;
pub use iced::Task;
pub mod task;
@ -85,7 +97,7 @@ pub mod process;
#[cfg(feature = "wayland")]
pub use cctk;
pub mod surface_message;
pub mod surface;
pub mod theme;

View file

@ -1,3 +1,6 @@
// Copyright 2025 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use std::os::raw::c_int;
const M_MMAP_THRESHOLD: c_int = -3;

View file

@ -1,3 +1,6 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
#[cfg(all(feature = "smol", not(feature = "tokio")))]
use smol::io::AsyncReadExt;
use std::io;

150
src/surface/action.rs Normal file
View file

@ -0,0 +1,150 @@
// Copyright 2025 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use super::Action;
use crate::Application;
use std::{any::Any, sync::Arc};
/// Used to produce a destroy popup message from within a widget.
#[cfg(feature = "wayland")]
#[must_use]
pub fn destroy_popup(id: iced_core::window::Id) -> Action {
Action::DestroyPopup(id)
}
#[cfg(feature = "wayland")]
#[must_use]
pub fn destroy_subsurface(id: iced_core::window::Id) -> Action {
Action::DestroySubsurface(id)
}
#[cfg(feature = "wayland")]
#[must_use]
pub fn app_popup<App: Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
view: Option<
Box<
dyn for<'a> Fn(&'a App) -> crate::Element<'a, crate::Action<App::Message>>
+ Send
+ Sync
+ 'static,
>,
>,
) -> Action {
let boxed: Box<
dyn Fn(&mut App) -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Action::AppPopup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
)
}
/// Used to create a subsurface message from within a widget.
#[cfg(feature = "wayland")]
#[must_use]
pub fn simple_subsurface<Message: 'static, V>(
settings: impl Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings
+ Send
+ Sync
+ 'static,
view: Option<
Box<dyn Fn() -> crate::Element<'static, crate::Action<Message>> + Send + Sync + 'static>,
>,
) -> Action {
let boxed: Box<
dyn Fn() -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Action::Subsurface(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
)
}
/// Used to create a popup message from within a widget.
#[cfg(feature = "wayland")]
#[must_use]
pub fn simple_popup<Message: 'static, V>(
settings: impl Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
view: Option<
impl Fn() -> crate::Element<'static, crate::Action<Message>> + Send + Sync + 'static,
>,
) -> Action {
let boxed: Box<
dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Action::Popup(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<
dyn Fn() -> crate::Element<'static, crate::Action<Message>> + Send + Sync + 'static,
> = Box::new(view);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Arc::new(boxed)
}),
)
}
#[cfg(feature = "wayland")]
#[must_use]
pub fn subsurface<App: Application>(
settings: impl Fn(&mut App) -> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings
+ Send
+ Sync
+ 'static,
// XXX Boxed trait object is required for less cumbersome type inference, but we box it anyways.
view: Option<
Box<
dyn for<'a> Fn(&'a App) -> crate::Element<'a, crate::Action<App::Message>>
+ Send
+ Sync
+ 'static,
>,
>,
) -> Action {
let boxed: Box<
dyn Fn(
&mut App,
)
-> iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings
+ Send
+ Sync
+ 'static,
> = Box::new(settings);
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
Action::AppSubsurface(
Arc::new(boxed),
view.map(|view| {
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(view);
Arc::new(boxed)
}),
)
}

View file

@ -1,26 +1,17 @@
// Copyright 2025 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
pub mod action;
use iced::Limits;
use iced::Size;
use iced::Task;
use std::future::Future;
use std::sync::Arc;
use iced::Size;
use iced::Limits;
use iced::Task;
pub enum MessageWrapper<M> {
Surface(SurfaceMessage),
Message(M),
}
#[cfg(not(feature = "surface-message"))]
impl<M> From<M> for MessageWrapper<M> {
fn from(value: M) -> Self {
MessageWrapper::Message(value)
}
}
/// Ignore this message in your application. It will be intercepted.
#[derive(Clone)]
pub enum SurfaceMessage {
pub enum Action {
/// Create a subsurface with a view function accepting the App as a parameter
AppSubsurface(
std::sync::Arc<Box<dyn std::any::Any + Send + Sync>>,
@ -55,10 +46,10 @@ pub enum SurfaceMessage {
size: Size,
},
Ignore,
Task(Arc<dyn Fn() -> Task<SurfaceMessage> + Send + Sync>),
Task(Arc<dyn Fn() -> Task<Action> + Send + Sync>),
}
impl std::fmt::Debug for SurfaceMessage {
impl std::fmt::Debug for Action {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::AppSubsurface(arg0, arg1) => f

25
src/task.rs Normal file
View file

@ -0,0 +1,25 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Create asynchronous actions to be performed in the background.
use std::future::Future;
/// Yields a task which contains a batch of tasks.
pub fn batch<X: Send + 'static + Into<Y>, Y: Send + 'static>(
tasks: impl IntoIterator<Item = iced::Task<X>>,
) -> iced::Task<Y> {
iced::Task::batch(tasks).map(Into::into)
}
/// Yields a task which will run the future on the runtime executor.
pub fn future<X: Into<Y>, Y: 'static>(
future: impl Future<Output = X> + Send + 'static,
) -> iced::Task<Y> {
iced::Task::future(async move { future.await.into() })
}
/// Yields a task which will return a message.
pub fn message<X: Send + 'static + Into<Y>, Y: 'static>(message: X) -> iced::Task<Y> {
future(async move { message.into() })
}

View file

@ -1,57 +0,0 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Create asynchronous actions to be performed in the background.
use iced::window;
use iced::Task;
use iced_core::window::Mode;
use std::future::Future;
/// Yields a task which contains a batch of tasks.
pub fn batch<X: Send + 'static + Into<Y>, Y: Send + 'static>(
tasks: impl IntoIterator<Item = Task<X>>,
) -> Task<Y> {
Task::batch(tasks).map(Into::into)
}
/// Yields a task which will run the future on the runtime executor.
pub fn future<X: Into<Y>, Y: 'static>(future: impl Future<Output = X> + Send + 'static) -> Task<Y> {
Task::future(async move { future.await.into() })
}
/// Yields a task which will return a message.
pub fn message<X: Send + 'static + Into<Y>, Y: 'static>(message: X) -> Task<Y> {
future(async move { message.into() })
}
/// Initiates a window drag.
pub fn drag<M>(id: window::Id) -> Task<M> {
iced_runtime::window::drag(id)
}
/// Maximizes the window.
pub fn maximize<M>(id: window::Id, maximized: bool) -> Task<M> {
iced_runtime::window::maximize(id, maximized)
}
/// Minimizes the window.
pub fn minimize<M>(id: window::Id) -> Task<M> {
iced_runtime::window::minimize(id, true)
}
/// Sets the title of a window.
#[allow(unused_variables, clippy::needless_pass_by_value)]
pub fn set_title<M>(id: window::Id, title: String) -> Task<M> {
Task::none()
}
/// Sets the window mode to windowed.
pub fn set_windowed<M>(id: window::Id) -> Task<M> {
iced_runtime::window::change_mode(id, Mode::Windowed)
}
/// Toggles the windows' maximize state.
pub fn toggle_maximize<M>(id: window::Id) -> Task<M> {
iced_runtime::window::toggle_maximize(id)
}

View file

@ -100,6 +100,12 @@ pub mod autosize;
#[cfg(feature = "winit")]
pub(crate) mod responsive_container;
#[cfg(feature = "surface-message")]
mod responsive_menu_bar;
#[cfg(feature = "surface-message")]
#[doc(inline)]
pub use responsive_menu_bar::responsive_menu_bar;
pub mod button;
#[doc(inline)]
pub use button::{Button, IconButton, LinkButton, TextButton};

View file

@ -9,18 +9,17 @@ use iced_core::renderer;
use iced_core::widget::{tree, Id, Tree};
use iced_core::{Clipboard, Element, Layout, Length, Rectangle, Shell, Vector, Widget};
use crate::surface_message::SurfaceMessage;
pub(crate) fn responsive_container<'a, Message: 'static + From<SurfaceMessage>, Theme, E>(
pub(crate) fn responsive_container<'a, Message: 'static, Theme, E>(
content: E,
id: Id,
on_action: impl Fn(crate::surface::Action) -> Message + 'static,
) -> ResponsiveContainer<'a, Message, Theme, crate::Renderer>
where
E: Into<Element<'a, Message, Theme, crate::Renderer>>,
Theme: iced_widget::container::Catalog,
<Theme as iced_widget::container::Catalog>::Class<'a>: From<crate::theme::Container<'a>>,
{
ResponsiveContainer::new(content, id)
ResponsiveContainer::new(content, id, on_action)
}
/// An element decorating some content.
@ -34,6 +33,7 @@ where
content: Element<'a, Message, Theme, Renderer>,
id: Id,
size: Option<Size>,
on_action: Box<dyn Fn(crate::surface::Action) -> Message>,
}
impl<'a, Message, Theme, Renderer> ResponsiveContainer<'a, Message, Theme, Renderer>
@ -41,7 +41,11 @@ where
Renderer: iced_core::Renderer,
{
/// Creates an empty [`IdContainer`].
pub(crate) fn new<T>(content: T, id: Id) -> Self
pub(crate) fn new<T>(
content: T,
id: Id,
on_action: impl Fn(crate::surface::Action) -> Message + 'static,
) -> Self
where
T: Into<Element<'a, Message, Theme, Renderer>>,
{
@ -49,6 +53,7 @@ where
content: content.into(),
id,
size: None,
on_action: Box::new(on_action),
}
}
@ -62,7 +67,6 @@ impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for ResponsiveContainer<'_, Message, Theme, Renderer>
where
Renderer: iced_core::Renderer,
Message: From<SurfaceMessage>,
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
@ -148,11 +152,13 @@ where
let state = tree.state.downcast_mut::<State>();
if state.needs_update {
shell.publish(Message::from(SurfaceMessage::ResponsiveMenuBar {
menu_bar: self.id.clone(),
limits: state.limits,
size: state.size,
}));
shell.publish((self.on_action)(
crate::surface::Action::ResponsiveMenuBar {
menu_bar: self.id.clone(),
limits: state.limits,
size: state.size,
},
));
state.needs_update = false;
}
@ -264,7 +270,7 @@ where
impl<'a, Message, Theme, Renderer> From<ResponsiveContainer<'a, Message, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer>
where
Message: 'a + From<SurfaceMessage>,
Message: 'a,
Renderer: 'a + iced_core::Renderer,
Theme: 'a,
{

View file

@ -0,0 +1,78 @@
use std::collections::HashMap;
use apply::Apply;
use crate::{
widget::{button, icon, responsive_container},
Core, Element,
};
use super::menu;
/// # Panics
///
/// Will panic if the menu bar collapses without tracking the size
pub fn responsive_menu_bar<'a, Message: Clone + 'static, A: menu::Action<Message = Message>>(
core: &Core,
key_binds: &HashMap<menu::KeyBind, A>,
id: crate::widget::Id,
action_message: impl Fn(crate::surface::Action) -> Message + 'static,
trees: Vec<(
std::borrow::Cow<'static, str>,
Vec<menu::Item<A, std::borrow::Cow<'static, str>>>,
)>,
) -> Element<'a, Message> {
use crate::widget::id_container;
let menu_bar_size = core.menu_bars.get(&id);
#[allow(clippy::if_not_else)]
if !menu_bar_size.is_some_and(|(limits, size)| {
let max_size = limits.max();
max_size.width < size.width
}) {
responsive_container::responsive_container(
id_container(
menu::bar(
trees
.into_iter()
.map(|mt| {
menu::Tree::<_>::with_children(
menu::root(mt.0),
menu::items(key_binds, mt.1),
)
})
.collect(),
),
crate::widget::Id::new(format!("menu_bar_expanded_{id}")),
),
id,
action_message,
)
.apply(Element::from)
} else {
responsive_container::responsive_container(
id_container(
menu::bar(vec![menu::Tree::<_>::with_children(
Element::from(
button::icon(icon::from_name("open-menu-symbolic"))
.padding([4, 12])
.class(crate::theme::Button::MenuRoot),
),
menu::items(
key_binds,
trees
.into_iter()
.map(|mt| menu::Item::Folder(mt.0, mt.1))
.collect(),
),
)]),
crate::widget::Id::new(format!("menu_bar_collapsed_{id}")),
),
id,
action_message,
)
.size(menu_bar_size.unwrap().1)
.apply(Element::from)
}
}

View file

@ -14,7 +14,7 @@ use iced::Task;
use iced_runtime::core::widget::Id;
use iced_core::event::{self, Event};
use iced_core::renderer::{self, Renderer};
use iced_core::renderer;
use iced_core::touch;
use iced_core::widget::tree::{self, Tree};
use iced_core::widget::Operation;
@ -25,8 +25,6 @@ use iced_core::{
Background, Clipboard, Color, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget,
};
use crate::surface_message::SurfaceMessage;
pub use super::{Catalog, Style};
/// Internally defines different button widget variants.
@ -41,7 +39,7 @@ enum Variant<Message> {
/// A generic button which emits a message when pressed.
#[allow(missing_debug_implementations)]
#[must_use]
pub struct Tooltip<'a, Message, AppMessage> {
pub struct Tooltip<'a, Message> {
id: Id,
#[cfg(feature = "a11y")]
name: Option<std::borrow::Cow<'a, str>>,
@ -51,6 +49,7 @@ pub struct Tooltip<'a, Message, AppMessage> {
label: Option<Vec<iced_accessibility::accesskit::NodeId>>,
content: crate::Element<'a, Message>,
on_leave: Message,
on_surface_action: Box<dyn Fn(crate::surface::Action) -> Message>,
width: Length,
height: Length,
padding: Padding,
@ -65,15 +64,10 @@ pub struct Tooltip<'a, Message, AppMessage> {
+ 'static,
>,
>,
view: Arc<
dyn Fn() -> crate::Element<'static, crate::app::Message<AppMessage>>
+ Send
+ Sync
+ 'static,
>,
view: Arc<dyn Fn() -> crate::Element<'static, Message> + Send + Sync + 'static>,
}
impl<'a, Message, AppMessage> Tooltip<'a, Message, AppMessage> {
impl<'a, Message> Tooltip<'a, Message> {
/// Creates a new [`Tooltip`] with the given content.
pub fn new(
content: impl Into<crate::Element<'a, Message>>,
@ -83,11 +77,9 @@ impl<'a, Message, AppMessage> Tooltip<'a, Message, AppMessage> {
+ Sync
+ 'static,
>,
view: impl Fn() -> crate::Element<'static, crate::app::Message<AppMessage>>
+ Send
+ Sync
+ 'static,
view: impl Fn() -> crate::Element<'static, Message> + Send + Sync + 'static,
on_leave: Message,
on_surface_action: impl Fn(crate::surface::Action) -> Message + 'static,
) -> Self {
Self {
id: Id::unique(),
@ -104,6 +96,7 @@ impl<'a, Message, AppMessage> Tooltip<'a, Message, AppMessage> {
selected: false,
style: crate::theme::Tooltip::default(),
on_leave,
on_surface_action: Box::new(on_surface_action),
delay: None,
settings: if let Some(s) = settings {
Some(Arc::new(s))
@ -189,11 +182,8 @@ impl<'a, Message, AppMessage> Tooltip<'a, Message, AppMessage> {
}
}
impl<
'a,
Message: 'a + Clone + From<SurfaceMessage>,
AppMessage: Clone + 'static + From<SurfaceMessage>,
> Widget<Message, crate::Theme, crate::Renderer> for Tooltip<'a, Message, AppMessage>
impl<'a, Message: 'static + Clone> Widget<Message, crate::Theme, crate::Renderer>
for Tooltip<'a, Message>
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
@ -273,6 +263,7 @@ impl<
&self.view,
self.delay,
&self.on_leave,
&self.on_surface_action,
|| tree.state.downcast_mut::<State>(),
);
status.merge(self.content.as_widget_mut().on_event(
@ -390,13 +381,8 @@ impl<
}
}
impl<
'a,
Message: From<SurfaceMessage> + Clone + 'a,
AppMessage: From<SurfaceMessage> + Clone + 'static,
> From<Tooltip<'a, Message, AppMessage>> for crate::Element<'a, Message>
{
fn from(button: Tooltip<'a, Message, AppMessage>) -> Self {
impl<'a, Message: Clone + 'static> From<Tooltip<'a, Message>> for crate::Element<'a, Message> {
fn from(button: Tooltip<'a, Message>) -> Self {
Self::new(button)
}
}
@ -419,11 +405,7 @@ impl State {
/// Processes the given [`Event`] and updates the [`State`] of a [`Tooltip`]
/// accordingly.
#[allow(clippy::needless_pass_by_value)]
pub fn update<
'a,
Message: Clone + From<SurfaceMessage>,
AppMessage: Clone + 'a + From<SurfaceMessage> + 'static,
>(
pub fn update<'a, Message: Clone + 'static>(
_id: Id,
event: Event,
layout: Layout<'_>,
@ -437,14 +419,10 @@ pub fn update<
+ 'static,
>,
>,
view: &Arc<
dyn Fn() -> crate::Element<'static, crate::app::Message<AppMessage>>
+ Send
+ Sync
+ 'static,
>,
view: &Arc<dyn Fn() -> crate::Element<'static, Message> + Send + Sync + 'static>,
delay: Option<Duration>,
on_leave: &Message,
on_surface_action: &dyn Fn(crate::surface::Action) -> Message,
state: impl FnOnce() -> &'a mut State,
) -> event::Status {
match event {
@ -491,46 +469,44 @@ pub fn update<
let view = view.clone();
let bounds = layout.bounds();
shell.publish(
SurfaceMessage::Task(Arc::new(move || {
let s = s.clone();
let view = view.clone();
let is_hovered = is_hovered.clone();
Task::future(async move {
_ = tokio::time::sleep(delay).await;
let sm = crate::surface::Action::Task(Arc::new(move || {
let s = s.clone();
let view = view.clone();
let is_hovered = is_hovered.clone();
Task::future(async move {
_ = tokio::time::sleep(delay).await;
let is_hovered = is_hovered.clone();
let g = is_hovered.lock().unwrap();
if !*g {
return SurfaceMessage::Ignore;
}
let boxed: Box<
dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(move || s(bounds));
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
SurfaceMessage::Popup(
Arc::new(boxed),
Some({
let boxed: Box<
dyn Fn() -> crate::Element<
'static,
crate::app::Message<AppMessage>,
> + Send
+ Sync
+ 'static,
> = Box::new(move || view());
let boxed: Box<dyn Any + Send + Sync + 'static> =
Box::new(boxed);
Arc::new(boxed)
}),
)
})
}))
.into(),
);
let is_hovered = is_hovered.clone();
let g = is_hovered.lock().unwrap();
if !*g {
return crate::surface::Action::Ignore;
}
let boxed: Box<
dyn Fn() -> iced_runtime::platform_specific::wayland::popup::SctkPopupSettings
+ Send
+ Sync
+ 'static,
> = Box::new(move || s(bounds));
let boxed: Box<dyn Any + Send + Sync + 'static> =
Box::new(boxed);
crate::surface::Action::Popup(
Arc::new(boxed),
Some({
let boxed: Box<
dyn Fn() -> crate::Element<'static, Message>
+ Send
+ Sync
+ 'static,
> = Box::new(move || view());
let boxed: Box<dyn Any + Send + Sync + 'static> =
Box::new(boxed);
Arc::new(boxed)
}),
)
})
}));
shell.publish((on_surface_action)(sm));
} else {
let s = settings.clone();
let view = view.clone();
@ -544,14 +520,12 @@ pub fn update<
> = Box::new(move || s(bounds));
let boxed: Box<dyn Any + Send + Sync + 'static> = Box::new(boxed);
let sm = SurfaceMessage::Popup(
let sm = crate::surface::Action::Popup(
Arc::new(boxed),
Some({
let boxed: Box<
dyn Fn() -> crate::Element<
'static,
crate::app::Message<AppMessage>,
> + Send
dyn Fn() -> crate::Element<'static, Message>
+ Send
+ Sync
+ 'static,
> = Box::new(move || view());
@ -560,7 +534,7 @@ pub fn update<
Arc::new(boxed)
}),
);
shell.publish(sm.into());
shell.publish((on_surface_action)(sm));
}
}
}