5

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.

Cristi Băluță
  • 1,295
  • 15
  • 27

2 Answers2

1

This works

func applicationDidFinishLaunching(aNotification: NSNotification) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
       popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
    }
}
Eric
  • 16,003
  • 15
  • 87
  • 139
0

I had this same problem. Just discovered another solution that doesn't require a delay.

func applicationDidFinishLaunching(aNotification: NSNotification) {
    showPopover() // If you only have this, then popover will open in bottom left corner

    hidePopover() // Adding this
    showPopover() // and this will open it from the status bar button.
}
user3225395
  • 581
  • 6
  • 23