Add NSWindow in front of fullscreen app - objective-c

I want to have a window in front of all the others apps.
I want this window appear on keyword shortcut like "Alfred" app.
I try a lot of solution but nothing work for having NSWindow in front of fullscreen app on main or second screen.
I tried the classic :
[self.window setLevel:NSMainMenuWindowLevel + 1];
[self.window setCollectionBehavior:NSWindowCollectionBehaviorStationary|NSWindowCollectionBehaviorCanJoinAllSpaces|NSWindowCollectionBehaviorFullScreenAuxiliary];
[self.window setHidesOnDeactivate:NO];
It's work except with Fullscreen app.
It's work with NSPanel with [self.videoWindow setStyleMask:NSBorderlessWindowMask];
but only on main screen.
Anyone have a solution ? Alfred developper around ?

Alfred is implemented as an Agent. Agents can display Windows on spaces of fullscreen Apps. You should also make your App an Agent.
This can be achieved by adding the Application is Agent flag to the info.plist file of your App and setting the value to YES.

I managed to disdplay auxiliary window on top of full screen app via the following:
self.collectionBehavior = NSWindowCollectionBehaviorMoveToActiveSpace | NSWindowCollectionBehaviorFullScreenAuxiliary;
do not call [NSApp activateblabla];
make application LSUIElement, and do not display the Dock icon via TransformProcessType
However, don't know yet how to display it correctly for app which has the Dock icon. From the other side I do not know which app can do it while having the Dock icon.

It may depend on exactly how the application in question achieves fullscreen, but you likely just need to use a higher window level than NSMainMenuWindowLevel + 1 such as CGShieldingWindowLevel()+1
You can download Quartz Debug from Apples developer downloads (required free developer account)
Quartz Debug can tell you the level of any window and all you need to do is set your window to be higher than the one you are having problems with (assuming the app in question isnt doing something odd to be fullscreen).

Related

How can I bring an OSX app to the front?

I'm working on a small, OSX app (it's a menubar app). When I initially build/launch the app, it goes to the foreground and all other apps go to the background (as they should). When I click on another app, that app comes forward and my app goes back (as it should), but when I click back on my app, it doesn't come forward again. The other app stays as the front-most app (regardless of what that other app is).
The big problem with this, aside from it just being weird, is that none of the hover actions or cursors work on my app when it is not the frontmost app.
Is there a way to programmatically force it to the front? How come clicking on it doesn't bring it forward?...
I found the answer in this question. It can be done using either this:
[[NSApplication sharedApplication] activateIgnoringOtherApps : YES];
or this:
[[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];

How do you hide the menu bar in a Cocoa app?

How do you programmatically hide the menu bar in a cocoa app? I would like to make full use of the screen area.
There are two good ways I know of to do this.
1
In Cocoa, you can call the NSMenu class method setMenuBarVisible: to show or hide the menu bar.
As of this writing, the documentation for the NSMenu class does not tell you the following additional information.
The menu bar will only be hidden for the app that calls this method.
The Dock will also be hidden at the same time.
(This is true at least in 10.9 and I have not tested any other versions.)
This is useful when you want to use an app in a full screen way where you have a cover window, a borderless window the size of the screen.
The nice feature of this (as opposed to playing with LSUIElement settings) is that your app can continue to be in the application switcher cycling, as well as visible in the Dock when other apps are active.
This allows users to still activate a full screen app through the Dock or application switcher.
That means you can still use your app's Dock menu to access a preferences window for your app or other features.
This is incredibly convenient if your app is indeed a full screen cover window that runs at a window level higher than other apps, but you still want to make preferences and the ability to quit your app available, and you want your app's visual functionality available when other apps are active.
2
Another option is via NSApplication's method setPresentationOptions: with the arguments from NSApplicationPresentationOptions enum, such as option NSApplicationPresentationHideMenuBar
With this approach be very wary of reading the documentation, although it gives you additional options, and is still app-specific only, you need to know that some of the options are mutually exclusive. There are rules you must follow, or you get nothing but exceptions spewed to the console.
3 There is a 3rd and crappy option. If you have a helper app that is a daemon, you can use it to change your app's LSUIElement state and basically relaunch your app. It's dumb and it takes you out of the app switcher completely, which is great if you really are writing something that should not be there, but that is rare.
There is also the NSView enterFullScreenMode:withOptions: method, although most apps for which that would be appropriate prior to 10.7 should probably use the modern full-screen-window API on 10.7 and later.

Show NSWindow as dropdown from NSStatusItem?

How can I show an NSWindow when clicking on an NSStatusItem, such that it shows over other applications when appearing, but without causing them to lose active status?
I have seen MAAttachedWindow, and it is neat but it does not show on top of other applications unless its own application is active.
I would check out this excellent tutorial and sample project:
Cocoa Popup window in the Status bar
That example shows how to "attach" a window to a status bar item, but you could position the window wherever you want. Basically, if you define your app as a LSUIElement you can display floating utility windows despite your app never activating or showing a dock icon, and other apps still remain key.
if you use MAAttachedWindow, you can set the winow level as NSStatusWindowLevel. Then it will show on top of other application.

How to make an app without titlebar?

I'm quite new to developing on the Mac, I've only done iPhone programming until now. I'm trying to make an app that doesn't have a titlebar. I'd still like to allow the user to move the window around by clicking anywhere within the window and dragging. Is this possible, and if so, how can I get started on something like this? Thanks!
You could make it a borderless window.
[myWindow setStyleMask:NSBorderlessWindowMask];

Cocoa Application with Menubar but no Dock Icon / switch menu

This is yet one more of those "how to switch from running with a dock icon to running without one" questions with a twist.. I don't want the dock icon but I do want a menu bar when the application is at the front. Is that possible?
Running an application with LSUIElement set to 1 in the plist will launch the application without a dock icon, not showing up in the command-tab switch list and without a menu.
You can switch from that mode to the "normal" mode with all three switched on via SetSystemModeUI from 10.2 onwards and via NSApplication setApplicationActivationPolicy since 10.6, but crucially there is no way back to the previous mode (go figure).
So one way around this would be to launch with LSUIElement = 1 and then activate the menu bar when the application gets the focus and deactivate it on the application losing the focus.. alas I can't find a way of doing that.
Can anybody help?
Best regards,
Frank
I too was looking for a solution, but it turned out to be quite simple:
In the project file Info.plist need to add the key
"Application is agent (UIElement)" = YES
Unfortunately, this is not possible. You can only transform the process type in one direction (from a background app to a foreground app) and not the other way.