Skip to content

Commit f3e651c

Browse files
committed
Simulate status item click when popping up at menu icon (#201)
This should fix the inaccurate positioning of popup and make it look more native overall. Might have effect on #163
1 parent d8b5d96 commit f3e651c

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

Maccy/Maccy.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ class Maccy: NSObject {
127127
self.menu.popUp(positioning: nil, at: NSPoint(x: topLeftX, y: topLeftY), in: nil)
128128
}
129129
case "statusItem":
130-
if let button = self.statusItem.button, let window = button.window {
131-
self.menu.popUp(positioning: nil, at: window.frame.origin, in: nil)
132-
}
130+
self.simulateStatusItemClick()
133131
default:
134132
self.menu.popUp(positioning: nil, at: NSEvent.mouseLocation, in: nil)
135133
}
@@ -146,13 +144,7 @@ class Maccy: NSObject {
146144
}
147145

148146
withFocus {
149-
if let buttonCell = self.statusItem.button?.cell as? NSButtonCell {
150-
buttonCell.highlightsBy = [.changeBackgroundCellMask, .changeGrayCellMask, .contentsCellMask, .pushInCellMask]
151-
self.statusItem.menu = self.menu
152-
self.statusItem.button?.performClick(self)
153-
self.statusItem.menu = nil
154-
buttonCell.highlightsBy = []
155-
}
147+
self.simulateStatusItemClick()
156148
}
157149
}
158150

@@ -272,6 +264,16 @@ class Maccy: NSObject {
272264
statusItem.button?.title = String(title.prefix(statusItemTitleMaxLength))
273265
}
274266

267+
private func simulateStatusItemClick() {
268+
if let buttonCell = statusItem.button?.cell as? NSButtonCell {
269+
buttonCell.highlightsBy = [.changeBackgroundCellMask, .changeGrayCellMask, .contentsCellMask, .pushInCellMask]
270+
statusItem.menu = menu
271+
statusItem.button?.performClick(self)
272+
statusItem.menu = nil
273+
buttonCell.highlightsBy = []
274+
}
275+
}
276+
275277
// Executes closure with application focus (pun intended).
276278
//
277279
// Beware of hacks. This code is so fragile that you should

0 commit comments

Comments
 (0)