Skip to content

Commit

Permalink
fix: bring back mouse hover selection (closes #2317)
Browse files Browse the repository at this point in the history
  • Loading branch information
decodism authored and lwouis committed Feb 12, 2023
1 parent f9bb33c commit abe2e0b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/logic/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Preferences {
"quitAppShortcut": "Q",
"hideShowAppShortcut": "H",
"arrowKeysEnabled": "true",
"mouseHoverEnabled": "true",
"mouseHoverEnabled": "false",
"cursorFollowFocusEnabled": "false",
"showMinimizedWindows": ShowHowPreference.show.rawValue,
"showMinimizedWindows2": ShowHowPreference.show.rawValue,
Expand Down
21 changes: 11 additions & 10 deletions src/logic/Windows.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class Windows {
if let app = Applications.find(NSWorkspace.shared.frontmostApplication?.processIdentifier),
app.focusedWindow == nil,
let lastFocusedWindowIndex = getLastFocusedWindowIndex() {
updateFocusedWindowIndex(lastFocusedWindowIndex)
updateHoveredAndFocusedWindowIndexes(lastFocusedWindowIndex)
} else {
cycleFocusedWindowIndex(1)
if focusedWindowIndex == 0 {
updateFocusedWindowIndex(0)
updateHoveredAndFocusedWindowIndexes(0)
}
}
}
Expand Down Expand Up @@ -99,22 +99,23 @@ class Windows {
return nil
}

static func updateFocusedWindowIndex(_ newIndex: Int, _ fromMouse: Bool = false) {
if (fromMouse && newIndex == hoveredWindowIndex) || (!fromMouse && newIndex == focusedWindowIndex) {
return
}
if fromMouse {
static func updateHoveredAndFocusedWindowIndexes(_ newIndex: Int, _ fromMouse: Bool = false) {
var index: Int?
if fromMouse && newIndex != hoveredWindowIndex {
let oldIndex = hoveredWindowIndex
hoveredWindowIndex = newIndex
if let oldIndex = oldIndex {
ThumbnailsView.highlight(oldIndex)
}
} else {
index = hoveredWindowIndex
}
if (!fromMouse || Preferences.mouseHoverEnabled) && newIndex != focusedWindowIndex {
let oldIndex = focusedWindowIndex
focusedWindowIndex = newIndex
ThumbnailsView.highlight(oldIndex)
index = focusedWindowIndex
}
let index = fromMouse ? hoveredWindowIndex! : focusedWindowIndex
guard let index = index else { return }
ThumbnailsView.highlight(index)
let focusedView = ThumbnailsView.recycledViews[index]
App.app.thumbnailsPanel.thumbnailsView.scrollView.contentView.scrollToVisible(focusedView.frame)
Expand Down Expand Up @@ -143,7 +144,7 @@ class Windows {
(KeyRepeatTimer.isARepeat || KeyRepeatTimer.timer?.isValid ?? false) {
return
}
updateFocusedWindowIndex(nextIndex)
updateHoveredAndFocusedWindowIndexes(nextIndex)
}

static func windowIndexAfterCycling(_ step: Int) -> Int {
Expand Down
6 changes: 2 additions & 4 deletions src/ui/main-window/ThumbnailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class ThumbnailView: NSStackView {
windowlessIcon.needsDisplay = true
}
self.mouseUpCallback = { () -> Void in App.app.focusSelectedWindow(element) }
self.mouseMovedCallback = { () -> Void in Windows.updateFocusedWindowIndex(index, true) }
self.mouseMovedCallback = { () -> Void in Windows.updateHoveredAndFocusedWindowIndexes(index, true) }
[quitIcon, closeIcon, minimizeIcon, maximizeIcon].forEach { $0.window_ = element }
showOrHideWindowControls(false)
// force a display to avoid flickering; see https://github.com/lwouis/alt-tab-macos/issues/197
Expand Down Expand Up @@ -263,9 +263,7 @@ class ThumbnailView: NSStackView {

func mouseMoved() {
showOrHideWindowControls(true)
if Preferences.mouseHoverEnabled {
mouseMovedCallback()
}
mouseMovedCallback()
}

override func mouseUp(with event: NSEvent) {
Expand Down
20 changes: 12 additions & 8 deletions src/ui/main-window/ThumbnailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ThumbnailsView: NSVisualEffectView {
return leadingSide ? NSMinX($0.frame) < originCenter : NSMaxX($0.frame) > originCenter
} ?? iterable.last!
let targetIndex = ThumbnailsView.recycledViews.firstIndex(of: targetView)!
Windows.updateFocusedWindowIndex(targetIndex)
Windows.updateHoveredAndFocusedWindowIndexes(targetIndex)
}
}

Expand Down Expand Up @@ -228,6 +228,14 @@ class ScrollView: NSScrollView {
self?.isCurrentlyScrolling = true
}
}

private func resetHoveredWindow() {
if let oldIndex = Windows.hoveredWindowIndex {
Windows.hoveredWindowIndex = nil
ThumbnailsView.highlight(oldIndex)
ThumbnailsView.recycledViews[oldIndex].showOrHideWindowControls(false)
}
}

override func mouseMoved(with event: NSEvent) {
// disable mouse hover during scrolling as it creates jank during elastic bounces at the start/end of the scrollview
Expand All @@ -245,19 +253,15 @@ class ScrollView: NSScrollView {
let target = target as! ThumbnailView
target.mouseMoved()
} else {
previousTarget?.showOrHideWindowControls(false)
resetHoveredWindow()
}
} else {
previousTarget?.showOrHideWindowControls(false)
resetHoveredWindow()
}
}

override func mouseExited(with event: NSEvent) {
if let oldIndex = Windows.hoveredWindowIndex {
Windows.hoveredWindowIndex = nil
ThumbnailsView.highlight(oldIndex)
ThumbnailsView.recycledViews[oldIndex].showOrHideWindowControls(false)
}
resetHoveredWindow()
}

/// holding shift and using the scrolling wheel will generate a horizontal movement
Expand Down

0 comments on commit abe2e0b

Please sign in to comment.