diff --git a/Sources/Core/Window/Window.swift b/Sources/Core/Window/Window.swift index cd0066f..92c7266 100644 --- a/Sources/Core/Window/Window.swift +++ b/Sources/Core/Window/Window.swift @@ -48,8 +48,7 @@ public struct Window: AdwaitaSceneElement { /// Whether the window uses the development style. var devel: Bool? /// Run this function when the window should close. - /// Return true to stop the closing process. - var onClose: (() -> Bool)? + var onClose: (() -> CloseConfirmation)? /// Create a window type with a certain identifier and user interface. /// - Parameters: @@ -88,6 +87,16 @@ public struct Window: AdwaitaSceneElement { } + /// Whether to close or keep a window. + public enum CloseConfirmation { + + /// Close the window. + case close + /// Keep the window. + case keep + + } + /// Get the actual window template. /// - Parameter content: The content view.s /// - Returns: The window. @@ -253,7 +262,7 @@ public struct Window: AdwaitaSceneElement { } } if let onClose { - storage.fields["on-close"] = onClose + storage.fields["on-close"] = { onClose() == .keep } as (() -> Bool) } storage.previousState = self } @@ -383,11 +392,9 @@ public struct Window: AdwaitaSceneElement { } /// Run this closure when the window should be closed. - /// - Parameter onClose: The closure. Return true to cancel the closing process. + /// - Parameter onClose: The closure. /// - Returns: The window. - /// - /// This does not get executed if the user quits the whole application. - public func onClose(onClose: @escaping () -> Bool) -> Self { + public func onClose(onClose: @escaping () -> CloseConfirmation) -> Self { var newSelf = self newSelf.onClose = onClose return newSelf diff --git a/Sources/Demo/Demo.swift b/Sources/Demo/Demo.swift index fbbc5eb..6b118c0 100644 --- a/Sources/Demo/Demo.swift +++ b/Sources/Demo/Demo.swift @@ -91,7 +91,7 @@ struct Demo: App { @State private var about = false @State private var preferences = false @State private var title: WindowName = .demo - @State private var closeAlert = false + @State private var closeAlert = false @State private var destroy = false var window: AdwaitaWindow var app: AdwaitaApp! @@ -212,7 +212,7 @@ struct Demo: App { window .size(width: $width, height: $height) .maximized($maximized) - .onClose { closeAlert = !destroy; return !destroy } + .onClose { closeAlert = !destroy; return destroy ? .close : .keep } } }