Adding a search bar in main menu - macos

I am using mac os with objective C and XCode.My recent task is to add a search bar in main menu of mac desktop programatically.Could u please suggest me how to proceed with.Thanks

To do this most easily, you require Mac OS X 10.5 or later.
The idea of a single menu bar is an illusion; it is a combination of two things: one that switches with the current application (a list of menus implemented by NSMenu, NSMenuItem, etc.), and one that doesn't change: the system's status bar (NSStatusBar).
If I understand correctly you want to call [NSStatusBar systemStatusBar] to obtain the main status bar (little icons in the corner of the screen) and add something to it, such as an icon that pops a menu.
You can use NSStatusItem to define something for this bar (e.g. to display a menu when clicked) and in your NSMenu definition you can add a view using setView: (this is the part that requires Mac OS X 10.5). The view can be anything, including a search bar.

Related

How do I prevent the menu bar from moving down when my popover is open?

I have an app with a popover that appears on a status bar item. The thing is, when you click on the icon while you're in a full screen app, then move the mouse away from the menu bar to click on something in the popup, the menu bar moves up, and so does the popup. It's annoying.
Anyone know of any way to solve this? I've tried attaching an invisible menu to the popup, but I can't get the menu to be invisible.
Screenshot for clarity, the annoying part is where I wave my mouse around:
The popover window is moving because its parent window is the status item window, and when the parent window moves, the child moves with it. (Before I investigated this, I didn't even know Cocoa had parent and child windows.) I solved the problem with this code immediately after showing the popover:
NSWindow *popoverWindow = self.popup.contentViewController.view.window;
[popoverWindow.parentWindow removeChildWindow:popoverWindow];
Now, the menu bar still moves up, but at least the popup stays in the same place.
Either use Carbon events or watch for things happening to the menu bar (window of type NSStatusBarWindow):
Notifications of type
NSWindowDidChangeOcclusionStateNotification
NSWindowDidMoveNotification
NSWindowWillCloseNotification
NSWindowDidCloseNotification
with an object of class NSStatusBarWindow should give you enough information about the menu bar showing or hiding to add proper handling.
Super-hacky approach:
Custom window with some super-high window level to make it appear over the menu bar, then add a transparent custom view to the new window that catches and handles/blocks mouse clicks according to your needs.
Or:
Get the window instance the popover is using to display and track/handle NSWindowWillMoveNotification / NSWindowDidMoveNotification.
I converted #tbodt's answer to Swift 4 and confirmed that is resolves this issue:
let popoverWindow = popup.contentViewController.view.window as? NSWindow
popoverWindow?.parent?.removeChildWindow(popoverWindow!)

Add NSTitlebarAccessoryViewController to the left of the titlebar/toolbar

I have implemented a unified titlebar/toolbar and now would like to add two buttons to the bar. They need to be located at the far left, just right of the stoplight buttons. I'm creating a toolbar that's very similar to System Preferences.
I have looked at the API for adding NSTitlebarAccessoryViewController and the only valid properties for layoutAttribute are NSLayoutAttributeRight (adds the view to the far right of the toolbar) and NSLayoutAttributeBottom which essentially places the view underneath the unified toolbar in its own toolbar.
I want neither of those options, so how would one add a bunch of buttons to the very left of the unified toolbar?
It is actually a lot easier to archive this:
Just add a toolbar to your window using the Interface Builder and set the title visibility to hidden:
[window setTitleVisibility:NSWindowTitleHidden];
This is now possible in OS X 10.11. Quoting from the header file:
For applications linked on Mac OS 10.11 or later, NSLayoutAttributeLeft is also supported; placing the item on the left side of the window (adjacent and to the right of the close/minimize/maximize buttons).
You can use WAYAppStoreWindow on GitHub to do this. I created a fork of the WAYWindow subproject to vertically centre the document title since this wasn't supported.

How can I get a two-row toolbar like in Mail.app and Xcode?

I'm trying to add a "second row" after my NSToolbar in my app, that remains part of the title bar. As an example, Mail has a thin gray divider line below the NSToolbar with some extras items below that. Very specifically, when the window is put into fullscreen mode, that second "row" stays attached to the title bar as it slides down under the system menu bar. Xcode has a similar story.
I tried setting my NSWindow to textured and placing my second row controls directly in the content view of the window. While this mostly looks correct in windowed mode, those controls of course won't appear attached to the toolbar when it slides down in fullscreen mode. So how can I achieve the same behavior that Mail and Xcode do? I've looked at a lot of toolbar customization code but none of them really cover this specific case.
fullScreenAccessoryView is deprecated in macOS 10.10
In order to do this in recent versions of macOS, use the addTitlebarAccessoryViewController method on your NSWindow and pass in a subclass of NSTitlebarAccessoryViewController.
For example:
NSTitlebarAccessoryViewController *accessoryViewController = [[NSStoryboard storyboardWithName:#"Main" bundle:nil] instantiateControllerWithIdentifier:#"AccessoryViewController"];
[self.mainWindowController.window addTitlebarAccessoryViewController:accessoryViewController];
What I needed to do was call [NSToolbar setFullScreenAccessoryView:] on the view below my toolbar. This results in the behavior I was aiming for. See the NSToolbar documentation for this method.
First one is normal toolbar. For second toolbar you can create a separate view of your desired height and add it in the main landing-window.

How to embed an NSMenu inside a borderless window to create a fake menu bar

I am trying to create a fake menu bar in a cocoa application. I have been able to acomplish 95% of what I need, only the menu bar does not match mainMenu. See screen grab below of what I have so far.
I am using a "Pop Up Button" object in a xib to try to emulate the system menu bar. The problem is that it is a popup menu and does not look exactly like the mainMenu. Additionally, the title menu item does not hilite, and the popup menu has rounded corners that overlap the menu title. So, it's close, but no cigar.
Is it possible to create a fake menu bar inside a borderless window that matches the default system menu bar? If so, how?
I also tried using an NSStatusItem, which by default matches the system menu bar more closely than what I have been able to accomplish with a button, but I was unable to embed NSStatusItem into a window.
Ideally, if there is a way to embed an NSMenu directly in a windows view, that would be the best solution.
You'll need to create a custom button or view that looks and acts like the top-level item, then assign an NSMenu to your view's menu outlet.
You can then either implement menuForEvent: in your view and return your menu when the mouse is down, or alternatively call the NSMenu class method +popUpContextMenu:withEvent:forView: when the mouse is clicked.
This seems to be a slightly odd idea, by the way. Why do you want to do it?

How to mimic Finder left tree in cocoa app?

I really like a look of left tree in Finder (Mac OS X >= 10.5). That with a blue background color and "DEVICES" and "PLACES" dropdowns. I'd like category view in my app to look the same. But since I'm new to Mac OS X app development, I can't figure out if it's just a OutlineView? If so is there a predefined color scheme like in finder?
It's a source list. See the source list section of the Human Interface Guidelines
To make one in IB, you add a vertical split view with a thin divider. Into the left pane you put an outline view and set the highlight style to "source list". That will automatically give you the correct background colour.

Resources