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 | pub block_out_from: Option<BlockOutFrom>, | 1019 | pub block_out_from: Option<BlockOutFrom>, |
| 1020 | #[knuffel(child, unwrap(argument))] | 1020 | #[knuffel(child, unwrap(argument))] |
| 1021 | pub variable_refresh_rate: Option<bool>, | 1021 | pub variable_refresh_rate: Option<bool>, |
| 1022 | + #[knuffel(child)] | ||
| 1023 | + pub default_floating_position: Option<FoIPosition>, | ||
| 1022 | } | 1024 | } |
| 1023 | 1025 | ||
| 1024 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] | 1026 | #[derive(knuffel::Decode, Debug, Default, Clone, PartialEq)] |
| @@ -1082,6 +1084,14 @@ pub struct BorderRule { | |||
| 1082 | pub inactive_gradient: Option<Gradient>, | 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 | #[derive(Debug, Default, PartialEq)] | 1095 | #[derive(Debug, Default, PartialEq)] |
| 1086 | pub struct Binds(pub Vec<Bind>); | 1096 | pub struct Binds(pub Vec<Bind>); |
| 1087 | 1097 | ||
| @@ -3192,6 +3202,7 @@ mod tests { | |||
| 3192 | open-floating false | 3202 | open-floating false |
| 3193 | open-focused true | 3203 | open-focused true |
| 3194 | default-window-height { fixed 500; } | 3204 | default-window-height { fixed 500; } |
| 3205 | + default-floating-position x=100 y=-200 | ||
| 3195 | 3206 | ||
| 3196 | focus-ring { | 3207 | focus-ring { |
| 3197 | off | 3208 | off |
| @@ -3476,6 +3487,10 @@ mod tests { | |||
| 3476 | open_floating: Some(false), | 3487 | open_floating: Some(false), |
| 3477 | open_focused: Some(true), | 3488 | open_focused: Some(true), |
| 3478 | default_window_height: Some(DefaultPresetSize(Some(PresetSize::Fixed(500)))), | 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 | focus_ring: BorderRule { | 3494 | focus_ring: BorderRule { |
| 3480 | off: true, | 3495 | off: true, |
| 3481 | width: Some(FloatOrInt(3.)), | 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 | 418 | + let pos = tile.floating_pos.map(|pos| self.scale_by_working_area(pos)); |
| 419 | - .floating_pos | 419 | + let pos = pos.or_else(|| { |
| 420 | - .map(|pos| self.scale_by_working_area(pos)) | 420 | + tile.default_floating_logical_pos() |
| 421 | - .unwrap_or_else(|| { | 421 | + .map(|pos| pos + self.working_area.loc) |
| 422 | - center_preferring_top_left_in_area(self.working_area, tile.tile_size()) | 422 | + }); |
| 423 | - }); | 423 | + let pos = pos.unwrap_or_else(|| { |
| 424 | + center_preferring_top_left_in_area(self.working_area, tile.tile_size()) | ||
| 425 | + }); | ||
| 424 | 426 | ||
| 425 | let data = Data::new(self.working_area, &tile, pos); | 427 | let data = Data::new(self.working_area, &tile, pos); |
| 426 | self.data.insert(idx, data); | 428 | self.data.insert(idx, data); |
src/layout/tile.rsmodified+5 -0
| @@ -999,6 +999,11 @@ impl<W: LayoutElement> Tile<W> { | |||
| 999 | &self.options | 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 | #[cfg(test)] | 1007 | #[cfg(test)] |
| 1003 | pub fn view_size(&self) -> Size<f64, Logical> { | 1008 | pub fn view_size(&self) -> Size<f64, Logical> { |
| 1004 | self.view_size | 1009 | self.view_size |
src/layout/workspace.rsmodified+15 -4
| @@ -1123,7 +1123,9 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1123 | removed.tile.stop_move_animations(); | 1123 | removed.tile.stop_move_animations(); |
| 1124 | 1124 | ||
| 1125 | // Come up with a default floating position close to the tile position. | 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 | let offset = if self.options.center_focused_column == CenterFocusedColumn::Always { | 1129 | let offset = if self.options.center_focused_column == CenterFocusedColumn::Always { |
| 1128 | Point::from((0., 0.)) | 1130 | Point::from((0., 0.)) |
| 1129 | } else { | 1131 | } else { |
| @@ -1214,17 +1216,26 @@ impl<W: LayoutElement> Workspace<W> { | |||
| 1214 | }; | 1216 | }; |
| 1215 | 1217 | ||
| 1216 | let working_area_loc = self.floating.working_area().loc; | 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 | // If there's no stored floating position, we can only set both components at once, not | 1227 | // If there's no stored floating position, we can only set both components at once, not |
| 1218 | // adjust. | 1228 | // adjust. |
| 1219 | - let Some(pos) = tile.floating_pos.or_else(|| { | 1229 | + let pos = pos.or_else(|| { |
| 1220 | (matches!(x, PositionChange::SetFixed(_)) | 1230 | (matches!(x, PositionChange::SetFixed(_)) |
| 1221 | && matches!(y, PositionChange::SetFixed(_))) | 1231 | && matches!(y, PositionChange::SetFixed(_))) |
| 1222 | .then_some(Point::default()) | 1232 | .then_some(Point::default()) |
| 1223 | - }) else { | 1233 | + }); |
| 1234 | + | ||
| 1235 | + let Some(mut pos) = pos else { | ||
| 1224 | return; | 1236 | return; |
| 1225 | }; | 1237 | }; |
| 1226 | 1238 | ||
| 1227 | - let mut pos = self.floating.scale_by_working_area(pos); | ||
| 1228 | match x { | 1239 | match x { |
| 1229 | PositionChange::SetFixed(x) => pos.x = x + working_area_loc.x, | 1240 | PositionChange::SetFixed(x) => pos.x = x + working_area_loc.x, |
| 1230 | PositionChange::AdjustFixed(x) => pos.x += x, | 1241 | PositionChange::AdjustFixed(x) => pos.x += x, |
src/window/mod.rsmodified+11 -1
| @@ -1,6 +1,8 @@ | |||
| 1 | use std::cmp::{max, min}; | 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 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; | 6 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 5 | use smithay::utils::{Logical, Size}; | 7 | use smithay::utils::{Logical, Size}; |
| 6 | use smithay::wayland::compositor::with_states; | 8 | use smithay::wayland::compositor::with_states; |
| @@ -41,6 +43,9 @@ pub struct ResolvedWindowRules { | |||
| 41 | /// - `Some(Some(width))`: set to a particular height. | 43 | /// - `Some(Some(width))`: set to a particular height. |
| 42 | pub default_height: Option<Option<PresetSize>>, | 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 | /// Output to open this window on. | 49 | /// Output to open this window on. |
| 45 | pub open_on_output: Option<String>, | 50 | pub open_on_output: Option<String>, |
| 46 | 51 | ||
| @@ -137,6 +142,7 @@ impl ResolvedWindowRules { | |||
| 137 | Self { | 142 | Self { |
| 138 | default_width: None, | 143 | default_width: None, |
| 139 | default_height: None, | 144 | default_height: None, |
| 145 | + default_floating_position: None, | ||
| 140 | open_on_output: None, | 146 | open_on_output: None, |
| 141 | open_on_workspace: None, | 147 | open_on_workspace: None, |
| 142 | open_maximized: None, | 148 | open_maximized: None, |
| @@ -219,6 +225,10 @@ impl ResolvedWindowRules { | |||
| 219 | resolved.default_height = Some(x.0); | 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 | if let Some(x) = rule.open_on_output.as_deref() { | 232 | if let Some(x) = rule.open_on_output.as_deref() { |
| 223 | open_on_output = Some(x); | 233 | open_on_output = Some(x); |
| 224 | } | 234 | } |
wiki/Configuration:-Window-Rules.mdmodified+24 -0
| @@ -52,6 +52,7 @@ window-rule { | |||
| 52 | block-out-from "screencast" | 52 | block-out-from "screencast" |
| 53 | // block-out-from "screen-capture" | 53 | // block-out-from "screen-capture" |
| 54 | variable-refresh-rate true | 54 | variable-refresh-rate true |
| 55 | + default-floating-position x=100 y=200 | ||
| 55 | 56 | ||
| 56 | focus-ring { | 57 | focus-ring { |
| 57 | // off | 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 | #### `draw-border-with-background` | 534 | #### `draw-border-with-background` |
| 511 | 535 | ||
| 512 | Override whether the border and the focus ring draw with a background. | 536 | Override whether the border and the focus ring draw with a background. |