smithay_client_toolkit/shell/xdg/window/
mod.rs1use std::{
4 num::NonZeroU32,
5 sync::{Arc, Weak},
6};
7
8use crate::reexports::client::{
9 protocol::{wl_output, wl_seat, wl_surface},
10 Connection, Proxy, QueueHandle,
11};
12use crate::reexports::csd_frame::{WindowManagerCapabilities, WindowState};
13use crate::reexports::protocols::{
14 xdg::decoration::zv1::client::zxdg_toplevel_decoration_v1::{self, Mode},
15 xdg::shell::client::{xdg_surface, xdg_toplevel},
16};
17
18use crate::shell::WaylandSurface;
19
20use self::inner::WindowInner;
21
22use super::XdgSurface;
23
24pub(super) mod inner;
25
26pub trait WindowHandler: Sized {
28 fn request_close(&mut self, conn: &Connection, qh: &QueueHandle<Self>, window: &Window);
33
34 fn configure(
47 &mut self,
48 conn: &Connection,
49 qh: &QueueHandle<Self>,
50 window: &Window,
51 configure: WindowConfigure,
52 serial: u32,
53 );
54}
55
56#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub enum DecorationMode {
59 Client,
61
62 Server,
64}
65
66#[non_exhaustive]
70#[derive(Debug, Clone)]
71pub struct WindowConfigure {
72 pub new_size: (Option<NonZeroU32>, Option<NonZeroU32>),
76
77 pub suggested_bounds: Option<(u32, u32)>,
83
84 pub decoration_mode: DecorationMode,
89
90 pub state: WindowState,
94
95 pub capabilities: WindowManagerCapabilities,
99}
100
101impl WindowConfigure {
102 #[inline]
104 pub fn is_maximized(&self) -> bool {
105 self.state.contains(WindowState::MAXIMIZED)
106 }
107
108 #[inline]
110 pub fn is_fullscreen(&self) -> bool {
111 self.state.contains(WindowState::FULLSCREEN)
112 }
113
114 #[inline]
116 pub fn is_resizing(&self) -> bool {
117 self.state.contains(WindowState::RESIZING)
118 }
119
120 #[inline]
122 pub fn is_tiled(&self) -> bool {
123 self.state.contains(WindowState::TILED)
124 }
125
126 #[inline]
128 pub fn is_activated(&self) -> bool {
129 self.state.contains(WindowState::ACTIVATED)
130 }
131
132 #[inline]
134 pub fn is_tiled_left(&self) -> bool {
135 self.state.contains(WindowState::TILED_LEFT)
136 }
137
138 #[inline]
140 pub fn is_tiled_right(&self) -> bool {
141 self.state.contains(WindowState::TILED_RIGHT)
142 }
143
144 #[inline]
146 pub fn is_tiled_top(&self) -> bool {
147 self.state.contains(WindowState::TILED_TOP)
148 }
149
150 #[inline]
152 pub fn is_tiled_bottom(&self) -> bool {
153 self.state.contains(WindowState::TILED_BOTTOM)
154 }
155}
156
157#[derive(Debug, Clone, Copy, PartialEq, Eq)]
159pub enum WindowDecorations {
160 ServerDefault,
165
166 RequestServer,
171
172 RequestClient,
177
178 ClientOnly,
180
181 None,
183}
184
185#[derive(Debug, Clone)]
186pub struct Window(pub(super) Arc<WindowInner>);
187
188impl Window {
189 pub fn from_xdg_toplevel(toplevel: &xdg_toplevel::XdgToplevel) -> Option<Window> {
190 toplevel.data::<WindowData>().and_then(|data| data.0.upgrade()).map(Window)
191 }
192
193 pub fn from_xdg_surface(surface: &xdg_surface::XdgSurface) -> Option<Window> {
194 surface.data::<WindowData>().and_then(|data| data.0.upgrade()).map(Window)
195 }
196
197 pub fn from_toplevel_decoration(
198 decoration: &zxdg_toplevel_decoration_v1::ZxdgToplevelDecorationV1,
199 ) -> Option<Window> {
200 decoration.data::<WindowData>().and_then(|data| data.0.upgrade()).map(Window)
201 }
202
203 pub fn show_window_menu(&self, seat: &wl_seat::WlSeat, serial: u32, position: (i32, i32)) {
204 self.xdg_toplevel().show_window_menu(seat, serial, position.0, position.1);
205 }
206
207 pub fn set_title(&self, title: impl Into<String>) {
208 self.xdg_toplevel().set_title(title.into());
209 }
210
211 pub fn set_app_id(&self, app_id: impl Into<String>) {
212 self.xdg_toplevel().set_app_id(app_id.into());
213 }
214
215 pub fn set_parent(&self, parent: Option<&Window>) {
216 self.xdg_toplevel().set_parent(parent.map(Window::xdg_toplevel));
217 }
218
219 pub fn set_maximized(&self) {
220 self.xdg_toplevel().set_maximized()
221 }
222
223 pub fn unset_maximized(&self) {
224 self.xdg_toplevel().unset_maximized()
225 }
226
227 pub fn set_minimized(&self) {
228 self.xdg_toplevel().set_minimized()
229 }
230
231 pub fn set_fullscreen(&self, output: Option<&wl_output::WlOutput>) {
232 self.xdg_toplevel().set_fullscreen(output)
233 }
234
235 pub fn unset_fullscreen(&self) {
236 self.xdg_toplevel().unset_fullscreen()
237 }
238
239 pub fn request_decoration_mode(&self, mode: Option<DecorationMode>) {
250 if let Some(toplevel_decoration) = &self.0.toplevel_decoration {
251 match mode {
252 Some(DecorationMode::Client) => toplevel_decoration.set_mode(Mode::ClientSide),
253 Some(DecorationMode::Server) => toplevel_decoration.set_mode(Mode::ServerSide),
254 None => toplevel_decoration.unset_mode(),
255 }
256 }
257 }
258
259 pub fn move_(&self, seat: &wl_seat::WlSeat, serial: u32) {
260 self.xdg_toplevel()._move(seat, serial)
261 }
262
263 pub fn resize(&self, seat: &wl_seat::WlSeat, serial: u32, edges: xdg_toplevel::ResizeEdge) {
264 self.xdg_toplevel().resize(seat, serial, edges)
265 }
266
267 pub fn set_min_size(&self, min_size: Option<(u32, u32)>) {
270 let min_size = min_size.unwrap_or_default();
271 self.xdg_toplevel().set_min_size(min_size.0 as i32, min_size.1 as i32);
272 }
273
274 pub fn set_max_size(&self, max_size: Option<(u32, u32)>) {
278 let max_size = max_size.unwrap_or_default();
279 self.xdg_toplevel().set_max_size(max_size.0 as i32, max_size.1 as i32);
280 }
281
282 pub fn xdg_toplevel(&self) -> &xdg_toplevel::XdgToplevel {
286 &self.0.xdg_toplevel
287 }
288}
289
290impl WaylandSurface for Window {
291 fn wl_surface(&self) -> &wl_surface::WlSurface {
292 self.0.xdg_surface.wl_surface()
293 }
294}
295
296impl XdgSurface for Window {
297 fn xdg_surface(&self) -> &xdg_surface::XdgSurface {
298 self.0.xdg_surface.xdg_surface()
299 }
300}
301
302impl PartialEq for Window {
303 fn eq(&self, other: &Self) -> bool {
304 Arc::ptr_eq(&self.0, &other.0)
305 }
306}
307
308#[derive(Debug, Clone)]
309pub struct WindowData(pub(crate) Weak<WindowInner>);
310
311#[macro_export]
312macro_rules! delegate_xdg_window {
313 ($(@<$( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+>)? $ty: ty) => {
314 $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
315 $crate::reexports::protocols::xdg::shell::client::xdg_surface::XdgSurface: $crate::shell::xdg::window::WindowData
316 ] => $crate::shell::xdg::XdgShell);
317 $crate::reexports::client::delegate_dispatch!($(@< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $ty: [
318 $crate::reexports::protocols::xdg::shell::client::xdg_toplevel::XdgToplevel: $crate::shell::xdg::window::WindowData
319 ] => $crate::shell::xdg::XdgShell);
320 };
321}