Skip to content

Commit 7d31eb6

Browse files
committed
Refactoring
1 parent d567f1e commit 7d31eb6

14 files changed

+44
-61
lines changed

Diff for: Ice/Events/EventManager.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ extension EventManager {
223223

224224
// Get the window that the user has clicked into.
225225
guard
226-
let mouseLocation = MouseCursor.location(in: .coreGraphics),
226+
let mouseLocation = MouseCursor.locationCoreGraphics,
227227
let windowUnderMouse = WindowInfo.getOnScreenWindows(excludeDesktopWindows: false)
228228
.filter({ $0.layer < CGWindowLevelForKey(.cursorWindow) })
229229
.first(where: { $0.frame.contains(mouseLocation) && $0.title?.isEmpty == false }),
@@ -257,7 +257,7 @@ extension EventManager {
257257
guard
258258
let appState,
259259
isMouseInsideEmptyMenuBarSpace,
260-
let mouseLocation = MouseCursor.location(in: .appKit)
260+
let mouseLocation = MouseCursor.locationAppKit
261261
else {
262262
return
263263
}
@@ -453,12 +453,12 @@ extension EventManager {
453453
}
454454
if appState.menuBarManager.isMenuBarHiddenBySystem || appState.isActiveSpaceFullscreen {
455455
if
456-
let mouseLocation = MouseCursor.location(in: .coreGraphics),
456+
let mouseLocation = MouseCursor.locationCoreGraphics,
457457
let menuBarWindow = WindowInfo.getMenuBarWindow(for: screen.displayID)
458458
{
459459
return menuBarWindow.frame.contains(mouseLocation)
460460
}
461-
} else if let mouseLocation = MouseCursor.location(in: .appKit) {
461+
} else if let mouseLocation = MouseCursor.locationAppKit {
462462
return mouseLocation.y > screen.visibleFrame.maxY && mouseLocation.y <= screen.frame.maxY
463463
}
464464
return false
@@ -468,7 +468,7 @@ extension EventManager {
468468
/// the bounds of the current application menu.
469469
var isMouseInsideApplicationMenu: Bool {
470470
guard
471-
let mouseLocation = MouseCursor.location(in: .coreGraphics),
471+
let mouseLocation = MouseCursor.locationCoreGraphics,
472472
let screen = bestScreen,
473473
let appState,
474474
var applicationMenuFrame = appState.menuBarManager.getApplicationMenuFrame(for: screen.displayID)
@@ -485,7 +485,7 @@ extension EventManager {
485485
var isMouseInsideMenuBarItem: Bool {
486486
guard
487487
let screen = bestScreen,
488-
let mouseLocation = MouseCursor.location(in: .coreGraphics)
488+
let mouseLocation = MouseCursor.locationCoreGraphics
489489
else {
490490
return false
491491
}
@@ -501,7 +501,7 @@ extension EventManager {
501501
var isMouseInsideNotch: Bool {
502502
guard
503503
let screen = bestScreen,
504-
let mouseLocation = MouseCursor.location(in: .appKit),
504+
let mouseLocation = MouseCursor.locationAppKit,
505505
let frameOfNotch = screen.frameOfNotch
506506
else {
507507
return false
@@ -523,7 +523,7 @@ extension EventManager {
523523
var isMouseInsideIceBar: Bool {
524524
guard
525525
let appState,
526-
let mouseLocation = MouseCursor.location(in: .appKit)
526+
let mouseLocation = MouseCursor.locationAppKit
527527
else {
528528
return false
529529
}
@@ -541,7 +541,7 @@ extension EventManager {
541541
let appState,
542542
let visibleSection = appState.menuBarManager.section(withName: .visible),
543543
let iceIconFrame = visibleSection.controlItem.windowFrame,
544-
let mouseLocation = MouseCursor.location(in: .appKit)
544+
let mouseLocation = MouseCursor.locationAppKit
545545
else {
546546
return false
547547
}
File renamed without changes.

Diff for: Ice/MenuBar/ItemManagement/MenuBarItemManager.swift renamed to Ice/MenuBar/MenuBarItems/MenuBarItemManager.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ extension MenuBarItemManager {
10571057
guard let appState else {
10581058
throw EventError(code: .invalidAppState, item: item)
10591059
}
1060-
guard let cursorLocation = MouseCursor.location(in: .coreGraphics) else {
1060+
guard let cursorLocation = MouseCursor.locationCoreGraphics else {
10611061
throw EventError(code: .invalidCursorLocation, item: item)
10621062
}
10631063
guard let initialFrame = getCurrentFrame(for: item) else {
@@ -1140,7 +1140,7 @@ extension MenuBarItemManager {
11401140
guard let source = CGEventSource(stateID: .hidSystemState) else {
11411141
throw EventError(code: .invalidEventSource, item: item)
11421142
}
1143-
guard let cursorLocation = MouseCursor.location(in: .coreGraphics) else {
1143+
guard let cursorLocation = MouseCursor.locationCoreGraphics else {
11441144
throw EventError(code: .invalidCursorLocation, item: item)
11451145
}
11461146
guard let currentFrame = getCurrentFrame(for: item) else {

Diff for: Ice/UI/IceBar/IceBar.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ final class IceBarPanel: NSPanel {
117117
}
118118
return getOrigin(for: .iceIcon)
119119
case .mousePointer:
120-
guard let location = MouseCursor.location(in: .appKit) else {
120+
guard let location = MouseCursor.locationAppKit else {
121121
return getOrigin(for: .iceIcon)
122122
}
123123

Diff for: Ice/Utilities/BindingExposable.swift

+23-24
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,9 @@
55

66
import SwiftUI
77

8-
/// A type that acts as a lens that exposes bindings to the writable properties
9-
/// of a base object.
10-
@dynamicMemberLookup
11-
struct ExposedBindings<Base: BindingExposable> {
12-
/// The base object whose bindings are exposed.
13-
fileprivate let base: Base
14-
15-
/// Returns a binding to the property at the given key path.
16-
subscript<Value>(dynamicMember keyPath: ReferenceWritableKeyPath<Base, Value>) -> Binding<Value> {
17-
Binding(
18-
get: { base[keyPath: keyPath] },
19-
set: { base[keyPath: keyPath] = $0 }
20-
)
21-
}
22-
23-
/// Returns a lens that exposes the bindings of the object
24-
/// at the given key path.
25-
subscript<T: BindingExposable>(dynamicMember keyPath: KeyPath<Base, T>) -> ExposedBindings<T> {
26-
ExposedBindings<T>(base: base[keyPath: keyPath])
27-
}
28-
}
29-
308
/// A type that exposes its writable properties as bindings.
319
protocol BindingExposable {
32-
/// A type that acts as a lens that exposes bindings to the writable properties
33-
/// of this type.
10+
/// A lens that exposes bindings to the writable properties of this type.
3411
typealias Bindings = ExposedBindings<Self>
3512

3613
/// A lens that exposes bindings to the writable properties of this instance.
@@ -42,3 +19,25 @@ extension BindingExposable {
4219
Bindings(base: self)
4320
}
4421
}
22+
23+
/// A lens that exposes bindings to the writable properties of a base object.
24+
@dynamicMemberLookup
25+
struct ExposedBindings<Base: BindingExposable> {
26+
/// The object whose bindings are exposed.
27+
private let base: Base
28+
29+
/// Creates a lens that exposes the bindings of the given object.
30+
init(base: Base) {
31+
self.base = base
32+
}
33+
34+
/// Returns a binding to the property at the given key path.
35+
subscript<Value>(dynamicMember keyPath: ReferenceWritableKeyPath<Base, Value>) -> Binding<Value> {
36+
Binding(get: { base[keyPath: keyPath] }, set: { base[keyPath: keyPath] = $0 })
37+
}
38+
39+
/// Returns a lens that exposes the bindings of the object at the given key path.
40+
subscript<T: BindingExposable>(dynamicMember keyPath: KeyPath<Base, T>) -> ExposedBindings<T> {
41+
ExposedBindings<T>(base: base[keyPath: keyPath])
42+
}
43+
}

Diff for: Ice/Utilities/MouseCursor.swift

+9-25
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ import CoreGraphics
77

88
/// A namespace for mouse cursor operations.
99
enum MouseCursor {
10-
/// A coordinate space for mouse cursor operations.
11-
enum CoordinateSpace {
12-
/// The coordinate space used by the `AppKit` framework.
13-
///
14-
/// The origin of this coordinate space is at the bottom left corner of the screen.
15-
case appKit
10+
/// Returns the location of the mouse cursor in the coordinate space used by
11+
/// the `AppKit` framework, with the origin at the bottom left of the screen.
12+
static var locationAppKit: CGPoint? {
13+
CGEvent(source: nil)?.unflippedLocation
14+
}
1615

17-
/// The coordinate space used by the `CoreGraphics` framework.
18-
///
19-
/// The origin of this coordinate space is at the top left corner of the screen.
20-
case coreGraphics
16+
/// Returns the location of the mouse cursor in the coordinate space used by
17+
/// the `CoreGraphics` framework, with the origin at the top left of the screen.
18+
static var locationCoreGraphics: CGPoint? {
19+
CGEvent(source: nil)?.location
2120
}
2221

2322
/// Hides the mouse cursor and increments the hide cursor count.
@@ -45,21 +44,6 @@ enum MouseCursor {
4544
Logger.mouseCursor.error("CGWarpMouseCursorPosition failed with error \(result.logString)")
4645
}
4746
}
48-
49-
/// Returns the location of the mouse pointer.
50-
///
51-
/// - Parameter coordinateSpace: The coordinate space of the returned location. See
52-
/// the constants defined in ``MouseCursor/CoordinateSpace`` for more information.
53-
static func location(in coordinateSpace: CoordinateSpace) -> CGPoint? {
54-
CGEvent(source: nil).map { event in
55-
switch coordinateSpace {
56-
case .appKit:
57-
event.unflippedLocation
58-
case .coreGraphics:
59-
event.location
60-
}
61-
}
62-
}
6347
}
6448

6549
// MARK: - Logger

0 commit comments

Comments
 (0)