Skip to content

Commit 006b095

Browse files
authored
MouseFollowsFocus: avoid getting the mouse stuck in screen corners (#335)
The mouse cursor moves in a straight line between its current position and the destination set with hs.mouse.absolutePosition(). If the mouse is moving to a new screen, it can get caught on a corner that does not connect between screens, depending on the screen sizes and layout. When moving the mouse between screens, first move the mouse to the center of the current screen, then to the center of the new screen, then the target destination. This path has the best chance at avoiding any non-connected edges between screens. These extra mouse movements happen instantaneously and are not visible to the user.
1 parent 63aa8a1 commit 006b095

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Source/MouseFollowsFocus.spoon/init.lua

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function obj:start()
5555
})
5656
self.window_filter:subscribe({
5757
hs.window.filter.windowFocused
58-
}, function(window)
58+
}, function(window)
5959
if self.onChangeOfScreenOnly and self.currentWindowScreen and self.currentWindowScreen:id() == window:screen():id() then return end
6060
self:updateMouse(window)
6161
self.currentWindowScreen = window:screen()
@@ -91,6 +91,13 @@ function obj:updateMouse(window)
9191
local current_pos = hs.geometry(hs.mouse.absolutePosition())
9292
local frame = window:frame()
9393
if not current_pos:inside(frame) then
94+
local current_screen = hs.mouse.getCurrentScreen()
95+
local window_screen = window:screen()
96+
if current_screen and window_screen and current_screen ~= window_screen then
97+
-- avoid getting the mouse stuck on a screen corner by moving through the center of each screen
98+
hs.mouse.absolutePosition(current_screen:frame().center)
99+
hs.mouse.absolutePosition(window_screen:frame().center)
100+
end
94101
hs.mouse.absolutePosition(frame.center)
95102
end
96103
end

Spoons/MouseFollowsFocus.spoon.zip

100 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)