niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·64a9351·Ivan Molodetskikh·2 years ago·1319 commits
| 1 | use niri_config::{BlockOutFrom, BorderRule, CornerRadius, Match, WindowRule}; |
| 2 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 3 | use smithay::wayland::compositor::with_states; |
| 4 | use smithay::wayland::shell::xdg::{ |
| 5 | ToplevelSurface, XdgToplevelSurfaceData, XdgToplevelSurfaceRoleAttributes, |
| 6 | }; |
| 7 | |
| 8 | use crate::layout::workspace::ColumnWidth; |
| 9 | |
| 10 | pub mod mapped; |
| 11 | pub use mapped::Mapped; |
| 12 | |
| 13 | pub mod unmapped; |
| 14 | pub use unmapped::{InitialConfigureState, Unmapped}; |
| 15 | |
| 16 | /// Reference to a mapped or unmapped window. |
| 17 | #[derive(Debug, Clone, Copy)] |
| 18 | pub enum WindowRef<'a> { |
| 19 | Unmapped(&'a Unmapped), |
| 20 | Mapped(&'a Mapped), |
| 21 | } |
| 22 | |
| 23 | /// Rules fully resolved for a window. |
| 24 | #[derive(Debug, PartialEq)] |
| 25 | pub 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 | /// Workspace to open this window on. |
| 37 | pub open_on_workspace: Option<String>, |
| 38 | |
| 39 | /// Whether the window should open full-width. |
| 40 | pub open_maximized: Option<bool>, |
| 41 | |
| 42 | /// Whether the window should open fullscreen. |
| 43 | pub open_fullscreen: Option<bool>, |
| 44 | |
| 45 | /// Extra bound on the minimum window width. |
| 46 | pub min_width: Option<u16>, |
| 47 | /// Extra bound on the minimum window height. |
| 48 | pub min_height: Option<u16>, |
| 49 | /// Extra bound on the maximum window width. |
| 50 | pub max_width: Option<u16>, |
| 51 | /// Extra bound on the maximum window height. |
| 52 | pub max_height: Option<u16>, |
| 53 | |
| 54 | /// Focus ring overrides. |
| 55 | pub focus_ring: BorderRule, |
| 56 | /// Window border overrides. |
| 57 | pub border: BorderRule, |
| 58 | |
| 59 | /// Whether or not to draw the border with a solid background. |
| 60 | /// |
| 61 | /// `None` means using the SSD heuristic. |
| 62 | pub draw_border_with_background: Option<bool>, |
| 63 | |
| 64 | /// Extra opacity to draw this window with. |
| 65 | pub opacity: Option<f32>, |
| 66 | |
| 67 | /// Corner radius to assume this window has. |
| 68 | pub geometry_corner_radius: Option<CornerRadius>, |
| 69 | |
| 70 | /// Whether to clip this window to its geometry, including the corner radius. |
| 71 | pub clip_to_geometry: Option<bool>, |
| 72 | |
| 73 | /// Whether to block out this window from certain render targets. |
| 74 | pub block_out_from: Option<BlockOutFrom>, |
| 75 | |
| 76 | /// Whether to enable VRR on this window's primary output if it is on-demand. |
| 77 | pub variable_refresh_rate: Option<bool>, |
| 78 | } |
| 79 | |
| 80 | impl<'a> WindowRef<'a> { |
| 81 | pub fn toplevel(self) -> &'a ToplevelSurface { |
| 82 | match self { |
| 83 | WindowRef::Unmapped(unmapped) => unmapped.toplevel(), |
| 84 | WindowRef::Mapped(mapped) => mapped.toplevel(), |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub fn is_focused(self) -> bool { |
| 89 | match self { |
| 90 | WindowRef::Unmapped(_) => false, |
| 91 | WindowRef::Mapped(mapped) => mapped.is_focused(), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub fn is_active_in_column(self) -> bool { |
| 96 | match self { |
| 97 | WindowRef::Unmapped(_) => false, |
| 98 | WindowRef::Mapped(mapped) => mapped.is_active_in_column(), |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl ResolvedWindowRules { |
| 104 | pub const fn empty() -> Self { |
| 105 | Self { |
| 106 | default_width: None, |
| 107 | open_on_output: None, |
| 108 | open_on_workspace: None, |
| 109 | open_maximized: None, |
| 110 | open_fullscreen: None, |
| 111 | min_width: None, |
| 112 | min_height: None, |
| 113 | max_width: None, |
| 114 | max_height: None, |
| 115 | focus_ring: BorderRule { |
| 116 | off: false, |
| 117 | on: false, |
| 118 | width: None, |
| 119 | active_color: None, |
| 120 | inactive_color: None, |
| 121 | active_gradient: None, |
| 122 | inactive_gradient: None, |
| 123 | }, |
| 124 | border: BorderRule { |
| 125 | off: false, |
| 126 | on: false, |
| 127 | width: None, |
| 128 | active_color: None, |
| 129 | inactive_color: None, |
| 130 | active_gradient: None, |
| 131 | inactive_gradient: None, |
| 132 | }, |
| 133 | draw_border_with_background: None, |
| 134 | opacity: None, |
| 135 | geometry_corner_radius: None, |
| 136 | clip_to_geometry: None, |
| 137 | block_out_from: None, |
| 138 | variable_refresh_rate: None, |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | pub fn compute(rules: &[WindowRule], window: WindowRef, is_at_startup: bool) -> Self { |
| 143 | let _span = tracy_client::span!("ResolvedWindowRules::compute"); |
| 144 | |
| 145 | let mut resolved = ResolvedWindowRules::empty(); |
| 146 | |
| 147 | let toplevel = window.toplevel(); |
| 148 | with_states(toplevel.wl_surface(), |states| { |
| 149 | let mut role = states |
| 150 | .data_map |
| 151 | .get::<XdgToplevelSurfaceData>() |
| 152 | .unwrap() |
| 153 | .lock() |
| 154 | .unwrap(); |
| 155 | |
| 156 | // Ensure server_pending like in Smithay's with_pending_state(). |
| 157 | if role.server_pending.is_none() { |
| 158 | role.server_pending = Some(role.current_server_state().clone()); |
| 159 | } |
| 160 | |
| 161 | let mut open_on_output = None; |
| 162 | let mut open_on_workspace = None; |
| 163 | |
| 164 | for rule in rules { |
| 165 | let matches = |m: &Match| { |
| 166 | if let Some(at_startup) = m.at_startup { |
| 167 | if at_startup != is_at_startup { |
| 168 | return false; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | window_matches(window, &role, m) |
| 173 | }; |
| 174 | |
| 175 | if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) { |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | if rule.excludes.iter().any(matches) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | if let Some(x) = rule |
| 184 | .default_column_width |
| 185 | .as_ref() |
| 186 | .map(|d| d.0.map(ColumnWidth::from)) |
| 187 | { |
| 188 | resolved.default_width = Some(x); |
| 189 | } |
| 190 | |
| 191 | if let Some(x) = rule.open_on_output.as_deref() { |
| 192 | open_on_output = Some(x); |
| 193 | } |
| 194 | |
| 195 | if let Some(x) = rule.open_on_workspace.as_deref() { |
| 196 | open_on_workspace = Some(x); |
| 197 | } |
| 198 | |
| 199 | if let Some(x) = rule.open_maximized { |
| 200 | resolved.open_maximized = Some(x); |
| 201 | } |
| 202 | |
| 203 | if let Some(x) = rule.open_fullscreen { |
| 204 | resolved.open_fullscreen = Some(x); |
| 205 | } |
| 206 | |
| 207 | if let Some(x) = rule.min_width { |
| 208 | resolved.min_width = Some(x); |
| 209 | } |
| 210 | if let Some(x) = rule.min_height { |
| 211 | resolved.min_height = Some(x); |
| 212 | } |
| 213 | if let Some(x) = rule.max_width { |
| 214 | resolved.max_width = Some(x); |
| 215 | } |
| 216 | if let Some(x) = rule.max_height { |
| 217 | resolved.max_height = Some(x); |
| 218 | } |
| 219 | |
| 220 | resolved.focus_ring.merge_with(&rule.focus_ring); |
| 221 | resolved.border.merge_with(&rule.border); |
| 222 | |
| 223 | if let Some(x) = rule.draw_border_with_background { |
| 224 | resolved.draw_border_with_background = Some(x); |
| 225 | } |
| 226 | if let Some(x) = rule.opacity { |
| 227 | resolved.opacity = Some(x); |
| 228 | } |
| 229 | if let Some(x) = rule.geometry_corner_radius { |
| 230 | resolved.geometry_corner_radius = Some(x); |
| 231 | } |
| 232 | if let Some(x) = rule.clip_to_geometry { |
| 233 | resolved.clip_to_geometry = Some(x); |
| 234 | } |
| 235 | if let Some(x) = rule.block_out_from { |
| 236 | resolved.block_out_from = Some(x); |
| 237 | } |
| 238 | if let Some(x) = rule.variable_refresh_rate { |
| 239 | resolved.variable_refresh_rate = Some(x); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | resolved.open_on_output = open_on_output.map(|x| x.to_owned()); |
| 244 | resolved.open_on_workspace = open_on_workspace.map(|x| x.to_owned()); |
| 245 | }); |
| 246 | |
| 247 | resolved |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool { |
| 252 | // Must be ensured by the caller. |
| 253 | let server_pending = role.server_pending.as_ref().unwrap(); |
| 254 | |
| 255 | if let Some(is_focused) = m.is_focused { |
| 256 | if window.is_focused() != is_focused { |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if let Some(is_active) = m.is_active { |
| 262 | // Our "is-active" definition corresponds to the window having a pending Activated state. |
| 263 | let pending_activated = server_pending |
| 264 | .states |
| 265 | .contains(xdg_toplevel::State::Activated); |
| 266 | if is_active != pending_activated { |
| 267 | return false; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if let Some(app_id_re) = &m.app_id { |
| 272 | let Some(app_id) = &role.app_id else { |
| 273 | return false; |
| 274 | }; |
| 275 | if !app_id_re.is_match(app_id) { |
| 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if let Some(title_re) = &m.title { |
| 281 | let Some(title) = &role.title else { |
| 282 | return false; |
| 283 | }; |
| 284 | if !title_re.is_match(title) { |
| 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if let Some(is_active_in_column) = m.is_active_in_column { |
| 290 | if window.is_active_in_column() != is_active_in_column { |
| 291 | return false; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | true |
| 296 | } |