Skip to content

Commit

Permalink
Delay new window detection until mouse is up
Browse files Browse the repository at this point in the history
_closes #1001
  • Loading branch information
nikitabobko committed Feb 17, 2025
1 parent e38a999 commit e977422
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
6 changes: 2 additions & 4 deletions Sources/AppBundle/GlobalObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@ class GlobalObserver {
resetManipulatedWithMouseIfPossible()
let mouseLocation = mouseLocation
let clickedMonitor = mouseLocation.monitorApproximation
let focus = focus
switch () {
// Detect clicks on desktop of different monitors
case _ where clickedMonitor.activeWorkspace != focus.workspace:
_ = refreshSession(.globalObserverLeftMouseUp, screenIsDefinitelyUnlocked: true) {
clickedMonitor.activeWorkspace.focusWorkspace()
}
// Detect close button clicks for unfocused windows. Yes, kAXUIElementDestroyedNotification is that unreliable
case _ where focus.windowOrNil?.getRect()?.contains(mouseLocation) == false: // todo replace getRect with preflushRect when it later becomes available
refreshAndLayout(.globalObserverLeftMouseUp, screenIsDefinitelyUnlocked: true)
// And trigger new window detection that could be delayed due to mouseDown event
default:
break
refreshAndLayout(.globalObserverLeftMouseUp, screenIsDefinitelyUnlocked: true)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/AppBundle/mouse/mouse.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AppKit

var currentlyManipulatedWithMouseWindowId: UInt32? = nil
var isLeftMouseButtonPressed: Bool { NSEvent.pressedMouseButtons == 1 }
var isLeftMouseButtonDown: Bool { NSEvent.pressedMouseButtons == 1 }

/// Same motivation as in monitorFrameNormalized
var mouseLocation: CGPoint {
Expand Down
2 changes: 1 addition & 1 deletion Sources/AppBundle/mouse/moveWithMouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func movedObs(_ obs: AXObserver, ax: AXUIElement, notif: CFString, data: UnsafeM

private func moveWithMouseIfTheCase(_ window: Window) { // todo cover with tests
if window.isHiddenInCorner || // Don't allow to move windows of hidden workspaces
!isLeftMouseButtonPressed ||
!isLeftMouseButtonDown ||
currentlyManipulatedWithMouseWindowId != nil && window.windowId != currentlyManipulatedWithMouseWindowId ||
getNativeFocusedWindow(startup: false) != window
{
Expand Down
2 changes: 1 addition & 1 deletion Sources/AppBundle/mouse/resizeWithMouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private let adaptiveWeightBeforeResizeWithMouseKey = TreeNodeUserDataKey<CGFloat

private func resizeWithMouseIfTheCase(_ window: Window) { // todo cover with tests
if window.isHiddenInCorner || // Don't allow to resize windows of hidden workspaces
!isLeftMouseButtonPressed ||
!isLeftMouseButtonDown ||
currentlyManipulatedWithMouseWindowId != nil && window.windowId != currentlyManipulatedWithMouseWindowId ||
getNativeFocusedWindow(startup: false) != window
{
Expand Down
6 changes: 5 additions & 1 deletion Sources/AppBundle/tree/MacWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ final class MacWindow: Window, CustomStringConvertible {
if let existing = allWindowsMap[id] {
return existing
} else {
// Delay new window detection if mouse is down
// It helps with apps that allow dragging their tabs out to create new windows
// https://github.com/nikitabobko/AeroSpace/issues/1001
if isLeftMouseButtonDown { return nil }

let data = getBindingDataForNewWindow(
axWindow,
startup ? (axWindow.center?.monitorApproximation ?? mainMonitor).activeWorkspace : focus.workspace,
Expand Down Expand Up @@ -283,7 +288,6 @@ func isDialogHeuristic(_ axWindow: AXUIElement, _ app: MacApp) -> Bool {
// - flameshot? https://github.com/nikitabobko/AeroSpace/issues/112
// - Drata Agent https://github.com/nikitabobko/AeroSpace/issues/134
if !isFullscreenable(axWindow) &&
app.id != "com.google.Chrome" && // "Drag out" a tab out of Chrome window
app.id != "org.gimp.gimp-2.10" && // Gimp doesn't show fullscreen button
app.id != "com.apple.ActivityMonitor" && // Activity Monitor doesn't show fullscreen button

Expand Down

0 comments on commit e977422

Please sign in to comment.