Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Mar 29, 2025

Conversation

lualeet
Copy link
Contributor

@lualeet lualeet commented Mar 22, 2025

Adds a per-output focus-at-startup option to focus a chosen output when niri starts.

This is implemented by adding a focus_default_monitor method to State and calling it in State::new. If the option is set to null or no monitor is successfully matched, centers the cursor on the first monitor by name. Otherwise, it focuses the first detected and matching monitor by order of declaration.

The contents of the focus_default_monitor aren't nice to look at, but I couldn't get it any simpler without a temp variable dropped too early or a borrow checker upset.

@bbb651
Copy link
Contributor

bbb651 commented Mar 23, 2025

I can't find the issue/discussion but I remember someone was asking to have the mouse centered on startup, since move_cursor_to_output also centers the mouse maybe it should also be done for the default output when it's set to null?

@lualeet
Copy link
Contributor Author

lualeet commented Mar 23, 2025

I can't find the issue/discussion but I remember someone was asking to have the mouse centered on startup, since move_cursor_to_output also centers the mouse maybe it should also be done for the default output when it's set to null?

Sure, that's a good idea. The sorting logic to pick a single output is duplicated a few times by now, but I don't think it should be an issue.

src/niri.rs Outdated
Comment on lines 800 to 801
let config_temp = self.niri.config.clone();
let config = config_temp.borrow();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let config_temp = self.niri.config.clone();
let config = config_temp.borrow();
let config = self.niri.config.borrow();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that. Unfortunately,

let config = self.niri.config.borrow();

leads to E0502: cannot borrow `*self` as mutable because it is also borrowed as immutable as the immutable reference to self.config cannot be dropped before the loop is done iterating and self must be mutably borrowed to call move_cursor_to_output

Inlining it to

let config = self.niri.config.clone().borrow();

leads to E0716: temporary value dropped while borrowed

@lualeet lualeet force-pushed the default-output branch 2 times, most recently from 6418f7f to 8af70dc Compare March 26, 2025 23:11
@lualeet lualeet changed the title Add option 'default-output' to focus a chosen output on start Add option 'focus-at-startup' to focus a chosen output on start Mar 26, 2025
@YaLTeR
Copy link
Owner

YaLTeR commented Mar 29, 2025

Simplified it a bit, good to merge now.

@YaLTeR YaLTeR enabled auto-merge (squash) March 29, 2025 10:03
@YaLTeR YaLTeR merged commit 8d43efe into YaLTeR:main Mar 29, 2025
12 checks passed
@YaLTeR
Copy link
Owner

YaLTeR commented Mar 29, 2025

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants