forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB90ef202Ivan Molodetskikh1 year ago2349 commits

mod.rs497 lines, 15.9 KiBHistoryRawDownload
1use std::cmp::{max, min};
2
3use niri_config::window_rule::{Match, WindowRule};
4use niri_config::{
5 BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize, ShadowRule,
6 TabIndicatorRule,
7};
8use niri_ipc::ColumnDisplay;
9use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
10use smithay::utils::{Logical, Size};
11use smithay::wayland::compositor::with_states;
12use smithay::wayland::shell::xdg::{
13 SurfaceCachedState, ToplevelSurface, XdgToplevelSurfaceRoleAttributes,
14};
15
16use crate::utils::with_toplevel_role;
17
18pub mod mapped;
19pub use mapped::Mapped;
20
21pub mod unmapped;
22pub use unmapped::{InitialConfigureState, Unmapped};
23
24/// Reference to a mapped or unmapped window.
25#[derive(Debug, Clone, Copy)]
26pub enum WindowRef<'a> {
27 Unmapped(&'a Unmapped),
28 Mapped(&'a Mapped),
29}
30
31/// Rules fully resolved for a window.
32#[derive(Debug, PartialEq)]
33pub struct ResolvedWindowRules {
34 /// Default width for this window.
35 ///
36 /// - `None`: unset (global default should be used).
37 /// - `Some(None)`: set to empty (window picks its own width).
38 /// - `Some(Some(width))`: set to a particular width.
39 pub default_width: Option<Option<PresetSize>>,
40
41 /// Default height for this window.
42 ///
43 /// - `None`: unset (global default should be used).
44 /// - `Some(None)`: set to empty (window picks its own height).
45 /// - `Some(Some(height))`: set to a particular height.
46 pub default_height: Option<Option<PresetSize>>,
47
48 /// Default column display for this window.
49 pub default_column_display: Option<ColumnDisplay>,
50
51 /// Default floating position for this window.
52 pub default_floating_position: Option<FloatingPosition>,
53
54 /// Output to open this window on.
55 pub open_on_output: Option<String>,
56
57 /// Workspace to open this window on.
58 pub open_on_workspace: Option<String>,
59
60 /// Whether the window should open full-width.
61 pub open_maximized: Option<bool>,
62
63 /// Whether the window should open fullscreen.
64 pub open_fullscreen: Option<bool>,
65
66 /// Whether the window should open floating.
67 pub open_floating: Option<bool>,
68
69 /// Whether the window should open focused.
70 pub open_focused: Option<bool>,
71
72 /// Extra bound on the minimum window width.
73 pub min_width: Option<u16>,
74 /// Extra bound on the minimum window height.
75 pub min_height: Option<u16>,
76 /// Extra bound on the maximum window width.
77 pub max_width: Option<u16>,
78 /// Extra bound on the maximum window height.
79 pub max_height: Option<u16>,
80
81 /// Focus ring overrides.
82 pub focus_ring: BorderRule,
83 /// Window border overrides.
84 pub border: BorderRule,
85 /// Shadow overrides.
86 pub shadow: ShadowRule,
87 /// Tab indicator overrides.
88 pub tab_indicator: TabIndicatorRule,
89
90 /// Whether or not to draw the border with a solid background.
91 ///
92 /// `None` means using the SSD heuristic.
93 pub draw_border_with_background: Option<bool>,
94
95 /// Extra opacity to draw this window with.
96 pub opacity: Option<f32>,
97
98 /// Corner radius to assume this window has.
99 pub geometry_corner_radius: Option<CornerRadius>,
100
101 /// Whether to clip this window to its geometry, including the corner radius.
102 pub clip_to_geometry: Option<bool>,
103
104 /// Whether to bob this window up and down.
105 pub baba_is_float: Option<bool>,
106
107 /// Whether to block out this window from certain render targets.
108 pub block_out_from: Option<BlockOutFrom>,
109
110 /// Whether to enable VRR on this window's primary output if it is on-demand.
111 pub variable_refresh_rate: Option<bool>,
112
113 /// Multiplier for all scroll events sent to this window.
114 pub scroll_factor: Option<f64>,
115
116 /// Override whether to set the Tiled xdg-toplevel state on the window.
117 pub tiled_state: Option<bool>,
118}
119
120impl<'a> WindowRef<'a> {
121 pub fn toplevel(self) -> &'a ToplevelSurface {
122 match self {
123 WindowRef::Unmapped(unmapped) => unmapped.toplevel(),
124 WindowRef::Mapped(mapped) => mapped.toplevel(),
125 }
126 }
127
128 pub fn is_focused(self) -> bool {
129 match self {
130 WindowRef::Unmapped(_) => false,
131 WindowRef::Mapped(mapped) => mapped.is_focused(),
132 }
133 }
134
135 pub fn is_urgent(self) -> bool {
136 match self {
137 WindowRef::Unmapped(_) => false,
138 WindowRef::Mapped(mapped) => mapped.is_urgent(),
139 }
140 }
141
142 pub fn is_active_in_column(self) -> bool {
143 match self {
144 WindowRef::Unmapped(_) => true,
145 WindowRef::Mapped(mapped) => mapped.is_active_in_column(),
146 }
147 }
148
149 pub fn is_floating(self) -> bool {
150 match self {
151 // FIXME: This means you cannot set initial configure rules based on is-floating. I'm
152 // not sure there's a good way to support it, since this matcher makes a cycle with the
153 // open-floating rule.
154 //
155 // That said, I don't think there are a lot of useful initial configure properties you
156 // may want to set through an is-floating matcher? Like, if you're configuring a
157 // specific window to open as floating, you can also set those properties in that same
158 // window rule, rather than relying on a different is-floating rule.
159 WindowRef::Unmapped(_) => false,
160 WindowRef::Mapped(mapped) => mapped.is_floating(),
161 }
162 }
163
164 pub fn is_window_cast_target(self) -> bool {
165 match self {
166 WindowRef::Unmapped(_) => false,
167 WindowRef::Mapped(mapped) => mapped.is_window_cast_target(),
168 }
169 }
170}
171
172impl ResolvedWindowRules {
173 pub const fn empty() -> Self {
174 Self {
175 default_width: None,
176 default_height: None,
177 default_column_display: None,
178 default_floating_position: None,
179 open_on_output: None,
180 open_on_workspace: None,
181 open_maximized: None,
182 open_fullscreen: None,
183 open_floating: None,
184 open_focused: None,
185 min_width: None,
186 min_height: None,
187 max_width: None,
188 max_height: None,
189 focus_ring: BorderRule {
190 off: false,
191 on: false,
192 width: None,
193 active_color: None,
194 inactive_color: None,
195 urgent_color: None,
196 active_gradient: None,
197 inactive_gradient: None,
198 urgent_gradient: None,
199 },
200 border: BorderRule {
201 off: false,
202 on: false,
203 width: None,
204 active_color: None,
205 inactive_color: None,
206 urgent_color: None,
207 active_gradient: None,
208 inactive_gradient: None,
209 urgent_gradient: None,
210 },
211 shadow: ShadowRule {
212 off: false,
213 on: false,
214 offset: None,
215 softness: None,
216 spread: None,
217 draw_behind_window: None,
218 color: None,
219 inactive_color: None,
220 },
221 tab_indicator: TabIndicatorRule {
222 active_color: None,
223 inactive_color: None,
224 urgent_color: None,
225 active_gradient: None,
226 inactive_gradient: None,
227 urgent_gradient: None,
228 },
229 draw_border_with_background: None,
230 opacity: None,
231 geometry_corner_radius: None,
232 clip_to_geometry: None,
233 baba_is_float: None,
234 block_out_from: None,
235 variable_refresh_rate: None,
236 scroll_factor: None,
237 tiled_state: None,
238 }
239 }
240
241 pub fn compute(rules: &[WindowRule], window: WindowRef, is_at_startup: bool) -> Self {
242 let _span = tracy_client::span!("ResolvedWindowRules::compute");
243
244 let mut resolved = ResolvedWindowRules::empty();
245
246 with_toplevel_role(window.toplevel(), |role| {
247 // Ensure server_pending like in Smithay's with_pending_state().
248 if role.server_pending.is_none() {
249 role.server_pending = Some(role.current_server_state().clone());
250 }
251
252 let mut open_on_output = None;
253 let mut open_on_workspace = None;
254
255 for rule in rules {
256 let matches = |m: &Match| {
257 if let Some(at_startup) = m.at_startup {
258 if at_startup != is_at_startup {
259 return false;
260 }
261 }
262
263 window_matches(window, role, m)
264 };
265
266 if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) {
267 continue;
268 }
269
270 if rule.excludes.iter().any(matches) {
271 continue;
272 }
273
274 if let Some(x) = rule.default_column_width {
275 resolved.default_width = Some(x.0);
276 }
277
278 if let Some(x) = rule.default_window_height {
279 resolved.default_height = Some(x.0);
280 }
281
282 if let Some(x) = rule.default_column_display {
283 resolved.default_column_display = Some(x);
284 }
285
286 if let Some(x) = rule.default_floating_position {
287 resolved.default_floating_position = Some(x);
288 }
289
290 if let Some(x) = rule.open_on_output.as_deref() {
291 open_on_output = Some(x);
292 }
293
294 if let Some(x) = rule.open_on_workspace.as_deref() {
295 open_on_workspace = Some(x);
296 }
297
298 if let Some(x) = rule.open_maximized {
299 resolved.open_maximized = Some(x);
300 }
301
302 if let Some(x) = rule.open_fullscreen {
303 resolved.open_fullscreen = Some(x);
304 }
305
306 if let Some(x) = rule.open_floating {
307 resolved.open_floating = Some(x);
308 }
309
310 if let Some(x) = rule.open_focused {
311 resolved.open_focused = Some(x);
312 }
313
314 if let Some(x) = rule.min_width {
315 resolved.min_width = Some(x);
316 }
317 if let Some(x) = rule.min_height {
318 resolved.min_height = Some(x);
319 }
320 if let Some(x) = rule.max_width {
321 resolved.max_width = Some(x);
322 }
323 if let Some(x) = rule.max_height {
324 resolved.max_height = Some(x);
325 }
326
327 resolved.focus_ring.merge_with(&rule.focus_ring);
328 resolved.border.merge_with(&rule.border);
329 resolved.shadow.merge_with(&rule.shadow);
330 resolved.tab_indicator.merge_with(&rule.tab_indicator);
331
332 if let Some(x) = rule.draw_border_with_background {
333 resolved.draw_border_with_background = Some(x);
334 }
335 if let Some(x) = rule.opacity {
336 resolved.opacity = Some(x);
337 }
338 if let Some(x) = rule.geometry_corner_radius {
339 resolved.geometry_corner_radius = Some(x);
340 }
341 if let Some(x) = rule.clip_to_geometry {
342 resolved.clip_to_geometry = Some(x);
343 }
344 if let Some(x) = rule.baba_is_float {
345 resolved.baba_is_float = Some(x);
346 }
347 if let Some(x) = rule.block_out_from {
348 resolved.block_out_from = Some(x);
349 }
350 if let Some(x) = rule.variable_refresh_rate {
351 resolved.variable_refresh_rate = Some(x);
352 }
353 if let Some(x) = rule.scroll_factor {
354 resolved.scroll_factor = Some(x.0);
355 }
356 if let Some(x) = rule.tiled_state {
357 resolved.tiled_state = Some(x);
358 }
359 }
360
361 resolved.open_on_output = open_on_output.map(|x| x.to_owned());
362 resolved.open_on_workspace = open_on_workspace.map(|x| x.to_owned());
363 });
364
365 resolved
366 }
367
368 pub fn apply_min_size(&self, min_size: Size<i32, Logical>) -> Size<i32, Logical> {
369 let mut size = min_size;
370
371 if let Some(x) = self.min_width {
372 size.w = max(size.w, i32::from(x));
373 }
374 if let Some(x) = self.min_height {
375 size.h = max(size.h, i32::from(x));
376 }
377
378 size
379 }
380
381 pub fn apply_max_size(&self, max_size: Size<i32, Logical>) -> Size<i32, Logical> {
382 let mut size = max_size;
383
384 if let Some(x) = self.max_width {
385 if size.w == 0 {
386 size.w = i32::from(x);
387 } else if x > 0 {
388 size.w = min(size.w, i32::from(x));
389 }
390 }
391 if let Some(x) = self.max_height {
392 if size.h == 0 {
393 size.h = i32::from(x);
394 } else if x > 0 {
395 size.h = min(size.h, i32::from(x));
396 }
397 }
398
399 size
400 }
401
402 pub fn apply_min_max_size(
403 &self,
404 min_size: Size<i32, Logical>,
405 max_size: Size<i32, Logical>,
406 ) -> (Size<i32, Logical>, Size<i32, Logical>) {
407 let min_size = self.apply_min_size(min_size);
408 let max_size = self.apply_max_size(max_size);
409 (min_size, max_size)
410 }
411
412 pub fn compute_open_floating(&self, toplevel: &ToplevelSurface) -> bool {
413 if let Some(res) = self.open_floating {
414 return res;
415 }
416
417 // Windows with a parent (usually dialogs) open as floating by default.
418 if toplevel.parent().is_some() {
419 return true;
420 }
421
422 let (min_size, max_size) = with_states(toplevel.wl_surface(), |state| {
423 let mut guard = state.cached_state.get::<SurfaceCachedState>();
424 let current = guard.current();
425 (current.min_size, current.max_size)
426 });
427 let (min_size, max_size) = self.apply_min_max_size(min_size, max_size);
428
429 // We open fixed-height windows as floating.
430 min_size.h > 0 && min_size.h == max_size.h
431 }
432}
433
434fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool {
435 // Must be ensured by the caller.
436 let server_pending = role.server_pending.as_ref().unwrap();
437
438 if let Some(is_focused) = m.is_focused {
439 if window.is_focused() != is_focused {
440 return false;
441 }
442 }
443
444 if let Some(is_urgent) = m.is_urgent {
445 if window.is_urgent() != is_urgent {
446 return false;
447 }
448 }
449
450 if let Some(is_active) = m.is_active {
451 // Our "is-active" definition corresponds to the window having a pending Activated state.
452 let pending_activated = server_pending
453 .states
454 .contains(xdg_toplevel::State::Activated);
455 if is_active != pending_activated {
456 return false;
457 }
458 }
459
460 if let Some(app_id_re) = &m.app_id {
461 let Some(app_id) = &role.app_id else {
462 return false;
463 };
464 if !app_id_re.0.is_match(app_id) {
465 return false;
466 }
467 }
468
469 if let Some(title_re) = &m.title {
470 let Some(title) = &role.title else {
471 return false;
472 };
473 if !title_re.0.is_match(title) {
474 return false;
475 }
476 }
477
478 if let Some(is_active_in_column) = m.is_active_in_column {
479 if window.is_active_in_column() != is_active_in_column {
480 return false;
481 }
482 }
483
484 if let Some(is_floating) = m.is_floating {
485 if window.is_floating() != is_floating {
486 return false;
487 }
488 }
489
490 if let Some(is_window_cast_target) = m.is_window_cast_target {
491 if window.is_window_cast_target() != is_window_cast_target {
492 return false;
493 }
494 }
495
496 true
497}