forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBf51dd67Ivan Molodetskikh2 years ago981 commits

mod.rs218 lines, 6.5 KiBHistoryRawDownload
1use niri_config::{BlockOutFrom, Match, WindowRule};
2use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
3use smithay::wayland::compositor::with_states;
4use smithay::wayland::shell::xdg::{
5 ToplevelSurface, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes,
6};
7
8use crate::layout::workspace::ColumnWidth;
9
10pub mod mapped;
11pub use mapped::Mapped;
12
13pub mod unmapped;
14pub use unmapped::{InitialConfigureState, Unmapped};
15
16/// Reference to a mapped or unmapped window.
17#[derive(Debug, Clone, Copy)]
18pub enum WindowRef<'a> {
19 Unmapped(&'a Unmapped),
20 Mapped(&'a Mapped),
21}
22
23/// Rules fully resolved for a window.
24#[derive(Debug, PartialEq)]
25pub struct ResolvedWindowRules {
26 /// Default width for this window.
27 ///
28 /// - `None`: unset (global default should be used).
29 /// - `Some(None)`: set to empty (window picks its own width).
30 /// - `Some(Some(width))`: set to a particular width.
31 pub default_width: Option<Option<ColumnWidth>>,
32
33 /// Output to open this window on.
34 pub open_on_output: Option<String>,
35
36 /// Whether the window should open full-width.
37 pub open_maximized: Option<bool>,
38
39 /// Whether the window should open fullscreen.
40 pub open_fullscreen: Option<bool>,
41
42 /// Extra bound on the minimum window width.
43 pub min_width: Option<u16>,
44 /// Extra bound on the minimum window height.
45 pub min_height: Option<u16>,
46 /// Extra bound on the maximum window width.
47 pub max_width: Option<u16>,
48 /// Extra bound on the maximum window height.
49 pub max_height: Option<u16>,
50
51 /// Whether or not to draw the border with a solid background.
52 ///
53 /// `None` means using the SSD heuristic.
54 pub draw_border_with_background: Option<bool>,
55
56 /// Extra opacity to draw this window with.
57 pub opacity: Option<f32>,
58
59 /// Whether to block out this window from certain render targets.
60 pub block_out_from: Option<BlockOutFrom>,
61}
62
63impl<'a> WindowRef<'a> {
64 pub fn toplevel(self) -> &'a ToplevelSurface {
65 match self {
66 WindowRef::Unmapped(unmapped) => unmapped.toplevel(),
67 WindowRef::Mapped(mapped) => mapped.toplevel(),
68 }
69 }
70
71 pub fn is_focused(self) -> bool {
72 match self {
73 WindowRef::Unmapped(_) => false,
74 WindowRef::Mapped(mapped) => mapped.is_focused(),
75 }
76 }
77}
78
79impl ResolvedWindowRules {
80 pub const fn empty() -> Self {
81 Self {
82 default_width: None,
83 open_on_output: None,
84 open_maximized: None,
85 open_fullscreen: None,
86 min_width: None,
87 min_height: None,
88 max_width: None,
89 max_height: None,
90 draw_border_with_background: None,
91 opacity: None,
92 block_out_from: None,
93 }
94 }
95
96 pub fn compute(rules: &[WindowRule], window: WindowRef) -> Self {
97 let _span = tracy_client::span!("ResolvedWindowRules::compute");
98
99 let mut resolved = ResolvedWindowRules::empty();
100
101 let toplevel = window.toplevel();
102 with_states(toplevel.wl_surface(), |states| {
103 let mut role = states
104 .data_map
105 .get::<XdgToplevelSurfaceData>()
106 .unwrap()
107 .lock()
108 .unwrap();
109
110 // Ensure server_pending like in Smithay's with_pending_state().
111 if role.server_pending.is_none() {
112 role.server_pending = Some(role.current_server_state().clone());
113 }
114
115 let mut open_on_output = None;
116
117 for rule in rules {
118 let matches = |m| window_matches(window, &role, m);
119
120 if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) {
121 continue;
122 }
123
124 if rule.excludes.iter().any(matches) {
125 continue;
126 }
127
128 if let Some(x) = rule
129 .default_column_width
130 .as_ref()
131 .map(|d| d.0.map(ColumnWidth::from))
132 {
133 resolved.default_width = Some(x);
134 }
135
136 if let Some(x) = rule.open_on_output.as_deref() {
137 open_on_output = Some(x);
138 }
139
140 if let Some(x) = rule.open_maximized {
141 resolved.open_maximized = Some(x);
142 }
143
144 if let Some(x) = rule.open_fullscreen {
145 resolved.open_fullscreen = Some(x);
146 }
147
148 if let Some(x) = rule.min_width {
149 resolved.min_width = Some(x);
150 }
151 if let Some(x) = rule.min_height {
152 resolved.min_height = Some(x);
153 }
154 if let Some(x) = rule.max_width {
155 resolved.max_width = Some(x);
156 }
157 if let Some(x) = rule.max_height {
158 resolved.max_height = Some(x);
159 }
160
161 if let Some(x) = rule.draw_border_with_background {
162 resolved.draw_border_with_background = Some(x);
163 }
164 if let Some(x) = rule.opacity {
165 resolved.opacity = Some(x);
166 }
167 if let Some(x) = rule.block_out_from {
168 resolved.block_out_from = Some(x);
169 }
170 }
171
172 resolved.open_on_output = open_on_output.map(|x| x.to_owned());
173 });
174
175 resolved
176 }
177}
178
179fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool {
180 // Must be ensured by the caller.
181 let server_pending = role.server_pending.as_ref().unwrap();
182
183 if let Some(is_focused) = m.is_focused {
184 if window.is_focused() != is_focused {
185 return false;
186 }
187 }
188
189 if let Some(is_active) = m.is_active {
190 // Our "is-active" definition corresponds to the window having a pending Activated state.
191 let pending_activated = server_pending
192 .states
193 .contains(xdg_toplevel::State::Activated);
194 if is_active != pending_activated {
195 return false;
196 }
197 }
198
199 if let Some(app_id_re) = &m.app_id {
200 let Some(app_id) = &role.app_id else {
201 return false;
202 };
203 if !app_id_re.is_match(app_id) {
204 return false;
205 }
206 }
207
208 if let Some(title_re) = &m.title {
209 let Some(title) = &role.title else {
210 return false;
211 };
212 if !title_re.is_match(title) {
213 return false;
214 }
215 }
216
217 true
218}