Skip to content

Commit

Permalink
feat: bring back the cursor follow focus feature (#3882)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allsochen authored and lwouis committed Dec 6, 2024
1 parent 9bafd03 commit 73382db
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 6 additions & 0 deletions resources/l10n/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@
/* No comment provided by engineer. */
"Crash reports policy" = "Crash reports policy";

/* No comment provided by engineer. */
"Cursor follows focus" = "Cursor follows focus";

/* No comment provided by engineer. */
"Customize App Icons style…" = "Customize App Icons style…";

Expand Down Expand Up @@ -265,6 +268,9 @@
/* No comment provided by engineer. */
"Minimize/Deminimize window" = "Minimize/Deminimize window";

/* No comment provided by engineer. */
"Miscellaneous" = "Miscellaneous";

/* No comment provided by engineer. */
"Multiple screens" = "Multiple screens";

Expand Down
2 changes: 2 additions & 0 deletions src/logic/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")) }
Expand Down
10 changes: 10 additions & 0 deletions src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 73382db

Please sign in to comment.