Skip to content

Commit

Permalink
Implement default-window-height window rule
Browse files Browse the repository at this point in the history
Only works for floats that aren't initially fullscreen atm.
  • Loading branch information
YaLTeR committed Dec 27, 2024
1 parent 5a316de commit cba66a1
Show file tree
Hide file tree
Showing 1,450 changed files with 16,540 additions and 14 deletions.
6 changes: 5 additions & 1 deletion niri-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ pub enum PresetSize {
Fixed(#[knuffel(argument)] i32),
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DefaultPresetSize(pub Option<PresetSize>);

#[derive(knuffel::Decode, Debug, Default, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -975,6 +975,8 @@ pub struct WindowRule {
// Rules applied at initial configure.
#[knuffel(child)]
pub default_column_width: Option<DefaultPresetSize>,
#[knuffel(child)]
pub default_window_height: Option<DefaultPresetSize>,
#[knuffel(child, unwrap(argument))]
pub open_on_output: Option<String>,
#[knuffel(child, unwrap(argument))]
Expand Down Expand Up @@ -3145,6 +3147,7 @@ mod tests {
open-fullscreen false
open-floating false
open-focused true
default-window-height { fixed 500; }
focus-ring {
off
Expand Down Expand Up @@ -3425,6 +3428,7 @@ mod tests {
open_fullscreen: Some(false),
open_floating: Some(false),
open_focused: Some(true),
default_window_height: Some(DefaultPresetSize(Some(PresetSize::Fixed(500)))),
focus_ring: BorderRule {
off: true,
width: Some(FloatOrInt(3.)),
Expand Down
4 changes: 2 additions & 2 deletions niri-visual-tests/src/cases/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl Layout {
let min_size = window.min_size();
let max_size = window.max_size();
window.request_size(
ws.new_window_size(width, false, window.rules(), (min_size, max_size)),
ws.new_window_size(width, None, false, window.rules(), (min_size, max_size)),
false,
None,
);
Expand All @@ -189,7 +189,7 @@ impl Layout {
let min_size = window.min_size();
let max_size = window.max_size();
window.request_size(
ws.new_window_size(width, false, window.rules(), (min_size, max_size)),
ws.new_window_size(width, None, false, window.rules(), (min_size, max_size)),
false,
None,
);
Expand Down
1 change: 1 addition & 0 deletions src/handlers/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl CompositorHandler for State {
rules,
width,
floating_width: _,
floating_height: _,
is_full_width,
output,
workspace_name,
Expand Down
17 changes: 15 additions & 2 deletions src/handlers/xdg_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ impl XdgShellHandler for State {
toplevel.with_pending_state(|state| {
state.states.set(xdg_toplevel::State::Fullscreen);
});
ws.configure_new_window(&unmapped.window, None, false, rules);
ws.configure_new_window(&unmapped.window, None, None, false, rules);
}

// We already sent the initial configure, so we need to reconfigure.
Expand Down Expand Up @@ -495,6 +495,7 @@ impl XdgShellHandler for State {
rules,
width,
floating_width,
floating_height,
is_full_width,
output,
workspace_name,
Expand Down Expand Up @@ -557,9 +558,11 @@ impl XdgShellHandler for State {
} else {
*width
};
let configure_height = if is_floating { *floating_height } else { None };
ws.configure_new_window(
&unmapped.window,
configure_width,
configure_height,
is_floating,
rules,
);
Expand Down Expand Up @@ -840,6 +843,7 @@ impl State {

let mut width = None;
let mut floating_width = None;
let mut floating_height = None;
let is_full_width = rules.open_maximized.unwrap_or(false);
let is_floating = rules.compute_open_floating(toplevel);

Expand All @@ -865,6 +869,7 @@ impl State {

width = ws.resolve_default_width(rules.default_width, false);
floating_width = ws.resolve_default_width(rules.default_width, true);
floating_height = ws.resolve_default_height(rules.default_height, true);

let configure_width = if is_floating {
floating_width
Expand All @@ -873,7 +878,14 @@ impl State {
} else {
width
};
ws.configure_new_window(window, configure_width, is_floating, &rules);
let configure_height = if is_floating { floating_height } else { None };
ws.configure_new_window(
window,
configure_width,
configure_height,
is_floating,
&rules,
);
}

// If the user prefers no CSD, it's a reasonable assumption that they would prefer to get
Expand All @@ -892,6 +904,7 @@ impl State {
rules,
width,
floating_width,
floating_height,
is_full_width,
output,
workspace_name: ws.and_then(|w| w.name().cloned()),
Expand Down
23 changes: 22 additions & 1 deletion src/layout/floating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,14 @@ impl<W: LayoutElement> FloatingSpace<W> {
width.resolve_no_gaps(&self.options, self.working_area.size.w)
}

pub fn resolve_height(&self, height: PresetSize) -> ResolvedSize {
resolve_preset_size(height, self.working_area.size.h)
}

pub fn new_window_size(
&self,
width: Option<ColumnWidth>,
height: Option<PresetSize>,
rules: &ResolvedWindowRules,
) -> Size<i32, Logical> {
let border = rules.border.resolve_against(self.options.border);
Expand All @@ -1125,7 +1130,23 @@ impl<W: LayoutElement> FloatingSpace<W> {
0
};

Size::from((width, 0))
let height = if let Some(height) = height {
let height = match self.resolve_height(height) {
ResolvedSize::Tile(mut size) => {
if !border.off {
size -= border.width.0 * 2.;
}
size
}
ResolvedSize::Window(size) => size,
};

max(1, height.floor() as i32)
} else {
0
};

Size::from((width, height))
}

#[cfg(test)]
Expand Down
23 changes: 20 additions & 3 deletions src/layout/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::max;
use std::rc::Rc;
use std::time::Duration;

use niri_config::{OutputName, Workspace as WorkspaceConfig};
use niri_config::{OutputName, PresetSize, Workspace as WorkspaceConfig};
use niri_ipc::SizeChange;
use smithay::backend::renderer::gles::GlesRenderer;
use smithay::desktop::{layer_map_for_output, Window};
Expand Down Expand Up @@ -715,15 +715,30 @@ impl<W: LayoutElement> Workspace<W> {
}
}

pub fn resolve_default_height(
&self,
default_height: Option<Option<PresetSize>>,
is_floating: bool,
) -> Option<PresetSize> {
match default_height {
Some(Some(height)) => Some(height),
Some(None) => None,
None if is_floating => None,
// We don't have a global default at the moment.
None => None,
}
}

pub fn new_window_size(
&self,
width: Option<ColumnWidth>,
height: Option<PresetSize>,
is_floating: bool,
rules: &ResolvedWindowRules,
(min_size, max_size): (Size<i32, Logical>, Size<i32, Logical>),
) -> Size<i32, Logical> {
let mut size = if is_floating {
self.floating.new_window_size(width, rules)
self.floating.new_window_size(width, height, rules)
} else {
self.scrolling.new_window_size(width, rules)
};
Expand All @@ -749,6 +764,7 @@ impl<W: LayoutElement> Workspace<W> {
&self,
window: &Window,
width: Option<ColumnWidth>,
height: Option<PresetSize>,
is_floating: bool,
rules: &ResolvedWindowRules,
) {
Expand All @@ -766,7 +782,8 @@ impl<W: LayoutElement> Workspace<W> {
if state.states.contains(xdg_toplevel::State::Fullscreen) {
state.size = Some(self.view_size.to_i32_round());
} else {
let size = self.new_window_size(width, is_floating, rules, (min_size, max_size));
let size =
self.new_window_size(width, height, is_floating, rules, (min_size, max_size));
state.size = Some(size);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: AN\nconfig:\nwindow-rule {\n default-window-height { fixed 500; }\n border { on; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 608 × 680, bounds: 1240 × 680, states: []

post-map configures:
size: 1280 × 720, bounds: 1240 × 680, states: [Fullscreen]
size: 1280 × 720, bounds: 1240 × 680, states: [Fullscreen, Activated]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: AU\nconfig:\nwindow-rule {\n default-window-height { fixed 500; }\n border { on; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 608 × 680, bounds: 1240 × 680, states: []

post-map configures:
size: 608 × 680, bounds: 1240 × 680, states: []
size: 608 × 680, bounds: 1240 × 680, states: [Activated]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: BN\nconfig:\nwindow-rule {\n default-window-height { fixed 500; }\n border { on; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 1280 × 720, bounds: 1240 × 680, states: [Fullscreen]

post-map configures:
size: 1280 × 720, bounds: 1240 × 680, states: [Fullscreen, Activated]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: BU\nconfig:\nwindow-rule {\n default-window-height { fixed 500; }\n border { on; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 608 × 680, bounds: 1240 × 680, states: []

post-map configures:
size: 608 × 680, bounds: 1240 × 680, states: [Activated]
11 changes: 11 additions & 0 deletions src/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/window_opening.rs
description: "config:\nwindow-rule {\n default-window-height { fixed 500; }\n border { on; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 608 × 680, bounds: 1240 × 680, states: []

post-map configures:
size: 608 × 680, bounds: 1240 × 680, states: [Activated]
12 changes: 12 additions & 0 deletions src/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: AN\nconfig:\nwindow-rule {\n default-window-height { fixed 500; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 616 × 688, bounds: 1248 × 688, states: []

post-map configures:
size: 1280 × 720, bounds: 1248 × 688, states: [Fullscreen]
size: 1280 × 720, bounds: 1248 × 688, states: [Fullscreen, Activated]
12 changes: 12 additions & 0 deletions src/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: AU\nconfig:\nwindow-rule {\n default-window-height { fixed 500; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 616 × 688, bounds: 1248 × 688, states: []

post-map configures:
size: 616 × 688, bounds: 1248 × 688, states: []
size: 616 × 688, bounds: 1248 × 688, states: [Activated]
11 changes: 11 additions & 0 deletions src/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: BN\nconfig:\nwindow-rule {\n default-window-height { fixed 500; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 1280 × 720, bounds: 1248 × 688, states: [Fullscreen]

post-map configures:
size: 1280 × 720, bounds: 1248 × 688, states: [Fullscreen, Activated]
11 changes: 11 additions & 0 deletions src/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: BU\nconfig:\nwindow-rule {\n default-window-height { fixed 500; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 616 × 688, bounds: 1248 × 688, states: []

post-map configures:
size: 616 × 688, bounds: 1248 × 688, states: [Activated]
11 changes: 11 additions & 0 deletions src/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/window_opening.rs
description: "config:\nwindow-rule {\n default-window-height { fixed 500; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 616 × 688, bounds: 1248 × 688, states: []

post-map configures:
size: 616 × 688, bounds: 1248 × 688, states: [Activated]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: AN\nconfig:\nwindow-rule {\n default-window-height { proportion 0.5; }\n border { on; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 608 × 680, bounds: 1240 × 680, states: []

post-map configures:
size: 1280 × 720, bounds: 1240 × 680, states: [Fullscreen]
size: 1280 × 720, bounds: 1240 × 680, states: [Fullscreen, Activated]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: AU\nconfig:\nwindow-rule {\n default-window-height { proportion 0.5; }\n border { on; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 608 × 680, bounds: 1240 × 680, states: []

post-map configures:
size: 608 × 680, bounds: 1240 × 680, states: []
size: 608 × 680, bounds: 1240 × 680, states: [Activated]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: src/tests/window_opening.rs
description: "want fullscreen: BN\nconfig:\nwindow-rule {\n default-window-height { proportion 0.5; }\n border { on; }\n}"
expression: snapshot
snapshot_kind: text
---
initial configure:
size: 1280 × 720, bounds: 1240 × 680, states: [Fullscreen]

post-map configures:
size: 1280 × 720, bounds: 1240 × 680, states: [Fullscreen, Activated]
Loading

0 comments on commit cba66a1

Please sign in to comment.