Add default-floating-position window rule
Browse files at this commit3c2e1554c6f1fe0d7d96094aa93f72f4ced613e5Parent: 744955b6 changed files +78 -11
- niri-config/src/lib.rs +15 -0
- src/layout/floating.rs +8 -6
- src/layout/tile.rs +5 -0
- src/layout/workspace.rs +15 -4
- src/window/mod.rs +11 -1
- wiki/Configuration:-Window-Rules.md +24 -0
niri-config/src/lib.rsmodified+15 -0
| @@ -1019,6 +1019,8 @@ pub struct WindowRule { | ||
| 1019 | 1019 | pub block_out_from: Option<BlockOutFrom>, |
| 1020 | 1020 | #[knuffel(child, unwrap(argument))] |
| 1021 | 1021 | pub variable_refresh_rate: Option<bool>, |
| 1022 | + #[knuffel(child)] | |
| 1023 | + pub default_floating_position: Option<FoIPosition>, | |
| 1022 | 1024 | } |
| 1023 | 1025 | |
| 1024 | 1026 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] |
| @@ -1082,6 +1084,14 @@ pub struct BorderRule { | ||
| 1082 | 1084 | pub inactive_gradient: Option<Gradient>, |
| 1083 | 1085 | } |
| 1084 | 1086 | |
| 1087 | +#[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)] | |
| 1088 | +pub struct FoIPosition { | |
| 1089 | + #[knuffel(property)] | |
| 1090 | + pub x: FloatOrInt<-65535, 65535>, | |
| 1091 | + #[knuffel(property)] | |
| 1092 | + pub y: FloatOrInt<-65535, 65535>, | |
| 1093 | +} | |
| 1094 | + | |
| 1085 | 1095 | #[derive(Debug, Default, PartialEq)] |
| 1086 | 1096 | pub struct Binds(pub Vec<Bind>); |
| 1087 | 1097 | |
| @@ -3192,6 +3202,7 @@ mod tests { | ||
| 3192 | 3202 | open-floating false |
| 3193 | 3203 | open-focused true |
| 3194 | 3204 | default-window-height { fixed 500; } |
| 3205 | + default-floating-position x=100 y=-200 | |
| 3195 | 3206 | |
| 3196 | 3207 | focus-ring { |
| 3197 | 3208 | off |
| @@ -3476,6 +3487,10 @@ mod tests { | ||
| 3476 | 3487 | open_floating: Some(false), |
| 3477 | 3488 | open_focused: Some(true), |
| 3478 | 3489 | default_window_height: Some(DefaultPresetSize(Some(PresetSize::Fixed(500)))), |
| 3490 | + default_floating_position: Some(FoIPosition { | |
| 3491 | + x: FloatOrInt(100.), | |
| 3492 | + y: FloatOrInt(-200.), | |
| 3493 | + }), | |
| 3479 | 3494 | focus_ring: BorderRule { |
| 3480 | 3495 | off: true, |
| 3481 | 3496 | width: Some(FloatOrInt(3.)), |
src/layout/floating.rsmodified+8 -6
| @@ -415,12 +415,14 @@ impl<W: LayoutElement> FloatingSpace<W> { | ||
| 415 | 415 | } |
| 416 | 416 | } |
| 417 | 417 | |
| 418 | - let pos = tile | |
| 419 | - .floating_pos | |
| 420 | - .map(|pos| self.scale_by_working_area(pos)) | |
| 421 | - .unwrap_or_else(|| { | |
| 422 | - center_preferring_top_left_in_area(self.working_area, tile.tile_size()) | |
| 423 | - }); | |
| 418 | + let pos = tile.floating_pos.map(|pos| self.scale_by_working_area(pos)); | |
| 419 | + let pos = pos.or_else(|| { | |
| 420 | + tile.default_floating_logical_pos() | |
| 421 | + .map(|pos| pos + self.working_area.loc) | |
| 422 | + }); | |
| 423 | + let pos = pos.unwrap_or_else(|| { | |
| 424 | + center_preferring_top_left_in_area(self.working_area, tile.tile_size()) | |
| 425 | + }); | |
| 424 | 426 | |
| 425 | 427 | let data = Data::new(self.working_area, &tile, pos); |
| 426 | 428 | self.data.insert(idx, data); |
src/layout/tile.rsmodified+5 -0
| @@ -999,6 +999,11 @@ impl<W: LayoutElement> Tile<W> { | ||
| 999 | 999 | &self.options |
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | + pub fn default_floating_logical_pos(&self) -> Option<Point<f64, Logical>> { | |
| 1003 | + let pos = self.window().rules().default_floating_position?; | |
| 1004 | + Some(Point::from((pos.x.0, pos.y.0))) | |
| 1005 | + } | |
| 1006 | + | |
| 1002 | 1007 | #[cfg(test)] |
| 1003 | 1008 | pub fn view_size(&self) -> Size<f64, Logical> { |
| 1004 | 1009 | self.view_size |
src/layout/workspace.rsmodified+15 -4
| @@ -1123,7 +1123,9 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 1123 | 1123 | removed.tile.stop_move_animations(); |
| 1124 | 1124 | |
| 1125 | 1125 | // Come up with a default floating position close to the tile position. |
| 1126 | - if removed.tile.floating_pos.is_none() { | |
| 1126 | + if removed.tile.floating_pos.is_none() | |
| 1127 | + && removed.tile.default_floating_logical_pos().is_none() | |
| 1128 | + { | |
| 1127 | 1129 | let offset = if self.options.center_focused_column == CenterFocusedColumn::Always { |
| 1128 | 1130 | Point::from((0., 0.)) |
| 1129 | 1131 | } else { |
| @@ -1214,17 +1216,26 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 1214 | 1216 | }; |
| 1215 | 1217 | |
| 1216 | 1218 | let working_area_loc = self.floating.working_area().loc; |
| 1219 | + let pos = tile | |
| 1220 | + .floating_pos | |
| 1221 | + .map(|pos| self.floating.scale_by_working_area(pos)); | |
| 1222 | + let pos = pos.or_else(|| { | |
| 1223 | + tile.default_floating_logical_pos() | |
| 1224 | + .map(|pos| pos + working_area_loc) | |
| 1225 | + }); | |
| 1226 | + | |
| 1217 | 1227 | // If there's no stored floating position, we can only set both components at once, not |
| 1218 | 1228 | // adjust. |
| 1219 | - let Some(pos) = tile.floating_pos.or_else(|| { | |
| 1229 | + let pos = pos.or_else(|| { | |
| 1220 | 1230 | (matches!(x, PositionChange::SetFixed(_)) |
| 1221 | 1231 | && matches!(y, PositionChange::SetFixed(_))) |
| 1222 | 1232 | .then_some(Point::default()) |
| 1223 | - }) else { | |
| 1233 | + }); | |
| 1234 | + | |
| 1235 | + let Some(mut pos) = pos else { | |
| 1224 | 1236 | return; |
| 1225 | 1237 | }; |
| 1226 | 1238 | |
| 1227 | - let mut pos = self.floating.scale_by_working_area(pos); | |
| 1228 | 1239 | match x { |
| 1229 | 1240 | PositionChange::SetFixed(x) => pos.x = x + working_area_loc.x, |
| 1230 | 1241 | PositionChange::AdjustFixed(x) => pos.x += x, |
src/window/mod.rsmodified+11 -1
| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | use std::cmp::{max, min}; |
| 2 | 2 | |
| 3 | -use niri_config::{BlockOutFrom, BorderRule, CornerRadius, Match, PresetSize, WindowRule}; | |
| 3 | +use niri_config::{ | |
| 4 | + BlockOutFrom, BorderRule, CornerRadius, FoIPosition, Match, PresetSize, WindowRule, | |
| 5 | +}; | |
| 4 | 6 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 5 | 7 | use smithay::utils::{Logical, Size}; |
| 6 | 8 | use smithay::wayland::compositor::with_states; |
| @@ -41,6 +43,9 @@ pub struct ResolvedWindowRules { | ||
| 41 | 43 | /// - `Some(Some(width))`: set to a particular height. |
| 42 | 44 | pub default_height: Option<Option<PresetSize>>, |
| 43 | 45 | |
| 46 | + /// Default floating position for this window. | |
| 47 | + pub default_floating_position: Option<FoIPosition>, | |
| 48 | + | |
| 44 | 49 | /// Output to open this window on. |
| 45 | 50 | pub open_on_output: Option<String>, |
| 46 | 51 | |
| @@ -137,6 +142,7 @@ impl ResolvedWindowRules { | ||
| 137 | 142 | Self { |
| 138 | 143 | default_width: None, |
| 139 | 144 | default_height: None, |
| 145 | + default_floating_position: None, | |
| 140 | 146 | open_on_output: None, |
| 141 | 147 | open_on_workspace: None, |
| 142 | 148 | open_maximized: None, |
| @@ -219,6 +225,10 @@ impl ResolvedWindowRules { | ||
| 219 | 225 | resolved.default_height = Some(x.0); |
| 220 | 226 | } |
| 221 | 227 | |
| 228 | + if let Some(x) = rule.default_floating_position { | |
| 229 | + resolved.default_floating_position = Some(x); | |
| 230 | + } | |
| 231 | + | |
| 222 | 232 | if let Some(x) = rule.open_on_output.as_deref() { |
| 223 | 233 | open_on_output = Some(x); |
| 224 | 234 | } |
wiki/Configuration:-Window-Rules.mdmodified+24 -0
| @@ -52,6 +52,7 @@ window-rule { | ||
| 52 | 52 | block-out-from "screencast" |
| 53 | 53 | // block-out-from "screen-capture" |
| 54 | 54 | variable-refresh-rate true |
| 55 | + default-floating-position x=100 y=200 | |
| 55 | 56 | |
| 56 | 57 | focus-ring { |
| 57 | 58 | // off |
| @@ -507,6 +508,29 @@ window-rule { | ||
| 507 | 508 | } |
| 508 | 509 | ``` |
| 509 | 510 | |
| 511 | +#### `default-floating-position` | |
| 512 | + | |
| 513 | +<sup>Since: next release</sup> | |
| 514 | + | |
| 515 | +Set the initial position for this window when it opens on, or moves to the floating layout. | |
| 516 | + | |
| 517 | +Afterward, the window will remember its last floating position. | |
| 518 | + | |
| 519 | +By default, new floating windows open at the center of the screen, and windows from the tiling layout open close to their visual screen position. | |
| 520 | + | |
| 521 | +The position uses logical coordinates relative to the working area. | |
| 522 | +For example, if you have a bar at the top, then `x=0 y=0` will put the top-left corner of the window directly below the bar. | |
| 523 | + | |
| 524 | +```kdl | |
| 525 | +// Open the Firefox picture-in-picture window at the top-left corner of the screen | |
| 526 | +// with a small gap. | |
| 527 | +window-rule { | |
| 528 | + match app-id="firefox$" title="^Picture-in-Picture$" | |
| 529 | + | |
| 530 | + default-floating-position x=32 y=32 | |
| 531 | +} | |
| 532 | +``` | |
| 533 | + | |
| 510 | 534 | #### `draw-border-with-background` |
| 511 | 535 | |
| 512 | 536 | Override whether the border and the focus ring draw with a background. |