Make onClose easier to understand
Some checks failed
Deploy Docs / publish (push) Has been cancelled
SwiftLint / SwiftLint (push) Has been cancelled

This commit is contained in:
david-swift 2025-10-12 21:53:46 +02:00
parent e430031bbc
commit 037a697c74
2 changed files with 16 additions and 9 deletions

View File

@ -48,8 +48,7 @@ public struct Window: AdwaitaSceneElement {
/// Whether the window uses the development style. /// Whether the window uses the development style.
var devel: Bool? var devel: Bool?
/// Run this function when the window should close. /// Run this function when the window should close.
/// Return true to stop the closing process. var onClose: (() -> CloseConfirmation)?
var onClose: (() -> Bool)?
/// Create a window type with a certain identifier and user interface. /// Create a window type with a certain identifier and user interface.
/// - Parameters: /// - 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. /// Get the actual window template.
/// - Parameter content: The content view.s /// - Parameter content: The content view.s
/// - Returns: The window. /// - Returns: The window.
@ -253,7 +262,7 @@ public struct Window: AdwaitaSceneElement {
} }
} }
if let onClose { if let onClose {
storage.fields["on-close"] = onClose storage.fields["on-close"] = { onClose() == .keep } as (() -> Bool)
} }
storage.previousState = self storage.previousState = self
} }
@ -383,11 +392,9 @@ public struct Window: AdwaitaSceneElement {
} }
/// Run this closure when the window should be closed. /// 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. /// - Returns: The window.
/// public func onClose(onClose: @escaping () -> CloseConfirmation) -> Self {
/// This does not get executed if the user quits the whole application.
public func onClose(onClose: @escaping () -> Bool) -> Self {
var newSelf = self var newSelf = self
newSelf.onClose = onClose newSelf.onClose = onClose
return newSelf return newSelf

View File

@ -91,7 +91,7 @@ struct Demo: App {
@State private var about = false @State private var about = false
@State private var preferences = false @State private var preferences = false
@State private var title: WindowName = .demo @State private var title: WindowName = .demo
@State private var closeAlert = false @State private var closeAlert = false
@State private var destroy = false @State private var destroy = false
var window: AdwaitaWindow var window: AdwaitaWindow
var app: AdwaitaApp! var app: AdwaitaApp!
@ -212,7 +212,7 @@ struct Demo: App {
window window
.size(width: $width, height: $height) .size(width: $width, height: $height)
.maximized($maximized) .maximized($maximized)
.onClose { closeAlert = !destroy; return !destroy } .onClose { closeAlert = !destroy; return destroy ? .close : .keep }
} }
} }