forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9ff2f83Ivan Molodetskikh1 year ago2099 commits

mod.rs477 lines, 15.3 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 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_active_in_column(self) -> bool {
135 match self {
136 WindowRef::Unmapped(_) => true,
137 WindowRef::Mapped(mapped) => mapped.is_active_in_column(),
138 }
139 }
140
141 pub fn is_floating(self) -> bool {
142 match self {
143 // FIXME: This means you cannot set initial configure rules based on is-floating. I'm
144 // not sure there's a good way to support it, since this matcher makes a cycle with the
145 // open-floating rule.
146 //
147 // That said, I don't think there are a lot of useful initial configure properties you
148 // may want to set through an is-floating matcher? Like, if you're configuring a
149 // specific window to open as floating, you can also set those properties in that same
150 // window rule, rather than relying on a different is-floating rule.
151 WindowRef::Unmapped(_) => false,
152 WindowRef::Mapped(mapped) => mapped.is_floating(),
153 }
154 }
155
156 pub fn is_window_cast_target(self) -> bool {
157 match self {
158 WindowRef::Unmapped(_) => false,
159 WindowRef::Mapped(mapped) => mapped.is_window_cast_target(),
160 }
161 }
162}
163
164impl ResolvedWindowRules {
165 pub const fn empty() -> Self {
166 Self {
167 default_width: None,
168 default_height: None,
169 default_column_display: None,
170 default_floating_position: None,
171 open_on_output: None,
172 open_on_workspace: None,
173 open_maximized: None,
174 open_fullscreen: None,
175 open_floating: None,
176 open_focused: None,
177 min_width: None,
178 min_height: None,
179 max_width: None,
180 max_height: None,
181 focus_ring: BorderRule {
182 off: false,
183 on: false,
184 width: None,
185 active_color: None,
186 inactive_color: None,
187 active_gradient: None,
188 inactive_gradient: None,
189 },
190 border: BorderRule {
191 off: false,
192 on: false,
193 width: None,
194 active_color: None,
195 inactive_color: None,
196 active_gradient: None,
197 inactive_gradient: None,
198 },
199 shadow: ShadowRule {
200 off: false,
201 on: false,
202 offset: None,
203 softness: None,
204 spread: None,
205 draw_behind_window: None,
206 color: None,
207 inactive_color: None,
208 },
209 tab_indicator: TabIndicatorRule {
210 active_color: None,
211 inactive_color: None,
212 active_gradient: None,
213 inactive_gradient: None,
214 },
215 draw_border_with_background: None,
216 opacity: None,
217 geometry_corner_radius: None,
218 clip_to_geometry: None,
219 baba_is_float: None,
220 block_out_from: None,
221 variable_refresh_rate: None,
222 scroll_factor: None,
223 tiled_state: None,
224 }
225 }
226
227 pub fn compute(rules: &[WindowRule], window: WindowRef, is_at_startup: bool) -> Self {
228 let _span = tracy_client::span!("ResolvedWindowRules::compute");
229
230 let mut resolved = ResolvedWindowRules::empty();
231
232 with_toplevel_role(window.toplevel(), |role| {
233 // Ensure server_pending like in Smithay's with_pending_state().
234 if role.server_pending.is_none() {
235 role.server_pending = Some(role.current_server_state().clone());
236 }
237
238 let mut open_on_output = None;
239 let mut open_on_workspace = None;
240
241 for rule in rules {
242 let matches = |m: &Match| {
243 if let Some(at_startup) = m.at_startup {
244 if at_startup != is_at_startup {
245 return false;
246 }
247 }
248
249 window_matches(window, role, m)
250 };
251
252 if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) {
253 continue;
254 }
255
256 if rule.excludes.iter().any(matches) {
257 continue;
258 }
259
260 if let Some(x) = rule.default_column_width {
261 resolved.default_width = Some(x.0);
262 }
263
264 if let Some(x) = rule.default_window_height {
265 resolved.default_height = Some(x.0);
266 }
267
268 if let Some(x) = rule.default_column_display {
269 resolved.default_column_display = Some(x);
270 }
271
272 if let Some(x) = rule.default_floating_position {
273 resolved.default_floating_position = Some(x);
274 }
275
276 if let Some(x) = rule.open_on_output.as_deref() {
277 open_on_output = Some(x);
278 }
279
280 if let Some(x) = rule.open_on_workspace.as_deref() {
281 open_on_workspace = Some(x);
282 }
283
284 if let Some(x) = rule.open_maximized {
285 resolved.open_maximized = Some(x);
286 }
287
288 if let Some(x) = rule.open_fullscreen {
289 resolved.open_fullscreen = Some(x);
290 }
291
292 if let Some(x) = rule.open_floating {
293 resolved.open_floating = Some(x);
294 }
295
296 if let Some(x) = rule.open_focused {
297 resolved.open_focused = Some(x);
298 }
299
300 if let Some(x) = rule.min_width {
301 resolved.min_width = Some(x);
302 }
303 if let Some(x) = rule.min_height {
304 resolved.min_height = Some(x);
305 }
306 if let Some(x) = rule.max_width {
307 resolved.max_width = Some(x);
308 }
309 if let Some(x) = rule.max_height {
310 resolved.max_height = Some(x);
311 }
312
313 resolved.focus_ring.merge_with(&rule.focus_ring);
314 resolved.border.merge_with(&rule.border);
315 resolved.shadow.merge_with(&rule.shadow);
316 resolved.tab_indicator.merge_with(&rule.tab_indicator);
317
318 if let Some(x) = rule.draw_border_with_background {
319 resolved.draw_border_with_background = Some(x);
320 }
321 if let Some(x) = rule.opacity {
322 resolved.opacity = Some(x);
323 }
324 if let Some(x) = rule.geometry_corner_radius {
325 resolved.geometry_corner_radius = Some(x);
326 }
327 if let Some(x) = rule.clip_to_geometry {
328 resolved.clip_to_geometry = Some(x);
329 }
330 if let Some(x) = rule.baba_is_float {
331 resolved.baba_is_float = Some(x);
332 }
333 if let Some(x) = rule.block_out_from {
334 resolved.block_out_from = Some(x);
335 }
336 if let Some(x) = rule.variable_refresh_rate {
337 resolved.variable_refresh_rate = Some(x);
338 }
339 if let Some(x) = rule.scroll_factor {
340 resolved.scroll_factor = Some(x.0);
341 }
342 if let Some(x) = rule.tiled_state {
343 resolved.tiled_state = Some(x);
344 }
345 }
346
347 resolved.open_on_output = open_on_output.map(|x| x.to_owned());
348 resolved.open_on_workspace = open_on_workspace.map(|x| x.to_owned());
349 });
350
351 resolved
352 }
353
354 pub fn apply_min_size(&self, min_size: Size<i32, Logical>) -> Size<i32, Logical> {
355 let mut size = min_size;
356
357 if let Some(x) = self.min_width {
358 size.w = max(size.w, i32::from(x));
359 }
360 if let Some(x) = self.min_height {
361 size.h = max(size.h, i32::from(x));
362 }
363
364 size
365 }
366
367 pub fn apply_max_size(&self, max_size: Size<i32, Logical>) -> Size<i32, Logical> {
368 let mut size = max_size;
369
370 if let Some(x) = self.max_width {
371 if size.w == 0 {
372 size.w = i32::from(x);
373 } else if x > 0 {
374 size.w = min(size.w, i32::from(x));
375 }
376 }
377 if let Some(x) = self.max_height {
378 if size.h == 0 {
379 size.h = i32::from(x);
380 } else if x > 0 {
381 size.h = min(size.h, i32::from(x));
382 }
383 }
384
385 size
386 }
387
388 pub fn apply_min_max_size(
389 &self,
390 min_size: Size<i32, Logical>,
391 max_size: Size<i32, Logical>,
392 ) -> (Size<i32, Logical>, Size<i32, Logical>) {
393 let min_size = self.apply_min_size(min_size);
394 let max_size = self.apply_max_size(max_size);
395 (min_size, max_size)
396 }
397
398 pub fn compute_open_floating(&self, toplevel: &ToplevelSurface) -> bool {
399 if let Some(res) = self.open_floating {
400 return res;
401 }
402
403 // Windows with a parent (usually dialogs) open as floating by default.
404 if toplevel.parent().is_some() {
405 return true;
406 }
407
408 let (min_size, max_size) = with_states(toplevel.wl_surface(), |state| {
409 let mut guard = state.cached_state.get::<SurfaceCachedState>();
410 let current = guard.current();
411 (current.min_size, current.max_size)
412 });
413 let (min_size, max_size) = self.apply_min_max_size(min_size, max_size);
414
415 // We open fixed-height windows as floating.
416 min_size.h > 0 && min_size.h == max_size.h
417 }
418}
419
420fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool {
421 // Must be ensured by the caller.
422 let server_pending = role.server_pending.as_ref().unwrap();
423
424 if let Some(is_focused) = m.is_focused {
425 if window.is_focused() != is_focused {
426 return false;
427 }
428 }
429
430 if let Some(is_active) = m.is_active {
431 // Our "is-active" definition corresponds to the window having a pending Activated state.
432 let pending_activated = server_pending
433 .states
434 .contains(xdg_toplevel::State::Activated);
435 if is_active != pending_activated {
436 return false;
437 }
438 }
439
440 if let Some(app_id_re) = &m.app_id {
441 let Some(app_id) = &role.app_id else {
442 return false;
443 };
444 if !app_id_re.0.is_match(app_id) {
445 return false;
446 }
447 }
448
449 if let Some(title_re) = &m.title {
450 let Some(title) = &role.title else {
451 return false;
452 };
453 if !title_re.0.is_match(title) {
454 return false;
455 }
456 }
457
458 if let Some(is_active_in_column) = m.is_active_in_column {
459 if window.is_active_in_column() != is_active_in_column {
460 return false;
461 }
462 }
463
464 if let Some(is_floating) = m.is_floating {
465 if window.is_floating() != is_floating {
466 return false;
467 }
468 }
469
470 if let Some(is_window_cast_target) = m.is_window_cast_target {
471 if window.is_window_cast_target() != is_window_cast_target {
472 return false;
473 }
474 }
475
476 true
477}