I'm customizing the menus in my Mac Catalyst app. I've added two items, as you can see in the following screen shot ("Set Window Size" and "Open Separate Document Viewer"). But I want to get rid of the default menu items that appear between these two items (the four tab-related functions).
In buildMenuWithBuilder, I can remove some items (child menus) like this:
[builder removeMenuForIdentifier:UIMenuMinimizeAndZoom];
But there is no defined identifier for the child menu containing the tab functions. More significantly, it doesn't yet exist when buildMenuWithBuilder runs. If I put this code at the end of that method...
UIMenu *test = [builder menuForIdentifier:UIMenuWindow];
NSArray *test2 = [test children];
...then test2 contains only two default menus, UIMenuMinimizeAndZoom and UIMenuBringAllToFront, plus my custom menus. So even if I had its identifier, I suspect removing it here would have no effect.
More oddly, it appears between my two custom menus, even though I'm adding those one after the other with insertChildMenu:atStartOfMenuForIdentifier:.
Finally, I noticed that when I open a second scene (in a new Mac window), these tab options disappear from the Window menu and don't come back. It seems that macOS is initially deciding these functions could be relevant to my app, but then when I open a scene, it decides they're not relevant and removes them. So I'm wondering if there is a way to tell macOS from the start not to include these menu items?
I thought this might be related to the NSWindow property tabbingMode, but setting my main window to NSWindowTabbingModeDisallowed makes no difference. My only Mac development experience is through Mac Catalyst, so I don't know what else to try.
I think if you disable tabs completely, they’ll go away. This is a bit of a hack because you need to call AppKit code, but an Apple engineer gave it to me:
Class _nswindow = NSClassFromString(#"NSWindow");
[_nswindow setAllowsAutomaticWindowTabbing:NO];
Related
I want to create a program that consists of just a menu in the status bar (no dock icon or menu bar) that, when the user clicks on menu items, opens a window that allows the user to enter text (NSTextField and NSTextView).
I can make the program background only by setting the "Application is background only" property to YES in my Info.plist file, however, when I do this and display a window, firstly the window always appears behind other windows, and secondly I can't type any text into it (text goes to whatever last had focus - usually a source file in Xcode!)
This is definitely not something as simple as the fields are not enabled. Simply changing the "background only" property to NO fixes the issue, but then I get a dock icon and menu bar which I don't want.
Is what I'm trying to do possible or is there something about the background only mode that means my application can never receive text?
If it is possible what do I need to do to fix this?
From the documentation for LSBackgroundOnly:
You can use this key to create faceless background apps. You should also use this key if your app uses higher-level frameworks that connect to the window server, but are not intended to be visible to users.
So when they say “background only”, they mean background only.
The key you want is LSUIElement. Xcode describes this as “Application is agent (UIElement)”.
I personally can't stand Xcode's default behavior of showing me descriptions of some of the keys' meanings. I recommend turning on “Show Raw Keys/Values”; then, Xcode will show you the real keys being used in the dictionary.
The built in battery, wi-fi, sound, etc, Apple supplied status items, can be dragged to any position on the menu by using cmd + click and drag:
How can I support similar functionality in my own status item?
There are two ways to get menus on the right side of the menu bar. The officially supported way is creating an NSStatusItem in your app. The other ways is creating an NSMenuExtra. You've noticed one difference: NSStatusItem menus are fixed in place, whereas the menu extras can be moved around by cmd-dragging them.
The other major difference, and the reason 3rd-party menu extras are not officially supported, is that unlike status items (which run as separate applications), menu extras are loaded into the address space of the SystemUIServer process. That means if they go haywire and crash, they can take down the UI.
You can see the Menu Extras installed on your system at /System/Library/CoreServices/Menu Extras. It may still be possible to create your own and have the OS load them, but you will run into problems if you try to ship the result. Better to go to bugreport.apple.com and let them know that NSStatusItems ought to be draggable.
Wikipedia has more info on Menu Extras, including some links to a tutorial on building your own.
I have created a NSStatusItem and I have assigned two key equivalents within interface builder (Xcode 4.0). These are for the preference menu cmd-, and the quit option cmd-q. Both of these will work when the Menu is highlighted/open but will not work otherwise even if the application is the foremost. Any ideas on why this is happening or how I can change this?
The operating system passes key events that it doesn't handle to the front application, which compares them to any key equivalents in its main menu and current window. They are not compared to items in the status bar, and since the status bar is actually a different application, it won't matter that your application is in the foreground. When you have the menu open, it is the window in focus. This is why it works in those circumstances.
Since the status item is only active while the menu is open, you need to get the key equivalents using one of two other methods. The first, newer method is to use event taps. I have never used this, so I can't advise on how to set it up. The other, older option, is to use hot keys. This method may become unsupported in the future, but if you are interested I posted an example in this answer.
My guess: your NSMenu is not in the responder chain. If you can figure out how to get it in there, you can probably get this to work.
I have a document based application. Every document can have multiple windows. Every window is automatically added to the "Window" menu. However, they are added in a more or less random and useless order. I would like the window titles to be organized according to the NSDocument they belong to, similar to how XCode or Photoshop do it.
How can I best do that? How can I prevent the default behaviour of AppKit to add all windows to this special menu, and where should I put the code that adds the menu items in the "correct" manner? I don't want to put handlers into every window controller!
It sure does seem like this is something Cocoa should do automatically. I don't know whether it does, but the first thing to check is whether the window controllers are properly connected to their documents. Does your document subclass's windowControllers property contain all the right objects?
If that's no good, then from NSWindow's reference it looks like the only way to prevent a window whose title has been set from being added to the Windows menu is -[NSWindow setExcludedFromWindowsMenu:]. It looks like you'll want to call that on all your windows, then set up an object (perhaps in the MainMenu nib) that takes care of all the windows' positions and grouping in the Windows menu (via NSApplication's methods). You may need to put in special disabled items and the like to get the grouping to look right. I would hope that windows could still be manually added even if you've previously asked them to be excluded.
I am using (a school modified version of) the "Squeak By Example" (SBE) image for a OOP/OOD class. However, my System Browser is missing a few features that appear in SBE. I assume there are some configuration options that can get them back for me, but I can't find them yet.
My questions are:
1) How do I get the buttons back? In the bottom pane there should be a bunch of button (browse, senders, implementors, versions, ..., source). My buttons are missing.
2) How do I get the small workspace area above the buttons to appear? There is supposed to be an area that I can type in, below the top panes, and above the buttons, but it doesn't appear.
Thanks in advance!
Robert
Edit - I did fool around in the Preferences Browser and tried a lot of settings. I managed to make the buttons come back and then later got that small (unknown name) workspace pane back, but I have no idea how. I have tried to systematically turn stuff off again to find out what setting(s) controlled what, but I was unable to determine what controlled either problem. So even though I have it working, I would like some squeak/smalltalk knowledgeable person to let me know how to control these as it might help me learn...
"In the bottom pane there should be a bunch of button (browse, senders, implementors, versions, ..., source). " -- switch on the optionalButtons preference in the preferences browser.
The "area that I can type in, below the top panes, and above the buttons, but it doesn't appear" sounds like the annotation pane - this gives you summary information about the method you're currently viewing, and it's controlled by the annotationPanes preference. Alternatively you may be referring to the Mercury Panel which is used for fast navigation to other classes and methods; this is (of course :-) controlled by the mercuryPanel preference.
If you've been messing around, you may also find that you now have an incorrect system browser selected. Squeak has a choice of browsers which can act as the System Browser. You can choose between them by clicking the menu button on the System Browser and selecting "Choose new default Browser". Open a new browser window to see what effect this has had.
You may also want to try a Pharo image which has everything configured the way you want by default.
Do you have access to the Preferences Browser? It should be in the main system menu. You can alter all sorts of things via this browser, including which buttons appear in teh system browser?