@@ -592,6 +592,15 @@ extension MenuBarItemManager {
592592 return CGPoint ( x: currentFrame. midX, y: currentFrame. midY)
593593 }
594594
595+ /// Returns the target item for the given destination.
596+ ///
597+ /// - Parameter destination: The destination to get the target item from.
598+ private func getTargetItem( for destination: MoveDestination ) -> MenuBarItem {
599+ switch destination {
600+ case . leftOfItem( let targetItem) , . rightOfItem( let targetItem) : targetItem
601+ }
602+ }
603+
595604 /// Returns a Boolean value that indicates whether the given item is in the
596605 /// correct position for the given destination.
597606 ///
@@ -911,12 +920,14 @@ extension MenuBarItemManager {
911920 type: . move( . leftMouseDown) ,
912921 location: CGPoint ( x: currentFrame. midX, y: currentFrame. midY) ,
913922 item: item,
923+ pid: item. ownerPID,
914924 source: source
915925 ) ,
916926 let mouseUpEvent = CGEvent . menuBarItemEvent (
917927 type: . move( . leftMouseUp) ,
918928 location: CGPoint ( x: currentFrame. midX, y: currentFrame. midY) ,
919929 item: item,
930+ pid: item. ownerPID,
920931 source: source
921932 )
922933 else {
@@ -959,24 +970,28 @@ extension MenuBarItemManager {
959970 let startPoint = CGPoint ( x: 20_000 , y: 20_000 )
960971 let endPoint = try getEndPoint ( for: destination)
961972 let fallbackPoint = try getFallbackPoint ( for: item)
973+ let targetItem = getTargetItem ( for: destination)
962974
963975 guard
964976 let mouseDownEvent = CGEvent . menuBarItemEvent (
965977 type: . move( . leftMouseDown) ,
966978 location: startPoint,
967979 item: item,
980+ pid: item. ownerPID,
968981 source: source
969982 ) ,
970983 let mouseUpEvent = CGEvent . menuBarItemEvent (
971984 type: . move( . leftMouseUp) ,
972985 location: endPoint,
973- item: nil ,
986+ item: targetItem,
987+ pid: item. ownerPID,
974988 source: source
975989 ) ,
976990 let fallbackEvent = CGEvent . menuBarItemEvent (
977991 type: . move( . leftMouseUp) ,
978992 location: fallbackPoint,
979- item: nil ,
993+ item: item,
994+ pid: item. ownerPID,
980995 source: source
981996 )
982997 else {
@@ -1140,18 +1155,21 @@ extension MenuBarItemManager {
11401155 type: . click( buttonStates. down) ,
11411156 location: clickPoint,
11421157 item: item,
1158+ pid: item. ownerPID,
11431159 source: source
11441160 ) ,
11451161 let mouseUpEvent = CGEvent . menuBarItemEvent (
11461162 type: . click( buttonStates. up) ,
11471163 location: clickPoint,
11481164 item: item,
1165+ pid: item. ownerPID,
11491166 source: source
11501167 ) ,
11511168 let fallbackEvent = CGEvent . menuBarItemEvent (
11521169 type: . click( buttonStates. up) ,
11531170 location: clickPoint,
11541171 item: item,
1172+ pid: item. ownerPID,
11551173 source: source
11561174 )
11571175 else {
@@ -1577,9 +1595,10 @@ private extension CGEvent {
15771595 /// - Parameters:
15781596 /// - type: The type of the event.
15791597 /// - location: The location of the event. Does not need to be within the bounds of the item.
1580- /// - item: The target item of the event, used to set the event's window. Can be `nil`.
1581- /// - source: The event source.
1582- class func menuBarItemEvent( type: MenuBarItemEventType , location: CGPoint , item: MenuBarItem ? , source: CGEventSource ) -> CGEvent ? {
1598+ /// - item: The target item of the event.
1599+ /// - pid: The target process identifier of the event. Does not need to be the item's `ownerPID`.
1600+ /// - source: The source of the event.
1601+ class func menuBarItemEvent( type: MenuBarItemEventType , location: CGPoint , item: MenuBarItem , pid: pid_t , source: CGEventSource ) -> CGEvent ? {
15831602 let mouseType = type. cgEventType
15841603 let mouseButton = type. mouseButton
15851604
@@ -1589,10 +1608,14 @@ private extension CGEvent {
15891608
15901609 event. flags = type. cgEventFlags
15911610
1611+ let targetPID = Int64 ( pid)
15921612 let userData = Int64 ( truncatingIfNeeded: Int ( bitPattern: ObjectIdentifier ( event) ) )
1593- let windowID = Int64 ( item? . windowID ?? kCGNullWindowID )
1613+ let windowID = Int64 ( item. windowID)
15941614
1615+ event. setIntegerValueField ( . eventTargetUnixProcessID, value: targetPID)
15951616 event. setIntegerValueField ( . eventSourceUserData, value: userData)
1617+ event. setIntegerValueField ( . mouseEventWindowUnderMousePointer, value: windowID)
1618+ event. setIntegerValueField ( . mouseEventWindowUnderMousePointerThatCanHandleThisEvent, value: windowID)
15961619 event. setIntegerValueField ( . windowID, value: windowID)
15971620
15981621 if case . click = type {
0 commit comments