forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBdde9214Ivan Molodetskikh1 year ago1835 commits

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