From 91a767a4f34cb025b9b9e7c51b08b67109cdf02e Mon Sep 17 00:00:00 2001 From: allsochen Date: Sat, 12 Oct 2024 15:42:00 +0200 Subject: [PATCH] feat: bring back the cursor follow focus feature (#3882) --- src/logic/Preferences.swift | 2 ++ src/ui/App.swift | 10 ++++++++++ .../controls/AdditionalControlsSheet.swift | 18 +++++++++++++----- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/logic/Preferences.swift b/src/logic/Preferences.swift index 99380d33..432f7322 100644 --- a/src/logic/Preferences.swift +++ b/src/logic/Preferences.swift @@ -26,6 +26,7 @@ class Preferences { "arrowKeysEnabled": "true", "vimKeysEnabled": "false", "mouseHoverEnabled": "false", + "cursorFollowFocusEnabled": "false", "showMinimizedWindows": ShowHowPreference.show.indexAsString, "showMinimizedWindows2": ShowHowPreference.show.indexAsString, "showMinimizedWindows3": ShowHowPreference.show.indexAsString, @@ -117,6 +118,7 @@ class Preferences { // periphery:ignore static var vimKeysEnabled: Bool { UserDefaults.standard.bool("vimKeysEnabled") } static var mouseHoverEnabled: Bool { UserDefaults.standard.bool("mouseHoverEnabled") } + static var cursorFollowFocusEnabled: Bool { UserDefaults.standard.bool("cursorFollowFocusEnabled") } static var showTabsAsWindows: Bool { UserDefaults.standard.bool("showTabsAsWindows") } static var hideColoredCircles: Bool { UserDefaults.standard.bool("hideColoredCircles") } static var windowDisplayDelay: DispatchTimeInterval { DispatchTimeInterval.milliseconds(UserDefaults.standard.int("windowDisplayDelay")) } diff --git a/src/ui/App.swift b/src/ui/App.swift index 917fe90e..180cc4c5 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -234,11 +234,21 @@ class App: AppCenterApplication, NSApplicationDelegate { hideUi(true) if let window = selectedWindow, MissionControl.state() == .inactive { window.focus() + if Preferences.cursorFollowFocusEnabled { + moveCursorToSelectedWindow(window) + } } else { previewPanel.orderOut(nil) } } + func moveCursorToSelectedWindow(_ window: Window) { + let referenceWindow = window.referenceWindowForTabbedWindow() + guard let position = referenceWindow?.position, let size = referenceWindow?.size else { return } + let point = CGPoint(x: position.x + size.width / 2, y: position.y + size.height / 2) + CGWarpMouseCursorPosition(point) + } + func refreshOpenUi(_ windowsToUpdate: [Window]? = nil) { guard appIsBeingUsed else { return } let currentScreen = NSScreen.preferred() // fix screen between steps since it could change (e.g. mouse moved to another screen) diff --git a/src/ui/preferences-window/tabs/controls/AdditionalControlsSheet.swift b/src/ui/preferences-window/tabs/controls/AdditionalControlsSheet.swift index 8b944469..0e5e3e5f 100644 --- a/src/ui/preferences-window/tabs/controls/AdditionalControlsSheet.swift +++ b/src/ui/preferences-window/tabs/controls/AdditionalControlsSheet.swift @@ -8,17 +8,25 @@ class AdditionalControlsSheet: SheetWindow { rightViews: [LabelAndControl.makeSwitch("vimKeysEnabled", extraAction: ControlsTab.vimKeysEnabledCallback)]) let enableMouse = TableGroupView.Row(leftTitle: NSLocalizedString("Select windows on mouse hover", comment: ""), rightViews: [LabelAndControl.makeSwitch("mouseHoverEnabled")]) + let enableCursorFollowFocus = TableGroupView.Row(leftTitle: NSLocalizedString("Cursor follows focus", comment: ""), + rightViews: [LabelAndControl.makeSwitch("cursorFollowFocusEnabled")]) ControlsTab.arrowKeysCheckbox = enableArrows.rightViews[0] as? Switch ControlsTab.vimKeysCheckbox = enableVimKeys.rightViews[0] as? Switch ControlsTab.arrowKeysEnabledCallback(ControlsTab.arrowKeysCheckbox) ControlsTab.vimKeysEnabledCallback(ControlsTab.vimKeysCheckbox) - let table = TableGroupView(title: NSLocalizedString("Additional controls", comment: ""), + let table1 = TableGroupView(title: NSLocalizedString("Additional controls", comment: ""), width: PreferencesWindow.width) - _ = table.addRow(enableArrows) - _ = table.addRow(enableVimKeys) - _ = table.addRow(enableMouse) - return table + _ = table1.addRow(enableArrows) + _ = table1.addRow(enableVimKeys) + _ = table1.addRow(enableMouse) + + let table2 = TableGroupView(title: NSLocalizedString("Miscellaneous", comment: ""), + width: PreferencesWindow.width) + _ = table2.addRow(enableCursorFollowFocus) + + let view = TableGroupSetView(originalViews: [table1, table2], padding: 0) + return view } }