Skip to main content

cosmic/
action.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4#[cfg(feature = "winit")]
5use crate::app;
6#[cfg(feature = "single-instance")]
7use crate::dbus_activation;
8
9pub const fn app<M>(message: M) -> Action<M> {
10    Action::App(message)
11}
12#[cfg(feature = "winit")]
13pub const fn cosmic<M>(message: app::Action) -> Action<M> {
14    Action::Cosmic(message)
15}
16
17pub const fn none<M>() -> Action<M> {
18    Action::None
19}
20
21/// Wrap a surface action, typically produced by a widget, to be handled by libcosmic.
22pub const fn surface<M>(action: crate::surface::Action<M>) -> Action<M> {
23    Action::Surface(action)
24}
25
26#[derive(Clone, Debug)]
27#[must_use]
28pub enum Action<M> {
29    /// Messages from the application, for the application.
30    App(M),
31    #[cfg(feature = "winit")]
32    /// Internal messages to be handled by libcosmic.
33    Cosmic(app::Action),
34    #[cfg(feature = "single-instance")]
35    /// Dbus activation messages
36    DbusActivation(dbus_activation::Message),
37    /// Surface (popup, subsurface, window, layer shell) requests, handled by libcosmic.
38    Surface(crate::surface::Action<M>),
39    /// Do nothing
40    None,
41}
42
43impl<M: 'static> Action<M> {
44    /// Map the application message inside, leaving libcosmic's own variants untouched.
45    pub fn map<N: 'static>(self, f: impl Fn(M) -> N + Clone + Send + Sync + 'static) -> Action<N> {
46        match self {
47            Action::App(message) => Action::App(f(message)),
48            #[cfg(feature = "winit")]
49            Action::Cosmic(action) => Action::Cosmic(action),
50            #[cfg(feature = "single-instance")]
51            Action::DbusActivation(message) => Action::DbusActivation(message),
52            Action::Surface(action) => Action::Surface(action.map(f)),
53            Action::None => Action::None,
54        }
55    }
56}
57
58impl<M: 'static> Action<Action<M>> {
59    /// Collapse a doubly wrapped action, as produced by widgets whose message type is already
60    /// an [`Action`], into a single one.
61    pub fn flatten(self) -> Action<M> {
62        match self {
63            Action::App(action) => action,
64            #[cfg(feature = "winit")]
65            Action::Cosmic(action) => Action::Cosmic(action),
66            #[cfg(feature = "single-instance")]
67            Action::DbusActivation(message) => Action::DbusActivation(message),
68            Action::Surface(action) => Action::Surface(action.flatten()),
69            Action::None => Action::None,
70        }
71    }
72}
73
74impl<M> From<M> for Action<M> {
75    fn from(value: M) -> Self {
76        Self::App(value)
77    }
78}