Skip to content

Commit

Permalink
feat: allow cursor follow focus behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
GrzegorzKazana authored and lwouis committed Feb 4, 2022
1 parent f5fc5ba commit be50758
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
6 changes: 6 additions & 0 deletions resources/l10n/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
/* No comment provided by engineer. */
"Also select windows using:" = "Also select windows using:";

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

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

/* No comment provided by engineer. */
"AltTab crashed last time you used it. Sending a crash report will help get the issue fixed" = "AltTab crashed last time you used it. Sending a crash report will help get the issue fixed";

Expand Down
6 changes: 6 additions & 0 deletions resources/l10n/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
/*No comment provided by engineer.*/
"Also select windows using:" = "Also select windows using:";

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

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

/*No comment provided by engineer.*/
"AltTab crashed last time you used it. Sending a crash report will help get the issue fixed" = "AltTab crashed last time you used it. Sending a crash report will help get the issue fixed";

Expand Down
2 changes: 2 additions & 0 deletions src/logic/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Preferences {
"hideShowAppShortcut": "H",
"arrowKeysEnabled": "true",
"mouseHoverEnabled": "true",
"cursorFollowFocusEnabled": "false",
"showMinimizedWindows": ShowHowPreference.show.rawValue,
"showMinimizedWindows2": ShowHowPreference.show.rawValue,
"showHiddenWindows": ShowHowPreference.show.rawValue,
Expand Down Expand Up @@ -90,6 +91,7 @@ class Preferences {
static var hideShowAppShortcut: String { defaults.string("hideShowAppShortcut") }
static var arrowKeysEnabled: Bool { defaults.bool("arrowKeysEnabled") }
static var mouseHoverEnabled: Bool { defaults.bool("mouseHoverEnabled") }
static var cursorFollowFocusEnabled: Bool { defaults.bool("cursorFollowFocusEnabled") }
static var showTabsAsWindows: Bool { defaults.bool("showTabsAsWindows") }
static var hideColoredCircles: Bool { defaults.bool("hideColoredCircles") }
static var windowDisplayDelay: DispatchTimeInterval { DispatchTimeInterval.milliseconds(defaults.int("windowDisplayDelay")) }
Expand Down
20 changes: 17 additions & 3 deletions src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,24 @@ class App: AppCenterApplication, NSApplicationDelegate {
KeyRepeatTimer.toggleRepeatingKeyPreviousWindow()
}

func focusSelectedWindow(_ window: Window?) {
func focusSelectedWindow(_ selectedWindow: Window?) {
hideUi()
guard !CGWindow.isMissionControlActive() else { return }
window?.focus()
guard let window = selectedWindow, !CGWindow.isMissionControlActive() else { return }
window.focus()
if Preferences.cursorFollowFocusEnabled {
moveCursorToSelectedWindow(window)
}
}

func moveCursorToSelectedWindow(_ window: Window) {
guard let position = window.position, let size = window.size else { return }

let point = CGPoint(
x: position.x + size.width / 2,
y: position.y + size.height / 2
)

CGWarpMouseCursorPosition(point)
}

func reopenUi() {
Expand Down
10 changes: 7 additions & 3 deletions src/ui/preferences-window/tabs/ControlsTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ class ControlsTab {
let hideShowAppShortcut = LabelAndControl.makeLabelWithRecorder(NSLocalizedString("Hide/Show app", comment: ""), "hideShowAppShortcut", Preferences.hideShowAppShortcut, labelPosition: .right)
let enableArrows = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Arrow keys", comment: ""), "arrowKeysEnabled", extraAction: ControlsTab.arrowKeysEnabledCallback, labelPosition: .right)
let enableMouse = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Mouse hover", comment: ""), "mouseHoverEnabled", labelPosition: .right)
let checkboxesExplanations = LabelAndControl.makeLabel(NSLocalizedString("Also select windows using:", comment: ""))
let checkboxes = StackView([StackView(enableArrows), StackView(enableMouse)], .vertical)
let enableCursorFollowFocus = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Cursor follows focus", comment: ""), "cursorFollowFocusEnabled", labelPosition: .right)
let selectWindowcheckboxesExplanations = LabelAndControl.makeLabel(NSLocalizedString("Also select windows using:", comment: ""))
let selectWindowCheckboxes = StackView([StackView(enableArrows), StackView(enableMouse)], .vertical)
let miscCheckboxesExplanations = LabelAndControl.makeLabel(NSLocalizedString("Miscellaneous:", comment: ""))
let miscCheckboxes = StackView([StackView(enableCursorFollowFocus)], .vertical)
let shortcuts = StackView([focusWindowShortcut, previousWindowShortcut, cancelShortcut, closeWindowShortcut, minDeminWindowShortcut, quitAppShortcut, hideShowAppShortcut].map { (view: [NSView]) in StackView(view) }, .vertical)
let orPress = LabelAndControl.makeLabel(NSLocalizedString("While open, press:", comment: ""), shouldFit: false)
let (holdShortcut, nextWindowShortcut, tab1View) = toShowSection("")
Expand All @@ -48,7 +51,8 @@ class ControlsTab {
let grid = GridView([
[tabView],
[orPress, shortcuts],
[checkboxesExplanations, checkboxes],
[selectWindowcheckboxesExplanations, selectWindowCheckboxes],
[miscCheckboxesExplanations, miscCheckboxes]
])
grid.column(at: 0).xPlacement = .trailing
grid.mergeCells(inHorizontalRange: NSRange(location: 0, length: 2), verticalRange: NSRange(location: 0, length: 1))
Expand Down

0 comments on commit be50758

Please sign in to comment.