forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

mod.rs462 lines, 14.9 KiBHistoryRawDownload
1use std::cmp::{max, min};
2
3use niri_config::utils::MergeWith as _;
4use niri_config::window_rule::{Match, OnXdgActivate, VrrRule, WindowRule};
5use niri_config::{
6 BackgroundEffect, BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize,
7 ResolvedPopupsRules, ShadowRule, 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, Default, PartialEq, Clone)]
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 /// What to do on xdg-activation requests.
77 pub on_xdg_activate: Option<OnXdgActivate>,
78
79 /// Extra bound on the minimum window width.
80 pub min_width: Option<u16>,
81 /// Extra bound on the minimum window height.
82 pub min_height: Option<u16>,
83 /// Extra bound on the maximum window width.
84 pub max_width: Option<u16>,
85 /// Extra bound on the maximum window height.
86 pub max_height: Option<u16>,
87
88 /// Focus ring overrides.
89 pub focus_ring: BorderRule,
90 /// Window border overrides.
91 pub border: BorderRule,
92 /// Shadow overrides.
93 pub shadow: ShadowRule,
94 /// Tab indicator overrides.
95 pub tab_indicator: TabIndicatorRule,
96
97 /// Whether or not to draw the border with a solid background.
98 ///
99 /// `None` means using the SSD heuristic.
100 pub draw_border_with_background: Option<bool>,
101
102 /// Extra opacity to draw this window with.
103 pub opacity: Option<f32>,
104
105 /// Corner radius to assume this window has.
106 pub geometry_corner_radius: Option<CornerRadius>,
107
108 /// Whether to clip this window to its geometry, including the corner radius.
109 pub clip_to_geometry: Option<bool>,
110
111 /// Whether to bob this window up and down.
112 pub baba_is_float: Option<bool>,
113
114 /// Whether to block out this window from certain render targets.
115 pub block_out_from: Option<BlockOutFrom>,
116
117 /// Whether to enable VRR on this window's primary output if it is on-demand.
118 pub variable_refresh_rate: Option<VrrRule>,
119
120 /// Multiplier for all scroll events sent to this window.
121 pub scroll_factor: Option<f64>,
122
123 /// Pinch gesture sensitivity for this window.
124 pub pinch_sensitivity: Option<f64>,
125
126 /// Override whether to set the Tiled xdg-toplevel state on the window.
127 pub tiled_state: Option<bool>,
128
129 /// Background effect configuration.
130 pub background_effect: BackgroundEffect,
131
132 /// Rules for this window's popups.
133 pub popups: ResolvedPopupsRules,
134}
135
136impl<'a> WindowRef<'a> {
137 pub fn toplevel(self) -> &'a ToplevelSurface {
138 match self {
139 WindowRef::Unmapped(unmapped) => unmapped.toplevel(),
140 WindowRef::Mapped(mapped) => mapped.toplevel(),
141 }
142 }
143
144 pub fn is_focused(self) -> bool {
145 match self {
146 WindowRef::Unmapped(_) => false,
147 WindowRef::Mapped(mapped) => mapped.is_focused(),
148 }
149 }
150
151 pub fn is_urgent(self) -> bool {
152 match self {
153 WindowRef::Unmapped(_) => false,
154 WindowRef::Mapped(mapped) => mapped.is_urgent(),
155 }
156 }
157
158 pub fn is_active_in_column(self) -> bool {
159 match self {
160 WindowRef::Unmapped(_) => true,
161 WindowRef::Mapped(mapped) => mapped.is_active_in_column(),
162 }
163 }
164
165 pub fn is_floating(self) -> bool {
166 match self {
167 // FIXME: This means you cannot set initial configure rules based on is-floating. I'm
168 // not sure there's a good way to support it, since this matcher makes a cycle with the
169 // open-floating rule.
170 //
171 // That said, I don't think there are a lot of useful initial configure properties you
172 // may want to set through an is-floating matcher? Like, if you're configuring a
173 // specific window to open as floating, you can also set those properties in that same
174 // window rule, rather than relying on a different is-floating rule.
175 WindowRef::Unmapped(_) => false,
176 WindowRef::Mapped(mapped) => mapped.is_floating(),
177 }
178 }
179
180 pub fn is_window_cast_target(self) -> bool {
181 match self {
182 WindowRef::Unmapped(_) => false,
183 WindowRef::Mapped(mapped) => mapped.is_window_cast_target(),
184 }
185 }
186}
187
188impl ResolvedWindowRules {
189 pub fn compute(rules: &[WindowRule], window: WindowRef, is_at_startup: bool) -> Self {
190 let _span = tracy_client::span!("ResolvedWindowRules::compute");
191
192 let mut resolved = ResolvedWindowRules::default();
193
194 with_toplevel_role(window.toplevel(), |role| {
195 // Ensure server_pending like in Smithay's with_pending_state().
196 if role.server_pending.is_none() {
197 role.server_pending = Some(role.current_server_state().clone());
198 }
199
200 let mut open_on_output = None;
201 let mut open_on_workspace = None;
202
203 for rule in rules {
204 let matches = |m: &Match| {
205 if let Some(at_startup) = m.at_startup {
206 if at_startup != is_at_startup {
207 return false;
208 }
209 }
210
211 window_matches(window, role, m)
212 };
213
214 if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) {
215 continue;
216 }
217
218 if rule.excludes.iter().any(matches) {
219 continue;
220 }
221
222 if let Some(x) = rule.default_column_width {
223 resolved.default_width = Some(x.0);
224 }
225
226 if let Some(x) = rule.default_window_height {
227 resolved.default_height = Some(x.0);
228 }
229
230 if let Some(x) = rule.default_column_display {
231 resolved.default_column_display = Some(x);
232 }
233
234 if let Some(x) = rule.default_floating_position {
235 resolved.default_floating_position = Some(x);
236 }
237
238 if let Some(x) = rule.open_on_output.as_deref() {
239 open_on_output = Some(x);
240 }
241
242 if let Some(x) = rule.open_on_workspace.as_deref() {
243 open_on_workspace = Some(x);
244 }
245
246 if let Some(x) = rule.open_maximized {
247 resolved.open_maximized = Some(x);
248 }
249
250 if let Some(x) = rule.open_maximized_to_edges {
251 resolved.open_maximized_to_edges = Some(x);
252 }
253
254 if let Some(x) = rule.open_fullscreen {
255 resolved.open_fullscreen = Some(x);
256 }
257
258 if let Some(x) = rule.open_floating {
259 resolved.open_floating = Some(x);
260 }
261
262 if let Some(x) = rule.open_focused {
263 resolved.open_focused = Some(x);
264 }
265
266 if let Some(x) = rule.on_xdg_activate {
267 resolved.on_xdg_activate = Some(x);
268 }
269
270 if let Some(x) = rule.min_width {
271 resolved.min_width = Some(x);
272 }
273 if let Some(x) = rule.min_height {
274 resolved.min_height = Some(x);
275 }
276 if let Some(x) = rule.max_width {
277 resolved.max_width = Some(x);
278 }
279 if let Some(x) = rule.max_height {
280 resolved.max_height = Some(x);
281 }
282
283 resolved.focus_ring.merge_with(&rule.focus_ring);
284 resolved.border.merge_with(&rule.border);
285 resolved.shadow.merge_with(&rule.shadow);
286 resolved.tab_indicator.merge_with(&rule.tab_indicator);
287
288 if let Some(x) = rule.draw_border_with_background {
289 resolved.draw_border_with_background = Some(x);
290 }
291 if let Some(x) = rule.opacity {
292 resolved.opacity = Some(x);
293 }
294 if let Some(x) = rule.geometry_corner_radius {
295 resolved.geometry_corner_radius = Some(x);
296 }
297 if let Some(x) = rule.clip_to_geometry {
298 resolved.clip_to_geometry = Some(x);
299 }
300 if let Some(x) = rule.baba_is_float {
301 resolved.baba_is_float = Some(x);
302 }
303 if let Some(x) = rule.block_out_from {
304 resolved.block_out_from = Some(x);
305 }
306 if let Some(x) = rule.variable_refresh_rate {
307 resolved.variable_refresh_rate = Some(x);
308 }
309 if let Some(x) = rule.scroll_factor {
310 resolved.scroll_factor = Some(x.0);
311 }
312 if let Some(x) = rule.pinch_sensitivity {
313 resolved.pinch_sensitivity = Some(x.0);
314 }
315 if let Some(x) = rule.tiled_state {
316 resolved.tiled_state = Some(x);
317 }
318
319 resolved
320 .background_effect
321 .merge_with(&rule.background_effect);
322
323 resolved.popups.merge_with(&rule.popups);
324 }
325
326 resolved.open_on_output = open_on_output.map(|x| x.to_owned());
327 resolved.open_on_workspace = open_on_workspace.map(|x| x.to_owned());
328 });
329
330 resolved
331 }
332
333 pub fn apply_min_size(&self, min_size: Size<i32, Logical>) -> Size<i32, Logical> {
334 let mut size = min_size;
335
336 if let Some(x) = self.min_width {
337 size.w = max(size.w, i32::from(x));
338 }
339 if let Some(x) = self.min_height {
340 size.h = max(size.h, i32::from(x));
341 }
342
343 size
344 }
345
346 pub fn apply_max_size(&self, max_size: Size<i32, Logical>) -> Size<i32, Logical> {
347 let mut size = max_size;
348
349 if let Some(x) = self.max_width {
350 if size.w == 0 {
351 size.w = i32::from(x);
352 } else if x > 0 {
353 size.w = min(size.w, i32::from(x));
354 }
355 }
356 if let Some(x) = self.max_height {
357 if size.h == 0 {
358 size.h = i32::from(x);
359 } else if x > 0 {
360 size.h = min(size.h, i32::from(x));
361 }
362 }
363
364 size
365 }
366
367 pub fn apply_min_max_size(
368 &self,
369 min_size: Size<i32, Logical>,
370 max_size: Size<i32, Logical>,
371 ) -> (Size<i32, Logical>, Size<i32, Logical>) {
372 let min_size = self.apply_min_size(min_size);
373 let max_size = self.apply_max_size(max_size);
374 (min_size, max_size)
375 }
376
377 pub fn compute_open_floating(&self, toplevel: &ToplevelSurface) -> bool {
378 if let Some(res) = self.open_floating {
379 return res;
380 }
381
382 // Windows with a parent (usually dialogs) open as floating by default.
383 if toplevel.parent().is_some() {
384 return true;
385 }
386
387 let (min_size, max_size) = with_states(toplevel.wl_surface(), |state| {
388 let mut guard = state.cached_state.get::<SurfaceCachedState>();
389 let current = guard.current();
390 (current.min_size, current.max_size)
391 });
392 let (min_size, max_size) = self.apply_min_max_size(min_size, max_size);
393
394 // We open fixed-height windows as floating.
395 min_size.h > 0 && min_size.h == max_size.h
396 }
397}
398
399fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool {
400 // Must be ensured by the caller.
401 let server_pending = role.server_pending.as_ref().unwrap();
402
403 if let Some(is_focused) = m.is_focused {
404 if window.is_focused() != is_focused {
405 return false;
406 }
407 }
408
409 if let Some(is_urgent) = m.is_urgent {
410 if window.is_urgent() != is_urgent {
411 return false;
412 }
413 }
414
415 if let Some(is_active) = m.is_active {
416 // Our "is-active" definition corresponds to the window having a pending Activated state.
417 let pending_activated = server_pending
418 .states
419 .contains(xdg_toplevel::State::Activated);
420 if is_active != pending_activated {
421 return false;
422 }
423 }
424
425 if let Some(app_id_re) = &m.app_id {
426 let Some(app_id) = &role.app_id else {
427 return false;
428 };
429 if !app_id_re.0.is_match(app_id) {
430 return false;
431 }
432 }
433
434 if let Some(title_re) = &m.title {
435 let Some(title) = &role.title else {
436 return false;
437 };
438 if !title_re.0.is_match(title) {
439 return false;
440 }
441 }
442
443 if let Some(is_active_in_column) = m.is_active_in_column {
444 if window.is_active_in_column() != is_active_in_column {
445 return false;
446 }
447 }
448
449 if let Some(is_floating) = m.is_floating {
450 if window.is_floating() != is_floating {
451 return false;
452 }
453 }
454
455 if let Some(is_window_cast_target) = m.is_window_cast_target {
456 if window.is_window_cast_target() != is_window_cast_target {
457 return false;
458 }
459 }
460
461 true
462}