Add option 'focus-at-startup' to focus a chosen output on start (#1323)
Browse files at this commit3 changed files +53 -0
- niri-config/src/lib.rs +5 -0
- src/niri.rs +25 -0
- wiki/Configuration:-Outputs.md +23 -0
niri-config/src/lib.rsmodified+5 -0
| @@ -440,6 +440,8 @@ pub struct Output { | ||
| 440 | 440 | pub mode: Option<ConfiguredMode>, |
| 441 | 441 | #[knuffel(child)] |
| 442 | 442 | pub variable_refresh_rate: Option<Vrr>, |
| 443 | + #[knuffel(child)] | |
| 444 | + pub focus_at_startup: bool, | |
| 443 | 445 | #[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)] |
| 444 | 446 | pub background_color: Color, |
| 445 | 447 | } |
| @@ -462,6 +464,7 @@ impl Default for Output { | ||
| 462 | 464 | fn default() -> Self { |
| 463 | 465 | Self { |
| 464 | 466 | off: false, |
| 467 | + focus_at_startup: false, | |
| 465 | 468 | name: String::new(), |
| 466 | 469 | scale: None, |
| 467 | 470 | transform: Transform::Normal, |
| @@ -3746,6 +3749,7 @@ mod tests { | ||
| 3746 | 3749 | } |
| 3747 | 3750 | |
| 3748 | 3751 | output "eDP-1" { |
| 3752 | + focus-at-startup | |
| 3749 | 3753 | scale 2 |
| 3750 | 3754 | transform "flipped-90" |
| 3751 | 3755 | position x=10 y=20 |
| @@ -4105,6 +4109,7 @@ mod tests { | ||
| 4105 | 4109 | on_demand: true, |
| 4106 | 4110 | }, |
| 4107 | 4111 | ), |
| 4112 | + focus_at_startup: true, | |
| 4108 | 4113 | background_color: Color { |
| 4109 | 4114 | r: 0.09803922, |
| 4110 | 4115 | g: 0.09803922, |
src/niri.rsmodified+25 -0
| @@ -630,6 +630,8 @@ impl State { | ||
| 630 | 630 | state.load_xkb_file(); |
| 631 | 631 | // Initialize some IPC server state. |
| 632 | 632 | state.ipc_keyboard_layouts_changed(); |
| 633 | + // Focus the default monitor if set by the user. | |
| 634 | + state.focus_default_monitor(); | |
| 633 | 635 | |
| 634 | 636 | Ok(state) |
| 635 | 637 | } |
| @@ -794,6 +796,29 @@ impl State { | ||
| 794 | 796 | rv |
| 795 | 797 | } |
| 796 | 798 | |
| 799 | + pub fn focus_default_monitor(&mut self) { | |
| 800 | + // Our default target is the first output in sorted order. | |
| 801 | + let Some(mut target) = self.niri.sorted_outputs.first().cloned() else { | |
| 802 | + // No outputs are connected. | |
| 803 | + return; | |
| 804 | + }; | |
| 805 | + | |
| 806 | + let config = self.niri.config.borrow(); | |
| 807 | + for config in &config.outputs.0 { | |
| 808 | + if !config.focus_at_startup { | |
| 809 | + continue; | |
| 810 | + } | |
| 811 | + if let Some(output) = self.niri.output_by_name_match(&config.name) { | |
| 812 | + target = output.clone(); | |
| 813 | + break; | |
| 814 | + } | |
| 815 | + } | |
| 816 | + drop(config); | |
| 817 | + | |
| 818 | + self.niri.layout.focus_output(&target); | |
| 819 | + self.move_cursor_to_output(&target); | |
| 820 | + } | |
| 821 | + | |
| 797 | 822 | /// Focus a specific window, taking care of a potential active output change and cursor |
| 798 | 823 | /// warp. |
| 799 | 824 | pub fn focus_window(&mut self, window: &Window) { |
wiki/Configuration:-Outputs.mdmodified+23 -0
| @@ -13,6 +13,7 @@ output "eDP-1" { | ||
| 13 | 13 | transform "90" |
| 14 | 14 | position x=1280 y=0 |
| 15 | 15 | variable-refresh-rate // on-demand=true |
| 16 | + focus-at-startup | |
| 16 | 17 | background-color "#003300" |
| 17 | 18 | } |
| 18 | 19 | |
| @@ -164,6 +165,28 @@ output "HDMI-A-1" { | ||
| 164 | 165 | } |
| 165 | 166 | ``` |
| 166 | 167 | |
| 168 | +### `focus-at-startup` | |
| 169 | + | |
| 170 | +<sup>Since: next release</sup> | |
| 171 | + | |
| 172 | +Focus this output by default when niri starts. | |
| 173 | + | |
| 174 | +If multiple outputs with `focus-at-startup` are connected, they are prioritized in the order that they appear in the config. | |
| 175 | + | |
| 176 | +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). | |
| 177 | + | |
| 178 | +```kdl | |
| 179 | +// Focus HDMI-A-1 by default. | |
| 180 | +output "HDMI-A-1" { | |
| 181 | + focus-at-startup | |
| 182 | +} | |
| 183 | + | |
| 184 | +// ...if HDMI-A-1 wasn't connected, focus DP-2 instead. | |
| 185 | +output "DP-2" { | |
| 186 | + focus-at-startup | |
| 187 | +} | |
| 188 | +``` | |
| 189 | + | |
| 167 | 190 | ### `background-color` |
| 168 | 191 | |
| 169 | 192 | <sup>Since: 0.1.8</sup> |