forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB66583cdIvan Molodetskikh1 year ago2489 commits

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