In my menu bar app, I have a menu item which opens a window. The window opens in the front with the window behind it remaining the key window.
I can't get window.makeKey() to work unless I run it after a breakpoint. I tried putting the line of code in different places in the window's life cycle and even put it in DispatchQueue.main.asyncAfter(deadline: execute:) and it didn't work.
I have also tried using window.makeKeyAndOrderFront(sender:). The property window.canBecomeKey is true.
Turns out I needed to put this line in the IBAction for the menu item that opens the window:
NSApplication.shared.activate(ignoringOtherApps: true)
That makes it the key window when it opens, so I don't have to call window.makeKey()
Related
I deleted all the menu items from the Window NSMenu except Minimize and Bring All to Front. However, when I run the app, three more menu items get added: Move Window to Left Side of Screen, Move Window to Right Side of Screen and Enter Full Screen. How do I remove these menu items? Has it got something to do with the attribute inspector of my Window (just a guess)?
For reference:
The title explains it all, when I press tab while writing code, it selects the "Problems" tab instead of adding 4 spaces to my code...
Perhaps you've enabled accessibility mode with Toggle Tab Key Moves Focus command (ctrl+m by default).
Run this command again or click Tab Moves Focus on status bar:
I have a simple Menubar app in which it has 2 options - namely Preferences & Quit
After clicking on Preferences it opens a new window but doesn't show the menu bar
I tried connecting from Main Menu to the Window Controller but it doesn't work.
I drag it from the Application Name to the Window Controller or View Controller using Ctrl+Drag but still it doesn't work.
Another issue is my Preferences Window is resizing even though I unchecked the resize button in the Attributes Inspector.
Even Maximise & Minimise checkboxes are disabled but they are still showing.
Any solutions?
See NSApplication.activationPolicy.
If you want the app's main menu bar to appear, you'll need to change the activationPolicy to NSApplicationActivationPolicyRegular. Once you do that, the app's main menu will appear when your app is the active one. (It will also appear in the dock.)
If you want this to happen only when your preferences window is open, then you'll need to change the activationPolicy when the window opens, then set it back again when it closes.
You don't need to connect any outlets for this; the Main Menu should already be a resource for your app and should be loaded automatically when your app launches.
I think I had given Storyboard Id to my View Controller rather than my Window Controller so it wasn't working.
I also checked some apps like Dropbox which didn't have Main Menu Bar because it was a background or accessory app so I ditched it.
Some apps like Focus have it though. But I went with the popular choice.
As for no resizing, no minimising & no maximising goes, I pasted below code & it worked
var myWindow: NSWindow? = nil
myWindow?.styleMask.remove(NSWindow.StyleMask.resizable)
myWindow?.standardWindowButton(.miniaturizeButton)?.isEnabled=false
myWindow?.standardWindowButton(.zoomButton)?.isEnabled=false
I have my UIElement set to YES in Info.plist. Then I registered a hotkey. When it's pressed, it calls a NSWindowController object and shows a window.
But I must call NSApp.activate(ignoringOtherApps: true) after mainWindowController.showWindow(self) , causing any other windows blur.
I want to create a window that does not change other window's status but can focus on (or receive local keyboard events), like what Alfred or Spotlight does.
When Alfred is called out, Chrome does not blur. When I hit my keyboard, I typed in Alfred instead of Chrome's input field.
When I press [ESC], Alfred hides. Chrome keeps activated and the text field is still focused on.
I added a menu bar icon that opens a popover. It works ok but when I close the app via the red cross and try to open the app again with a click on the dock icon it opens the application window (as it should) but also the menu bar popover (which it should not). How can I only open the application window and not the popover when I click on the dock icon? My code that handles the opening of when clicking the dock icon looks like this:
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag {
for window: AnyObject in sender.windows {
window.makeKeyAndOrderFront(self)
}
}
return true
}
I met this problem today too.
I had the same code so I tried to detect somehow if window is popover but failed.
Also I found that if you close main window after start and then click dock icon, if you print sender.windows, it shows 3 items (in my case): <NSStatusBarWindow: 0x101300110>, <NSStatusBarWindow: 0x101108800>, <NSWindow: 0x6080001e0400>and opens only main window, without popover, but if popover was opened once, then clicking on dock icon (when the main window is closed) causes show of both the main window and the popover. And print(window) in the for cycle now shows 4 items - the last is <_NSPopoverWindow: 0x1011284b0>.
And also I printed popover.isShown, and it says false even after the popover is opened by clicking the dock icon. I couldn't find a way to detect and ignore this particular window.
So the only way I found is to replace
for window: AnyObject in sender.windows {
window.makeKeyAndOrderFront(self)
}
with
sender.windows[2].makeKeyAndOrderFront(self)
because every time the main window appears on the third place
Hope there's a better way and someone will teach us.