Skip to content

Providing NSWindow via SwiftUI scene.

License

Notifications You must be signed in to change notification settings

Kyome22/WindowSceneKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WindowSceneKit

Providing NSWindow via SwiftUI Scene.

Requirements

  • Development with Xcode 16.0+
  • Written in Swift 6.0
  • Compatible with macOS 14.0+

Usage

Below is an example of displaying a custom NSWindow.

@main
struct SampleApp: App {
    @WindowState("SomeWindowKey") var isPresented = true

    var body: some Scene {
        WindowScene(isPresented: $isPresented, window: { _ in
            CustomWindow(content: { ContentView() })
        })
    }
}
final class CustomWindow: NSWindow {
    init<Content: View>(@ViewBuilder content: () -> Content) {
        super.init(
            contentRect: .zero,
            styleMask: [.titled, .closable, .miniaturizable, .resizable],
            backing: .buffered,
            defer: false
        )
        level = .floating
        collectionBehavior = [.canJoinAllSpaces]
        contentView = NSHostingView(rootView: content())
    }
}

You can toggle the visibility of the specified WindowScene from a remote scope.

// Request to open the specified WindowScene.
WindowSceneMessenger.request(windowAction: .open, windowKey: "SomeWindowKey")

// Request to close the specified WindowScene.
WindowSceneMessenger.request(windowAction: .close, windowKey: "SomeWindowKey")

Additionally, you can submit supplementary information.

WindowSceneMessenger.request(
    windowAction: .open, 
    windowKey: "SomeWindowKey",
    supplements: ["name", "Sakura"]
)

@main
struct SampleApp: App {
    @WindowState("SomeWindowKey") var isPresented = true

    var body: some Scene {
        WindowScene(isPresented: $isPresented, window: { supplements in
            CustomWindow(content: { ContentView(name: supplements["name"] as? String) })
        })
    }
}

Privacy Manifest

This library does not collect or track user information, so it does not include a PrivacyInfo.xcprivacy file.