Skip to content

Conversation

jackssrt
Copy link
Contributor

@jackssrt jackssrt commented Feb 2, 2025

This PR makes the general.focus_follows_cursor code focus the monitor that the cursor is on when there isn't a window to focus. This makes it so if you focus monitor 1, move your cursor to monitor 2 and open a program, the program will open on monitor 2 and not on monitor 1.

Before:

2025-02-02-T-13-51-11-explorer-z5LW7ZGThT.mp4

After:

2025-02-02-T-13-50-14-explorer-kKY2nnQe4m.mp4

This has been annoying me for a while, so I decided to fix it...

I don't know if it would've been better to make it focus the workspace under the cursor or not. Since there can only be one workspace displayed per monitor, I chose to make it focus the monitor.
Just let me know if you think it would be better to focus the workspace instead!

@jackssrt jackssrt force-pushed the feat-focus-monitor-automatically branch from 10f08f0 to fe38746 Compare February 2, 2025 14:58
@jackssrt
Copy link
Contributor Author

jackssrt commented Feb 4, 2025

This causes some problems with the ShareX screenshot overlay. When you open it and move your cursor to another monitor it steals focus away from the overlay. This happens even tough the overlay spans all your monitors, and it should be detected by the window focusing code...

Maybe the Windows API function for finding windows overlapping a point doesn't really work, and we should instead make our own implementation? Or maybe we should add a command to always keep a window focused?

@lars-berger
Copy link
Member

This causes some problems with the ShareX screenshot overlay. When you open it and move your cursor to another monitor it steals focus away from the overlay. This happens even tough the overlay spans all your monitors, and it should be detected by the window focusing code...

Maybe the Windows API function for finding windows overlapping a point doesn't really work, and we should instead make our own implementation? Or maybe we should add a command to always keep a window focused?

Just did a lil testing and ShareX is causing issues because it's ignored by the WM. Since ignored windows aren't part of the WM's tree of windows that it manages, state.focused_container() will point to the last focused window/workspace that had focus prior to ShareX gaining focus. So it'll currently try to set focus away from ShareX when the cursor goes to a different monitor.

What I'd suggest is we add a new is_focus_synced field to WmState which represents whether OS focus is synced with whatever window/workspace the WM believes should have focus. We can then update this field by adding to L26 of handle_window_focused.rs (also refactor L98-101 of the same file):

  state.is_focus_synced = match focused_container.as_window_container() {
    Ok(window) => *window.native() == *native_window,
    _ => Platform::desktop_window() == *native_window,
  };

We could then return early in handle_mouse_move.rs L16 if !state.is_focus_synced. So we'd essentially pause focus_follows_mouse if an ignored window has focus.

@jackssrt jackssrt force-pushed the feat-focus-monitor-automatically branch from fe38746 to bdd9704 Compare February 8, 2025 20:23
@jackssrt
Copy link
Contributor Author

jackssrt commented Feb 9, 2025

80b01dc has some problems right now when moving the mouse between monitors and windows. I think the is_focus_synced field isn't being set to true in all the cases it should be. I will look into it.

@jackssrt jackssrt marked this pull request as draft February 9, 2025 19:49
@lars-berger
Copy link
Member

80b01dc has some problems right now when moving the mouse between monitors and windows. I think the is_focus_synced field isn't being set to true in all the cases it should be. I will look into it.

Put in a small fix commit for this just now - could you check if it's fully working on your end? Did some quick testing and it all seems to be behaving nicely now

@lars-berger lars-berger marked this pull request as ready for review February 23, 2025 11:33
@jackssrt
Copy link
Contributor Author

jackssrt commented Mar 1, 2025

It seems to be working now, thanks for fixing! And sorry for taking so long to reply… You can merge it now.

@lars-berger lars-berger changed the title feat: focus monitor with general.focus_follows_cursor feat: change monitor focus with general.focus_follows_cursor Mar 2, 2025
@lars-berger lars-berger merged commit 1511366 into glzr-io:main Mar 2, 2025
3 checks passed
@github-project-automation github-project-automation bot moved this from 📬 Needs triage to ✅ Done in glazewm Mar 2, 2025
Miitto pushed a commit to Miitto/glazewm that referenced this pull request Jun 12, 2025
Copy link

🎉 This PR is included in version 3.9.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Copy link

🎉 This PR is included in version 3.9.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

2 participants