From 6e266574b0e620ab3f05613c98b8bf3398382c4d Mon Sep 17 00:00:00 2001 From: david-swift Date: Fri, 6 Dec 2024 23:10:08 +0100 Subject: [PATCH] Add window object for closing --- Sources/Core/Window/Window.swift | 38 +++++++++++++++++++++++++------- Sources/Demo/Demo.swift | 2 +- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Sources/Core/Window/Window.swift b/Sources/Core/Window/Window.swift index 887a866..aa2addd 100644 --- a/Sources/Core/Window/Window.swift +++ b/Sources/Core/Window/Window.swift @@ -15,7 +15,7 @@ public struct Window: MacSceneElement { /// The window's identifier. public var id: String /// The window's content. - var content: Body + var content: (MacWindow) -> Body /// Whether an instance of the window type should be opened when the app is starting up. var `open`: Int /// The window's title. @@ -36,14 +36,32 @@ public struct Window: MacSceneElement { /// - open: The number of instances of the window type when the app is starting. /// - content: The window's content. /// - title: The window's title. - public init(_ title: String = "", id: String, `open`: Int = 1, @ViewBuilder content: @escaping () -> Body) { + public init( + _ title: String = "", + id: String, + `open`: Int = 1, + @ViewBuilder content: @escaping (MacWindow) -> Body + ) { self.title = title - self.content = content() + self.content = content self.id = id self.open = open } // swiftlint:enable function_default_parameter_at_end + /// The window type. + public struct MacWindow { + + /// The window. + var window: NSWindow + + /// Close the window. + public func close() { + window.close() + } + + } + /// Set up the initial scene storages. /// - Parameter app: The app storage. public func setupInitialContainers(app: Storage) where Storage: AppStorage { @@ -65,7 +83,8 @@ public struct Window: MacSceneElement { .addObserver(forName: NSWindow.willCloseNotification, object: window, queue: nil) { _ in storage.destroy = true } - let content = content.storage(data: .init(sceneStorage: storage, appStorage: app), type: MacMainView.self) + let content = content(.init(window: window)) + .storage(data: .init(sceneStorage: storage, appStorage: app), type: MacMainView.self) if let pointer = content.pointer as? NSView { window.contentView = pointer } @@ -89,17 +108,17 @@ public struct Window: MacSceneElement { app: Storage, updateProperties: Bool ) where Storage: AppStorage { + guard let window = storage.pointer as? NSWindow else { + return + } if let content = storage.content[.mainContent]?.first { - self.content.updateStorage( + self.content(.init(window: window)).updateStorage( content, data: .init(sceneStorage: storage, appStorage: app), updateProperties: updateProperties, type: MacMainView.self ) } - guard let window = storage.pointer as? NSWindow else { - return - } guard updateProperties else { return } @@ -155,3 +174,6 @@ public struct Window: MacSceneElement { } } + +/// The window type. +public typealias MacWindow = Window.MacWindow diff --git a/Sources/Demo/Demo.swift b/Sources/Demo/Demo.swift index 63b94bb..64df973 100644 --- a/Sources/Demo/Demo.swift +++ b/Sources/Demo/Demo.swift @@ -27,7 +27,7 @@ struct Demo: App { @State private var menuPresented = false var scene: Scene { - Window("Main", id: "main") { + Window("Main", id: "main") { _ in NavigationSplitView { List(elements, selection: $selectedElement) { element in Label(element.id, icon: .system(name: "play.fill"))