forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBa6baef7Anton Kesy10 months ago2623 commits

mod.rs437 lines, 14.1 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, 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 /// 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 fn compute(rules: &[WindowRule], window: WindowRef, is_at_startup: bool) -> Self {
178 let _span = tracy_client::span!("ResolvedWindowRules::compute");
179
180 let mut resolved = ResolvedWindowRules::default();
181
182 with_toplevel_role(window.toplevel(), |role| {
183 // Ensure server_pending like in Smithay's with_pending_state().
184 if role.server_pending.is_none() {
185 role.server_pending = Some(role.current_server_state().clone());
186 }
187
188 let mut open_on_output = None;
189 let mut open_on_workspace = None;
190
191 for rule in rules {
192 let matches = |m: &Match| {
193 if let Some(at_startup) = m.at_startup {
194 if at_startup != is_at_startup {
195 return false;
196 }
197 }
198
199 window_matches(window, role, m)
200 };
201
202 if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) {
203 continue;
204 }
205
206 if rule.excludes.iter().any(matches) {
207 continue;
208 }
209
210 if let Some(x) = rule.default_column_width {
211 resolved.default_width = Some(x.0);
212 }
213
214 if let Some(x) = rule.default_window_height {
215 resolved.default_height = Some(x.0);
216 }
217
218 if let Some(x) = rule.default_column_display {
219 resolved.default_column_display = Some(x);
220 }
221
222 if let Some(x) = rule.default_floating_position {
223 resolved.default_floating_position = Some(x);
224 }
225
226 if let Some(x) = rule.open_on_output.as_deref() {
227 open_on_output = Some(x);
228 }
229
230 if let Some(x) = rule.open_on_workspace.as_deref() {
231 open_on_workspace = Some(x);
232 }
233
234 if let Some(x) = rule.open_maximized {
235 resolved.open_maximized = Some(x);
236 }
237
238 if let Some(x) = rule.open_maximized_to_edges {
239 resolved.open_maximized_to_edges = Some(x);
240 }
241
242 if let Some(x) = rule.open_fullscreen {
243 resolved.open_fullscreen = Some(x);
244 }
245
246 if let Some(x) = rule.open_floating {
247 resolved.open_floating = Some(x);
248 }
249
250 if let Some(x) = rule.open_focused {
251 resolved.open_focused = Some(x);
252 }
253
254 if let Some(x) = rule.min_width {
255 resolved.min_width = Some(x);
256 }
257 if let Some(x) = rule.min_height {
258 resolved.min_height = Some(x);
259 }
260 if let Some(x) = rule.max_width {
261 resolved.max_width = Some(x);
262 }
263 if let Some(x) = rule.max_height {
264 resolved.max_height = Some(x);
265 }
266
267 resolved.focus_ring.merge_with(&rule.focus_ring);
268 resolved.border.merge_with(&rule.border);
269 resolved.shadow.merge_with(&rule.shadow);
270 resolved.tab_indicator.merge_with(&rule.tab_indicator);
271
272 if let Some(x) = rule.draw_border_with_background {
273 resolved.draw_border_with_background = Some(x);
274 }
275 if let Some(x) = rule.opacity {
276 resolved.opacity = Some(x);
277 }
278 if let Some(x) = rule.geometry_corner_radius {
279 resolved.geometry_corner_radius = Some(x);
280 }
281 if let Some(x) = rule.clip_to_geometry {
282 resolved.clip_to_geometry = Some(x);
283 }
284 if let Some(x) = rule.baba_is_float {
285 resolved.baba_is_float = Some(x);
286 }
287 if let Some(x) = rule.block_out_from {
288 resolved.block_out_from = Some(x);
289 }
290 if let Some(x) = rule.variable_refresh_rate {
291 resolved.variable_refresh_rate = Some(x);
292 }
293 if let Some(x) = rule.scroll_factor {
294 resolved.scroll_factor = Some(x.0);
295 }
296 if let Some(x) = rule.tiled_state {
297 resolved.tiled_state = Some(x);
298 }
299 }
300
301 resolved.open_on_output = open_on_output.map(|x| x.to_owned());
302 resolved.open_on_workspace = open_on_workspace.map(|x| x.to_owned());
303 });
304
305 resolved
306 }
307
308 pub fn apply_min_size(&self, min_size: Size<i32, Logical>) -> Size<i32, Logical> {
309 let mut size = min_size;
310
311 if let Some(x) = self.min_width {
312 size.w = max(size.w, i32::from(x));
313 }
314 if let Some(x) = self.min_height {
315 size.h = max(size.h, i32::from(x));
316 }
317
318 size
319 }
320
321 pub fn apply_max_size(&self, max_size: Size<i32, Logical>) -> Size<i32, Logical> {
322 let mut size = max_size;
323
324 if let Some(x) = self.max_width {
325 if size.w == 0 {
326 size.w = i32::from(x);
327 } else if x > 0 {
328 size.w = min(size.w, i32::from(x));
329 }
330 }
331 if let Some(x) = self.max_height {
332 if size.h == 0 {
333 size.h = i32::from(x);
334 } else if x > 0 {
335 size.h = min(size.h, i32::from(x));
336 }
337 }
338
339 size
340 }
341
342 pub fn apply_min_max_size(
343 &self,
344 min_size: Size<i32, Logical>,
345 max_size: Size<i32, Logical>,
346 ) -> (Size<i32, Logical>, Size<i32, Logical>) {
347 let min_size = self.apply_min_size(min_size);
348 let max_size = self.apply_max_size(max_size);
349 (min_size, max_size)
350 }
351
352 pub fn compute_open_floating(&self, toplevel: &ToplevelSurface) -> bool {
353 if let Some(res) = self.open_floating {
354 return res;
355 }
356
357 // Windows with a parent (usually dialogs) open as floating by default.
358 if toplevel.parent().is_some() {
359 return true;
360 }
361
362 let (min_size, max_size) = with_states(toplevel.wl_surface(), |state| {
363 let mut guard = state.cached_state.get::<SurfaceCachedState>();
364 let current = guard.current();
365 (current.min_size, current.max_size)
366 });
367 let (min_size, max_size) = self.apply_min_max_size(min_size, max_size);
368
369 // We open fixed-height windows as floating.
370 min_size.h > 0 && min_size.h == max_size.h
371 }
372}
373
374fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool {
375 // Must be ensured by the caller.
376 let server_pending = role.server_pending.as_ref().unwrap();
377
378 if let Some(is_focused) = m.is_focused {
379 if window.is_focused() != is_focused {
380 return false;
381 }
382 }
383
384 if let Some(is_urgent) = m.is_urgent {
385 if window.is_urgent() != is_urgent {
386 return false;
387 }
388 }
389
390 if let Some(is_active) = m.is_active {
391 // Our "is-active" definition corresponds to the window having a pending Activated state.
392 let pending_activated = server_pending
393 .states
394 .contains(xdg_toplevel::State::Activated);
395 if is_active != pending_activated {
396 return false;
397 }
398 }
399
400 if let Some(app_id_re) = &m.app_id {
401 let Some(app_id) = &role.app_id else {
402 return false;
403 };
404 if !app_id_re.0.is_match(app_id) {
405 return false;
406 }
407 }
408
409 if let Some(title_re) = &m.title {
410 let Some(title) = &role.title else {
411 return false;
412 };
413 if !title_re.0.is_match(title) {
414 return false;
415 }
416 }
417
418 if let Some(is_active_in_column) = m.is_active_in_column {
419 if window.is_active_in_column() != is_active_in_column {
420 return false;
421 }
422 }
423
424 if let Some(is_floating) = m.is_floating {
425 if window.is_floating() != is_floating {
426 return false;
427 }
428 }
429
430 if let Some(is_window_cast_target) = m.is_window_cast_target {
431 if window.is_window_cast_target() != is_window_cast_target {
432 return false;
433 }
434 }
435
436 true
437}