niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·ad3847f·Ivan Molodetskikh·1 year ago·2457 commits
| 1 | use std::cmp::{max, min}; |
| 2 | |
| 3 | use niri_config::utils::MergeWith as _; |
| 4 | use niri_config::window_rule::{Match, WindowRule}; |
| 5 | use niri_config::{ |
| 6 | BlockOutFrom, BorderRule, CornerRadius, FloatingPosition, PresetSize, ShadowRule, |
| 7 | TabIndicatorRule, |
| 8 | }; |
| 9 | use niri_ipc::ColumnDisplay; |
| 10 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 11 | use smithay::utils::{Logical, Size}; |
| 12 | use smithay::wayland::compositor::with_states; |
| 13 | use smithay::wayland::shell::xdg::{ |
| 14 | SurfaceCachedState, ToplevelSurface, XdgToplevelSurfaceRoleAttributes, |
| 15 | }; |
| 16 | |
| 17 | use crate::utils::with_toplevel_role; |
| 18 | |
| 19 | pub mod mapped; |
| 20 | pub use mapped::Mapped; |
| 21 | |
| 22 | pub mod unmapped; |
| 23 | pub use unmapped::{InitialConfigureState, Unmapped}; |
| 24 | |
| 25 | /// Reference to a mapped or unmapped window. |
| 26 | #[derive(Debug, Clone, Copy)] |
| 27 | pub enum WindowRef<'a> { |
| 28 | Unmapped(&'a Unmapped), |
| 29 | Mapped(&'a Mapped), |
| 30 | } |
| 31 | |
| 32 | /// Rules fully resolved for a window. |
| 33 | #[derive(Debug, PartialEq)] |
| 34 | pub 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 fullscreen. |
| 65 | pub open_fullscreen: Option<bool>, |
| 66 | |
| 67 | /// Whether the window should open floating. |
| 68 | pub open_floating: Option<bool>, |
| 69 | |
| 70 | /// Whether the window should open focused. |
| 71 | pub open_focused: Option<bool>, |
| 72 | |
| 73 | /// Extra bound on the minimum window width. |
| 74 | pub min_width: Option<u16>, |
| 75 | /// Extra bound on the minimum window height. |
| 76 | pub min_height: Option<u16>, |
| 77 | /// Extra bound on the maximum window width. |
| 78 | pub max_width: Option<u16>, |
| 79 | /// Extra bound on the maximum window height. |
| 80 | pub max_height: Option<u16>, |
| 81 | |
| 82 | /// Focus ring overrides. |
| 83 | pub focus_ring: BorderRule, |
| 84 | /// Window border overrides. |
| 85 | pub border: BorderRule, |
| 86 | /// Shadow overrides. |
| 87 | pub shadow: ShadowRule, |
| 88 | /// Tab indicator overrides. |
| 89 | pub tab_indicator: TabIndicatorRule, |
| 90 | |
| 91 | /// Whether or not to draw the border with a solid background. |
| 92 | /// |
| 93 | /// `None` means using the SSD heuristic. |
| 94 | pub draw_border_with_background: Option<bool>, |
| 95 | |
| 96 | /// Extra opacity to draw this window with. |
| 97 | pub opacity: Option<f32>, |
| 98 | |
| 99 | /// Corner radius to assume this window has. |
| 100 | pub geometry_corner_radius: Option<CornerRadius>, |
| 101 | |
| 102 | /// Whether to clip this window to its geometry, including the corner radius. |
| 103 | pub clip_to_geometry: Option<bool>, |
| 104 | |
| 105 | /// Whether to bob this window up and down. |
| 106 | pub baba_is_float: Option<bool>, |
| 107 | |
| 108 | /// Whether to block out this window from certain render targets. |
| 109 | pub block_out_from: Option<BlockOutFrom>, |
| 110 | |
| 111 | /// Whether to enable VRR on this window's primary output if it is on-demand. |
| 112 | pub variable_refresh_rate: Option<bool>, |
| 113 | |
| 114 | /// Multiplier for all scroll events sent to this window. |
| 115 | pub scroll_factor: Option<f64>, |
| 116 | |
| 117 | /// Override whether to set the Tiled xdg-toplevel state on the window. |
| 118 | pub tiled_state: Option<bool>, |
| 119 | } |
| 120 | |
| 121 | impl<'a> WindowRef<'a> { |
| 122 | pub fn toplevel(self) -> &'a ToplevelSurface { |
| 123 | match self { |
| 124 | WindowRef::Unmapped(unmapped) => unmapped.toplevel(), |
| 125 | WindowRef::Mapped(mapped) => mapped.toplevel(), |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | pub fn is_focused(self) -> bool { |
| 130 | match self { |
| 131 | WindowRef::Unmapped(_) => false, |
| 132 | WindowRef::Mapped(mapped) => mapped.is_focused(), |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | pub fn is_urgent(self) -> bool { |
| 137 | match self { |
| 138 | WindowRef::Unmapped(_) => false, |
| 139 | WindowRef::Mapped(mapped) => mapped.is_urgent(), |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pub fn is_active_in_column(self) -> bool { |
| 144 | match self { |
| 145 | WindowRef::Unmapped(_) => true, |
| 146 | WindowRef::Mapped(mapped) => mapped.is_active_in_column(), |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | pub fn is_floating(self) -> bool { |
| 151 | match self { |
| 152 | // FIXME: This means you cannot set initial configure rules based on is-floating. I'm |
| 153 | // not sure there's a good way to support it, since this matcher makes a cycle with the |
| 154 | // open-floating rule. |
| 155 | // |
| 156 | // That said, I don't think there are a lot of useful initial configure properties you |
| 157 | // may want to set through an is-floating matcher? Like, if you're configuring a |
| 158 | // specific window to open as floating, you can also set those properties in that same |
| 159 | // window rule, rather than relying on a different is-floating rule. |
| 160 | WindowRef::Unmapped(_) => false, |
| 161 | WindowRef::Mapped(mapped) => mapped.is_floating(), |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | pub fn is_window_cast_target(self) -> bool { |
| 166 | match self { |
| 167 | WindowRef::Unmapped(_) => false, |
| 168 | WindowRef::Mapped(mapped) => mapped.is_window_cast_target(), |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | impl ResolvedWindowRules { |
| 174 | pub const fn empty() -> Self { |
| 175 | Self { |
| 176 | default_width: None, |
| 177 | default_height: None, |
| 178 | default_column_display: None, |
| 179 | default_floating_position: None, |
| 180 | open_on_output: None, |
| 181 | open_on_workspace: None, |
| 182 | open_maximized: None, |
| 183 | open_fullscreen: None, |
| 184 | open_floating: None, |
| 185 | open_focused: None, |
| 186 | min_width: None, |
| 187 | min_height: None, |
| 188 | max_width: None, |
| 189 | max_height: None, |
| 190 | focus_ring: BorderRule { |
| 191 | off: false, |
| 192 | on: false, |
| 193 | width: None, |
| 194 | active_color: None, |
| 195 | inactive_color: None, |
| 196 | urgent_color: None, |
| 197 | active_gradient: None, |
| 198 | inactive_gradient: None, |
| 199 | urgent_gradient: None, |
| 200 | }, |
| 201 | border: BorderRule { |
| 202 | off: false, |
| 203 | on: false, |
| 204 | width: None, |
| 205 | active_color: None, |
| 206 | inactive_color: None, |
| 207 | urgent_color: None, |
| 208 | active_gradient: None, |
| 209 | inactive_gradient: None, |
| 210 | urgent_gradient: None, |
| 211 | }, |
| 212 | shadow: ShadowRule { |
| 213 | off: false, |
| 214 | on: false, |
| 215 | offset: None, |
| 216 | softness: None, |
| 217 | spread: None, |
| 218 | draw_behind_window: None, |
| 219 | color: None, |
| 220 | inactive_color: None, |
| 221 | }, |
| 222 | tab_indicator: TabIndicatorRule { |
| 223 | active_color: None, |
| 224 | inactive_color: None, |
| 225 | urgent_color: None, |
| 226 | active_gradient: None, |
| 227 | inactive_gradient: None, |
| 228 | urgent_gradient: None, |
| 229 | }, |
| 230 | draw_border_with_background: None, |
| 231 | opacity: None, |
| 232 | geometry_corner_radius: None, |
| 233 | clip_to_geometry: None, |
| 234 | baba_is_float: None, |
| 235 | block_out_from: None, |
| 236 | variable_refresh_rate: None, |
| 237 | scroll_factor: None, |
| 238 | tiled_state: None, |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | pub fn compute(rules: &[WindowRule], window: WindowRef, is_at_startup: bool) -> Self { |
| 243 | let _span = tracy_client::span!("ResolvedWindowRules::compute"); |
| 244 | |
| 245 | let mut resolved = ResolvedWindowRules::empty(); |
| 246 | |
| 247 | with_toplevel_role(window.toplevel(), |role| { |
| 248 | // Ensure server_pending like in Smithay's with_pending_state(). |
| 249 | if role.server_pending.is_none() { |
| 250 | role.server_pending = Some(role.current_server_state().clone()); |
| 251 | } |
| 252 | |
| 253 | let mut open_on_output = None; |
| 254 | let mut open_on_workspace = None; |
| 255 | |
| 256 | for rule in rules { |
| 257 | let matches = |m: &Match| { |
| 258 | if let Some(at_startup) = m.at_startup { |
| 259 | if at_startup != is_at_startup { |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | window_matches(window, role, m) |
| 265 | }; |
| 266 | |
| 267 | if !(rule.matches.is_empty() || rule.matches.iter().any(matches)) { |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | if rule.excludes.iter().any(matches) { |
| 272 | continue; |
| 273 | } |
| 274 | |
| 275 | if let Some(x) = rule.default_column_width { |
| 276 | resolved.default_width = Some(x.0); |
| 277 | } |
| 278 | |
| 279 | if let Some(x) = rule.default_window_height { |
| 280 | resolved.default_height = Some(x.0); |
| 281 | } |
| 282 | |
| 283 | if let Some(x) = rule.default_column_display { |
| 284 | resolved.default_column_display = Some(x); |
| 285 | } |
| 286 | |
| 287 | if let Some(x) = rule.default_floating_position { |
| 288 | resolved.default_floating_position = Some(x); |
| 289 | } |
| 290 | |
| 291 | if let Some(x) = rule.open_on_output.as_deref() { |
| 292 | open_on_output = Some(x); |
| 293 | } |
| 294 | |
| 295 | if let Some(x) = rule.open_on_workspace.as_deref() { |
| 296 | open_on_workspace = Some(x); |
| 297 | } |
| 298 | |
| 299 | if let Some(x) = rule.open_maximized { |
| 300 | resolved.open_maximized = Some(x); |
| 301 | } |
| 302 | |
| 303 | if let Some(x) = rule.open_fullscreen { |
| 304 | resolved.open_fullscreen = Some(x); |
| 305 | } |
| 306 | |
| 307 | if let Some(x) = rule.open_floating { |
| 308 | resolved.open_floating = Some(x); |
| 309 | } |
| 310 | |
| 311 | if let Some(x) = rule.open_focused { |
| 312 | resolved.open_focused = Some(x); |
| 313 | } |
| 314 | |
| 315 | if let Some(x) = rule.min_width { |
| 316 | resolved.min_width = Some(x); |
| 317 | } |
| 318 | if let Some(x) = rule.min_height { |
| 319 | resolved.min_height = Some(x); |
| 320 | } |
| 321 | if let Some(x) = rule.max_width { |
| 322 | resolved.max_width = Some(x); |
| 323 | } |
| 324 | if let Some(x) = rule.max_height { |
| 325 | resolved.max_height = Some(x); |
| 326 | } |
| 327 | |
| 328 | resolved.focus_ring.merge_with(&rule.focus_ring); |
| 329 | resolved.border.merge_with(&rule.border); |
| 330 | resolved.shadow.merge_with(&rule.shadow); |
| 331 | resolved.tab_indicator.merge_with(&rule.tab_indicator); |
| 332 | |
| 333 | if let Some(x) = rule.draw_border_with_background { |
| 334 | resolved.draw_border_with_background = Some(x); |
| 335 | } |
| 336 | if let Some(x) = rule.opacity { |
| 337 | resolved.opacity = Some(x); |
| 338 | } |
| 339 | if let Some(x) = rule.geometry_corner_radius { |
| 340 | resolved.geometry_corner_radius = Some(x); |
| 341 | } |
| 342 | if let Some(x) = rule.clip_to_geometry { |
| 343 | resolved.clip_to_geometry = Some(x); |
| 344 | } |
| 345 | if let Some(x) = rule.baba_is_float { |
| 346 | resolved.baba_is_float = Some(x); |
| 347 | } |
| 348 | if let Some(x) = rule.block_out_from { |
| 349 | resolved.block_out_from = Some(x); |
| 350 | } |
| 351 | if let Some(x) = rule.variable_refresh_rate { |
| 352 | resolved.variable_refresh_rate = Some(x); |
| 353 | } |
| 354 | if let Some(x) = rule.scroll_factor { |
| 355 | resolved.scroll_factor = Some(x.0); |
| 356 | } |
| 357 | if let Some(x) = rule.tiled_state { |
| 358 | resolved.tiled_state = Some(x); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | resolved.open_on_output = open_on_output.map(|x| x.to_owned()); |
| 363 | resolved.open_on_workspace = open_on_workspace.map(|x| x.to_owned()); |
| 364 | }); |
| 365 | |
| 366 | resolved |
| 367 | } |
| 368 | |
| 369 | pub fn apply_min_size(&self, min_size: Size<i32, Logical>) -> Size<i32, Logical> { |
| 370 | let mut size = min_size; |
| 371 | |
| 372 | if let Some(x) = self.min_width { |
| 373 | size.w = max(size.w, i32::from(x)); |
| 374 | } |
| 375 | if let Some(x) = self.min_height { |
| 376 | size.h = max(size.h, i32::from(x)); |
| 377 | } |
| 378 | |
| 379 | size |
| 380 | } |
| 381 | |
| 382 | pub fn apply_max_size(&self, max_size: Size<i32, Logical>) -> Size<i32, Logical> { |
| 383 | let mut size = max_size; |
| 384 | |
| 385 | if let Some(x) = self.max_width { |
| 386 | if size.w == 0 { |
| 387 | size.w = i32::from(x); |
| 388 | } else if x > 0 { |
| 389 | size.w = min(size.w, i32::from(x)); |
| 390 | } |
| 391 | } |
| 392 | if let Some(x) = self.max_height { |
| 393 | if size.h == 0 { |
| 394 | size.h = i32::from(x); |
| 395 | } else if x > 0 { |
| 396 | size.h = min(size.h, i32::from(x)); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | size |
| 401 | } |
| 402 | |
| 403 | pub fn apply_min_max_size( |
| 404 | &self, |
| 405 | min_size: Size<i32, Logical>, |
| 406 | max_size: Size<i32, Logical>, |
| 407 | ) -> (Size<i32, Logical>, Size<i32, Logical>) { |
| 408 | let min_size = self.apply_min_size(min_size); |
| 409 | let max_size = self.apply_max_size(max_size); |
| 410 | (min_size, max_size) |
| 411 | } |
| 412 | |
| 413 | pub fn compute_open_floating(&self, toplevel: &ToplevelSurface) -> bool { |
| 414 | if let Some(res) = self.open_floating { |
| 415 | return res; |
| 416 | } |
| 417 | |
| 418 | // Windows with a parent (usually dialogs) open as floating by default. |
| 419 | if toplevel.parent().is_some() { |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | let (min_size, max_size) = with_states(toplevel.wl_surface(), |state| { |
| 424 | let mut guard = state.cached_state.get::<SurfaceCachedState>(); |
| 425 | let current = guard.current(); |
| 426 | (current.min_size, current.max_size) |
| 427 | }); |
| 428 | let (min_size, max_size) = self.apply_min_max_size(min_size, max_size); |
| 429 | |
| 430 | // We open fixed-height windows as floating. |
| 431 | min_size.h > 0 && min_size.h == max_size.h |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | fn window_matches(window: WindowRef, role: &XdgToplevelSurfaceRoleAttributes, m: &Match) -> bool { |
| 436 | // Must be ensured by the caller. |
| 437 | let server_pending = role.server_pending.as_ref().unwrap(); |
| 438 | |
| 439 | if let Some(is_focused) = m.is_focused { |
| 440 | if window.is_focused() != is_focused { |
| 441 | return false; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if let Some(is_urgent) = m.is_urgent { |
| 446 | if window.is_urgent() != is_urgent { |
| 447 | return false; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | if let Some(is_active) = m.is_active { |
| 452 | // Our "is-active" definition corresponds to the window having a pending Activated state. |
| 453 | let pending_activated = server_pending |
| 454 | .states |
| 455 | .contains(xdg_toplevel::State::Activated); |
| 456 | if is_active != pending_activated { |
| 457 | return false; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | if let Some(app_id_re) = &m.app_id { |
| 462 | let Some(app_id) = &role.app_id else { |
| 463 | return false; |
| 464 | }; |
| 465 | if !app_id_re.0.is_match(app_id) { |
| 466 | return false; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | if let Some(title_re) = &m.title { |
| 471 | let Some(title) = &role.title else { |
| 472 | return false; |
| 473 | }; |
| 474 | if !title_re.0.is_match(title) { |
| 475 | return false; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if let Some(is_active_in_column) = m.is_active_in_column { |
| 480 | if window.is_active_in_column() != is_active_in_column { |
| 481 | return false; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if let Some(is_floating) = m.is_floating { |
| 486 | if window.is_floating() != is_floating { |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if let Some(is_window_cast_target) = m.is_window_cast_target { |
| 492 | if window.is_window_cast_target() != is_window_cast_target { |
| 493 | return false; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | true |
| 498 | } |