0

You can use following code to bring your app window to focus when a specific event was triggered, e.g. a global hotkey was pressed or a tea timer has expired:

NSApp.activate(ignoringOtherApps: true)
myView?.view.window?.makeKeyAndOrderFront(self);

Problem: If this code executes while another app is the fullscreen mode, the user will be pushed back to the first desktop. This is a bad user experience because it interrupts the user.

How would I present my view just in front of the currently active fullscreen app?

user688262
  • 171
  • 11

2 Answers2

1

You need to use NSWindow.Level to stay above other apps.

Set Window Level:

It controls your apps z-axis. Higher the value goes higher level. There are predefined values like normal, floating and all.

view.window?.level = NSWindow.Level.floating

But you can define your own to achieve much higher level like this

view.window?.level = NSWindow.Level(rawValue: 104) //where 104 is the window level

For more information about NSWindow.Level check here

Set Collection Behaviour:

It is what let your app stays in other apps space. Refer the documentation for detailed explanation check here

view.window?.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]

My above solution will only if you are using NSPanel which is a subclass of NSWindow.

Set Activation Policy:

If you prefer to use NSWindow, you need to additionally set the NSApplication.ActivationPolicy to .accessory like this

NSApplication.shared.setActivationPolicy(NSApplication.ActivationPolicy.accessory)

Activation Policy determines how your app can be activated. For more information check here

Somesh Karthik
  • 498
  • 4
  • 7
0

The solution is the window's property collectionBehaviour and level.

Use the following code to display the window:

Swift 5

NSApp.activate(ignoringOtherApps: true)   
myView.window?.makeKeyAndOrderFront(self)
myView.window?.collectionBehavior = .canJoinAllSpaces
myView.window?.level = NSWindow.Level.tornOffMenu
user688262
  • 171
  • 11