From 8d43efe4ac38ddb6ce298d6fbdfc3ec610b8da56 Mon Sep 17 00:00:00 2001 From: lualeet <43719546+lualeet@users.noreply.github.com> Date: Sat, 29 Mar 2025 10:13:59 +0000 Subject: Add option 'focus-at-startup' to focus a chosen output on start (#1323) * Implement default-output * Fix incorrect wiki string * Center mouse on start * Move default-output to Output.focus-at-startup * fixes --------- Co-authored-by: lualeet Co-authored-by: Ivan Molodetskikh --- niri-config/src/lib.rs | 5 +++++ src/niri.rs | 25 +++++++++++++++++++++++++ wiki/Configuration:-Outputs.md | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/niri-config/src/lib.rs b/niri-config/src/lib.rs index 6ae66aeb..4b597f99 100644 --- a/niri-config/src/lib.rs +++ b/niri-config/src/lib.rs @@ -440,6 +440,8 @@ pub struct Output { pub mode: Option, #[knuffel(child)] pub variable_refresh_rate: Option, + #[knuffel(child)] + pub focus_at_startup: bool, #[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)] pub background_color: Color, } @@ -462,6 +464,7 @@ impl Default for Output { fn default() -> Self { Self { off: false, + focus_at_startup: false, name: String::new(), scale: None, transform: Transform::Normal, @@ -3746,6 +3749,7 @@ mod tests { } output "eDP-1" { + focus-at-startup scale 2 transform "flipped-90" position x=10 y=20 @@ -4105,6 +4109,7 @@ mod tests { on_demand: true, }, ), + focus_at_startup: true, background_color: Color { r: 0.09803922, g: 0.09803922, diff --git a/src/niri.rs b/src/niri.rs index 35c8b2d7..14c48f17 100644 --- a/src/niri.rs +++ b/src/niri.rs @@ -630,6 +630,8 @@ impl State { state.load_xkb_file(); // Initialize some IPC server state. state.ipc_keyboard_layouts_changed(); + // Focus the default monitor if set by the user. + state.focus_default_monitor(); Ok(state) } @@ -794,6 +796,29 @@ impl State { rv } + pub fn focus_default_monitor(&mut self) { + // Our default target is the first output in sorted order. + let Some(mut target) = self.niri.sorted_outputs.first().cloned() else { + // No outputs are connected. + return; + }; + + let config = self.niri.config.borrow(); + for config in &config.outputs.0 { + if !config.focus_at_startup { + continue; + } + if let Some(output) = self.niri.output_by_name_match(&config.name) { + target = output.clone(); + break; + } + } + drop(config); + + self.niri.layout.focus_output(&target); + self.move_cursor_to_output(&target); + } + /// Focus a specific window, taking care of a potential active output change and cursor /// warp. pub fn focus_window(&mut self, window: &Window) { diff --git a/wiki/Configuration:-Outputs.md b/wiki/Configuration:-Outputs.md index 22e1c639..2a2ee4cf 100644 --- a/wiki/Configuration:-Outputs.md +++ b/wiki/Configuration:-Outputs.md @@ -13,6 +13,7 @@ output "eDP-1" { transform "90" position x=1280 y=0 variable-refresh-rate // on-demand=true + focus-at-startup background-color "#003300" } @@ -164,6 +165,28 @@ output "HDMI-A-1" { } ``` +### `focus-at-startup` + +Since: next release + +Focus this output by default when niri starts. + +If multiple outputs with `focus-at-startup` are connected, they are prioritized in the order that they appear in the config. + +When none of the connected outputs are explicitly `focus-at-startup`, niri will focus the first one sorted by name (same output sorting as used elsewhere in niri). + +```kdl +// Focus HDMI-A-1 by default. +output "HDMI-A-1" { + focus-at-startup +} + +// ...if HDMI-A-1 wasn't connected, focus DP-2 instead. +output "DP-2" { + focus-at-startup +} +``` + ### `background-color` Since: 0.1.8 -- cgit