'Dialog opens in another workspace when in MacOS Fullscreen

While in MacOS fullscreen mode, when I open another window, it opens it in the original workspace where the application was launched (Note: FullScreen will create its own workspace).

Here is the simplified code. Please don't mind if its bad as I am new to SwiftUI

// ContentView.swift
import SwiftUI

class MyWindow : NSWindow {
    override func cancelOperation(_ sender: Any?) {
        orderOut(self)
    }
}

struct ContentView: View {
    let window = MyWindow(
        contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
        styleMask: [.borderless, .titled, .closable, .miniaturizable],
        backing: .buffered, defer: false)
    
    var body: some View {
        Button(action: {
            window.isReleasedWhenClosed = false
            window.center()
            window.titlebarAppearsTransparent = true
            window.titleVisibility = .hidden
            window.standardWindowButton(.miniaturizeButton)!.isHidden = true
            window.standardWindowButton(.zoomButton)!.isHidden = true
            window.standardWindowButton(.closeButton)!.isHidden = true

            window.isRestorable = true
            window.level = .normal
            window.makeKeyAndOrderFront(self)
            window.collectionBehavior = [.fullScreenAuxiliary]
        }) {
            Text("Open frameless dialog")
        }
    }
}

//AppDelegate.swift
@main
class AppDelegate: NSObject, NSApplicationDelegate {

    var window: NSWindow!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        
        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()

        // Create the window and set the content view.
        window = NSWindow(
            contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.isReleasedWhenClosed = false
        window.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
    }
}

The exact step to repro the issue:

  1. Open the app
  2. Press "Open Frameless Dialog" button to open the dialog
  3. Close the dialog using ESC key
  4. Enter Fullscreen mode using green traffic light button
  5. While in fullscreen mode, repeat step 2)
  6. Notice the dialog will be opened in the original workspace where the app was launched

I would like to know if there is a way to always ensure the dialog is opened in the same workspace as the fullscreen window. Specifically is there combination of flag for collectionBehavior that can achieve this as in my actual application, it won't be easy to change the window.level

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source