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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
//! Tools for inspecting and manipulating windows.
use crate::_private::WindowThemeKind;
use crate::Axis;
use crate::ContainerTarget;
use crate::Direction;
use crate::RelativeAxis;
use crate::Workspace;
use crate::client::Client;
use crate::client::ClientCriterion;
use crate::input::Seat;
use crate::theme::ContainerTheme;
use crate::theme::ThemeOverrides;
use crate::theme::WindowTheme;
use jay_proc::PrivateEnum;
use serde::Deserialize;
use serde::Serialize;
use std::ops::Deref;
/// A toplevel window.
///
/// A toplevel window is anything that can be stored within a container tile or within a
/// floating window.
///
/// There are currently four types of windows:
///
/// - Containers
/// - Placeholders that take the place of a window when it goes fullscreen
/// - XDG toplevels
/// - X windows
///
/// You can find out the type of a window by using the [`Window::type_`] function.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct Window(pub u64);
bitflags! {
/// The type of a window.
#[derive(Serialize, Deserialize, Copy, Clone, Hash, Eq, PartialEq)]
pub struct WindowType(pub u64) {
/// A container.
pub const CONTAINER = 1 << 0,
/// A placeholder.
pub const PLACEHOLDER = 1 << 1,
/// An XDG toplevel.
pub const XDG_TOPLEVEL = 1 << 2,
/// An X window.
pub const X_WINDOW = 1 << 3,
}
}
bitflags! {
/// The content type of a window.
#[derive(Serialize, Deserialize, Copy, Clone, Hash, Eq, PartialEq)]
pub struct ContentType(pub u64) {
/// No content type.
pub const NO_CONTENT_TYPE = 1 << 0,
/// Photo content type.
pub const PHOTO_CONTENT = 1 << 1,
/// Video content type.
pub const VIDEO_CONTENT = 1 << 2,
/// Game content type.
pub const GAME_CONTENT = 1 << 3,
}
}
/// The tile state of a window.
#[non_exhaustive]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq, PrivateEnum)]
pub enum TileState {
/// The window is tiled.
Tiled,
/// The window is floating.
Floating,
}
/// A window created by a client.
///
/// This is the same as `XDG_TOPLEVEL | X_WINDOW`.
pub const CLIENT_WINDOW: WindowType = WindowType(XDG_TOPLEVEL.0 | X_WINDOW.0);
impl Window {
/// Returns whether the window exists.
pub fn exists(self) -> bool {
self.0 != 0 && get!(false).window_exists(self)
}
/// Returns whether the window does not exist.
///
/// This is a shorthand for `!self.exists()`.
pub fn does_not_exist(self) -> bool {
!self.exists()
}
/// Returns the client of the window.
///
/// If the window does not have a client, [`Client::exists`] return false.
pub fn client(self) -> Client {
get!(Client(0)).window_client(self)
}
/// Returns the title of the window.
pub fn title(self) -> String {
get!().window_title(self)
}
/// Returns the type of the window.
pub fn type_(self) -> WindowType {
get!(WindowType(0)).window_type(self)
}
/// Returns the content type of the window.
pub fn content_type(self) -> ContentType {
get!(ContentType(0)).content_type(self)
}
/// Returns the identifier of the window.
///
/// This is the identifier used in the `ext-foreign-toplevel-list-v1` protocol.
pub fn id(self) -> String {
get!().window_id(self)
}
/// Returns whether this window is visible.
pub fn is_visible(self) -> bool {
get!().window_is_visible(self)
}
/// Returns the parent of this window.
///
/// If this window has no parent, [`Window::exists`] returns false.
pub fn parent(self) -> Window {
get!(Window(0)).window_parent(self)
}
/// Returns the children of this window.
///
/// Only containers have children.
pub fn children(self) -> Vec<Window> {
get!().window_children(self)
}
/// Moves the window in the specified direction.
pub fn move_(self, direction: Direction) {
get!().window_move(self, direction)
}
/// Returns whether the parent-container of the window is in mono-mode.
pub fn mono(self) -> bool {
get!(false).window_mono(self)
}
/// Sets whether the parent-container of the window is in mono-mode.
pub fn set_mono(self, mono: bool) {
get!().set_window_mono(self, mono)
}
/// Toggles whether the parent-container of the window is in mono-mode.
pub fn toggle_mono(self) {
self.set_mono(!self.mono());
}
/// Returns the split axis of the parent-container of the window.
pub fn split(self) -> Axis {
get!(Axis::Horizontal).window_split(self)
}
/// Sets the split axis of the parent-container of the window.
pub fn set_split(self, axis: Axis) {
get!().set_window_split(self, axis)
}
/// Toggles the split axis of the parent-container of the window.
pub fn toggle_split(self) {
self.set_split(self.split().other());
}
/// Returns whether the target container of the window is in mono-mode.
pub fn container_mono(self, target: ContainerTarget) -> bool {
get!(false).window_container_mono(self, target.to_private())
}
/// Sets whether the target container of the window is in mono-mode.
pub fn set_container_mono(self, target: ContainerTarget, mono: bool) {
get!().set_window_container_mono(self, target.to_private(), mono)
}
/// Toggles whether the target container of the window is in mono-mode.
pub fn toggle_container_mono(self, target: ContainerTarget) {
self.set_container_mono(target, !self.container_mono(target));
}
/// Returns the split axis of the target container of the window.
pub fn container_split(self, target: ContainerTarget) -> Axis {
get!(Axis::Horizontal).window_container_split(self, target.to_private())
}
/// Sets the split axis of the target container of the window.
pub fn set_container_split(self, target: ContainerTarget, axis: Axis) {
get!().set_window_container_split(self, target.to_private(), axis)
}
/// Toggles the split axis of the target container of the window.
pub fn toggle_container_split(self, target: ContainerTarget) {
self.set_container_split(target, self.container_split(target).other());
}
/// Sets the split axis of the target container of the window relative to the
/// dimensions of that container.
pub fn set_container_split_relative(self, target: ContainerTarget, axis: RelativeAxis) {
get!().set_window_container_split_relative(self, target.to_private(), axis.to_private())
}
/// Creates a new container with the specified split in place of the window.
///
/// If the window is the only child of its container and
/// [`set_split_reuses_container`](crate::set_split_reuses_container) is enabled, the
/// split axis of that container is changed instead.
pub fn create_split(self, axis: Axis) {
get!().create_window_split(self, axis);
}
/// Creates a new container in place of the window with a split relative to the
/// dimensions of that window.
///
/// If the window is the only child of its container and
/// [`set_split_reuses_container`](crate::set_split_reuses_container) is enabled, the
/// split axis of that container is changed instead.
pub fn create_split_relative(self, axis: RelativeAxis) {
get!().create_window_split_relative(self, axis.to_private());
}
/// Requests the window to be closed.
pub fn close(self) {
get!().close_window(self);
}
/// Returns whether the window is floating.
pub fn floating(self) -> bool {
get!().get_window_floating(self)
}
/// Sets whether the window is floating.
pub fn set_floating(self, floating: bool) {
get!().set_window_floating(self, floating);
}
/// Toggles whether the window is floating.
///
/// You can do the same by double-clicking on the header.
pub fn toggle_floating(self) {
self.set_floating(!self.floating());
}
/// Returns the workspace that this window belongs to.
///
/// If no such workspace exists, `exists` returns `false` for the returned workspace.
pub fn workspace(self) -> Workspace {
get!(Workspace(0)).get_window_workspace(self)
}
/// Moves the window to the workspace.
pub fn set_workspace(self, workspace: Workspace) {
get!().set_window_workspace(self, workspace)
}
/// Toggles whether the currently focused window is fullscreen.
pub fn toggle_fullscreen(self) {
self.set_fullscreen(!self.fullscreen())
}
/// Returns whether the window is fullscreen.
pub fn fullscreen(self) -> bool {
get!(false).get_window_fullscreen(self)
}
/// Sets whether the window is fullscreen.
pub fn set_fullscreen(self, fullscreen: bool) {
get!().set_window_fullscreen(self, fullscreen)
}
/// Gets whether the window is pinned.
///
/// If a floating window is pinned, it will stay visible even when switching to a
/// different workspace.
pub fn float_pinned(self) -> bool {
get!().get_window_pinned(self)
}
/// Sets whether the window is pinned.
pub fn set_float_pinned(self, pinned: bool) {
get!().set_window_pinned(self, pinned);
}
/// Toggles whether the window is pinned.
pub fn toggle_float_pinned(self) {
self.set_float_pinned(!self.float_pinned());
}
/// Resizes the window.
pub fn resize(self, dx1: i32, dy1: i32, dx2: i32, dy2: i32) {
get!().resize_window(self, dx1, dy1, dx2, dy2);
}
/// Returns the position of the window in the global compositor space.
///
/// This value is only accurate for visible windows.
pub fn position(self) -> (i32, i32) {
let (x, y, _, _) = get!((0, 0)).get_window_position(self);
(x, y)
}
/// Returns the size of the window.
///
/// This value is only accurate for visible windows.
pub fn size(self) -> (i32, i32) {
let (_, _, width, height) = get!((0, 0)).get_window_position(self);
(width, height)
}
/// Returns the theme overrides of this window.
///
/// See [`WindowTheme`] for details.
pub fn theme(self) -> WindowTheme {
WindowTheme(ThemeOverrides {
window: self,
kind: WindowThemeKind::ParentTheme,
})
}
/// Returns the theme overrides of how this container decorates its children.
///
/// This is only supported for containers.
///
/// See [`ContainerTheme`] for details.
pub fn container_theme(self) -> ContainerTheme {
ContainerTheme(ThemeOverrides {
window: self,
kind: WindowThemeKind::SelfTheme,
})
}
}
/// A window matcher.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct WindowMatcher(pub u64);
/// A matched window.
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub struct MatchedWindow {
pub(crate) matcher: WindowMatcher,
pub(crate) window: Window,
}
/// A criterion for matching a window.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[non_exhaustive]
pub enum WindowCriterion<'a> {
/// Matches if the contained matcher matches.
Matcher(WindowMatcher),
/// Matches if the contained criterion does not match.
Not(&'a WindowCriterion<'a>),
/// Matches if the window has one of the types.
Types(WindowType),
/// Matches if all of the contained criteria match.
All(&'a [WindowCriterion<'a>]),
/// Matches if any of the contained criteria match.
Any(&'a [WindowCriterion<'a>]),
/// Matches if an exact number of the contained criteria match.
Exactly(usize, &'a [WindowCriterion<'a>]),
/// Matches if the window's client matches the client criterion.
Client(&'a ClientCriterion<'a>),
/// Matches the title of the window verbatim.
Title(&'a str),
/// Matches the title of the window with a regular expression.
TitleRegex(&'a str),
/// Matches the app-id of the window verbatim.
AppId(&'a str),
/// Matches the app-id of the window with a regular expression.
AppIdRegex(&'a str),
/// Matches if the window is floating.
Floating,
/// Matches if the window is visible.
Visible,
/// Matches if the window has the urgency flag set.
Urgent,
/// Matches if the window has the keyboard focus of the seat.
Focus(Seat),
/// Matches if the window is fullscreen.
Fullscreen,
/// Matches if the window has/hasn't just been mapped.
///
/// This is true for one iteration of the compositor's main loop immediately after the
/// window has been mapped.
JustMapped,
/// Matches the toplevel-tag of the window verbatim.
Tag(&'a str),
/// Matches the toplevel-tag of the window with a regular expression.
TagRegex(&'a str),
/// Matches the X class of the window verbatim.
XClass(&'a str),
/// Matches the X class of the window with a regular expression.
XClassRegex(&'a str),
/// Matches the X instance of the window verbatim.
XInstance(&'a str),
/// Matches the X instance of the window with a regular expression.
XInstanceRegex(&'a str),
/// Matches the X role of the window verbatim.
XRole(&'a str),
/// Matches the X role of the window with a regular expression.
XRoleRegex(&'a str),
/// Matches the workspace the window.
Workspace(Workspace),
/// Matches the workspace name of the window verbatim.
WorkspaceName(&'a str),
/// Matches the workspace name of the window with a regular expression.
WorkspaceNameRegex(&'a str),
/// Matches if the window has one of the content types.
ContentTypes(ContentType),
/// Matches if the window is the root container of a workspace.
IsWorkspaceContainer,
}
impl WindowCriterion<'_> {
/// Converts the criterion to a matcher.
pub fn to_matcher(self) -> WindowMatcher {
get!(WindowMatcher(0)).create_window_matcher(self)
}
/// Binds a function to execute when the criterion matches a window.
///
/// This leaks the matcher.
pub fn bind<F: FnMut(MatchedWindow) + 'static>(self, cb: F) {
self.to_matcher().bind(cb);
}
/// Sets whether newly mapped windows that match this criterion get the keyboard focus.
///
/// If a window matches any criterion for which this is false, the window will not be
/// automatically focused.
///
/// This leaks the matcher.
pub fn set_auto_focus(self, auto_focus: bool) {
self.to_matcher().set_auto_focus(auto_focus);
}
/// Sets whether newly mapped windows that match this matcher are mapped tiling or
/// floating.
///
/// If multiple such window matchers match a window, the used tile state is
/// unspecified.
///
/// This leaks the matcher.
pub fn set_initial_tile_state(self, tile_state: TileState) {
self.to_matcher().set_initial_tile_state(tile_state);
}
/// Sets the size that matched windows have when they are initially mapped floating.
///
/// This has no effect on windows that are initially mapped tiled, even if they later
/// become floating.
///
/// If multiple such window matchers match a window, the used size is unspecified.
///
/// This leaks the matcher.
pub fn set_initial_floating_size(self, width: i32, height: i32) {
self.to_matcher().set_initial_floating_size(width, height);
}
/// Sets the position that matched windows have when they are initially mapped
/// floating.
///
/// The position is relative to the top-left corner of the output that the window is
/// mapped on and refers to the top-left corner of the window itself, excluding any
/// decorations. It is clamped so that the window remains at least partially visible
/// on that output.
///
/// This has no effect on windows that are initially mapped tiled, even if they later
/// become floating.
///
/// If multiple such window matchers match a window, the used position is unspecified.
///
/// This leaks the matcher.
pub fn set_initial_floating_position(self, x: i32, y: i32) {
self.to_matcher().set_initial_floating_position(x, y);
}
}
impl WindowMatcher {
/// Destroys the matcher.
///
/// Any bound callback will no longer be executed.
pub fn destroy(self) {
get!().destroy_window_matcher(self);
}
/// Sets a function to execute when the criterion matches a window.
///
/// Replaces any already bound callback.
pub fn bind<F: FnMut(MatchedWindow) + 'static>(self, cb: F) {
get!().set_window_matcher_handler(self, cb);
}
/// Sets whether newly mapped windows that match this matcher get the keyboard focus.
///
/// If a window matches any matcher for which this is false, the window will not be
/// automatically focused.
pub fn set_auto_focus(self, auto_focus: bool) {
get!().set_window_matcher_auto_focus(self, auto_focus);
}
/// Sets whether newly mapped windows that match this matcher are mapped tiling or
/// floating.
///
/// If multiple such window matchers match a window, the used tile state is
/// unspecified.
pub fn set_initial_tile_state(self, tile_state: TileState) {
get!().set_window_matcher_initial_tile_state(self, tile_state.to_private());
}
/// Sets the size that matched windows have when they are initially mapped floating.
///
/// This has no effect on windows that are initially mapped tiled, even if they later
/// become floating.
///
/// If multiple such window matchers match a window, the used size is unspecified.
pub fn set_initial_floating_size(self, width: i32, height: i32) {
get!().set_window_matcher_initial_floating_size(self, width, height);
}
/// Sets the position that matched windows have when they are initially mapped
/// floating.
///
/// The position is relative to the top-left corner of the output that the window is
/// mapped on and refers to the top-left corner of the window itself, excluding any
/// decorations. It is clamped so that the window remains at least partially visible
/// on that output.
///
/// This has no effect on windows that are initially mapped tiled, even if they later
/// become floating.
///
/// If multiple such window matchers match a window, the used position is unspecified.
pub fn set_initial_floating_position(self, x: i32, y: i32) {
get!().set_window_matcher_initial_floating_position(self, x, y);
}
}
impl MatchedWindow {
/// Returns the window that matched.
pub fn window(self) -> Window {
self.window
}
/// Returns the matcher.
pub fn matcher(self) -> WindowMatcher {
self.matcher
}
/// Latches a function to be executed when the window no longer matches the criteria.
pub fn latch<F: FnOnce() + 'static>(self, cb: F) {
get!().set_window_matcher_latch_handler(self.matcher, self.window, cb);
}
}
impl Deref for MatchedWindow {
type Target = Window;
fn deref(&self) -> &Self::Target {
&self.window
}
}