4

I have an NSPopover connected to the NSView of a window. Currently I have a NSStatusItem that displays a NSStatusMenu. When you click a certain option in that menu I set the menu to nil and then display the NSPopover. The problem is I want the status bar button to remain highlighted when the NSPopover is displayed, but it only flashes highlight when I click the button to open the NSPopover. I have tried statusItem.button?.highlight(true) to no avail, and it seems changing the button type does not do anything either. Any ideas? Thanks. Also, any way to make the NSView inside the popover or more specifically the text field in the NSView selected once the NSPopover is opened? I have the popover behavior set to transient but it will only close if you click on the popover first, then outside the popover.

@IBOutlet weak var mainMenu: NSMenu!
@IBOutlet weak var popover: NSPopover!
@IBOutlet weak var popoverView: NSView!
@IBOutlet weak var textField: NSTextField!

// init new menu bar item
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)

func applicationDidFinishLaunching(aNotification: NSNotification) {

    // init menu bar item icon
    let icon = NSImage(named: "menuIcon")
    icon?.setTemplate(true)         // now compatible with "dark mode"
    statusItem.image = icon
    statusItem.menu = mainMenu
    popover.behavior = NSPopoverBehavior.Transient
    //statusItem.button?.setButtonType(NSButtonType.OnOffButton)
}

@IBAction func StatusItemClicked(sender: NSButton) {
    if !(popover.shown) {
        popover.showRelativeToRect(sender.bounds, ofView: statusItem.button!, preferredEdge: NSMinYEdge)
    }
    else {
        popover.close()
    }
}

@IBAction func movieRegular(sender: NSMenuItem) {
    statusItem.menu = nil                                   // get rid of statusItem menu
    statusItem.action = Selector("StatusItemClicked:")      // func StatusItemClicked called when button clicked
    StatusItemClicked(statusItem.button!)                   // call it so popover immediately displays first time
}
alexk403
  • 93
  • 1
  • 5
  • possible duplicate of [NSStatusBarButton keep highlighted](http://stackoverflow.com/questions/26004684/nsstatusbarbutton-keep-highlighted) – Valentin Shergin May 20 '15 at 09:46

0 Answers0