Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPopover wrong position sometimes, in El Capitan

I'm building a little status bar app, when i click the icon it shows a NSPopover. All worked fine till i upgraded to El Capitan. The problem is when i first launch the app, i simulate the status bar icon press automatically because i want to see the popover, and it appears in the lower left corner of the screen. After i close it and open again it appears alright from the top of the screen. Do you see anything wrong? This is the code and when i print the values in showPopover there's nothing wrong

private let menu = MenuBarController()

override init() {
    super.init()

    self.menu.onMouseDown = {
        if (self.menu.iconView?.isSelected == true) {
            self.showPopover()
        } else {
            self.hidePopover()
        }
    }
}
func showPopover() {
    let icon = self.menu.iconView!
    let edge = NSRectEdge.MinY
    let rect = icon.frame
    self.popover?.showRelativeToRect(rect, ofView: icon, preferredEdge: edge);
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
    let icon = self.menu.iconView!
    icon.mouseDown(NSEvent())
}

Also, if i simulate the icon press after a small delay it works.

like image 647
Cristi Băluță Avatar asked Nov 10 '22 03:11

Cristi Băluță


1 Answers

This works

func applicationDidFinishLaunching(aNotification: NSNotification) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
       popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
    }
}
like image 62
Eric Avatar answered Nov 15 '22 05:11

Eric