NSDatePicker in NSStatusBar NSSMenuItem not receiving input

余生长醉 提交于 2021-02-08 13:10:54

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/39944383/nsdatepicker-in-nsstatusbar-nssmenuitem-not-receiving-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!