Reputation: 213
I'm developing a simple menubar app for OS X Yosemite using Swift. What is I need is to show Preferences window (when user clicks on menu item) Window should be hidden at launch, and should be shown when user calls it.
I found an example which implements the same thing that I need: http://www.johnmullins.co/blog/2014/08/08/menubar-app/
This is a piece of code from my app:
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var buildStatusMenu: NSMenu!
@IBOutlet weak var preferencesWindow: NSWindow!
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1)
func applicationDidFinishLaunching(aNotification: NSNotification) {
self.preferencesWindow!.orderOut(self)
}
func showPreferencesWindow(sender: AnyObject?) {
self.preferencesWindow!.orderFront(self)
NSLog("Show window")
}
orderOut works correctly, and I don't see window at launch but when I try to call showPreferencesWindow(), nothing happens. (But I see a record in log) I'm sure there is no magic here, I'm just doing something wrong. Can someone help me? Thanks in advance.
Upvotes: 3
Views: 1929
Reputation: 90551
Normally, an application with LSUIElement
in its Info.plist will not be the active app. The user can't switch to it using Command-Tab or the Dock because it doesn't appear there.
When an inactive app orders a window to the front, it actually doesn't go in front of the active app. That would steal the focus and interrupt the user, which is usually undesirable. Instead, the window ends up behind the front window.
For a menubar app, however, you do want to "steal" focus. The user has just requested one of your app's windows and so it expecting your app to become active. This is one of the rare cases where it's appropriate to pass true to the activateIgnoringOtherApps()
method of NSApplication
. So, you should call NSApplication.sharedApplication().activateIgnoringOtherApps(true)
. That will make sure your window actually ends up in front.
Upvotes: 5