1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#![deny(rust_2018_idioms)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(unsafe_op_in_unsafe_fn)]
#![deny(improper_ctypes, improper_ctypes_definitions)]
#![deny(clippy::all)]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
#![forbid(unsafe_code)]
#![cfg_attr(feature = "cargo-clippy", deny(warnings))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

//! The interface for wayland client side decorations (CSD).
//!
//! The crate is intended to be used by libraries providing client
//! side decorations for the xdg-shell protocol.
//!
//! Examples could be found in [`client toolkit`] and [`sctk-adwaita`].
//!
//! [`client toolkit`]: https://github.com/smithay/client-toolkit
//! [`sctk-adwaita`]: https://github.com/PolyMeilex/sctk-adwaita

use std::num::NonZeroU32;
use std::time::Duration;

use bitflags::bitflags;
use wayland_backend::client::ObjectId;

#[doc(inline)]
pub use cursor_icon::{CursorIcon, ParseError as CursorIconParseError};

/// The interface for the client side decorations.
pub trait DecorationsFrame: Sized {
    /// Emulate click on the decorations.
    ///
    /// The `click` is a variant of click to use, see [`FrameClick`] for more
    /// information. `timestamp` is the time when event happened.
    ///
    /// The return value is a [`FrameAction`] you should apply, this action
    /// could be ignored.
    ///
    /// The location of the click is the one passed to
    /// [`Self::click_point_moved`].
    fn on_click(
        &mut self,
        timestamp: Duration,
        click: FrameClick,
        pressed: bool,
    ) -> Option<FrameAction>;

    /// Emulate pointer moved event on the decorations frame.
    ///
    /// The `x` and `y` are location in the surface local coordinates relative
    /// to the `surface`. `timestamp` is the time when event happened.
    ///
    /// The return value is the new cursor icon you should apply to provide
    /// better visual feedback for the user. However, you might want to
    /// ignore it, if you're using touch events to drive the movements.
    fn click_point_moved(
        &mut self,
        timestamp: Duration,
        surface_id: &ObjectId,
        x: f64,
        y: f64,
    ) -> Option<CursorIcon>;

    /// All clicks left the decorations.
    ///
    /// This function should be called when input leaves the decorations.
    fn click_point_left(&mut self);

    /// Update the state of the frame.
    ///
    /// The state is usually obtained from the `xdg_toplevel::configure` event.
    fn update_state(&mut self, state: WindowState);

    /// Update the window manager capabilites.
    ///
    /// The capabilites are usually obtained from the
    /// `xdg_toplevel::wm_capabilities` event.
    fn update_wm_capabilities(&mut self, wm_capabilities: WindowManagerCapabilities);

    /// Resize the window to the new size.
    ///
    /// The size must be without the borders, as in [`Self::subtract_borders]`
    /// were used on it.
    ///
    /// **Note:** The [`Self::update_state`] and
    /// [`Self::update_wm_capabilities`] **must be** applied before calling
    /// this function.
    ///
    /// # Panics
    ///
    /// Panics when resizing the hidden frame.
    fn resize(&mut self, width: NonZeroU32, height: NonZeroU32);

    /// Set the scaling of the decorations frame.
    ///
    /// If the decorations frame is not supporting fractional scaling it'll
    /// `ceil` the scaling factor.
    fn set_scaling_factor(&mut self, scale_factor: f64);

    /// Return the coordinates of the top-left corner of the borders relative to
    /// the content.
    ///
    /// Values **must** thus be non-positive.
    fn location(&self) -> (i32, i32);

    /// Subtract the borders from the given `width` and `height`.
    ///
    /// `None` will be returned for the particular dimension when the given
    /// value for it was too small.
    fn subtract_borders(
        &self,
        width: NonZeroU32,
        height: NonZeroU32,
    ) -> (Option<NonZeroU32>, Option<NonZeroU32>);

    /// Add the borders to the given `width` and `height`.
    ///
    /// Passing zero for both width and height could be used to get the size
    /// of the decorations frame.
    fn add_borders(&self, width: u32, height: u32) -> (u32, u32);

    /// Whether the given frame is dirty and should be redrawn.
    fn is_dirty(&self) -> bool;

    /// Set the frame as hidden.
    ///
    /// The frame **must be** visible by default.
    fn set_hidden(&mut self, hidden: bool);

    /// Get the frame hidden state.
    ///
    /// Get the state of the last [`DecorationsFrame::set_hidden`].
    fn is_hidden(&self) -> bool;

    /// Mark the frame as resizable.
    ///
    /// By default the frame is resizable.
    fn set_resizable(&mut self, resizable: bool);

    /// Draw the decorations frame.
    ///
    /// Return `true` when the main surface must be redrawn as well. This
    /// usually happens when `sync` is being set on the internal subsurfaces and
    /// they've changed their size.
    ///
    /// The user of the frame **must** commit the base surface afterwards.
    fn draw(&mut self) -> bool;

    /// Set the frames title.
    fn set_title(&mut self, title: impl Into<String>);
}

/// The Frame action user should perform in responce to mouse click events.
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub enum FrameAction {
    /// The window should be minimized.
    Minimize,
    /// The window should be maximized.
    Maximize,
    /// The window should be unmaximized.
    UnMaximize,
    /// The window should be closed.
    Close,
    /// An interactive move should be started.
    Move,
    /// An interactive resize should be started with the provided edge.
    Resize(ResizeEdge),
    /// Show window menu.
    ///
    /// The coordinates are relative to the base surface, as in should be
    /// directly passed to the `xdg_toplevel::show_window_menu`.
    ShowMenu(i32, i32),
}

/// The user clicked or touched the decoractions frame.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FrameClick {
    /// The user done normal click, likely with left mouse button or single
    /// finger touch.
    Normal,

    /// The user done right mouse click or some touch sequence that was treated
    /// as alternate click.
    ///
    /// The alternate click exists solely to provide alternative action, like
    /// show window menu when doing right mouse button cilck on the header
    /// decorations, nothing more.
    Alternate,
}

bitflags! {
    /// The configured state of the window.
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    pub struct WindowState: u16 {
        /// The surface is maximized. The window geometry specified in the
        /// configure event must be obeyed by the client. The client should
        /// draw without shadow or other decoration outside of the window
        /// geometry.
        const MAXIMIZED    = 0b0000_0000_0000_0001;
        /// The surface is fullscreen. The window geometry specified in the
        /// configure event is a maximum; the client cannot resize beyond it.
        /// For a surface to cover the whole fullscreened area, the geometry
        /// dimensions must be obeyed by the client. For more details, see
        /// xdg_toplevel.set_fullscreen.
        const FULLSCREEN   = 0b0000_0000_0000_0010;
        /// The surface is being resized. The window geometry specified in the
        /// configure event is a maximum; the client cannot resize beyond it.
        /// Clients that have aspect ratio or cell sizing configuration can use
        /// a smaller size, however.
        const RESIZING     = 0b0000_0000_0000_0100;
        /// Client window decorations should be painted as if the window is
        /// active. Do not assume this means that the window actually has
        /// keyboard or pointer focus.
        const ACTIVATED    = 0b0000_0000_0000_1000;
        /// The window is currently in a tiled layout and the left edge is
        /// considered to be adjacent to another part of the tiling grid.
        const TILED_LEFT   = 0b0000_0000_0001_0000;
        /// The window is currently in a tiled layout and the right edge is
        /// considered to be adjacent to another part of the tiling grid.
        const TILED_RIGHT  = 0b0000_0000_0010_0000;
        /// The window is currently in a tiled layout and the top edge is
        /// considered to be adjacent to another part of the tiling grid.
        const TILED_TOP    = 0b0000_0000_0100_0000;
        /// The window is currently in a tiled layout and the bottom edge is
        /// considered to be adjacent to another part of the tiling grid.
        const TILED_BOTTOM = 0b0000_0000_1000_0000;
        /// An alias for all tiled bits set.
        const TILED        = Self::TILED_TOP.bits() | Self::TILED_LEFT.bits() | Self::TILED_RIGHT.bits() | Self::TILED_BOTTOM.bits();
        /// The surface is currently not ordinarily being repainted; for example
        /// because its content is occluded by another window, or its outputs are
        /// switched off due to screen locking.
        const SUSPENDED    = 0b0000_0001_0000_0000;
    }
}

bitflags! {
    /// The capabilities of the window manager.
    ///
    /// This is a hint to hide UI elements which provide functionality
    /// not supported by compositor.
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    pub struct WindowManagerCapabilities : u16 {
        /// `show_window_menu` is available.
        const WINDOW_MENU = 0b0000_0000_0000_0001;
        /// Window can be maximized and unmaximized.
        const MAXIMIZE = 0b0000_0000_0000_0010;
        /// Window can be fullscreened and unfullscreened.
        const FULLSCREEN = 0b0000_0000_0000_0100;
        /// Window could be minimized.
        const MINIMIZE = 0b0000_0000_0000_1000;
    }
}

/// Which edge or corner is being dragged.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ResizeEdge {
    /// Nothing is being dragged.
    None,
    /// The top edge is being dragged.
    Top,
    /// The bottom edge is being dragged.
    Bottom,
    /// The left edge is being dragged.
    Left,
    /// The top left corner is being dragged.
    TopLeft,
    /// The bottom left corner is being dragged.
    BottomLeft,
    /// The right edge is being dragged.
    Right,
    /// The top right corner is being dragged.
    TopRight,
    /// The bottom right corner is being dragged.
    BottomRight,
}