Skip to content

Commit

Permalink
Update MenuBarItemManager.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Jan 14, 2025
1 parent ee38def commit bd4ff51
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions Ice/MenuBar/MenuBarItems/MenuBarItemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,6 @@ extension MenuBarItemManager {
return CGPoint(x: currentFrame.midX, y: currentFrame.midY)
}

/// Returns the target item for the given destination.
///
/// - Parameter destination: The destination to get the target item from.
private func getTargetItem(for destination: MoveDestination) -> MenuBarItem {
switch destination {
case .leftOfItem(let targetItem), .rightOfItem(let targetItem): targetItem
}
}

/// Returns a Boolean value that indicates whether the given item is in the
/// correct position for the given destination.
///
Expand Down Expand Up @@ -920,14 +911,12 @@ extension MenuBarItemManager {
type: .move(.leftMouseDown),
location: CGPoint(x: currentFrame.midX, y: currentFrame.midY),
item: item,
pid: item.ownerPID,
source: source
),
let mouseUpEvent = CGEvent.menuBarItemEvent(
type: .move(.leftMouseUp),
location: CGPoint(x: currentFrame.midX, y: currentFrame.midY),
item: item,
pid: item.ownerPID,
source: source
)
else {
Expand Down Expand Up @@ -970,28 +959,24 @@ extension MenuBarItemManager {
let startPoint = CGPoint(x: 20_000, y: 20_000)
let endPoint = try getEndPoint(for: destination)
let fallbackPoint = try getFallbackPoint(for: item)
let targetItem = getTargetItem(for: destination)

guard
let mouseDownEvent = CGEvent.menuBarItemEvent(
type: .move(.leftMouseDown),
location: startPoint,
item: item,
pid: item.ownerPID,
source: source
),
let mouseUpEvent = CGEvent.menuBarItemEvent(
type: .move(.leftMouseUp),
location: endPoint,
item: targetItem,
pid: item.ownerPID,
item: nil,
source: source
),
let fallbackEvent = CGEvent.menuBarItemEvent(
type: .move(.leftMouseUp),
location: fallbackPoint,
item: item,
pid: item.ownerPID,
item: nil,
source: source
)
else {
Expand Down Expand Up @@ -1155,21 +1140,18 @@ extension MenuBarItemManager {
type: .click(buttonStates.down),
location: clickPoint,
item: item,
pid: item.ownerPID,
source: source
),
let mouseUpEvent = CGEvent.menuBarItemEvent(
type: .click(buttonStates.up),
location: clickPoint,
item: item,
pid: item.ownerPID,
source: source
),
let fallbackEvent = CGEvent.menuBarItemEvent(
type: .click(buttonStates.up),
location: clickPoint,
item: item,
pid: item.ownerPID,
source: source
)
else {
Expand Down Expand Up @@ -1595,10 +1577,9 @@ private extension CGEvent {
/// - Parameters:
/// - type: The type of the event.
/// - location: The location of the event. Does not need to be within the bounds of the item.
/// - item: The target item of the event.
/// - pid: The target process identifier of the event. Does not need to be the item's `ownerPID`.
/// - source: The source of the event.
class func menuBarItemEvent(type: MenuBarItemEventType, location: CGPoint, item: MenuBarItem, pid: pid_t, source: CGEventSource) -> CGEvent? {
/// - item: The target item of the event, used to set the event's window. Can be `nil`.
/// - source: The event source.
class func menuBarItemEvent(type: MenuBarItemEventType, location: CGPoint, item: MenuBarItem?, source: CGEventSource) -> CGEvent? {
let mouseType = type.cgEventType
let mouseButton = type.mouseButton

Expand All @@ -1608,14 +1589,10 @@ private extension CGEvent {

event.flags = type.cgEventFlags

let targetPID = Int64(pid)
let userData = Int64(truncatingIfNeeded: Int(bitPattern: ObjectIdentifier(event)))
let windowID = Int64(item.windowID)
let windowID = Int64(item?.windowID ?? kCGNullWindowID)

event.setIntegerValueField(.eventTargetUnixProcessID, value: targetPID)
event.setIntegerValueField(.eventSourceUserData, value: userData)
event.setIntegerValueField(.mouseEventWindowUnderMousePointer, value: windowID)
event.setIntegerValueField(.mouseEventWindowUnderMousePointerThatCanHandleThisEvent, value: windowID)
event.setIntegerValueField(.windowID, value: windowID)

if case .click = type {
Expand Down

0 comments on commit bd4ff51

Please sign in to comment.