Add suggested content dialog responses
Some checks failed
SwiftLint / SwiftLint (push) Failing after 5s
Deploy Docs / publish (push) Successful in 30m30s

This commit is contained in:
david-swift 2024-11-13 22:52:14 +01:00
parent 821ba1f006
commit 8ec85b877f
2 changed files with 18 additions and 7 deletions

View File

@ -19,9 +19,9 @@ public struct ContentDialog: WinUIWidget {
/// The body text.
var body: Body
/// The primary response.
var primary: (String, () -> Void)?
var primary: (String, Bool, () -> Void)?
/// The secondary response.
var secondary: (String, () -> Void)?
var secondary: (String, Bool, () -> Void)?
/// The close response.
var close: (String, () -> Void)?
/// The child view.
@ -114,17 +114,24 @@ public struct ContentDialog: WinUIWidget {
/// - storage: The view storage.
func updateDialogProperties(dialog: WinUI.ContentDialog, storage: ViewStorage) {
dialog.title = title
let suggested = Application.current.resources.lookup("AccentButtonStyle") as? Style
if let close {
dialog.closeButtonText = close.0
storage.fields["close-action"] = close.1
}
if let primary {
dialog.primaryButtonText = primary.0
storage.fields["primary-action"] = primary.1
storage.fields["primary-action"] = primary.2
if primary.1 {
dialog.primaryButtonStyle = suggested
}
}
if let secondary {
dialog.secondaryButtonText = secondary.0
storage.fields["secondary-action"] = secondary.1
storage.fields["secondary-action"] = secondary.2
if secondary.1 {
dialog.secondaryButtonStyle = suggested
}
}
}
@ -142,23 +149,27 @@ public struct ContentDialog: WinUIWidget {
/// Set the primary response.
/// - Parameters:
/// - title: The response.
/// - suggested: Whether it is suggested.
/// - action: The action.
public func primaryResponse(
_ title: String,
suggested: Bool = false,
action: @escaping () -> Void
) -> Self {
modify { $0.primary = (title, action) }
modify { $0.primary = (title, suggested, action) }
}
/// Set the secondary response.
/// - Parameters:
/// - title: The response.
/// - suggested: Whether it is suggested.
/// - action: The action.
public func secondaryResponse(
_ title: String,
suggested: Bool = false,
action: @escaping () -> Void
) -> Self {
modify { $0.secondary = (title, action) }
modify { $0.secondary = (title, suggested, action) }
}
}

View File

@ -77,7 +77,7 @@ struct ContentView: View {
}
.contentDialog(visible: $dialog, title: "Sample Dialog")
.closeResponse("Close") { dialog = false }
.primaryResponse("Delete") { dialog = false }
.primaryResponse("Delete", suggested: true) { dialog = false }
}
/// The settings page.