forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

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