forge
Explore
Log in

Don't you dare go Hollow

Add option 'focus-at-startup' to focus a chosen output on start (#1323)

Browse files at this commit

8d43efe4ac38ddb6ce298d6fbdfc3ec610b8da56Unverifiedlualeet and lualeet, Ivan MolodetskikhParent: 3bb7e60
* Implement default-output

* Fix incorrect wiki string

* Center mouse on start

* Move default-output to Output.focus-at-startup

* fixes

---------
niri-config/src/lib.rsmodified+5 -0
@@ -440,6 +440,8 @@ pub struct Output {
440440 pub mode: Option<ConfiguredMode>,
441441 #[knuffel(child)]
442442 pub variable_refresh_rate: Option<Vrr>,
443+ #[knuffel(child)]
444+ pub focus_at_startup: bool,
443445 #[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)]
444446 pub background_color: Color,
445447 }
@@ -462,6 +464,7 @@ impl Default for Output {
462464 fn default() -> Self {
463465 Self {
464466 off: false,
467+ focus_at_startup: false,
465468 name: String::new(),
466469 scale: None,
467470 transform: Transform::Normal,
@@ -3746,6 +3749,7 @@ mod tests {
37463749 }
37473750
37483751 output "eDP-1" {
3752+ focus-at-startup
37493753 scale 2
37503754 transform "flipped-90"
37513755 position x=10 y=20
@@ -4105,6 +4109,7 @@ mod tests {
41054109 on_demand: true,
41064110 },
41074111 ),
4112+ focus_at_startup: true,
41084113 background_color: Color {
41094114 r: 0.09803922,
41104115 g: 0.09803922,
src/niri.rsmodified+25 -0
@@ -630,6 +630,8 @@ impl State {
630630 state.load_xkb_file();
631631 // Initialize some IPC server state.
632632 state.ipc_keyboard_layouts_changed();
633+ // Focus the default monitor if set by the user.
634+ state.focus_default_monitor();
633635
634636 Ok(state)
635637 }
@@ -794,6 +796,29 @@ impl State {
794796 rv
795797 }
796798
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+
797822 /// Focus a specific window, taking care of a potential active output change and cursor
798823 /// warp.
799824 pub fn focus_window(&mut self, window: &Window) {
wiki/Configuration:-Outputs.mdmodified+23 -0
@@ -13,6 +13,7 @@ output "eDP-1" {
1313 transform "90"
1414 position x=1280 y=0
1515 variable-refresh-rate // on-demand=true
16+ focus-at-startup
1617 background-color "#003300"
1718 }
1819
@@ -164,6 +165,28 @@ output "HDMI-A-1" {
164165 }
165166 ```
166167
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+
167190 ### `background-color`
168191
169192 <sup>Since: 0.1.8</sup>