forge
Explore
Log in

Don't you dare go Hollow

Add default-floating-position relative-to property

Browse files at this commit

793e92e9d69f3640754d9ece40ddc7723cf49292Ivan MolodetskikhParent: a7c57f4
niri-config/src/lib.rsmodified+13 -1
@@ -1090,6 +1090,17 @@ pub struct FoIPosition {
10901090 pub x: FloatOrInt<-65535, 65535>,
10911091 #[knuffel(property)]
10921092 pub y: FloatOrInt<-65535, 65535>,
1093+ #[knuffel(property, default)]
1094+ pub relative_to: RelativeTo,
1095+}
1096+
1097+#[derive(knuffel::DecodeScalar, Debug, Default, Clone, Copy, PartialEq, Eq)]
1098+pub enum RelativeTo {
1099+ #[default]
1100+ TopLeft,
1101+ TopRight,
1102+ BottomLeft,
1103+ BottomRight,
10931104 }
10941105
10951106 #[derive(Debug, Default, PartialEq)]
@@ -3214,7 +3225,7 @@ mod tests {
32143225 open-floating false
32153226 open-focused true
32163227 default-window-height { fixed 500; }
3217- default-floating-position x=100 y=-200
3228+ default-floating-position x=100 y=-200 relative-to="bottom-left"
32183229
32193230 focus-ring {
32203231 off
@@ -3502,6 +3513,7 @@ mod tests {
35023513 default_floating_position: Some(FoIPosition {
35033514 x: FloatOrInt(100.),
35043515 y: FloatOrInt(-200.),
3516+ relative_to: RelativeTo::BottomLeft,
35053517 }),
35063518 focus_ring: BorderRule {
35073519 off: true,
src/layout/floating.rsmodified+23 -7
@@ -2,7 +2,7 @@ use std::cmp::max;
22 use std::iter::zip;
33 use std::rc::Rc;
44
5-use niri_config::PresetSize;
5+use niri_config::{PresetSize, RelativeTo};
66 use niri_ipc::{PositionChange, SizeChange};
77 use smithay::backend::renderer::gles::GlesRenderer;
88 use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size};
@@ -415,12 +415,7 @@ impl<W: LayoutElement> FloatingSpace<W> {
415415 }
416416 }
417417
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(|| {
418+ let pos = self.stored_or_default_tile_pos(&tile).unwrap_or_else(|| {
424419 center_preferring_top_left_in_area(self.working_area, tile.tile_size())
425420 });
426421
@@ -1191,6 +1186,27 @@ impl<W: LayoutElement> FloatingSpace<W> {
11911186 Size::from((width, height))
11921187 }
11931188
1189+ pub fn stored_or_default_tile_pos(&self, tile: &Tile<W>) -> Option<Point<f64, Logical>> {
1190+ let pos = tile.floating_pos.map(|pos| self.scale_by_working_area(pos));
1191+ pos.or_else(|| {
1192+ tile.window().rules().default_floating_position.map(|pos| {
1193+ let relative_to = pos.relative_to;
1194+ let size = tile.tile_size();
1195+ let area = self.working_area;
1196+
1197+ let mut pos = Point::from((pos.x.0, pos.y.0));
1198+ if relative_to == RelativeTo::TopRight || relative_to == RelativeTo::BottomRight {
1199+ pos.x = area.size.w - size.w - pos.x;
1200+ }
1201+ if relative_to == RelativeTo::BottomLeft || relative_to == RelativeTo::BottomRight {
1202+ pos.y = area.size.h - size.h - pos.y;
1203+ }
1204+
1205+ pos + self.working_area.loc
1206+ })
1207+ })
1208+ }
1209+
11941210 #[cfg(test)]
11951211 pub fn view_size(&self) -> Size<f64, Logical> {
11961212 self.view_size
src/layout/tile.rsmodified+0 -5
@@ -999,11 +999,6 @@ impl<W: LayoutElement> Tile<W> {
999999 &self.options
10001000 }
10011001
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-
10071002 #[cfg(test)]
10081003 pub fn view_size(&self) -> Size<f64, Logical> {
10091004 self.view_size
src/layout/workspace.rsmodified+3 -10
@@ -1143,9 +1143,8 @@ impl<W: LayoutElement> Workspace<W> {
11431143 removed.tile.stop_move_animations();
11441144
11451145 // Come up with a default floating position close to the tile position.
1146- if removed.tile.floating_pos.is_none()
1147- && removed.tile.default_floating_logical_pos().is_none()
1148- {
1146+ let stored_or_default = self.floating.stored_or_default_tile_pos(&removed.tile);
1147+ if stored_or_default.is_none() {
11491148 let offset = if self.options.center_focused_column == CenterFocusedColumn::Always {
11501149 Point::from((0., 0.))
11511150 } else {
@@ -1236,13 +1235,7 @@ impl<W: LayoutElement> Workspace<W> {
12361235 };
12371236
12381237 let working_area_loc = self.floating.working_area().loc;
1239- let pos = tile
1240- .floating_pos
1241- .map(|pos| self.floating.scale_by_working_area(pos));
1242- let pos = pos.or_else(|| {
1243- tile.default_floating_logical_pos()
1244- .map(|pos| pos + working_area_loc)
1245- });
1238+ let pos = self.floating.stored_or_default_tile_pos(tile);
12461239
12471240 // If there's no stored floating position, we can only set both components at once, not
12481241 // adjust.
wiki/Configuration:-Window-Rules.mdmodified+10 -3
@@ -52,7 +52,7 @@ window-rule {
5252 block-out-from "screencast"
5353 // block-out-from "screen-capture"
5454 variable-refresh-rate true
55- default-floating-position x=100 y=200
55+ default-floating-position x=100 y=200 relative-to="bottom-left"
5656
5757 focus-ring {
5858 // off
@@ -523,15 +523,22 @@ Afterward, the window will remember its last floating position.
523523 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.
524524
525525 The position uses logical coordinates relative to the working area.
526+By default, they are relative to the top-left corner of the working area, but you can change this by setting `relative-to` to one of these values: `top-left`, `top-right`, `bottom-left`, `bottom-right`.
527+
526528 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.
529+If instead you write `x=0 y=0 relative-to="top-right"`, then the top-right corner of the window will align with the top-right corner of the workspace, also directly below the bar.
530+
531+The coordinates change direction based on `relative-to`.
532+For example, by default (top-left), `x=100 y=200` will put the window 100 pixels to the right and 200 pixels down from the top-left corner.
533+If you use `x=100 y=200 relative-to="bottom-left"`, it will put the window 100 pixels to the right and 200 pixels *up* from the bottom-left corner.
527534
528535 ```kdl
529-// Open the Firefox picture-in-picture window at the top-left corner of the screen
536+// Open the Firefox picture-in-picture window at the bottom-left corner of the screen
530537 // with a small gap.
531538 window-rule {
532539 match app-id="firefox$" title="^Picture-in-Picture$"
533540
534- default-floating-position x=32 y=32
541+ default-floating-position x=32 y=32 relative-to="bottom-left"
535542 }
536543 ```
537544