A while back I wrote a simple calendar menu bar widget called Day-O. The calendar uses the stock NSDatePicker nested inside an NSView inside an NSMenuItem in an NSMenu added to an NSStatusItem. The app has an activationPolicy of .accessory
(originally set via the plist's "Application is agent" bool). At some point (maybe with the El Capitan update?) the calendar stopped responding to input. When the app first launches you can click the status bar item to expand the menu and interact with the calendar and it responds to user input as expected. But once the application loses that initial focus the calendar stops responding to input.
I think I've tracked the issue down to the app not activating after initially resigning active. The problem is that if I try to manually activate the app when the menu bar item is clicked (using NSApp.activate(ignoringOtherApps:true)
) the menu (you just opened) is dismissed.
I've also tried telling the window to make the NSDatePicker the first responder but that doesn't seem to have any effect either.
How do I get NSDatePicker in a menu item to accept user input when the status bar app is not the active app?
Edit: Simplest code to illustrate the problem (add this to an AppDelegate in a new macOS Cocoa app):
let statusItem = NSStatusBar.system().statusItem(withLength:NSVariableStatusItemLength)
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSApp.setActivationPolicy(.accessory)
statusItem.button?.title = "Calendar"
let menu = NSMenu()
let item = NSMenuItem()
let picker = NSDatePicker(frame: NSRect(x: 0, y: 0, width: 140, height: 148))
picker.datePickerStyle = .clockAndCalendarDatePickerStyle
item.view = picker
menu.addItem(item)
statusItem.menu = menu
}
Launch the app, click the menu item, and click around the calendar. Then click on another app, then click back on the menu item again. The calendar no longer responds to clicks.
Your NSDatePicker needs to return "acceptsFirstMouse" as true, which is readonly, so you'll to do that in a subclass.
Works in a test project running the code you supplied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With