Skip to content

Commit

Permalink
Rework cursor location getter
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Oct 5, 2024
1 parent 94e6279 commit 5ac0694
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
18 changes: 9 additions & 9 deletions Ice/Events/EventManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ extension EventManager {

// Get the window that the user has clicked into.
guard
let mouseLocation = MouseCursor.location(flipped: true),
let mouseLocation = MouseCursor.coreGraphicsLocation,
let windowUnderMouse = WindowInfo.getOnScreenWindows(excludeDesktopWindows: false)
.filter({ $0.layer < CGWindowLevelForKey(.cursorWindow) })
.first(where: { $0.frame.contains(mouseLocation) }),
Expand Down Expand Up @@ -247,7 +247,7 @@ extension EventManager {
guard
let appState,
isMouseInsideEmptyMenuBarSpace,
let mouseLocation = MouseCursor.location(flipped: false)
let mouseLocation = MouseCursor.appKitLocation
else {
return
}
Expand Down Expand Up @@ -447,12 +447,12 @@ extension EventManager {
}
if appState.menuBarManager.isMenuBarHiddenBySystem || appState.isActiveSpaceFullscreen {
if
let mouseLocation = MouseCursor.location(flipped: true),
let mouseLocation = MouseCursor.coreGraphicsLocation,
let menuBarWindow = WindowInfo.getMenuBarWindow(for: screen.displayID)
{
return menuBarWindow.frame.contains(mouseLocation)
}
} else if let mouseLocation = MouseCursor.location(flipped: false) {
} else if let mouseLocation = MouseCursor.appKitLocation {
return mouseLocation.y > screen.visibleFrame.maxY && mouseLocation.y <= screen.frame.maxY
}
return false
Expand All @@ -462,7 +462,7 @@ extension EventManager {
/// the bounds of the current application menu.
var isMouseInsideApplicationMenu: Bool {
guard
let mouseLocation = MouseCursor.location(flipped: true),
let mouseLocation = MouseCursor.coreGraphicsLocation,
let screen = bestScreen,
let appState,
var applicationMenuFrame = appState.menuBarManager.getApplicationMenuFrame(for: screen.displayID)
Expand All @@ -479,7 +479,7 @@ extension EventManager {
var isMouseInsideMenuBarItem: Bool {
guard
let screen = bestScreen,
let mouseLocation = MouseCursor.location(flipped: true)
let mouseLocation = MouseCursor.coreGraphicsLocation
else {
return false
}
Expand All @@ -495,7 +495,7 @@ extension EventManager {
var isMouseInsideNotch: Bool {
guard
let screen = bestScreen,
let mouseLocation = MouseCursor.location(flipped: false),
let mouseLocation = MouseCursor.appKitLocation,
let frameOfNotch = screen.frameOfNotch
else {
return false
Expand All @@ -517,7 +517,7 @@ extension EventManager {
var isMouseInsideIceBar: Bool {
guard
let appState,
let mouseLocation = MouseCursor.location(flipped: false)
let mouseLocation = MouseCursor.appKitLocation
else {
return false
}
Expand All @@ -535,7 +535,7 @@ extension EventManager {
let appState,
let visibleSection = appState.menuBarManager.section(withName: .visible),
let iceIconFrame = visibleSection.controlItem.windowFrame,
let mouseLocation = MouseCursor.location(flipped: false)
let mouseLocation = MouseCursor.appKitLocation
else {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions Ice/MenuBar/MenuBarItemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ extension MenuBarItemManager {
guard let appState else {
throw EventError(code: .invalidAppState, item: item)
}
guard let cursorLocation = MouseCursor.location(flipped: true) else {
guard let cursorLocation = MouseCursor.coreGraphicsLocation else {
throw EventError(code: .invalidCursorLocation, item: item)
}
guard let initialFrame = getCurrentFrame(for: item) else {
Expand Down Expand Up @@ -1001,7 +1001,7 @@ extension MenuBarItemManager {
guard let source = CGEventSource(stateID: .hidSystemState) else {
throw EventError(code: .invalidEventSource, item: item)
}
guard let cursorLocation = MouseCursor.location(flipped: true) else {
guard let cursorLocation = MouseCursor.coreGraphicsLocation else {
throw EventError(code: .invalidCursorLocation, item: item)
}
guard let currentFrame = getCurrentFrame(for: item) else {
Expand Down
2 changes: 1 addition & 1 deletion Ice/UI/IceBar/IceBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ final class IceBarPanel: NSPanel {
}
return getOrigin(for: .iceIcon)
case .mousePointer:
guard let location = MouseCursor.location(flipped: false) else {
guard let location = MouseCursor.appKitLocation else {
return getOrigin(for: .iceIcon)
}

Expand Down
36 changes: 18 additions & 18 deletions Ice/Utilities/MouseCursor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import CoreGraphics

/// A namespace for mouse cursor operations.
enum MouseCursor {
/// The location of the mouse pointer in the coordinate system used by the
/// `CoreGraphics` framework.
///
/// The coordinate system of the returned location is relative to the top
/// left corner of the screen.
static var coreGraphicsLocation: CGPoint? {
CGEvent(source: nil)?.location
}

/// The location of the mouse pointer in the coordinate system used by the
/// `AppKit` framework.
///
/// The coordinate system of the returned location is relative to the bottom
/// left corner of the screen.
static var appKitLocation: CGPoint? {
CGEvent(source: nil)?.unflippedLocation
}

/// Hides the mouse cursor and increments the hide cursor count.
static func hide() {
let result = CGDisplayHideCursor(CGMainDisplayID())
Expand All @@ -32,24 +50,6 @@ enum MouseCursor {
Logger.mouseCursor.error("CGWarpMouseCursorPosition failed with error \(result.logString)")
}
}

/// Returns the location of the mouse pointer.
///
/// If `flipped` is `true`, the coordinate system of the returned location
/// is relative to the top left corner of the screen, and is compatible with
/// the coordinate system used by the `CoreGraphics` framework. Otherwise,
/// the coordinate system of the returned location is relative to the bottom
/// left corner of the screen, and is compatible with coordinate system used
/// by the `AppKit` framework.
static func location(flipped: Bool) -> CGPoint? {
CGEvent(source: nil).map { event in
if flipped {
event.location
} else {
event.unflippedLocation
}
}
}
}

// MARK: - Logger
Expand Down

0 comments on commit 5ac0694

Please sign in to comment.