Make onClose easier to understand
This commit is contained in:
parent
e430031bbc
commit
037a697c74
@ -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
|
||||
|
||||
@ -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 }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user