How can I keep an NSPopUpButton open after the user selects a menu item? - objective-c

I have an NSPopUpButton providing the NSMenu for a status item with a custom view. The popup button displays a list of links. When the user selects a link from the list, the link is displayed in the user's browser (in the background).
Naturally, the menu closes every time the user selects a link.
I would like to change this: I want the menu to stay open while the user clicks on various links, all of which can be opened in the background. The menu can then go away when the user clicks elsewhere.
How can this be accomplished? Should I subclass NSMenuItem and intercept the mouse clicks somehow? Overlay a transparent NSView on the popped-up menu and, again, intercept the clicks somehow? I make these suggestions blithely, but I would have trouble implementing either of these...pointers to the right methods for override would be appreciated.

Instead of using a menu, one might use a collapsible box.I have seen that in many apps ( also provided by Apple) , so I guess this is the recommended style guide for multiple selections.
The collapsible box expands when you click the disclosure button, and it gives free all items desired - like a tableview with checkboxes.
Views below this box must move down in this case, not to interfere with the box.
Clicking again on the disclosure button will shrink the box back to its origin. The effect is similar to closing a menu.

Usually you should not bend a control too far past it's original intent. Users expect pop up buttons to close after making a selection. I don't think you should, or can, force NSPopUpButtonCell to behave in this way. If you do, you'll be subclassing and modifying the control so heavily that it might change/break with a future version of Mac OS X. You'd also have to worry about the usability problem of users thinking the menu will close after making a selection.
You might consider writing you're own subclass of NSView to work like the menu button you're describing. After the user clicks on the button. You'll want to create a new NSWindow, with no border by using NSBorderlessWindowMask as the style mask. The content view of that window should be another custom view of yours that you implement the menu selection in.

Related

UIFocusGuide: only forward if system has no solution

I have a wide "dock" area at the bottom of the screen. I have placed a UIFocusGuide across the top of it. Depending on which element in the dock has focus, the system may or may not have a focus solution if the user swipes up.
How can I tell my focus guide to use the system-calculated new focus item if there is one, but if not, it should use the one configured in my setPreferredFocusEnvironments:
Alternatively, is there a way to determine where a "swipe up" will take the focus without actually doing it? As the focused element in the dock changes, I could check this and if it has no destination provided by the system, I can add one.
My dock is a view within a main view controller but preferredFocusEnvironments is never called when the user tries to move to a non-existent view from an element in the dock.

How to create a NSWindow like iTunes mini player (always on top but get no focus!)

I have a borderless window with a webview in it, which is always on top.
I want to create a NSWindow which is:
Always on top
Does not take the focus from the current foreground app
Does enable user mouse interaction (without forcing the user to switch focus to it - aka "click on it twice")
The problem I have atm is that to interact with the window (e.g. see hover effect, or click a link in the webview), the user has to click the window (which gives it focus) and only then the hover effect shows.
How can I make a window like iTunes mini player which doesn't take the focus from the current app - but also interacts with mouse? (see screenshots below)
Thanks!!
To receive mouseEntered:, mouseMoved: and mouseExited: events even when your app is not active, you must add a tracking area to some view in your window and set the tracking area's properties accordingly.
Take a look at NSTrackingArea.
You'll probably want to add a tracking area with the options NSTrackingActiveAlways and NSTrackingMouseEnteredAndExited.

How to get the Spotlight-like text input effect in menu bar?

I want to have an icon in the menubar in my Mac app - and the icon should spawn a menu upon clicking. While having more entries in the menu, I would like to have a top row as a universal text entry field - like it is in Spotlight:
http://dl.dropbox.com/u/3943878/_mine/Screen%20shot%202011-07-16%20at%2012.29.18.png
Is it possible to add such a field to NSMenu? Or should I do it as a panel-type window?
If you're using xcode 4 , make a custom view in interface builder and add a textfield or anything you want to it. In IB also drag and drop a "Menu" from the objects library with as many items as you want in it. Then simply ctrl+click the menu item you want to make into the text field (In your case it would be the top one) and drag to the custom view and select "view". Now when you open the menu, instead of showing a menu item in that space, it shows whatever was in your custom view.
EDIT: As for your comment here's what you should do. Make your menu an outlet by opening the assistant editor view and ctrl+click from your menu to the header file that you want to use. now, simply make a method that will run whenever the menu will open, conveniently apple already made this, it's called menuWillOpen.
- (void)menuWillOpen: nameOfYourMenu{
[self performSelector:#selector(methodExecutedWhenMenuIsClicked) withObject:nil afterDelay:0.0 inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
the delay at 0 will make it happen immediately, it must be done in the common modes run loop so that the menu will be updated even while it's open. Now just make the methodExecutedWhenMenuIsClicked and set it so the text field responds.
- (void)methodExecutedWhenMenuIsClicked{
[[yourTextfiled window] makeFirstResponder:yourTextField];
You can put any view in a menu using -[NSMenuItem setView:]. See the long comment in NSMenuItem.h and the section Views in Menus in Application Menu and Pop-up List Programming Topics.
You're probably going to struggle quite a bit. I just tried doing the same thing, and reading the Views in Menus in Application Menu and Pop-up List Programming Topics document referenced by Ahruman, I found this:
A view in a menu item can receive all mouse events as normal, but keyboard events are not supported. During “non-sticky” menu tracking (that is, manipulating menus with the mouse button held down), a view in a menu item receives mouseDragged: events.
I think we're SOL. Apparently Spotlight pops up a borderless window instead.

Is there a way to control the NSMenuItem item that is initially highlighted when opening a menu?

I'm working on the details of a symbols pop up button, similar to what Xcode 3 at the top of its editor window.
My controller object is the delegate of the NSMenu that is shown when the NSPopUpButton is shown. I implement the two methods numberOfItemsInMenu: and menu:updateItem:atIndex:shouldCancel: to populate the menu right before it's about to be displayed.
However, the title and image of the selected NSMenuItem have to change each time the user changes the selection in the editor NSTextView, or makes changes to the text; just as is the case with Xcode.
The problem I'm having is when the user goes to click on the NSPopUpButton to display the NSMenu, the selected NSMenuItem and the item that should be selected do not match up, since the menu doesn't have the proper number of items yet.
I'm wondering if there is a way to control which NSMenuItem is initially highlighted and tracked when the user clicks to open the menu. As it stands, the first item it always highlighted and tracked or, if the user had previously selected a item, that item is highlighted and tracked.
I tried explaining as best I could, but here is a image illustrating my problem:
I want the highlighted item to be the same as the selected item when the user opens the menu. Any ideas?
I would use a NSPopupbutton - it can do what you want. maybe you even hide it?
I am not very sure that I understood your problem but If we can add tags to these menu Items.
e.g.
[mMenuItemOne setTag : 1];
[mMenuItemTwo setTag : 2];
[mMenuItemThree setTag : 3];
[mMenuItemFour setTag : 4];}
we can select any Menu item using
[_popUp selectItemWithTag: _selectedItem];
Just a hint not a full solution.
Try to post an NSMouseMoved event to your app right after the menu shown.
Main problem is here to detect the position of the item you want to be highlighted.
Just a starting point.
Have you tried this NSMenuDelegate method:Handling Highlighting
– menu:willHighlightItem:
NSMenuDelegate Protocol Reference
Also you can store the NSMenuItem index in some var to keep track of the selected item for later use.
I'm not sure, do you want to move the mouse selection highlight (the blue stuff) or the checkmark?
In general, the checkmark is the thing you want to change. You can do so from your validateMenuItem: method (or at any other time if your item is set not to take part in validation) using the -setState: method.
The blue highlight is an indicator of the user's keyboard input or mouse location, and you should not mess with it. Changing it does not make any sense, as it would be changed back the moment the user moves the mouse even a single pixel.
That said, you can set the selectedItem of the NSPopUpButton, which will cause the entire menu to be moved so the selectedItem is under the mouse (assuming the mouse just clicked the popup button).
If your menu delegate method is called after the pop up button has decided what item to select, you can't use it. Instead, you could probably set the selectedItem and menu of the popup button from a NSPopUpButtonWillPopUpNotification handler.
If that is also called too late, you'll probably have to subclass NSPopupButtonCell and do it in an override of -attachPopUpWithFrame:inView: (I think that's the spot that should also work when you don't click and just hit the space key while the popup button is selected).

How would you make a Status Item show or Hide a window when clicked?

How would make a Status Item when the actually button is clicked in the Menu Bar not in a drop down menu show or hide a window?
Sorry if this is a bit vague.
NSStatusItem supports the target/action mechanism like many other controls. I haven't used this myself -- I've only ever used an NSStatusItem with a menu attached -- so I don't know when the message is sent (i.e. when the mouse button is clicked or when it's released). If it doesn't do what you want by default, you would need a custom view like Daniel suggests.
To achieve this with NSStatusItem you need to create a custom view and replace the default NSStatusItem view by calling its "setView:" method.
You'll implement code in your custom view to react to mouse clicks by e.g. putting up a window. (You can use a button, or other standard views if it works best for you).
I'll warn you this is a bit tricky to get right. Lots of little nuances e.g. with getting the look of your custom view to look right in the menu bar. But this is the general approach you need to take if you want to override the default menu-prompting status item view.