SettingsAccess icon indicating copy to clipboard operation
SettingsAccess copied to clipboard

macOS 26 Scene Bridging

Open orchetect opened this issue 6 months ago • 0 comments

With the first macOS 26 beta released today, Apple has introduced support for SwiftUI scene bridging in AppKit apps.

https://developer.apple.com/documentation/swiftui/nshostingscenerepresentation

This could use some investigation to see if there is a way to leverage it within a SwiftUI app to allow opening the Settings window globally (outside of the SwiftUI environment).

The documentation even cites the use of the SwiftUI Settings scene with it:

@main
class ApplicationDelegate: NSApplicationDelegate {
    let settingsScene = NSHostingSceneRepresentation {
        Settings {
            SettingsView()
        }
    }

    func applicationWillFinishLaunching(_ notification: Notification) {
        NSApplication.shared.addSceneRepresentation(settingsScene)
    }

    @IBAction func showAppSettings(_ sender: NSMenuItem) {
        settingsScene.environment.openSettings()
    }
}

It's apparent that you would not want to define two Settings scenes (one in the SwiftUI App struct and the other in this manner within the NSApplicationDelegate).

It's unclear if a scene created in this way has a compartmentalized SwiftUI environment to itself, or has access to other scenes in the SwiftUI App struct. The documentation simply says "Scenes defined by the representation can be presented programmatically via the environment actions available on NSHostingSceneRepresentation" which would suggest that it is in fact compartmentalized when accessed through the representation wrapper.

Some testing needs to be done with NSHostingSceneRepresentation to see if it could wrap an unrelated empty/dummy scene in order to access the full environment.

orchetect avatar Jun 10 '25 06:06 orchetect