forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

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