Add custom content to alert dialog
All checks were successful
Deploy Docs / publish (push) Successful in 22m31s
SwiftLint / SwiftLint (push) Successful in 5s

This commit is contained in:
david-swift 2024-11-27 09:15:35 +01:00
parent 776b63f46a
commit 90beecaeb9
2 changed files with 42 additions and 4 deletions

View File

@ -45,6 +45,8 @@ extension AnyView {
/// - visible: Whether the dialog is presented.
/// - heading: The heading.
/// - body: The body text.
/// - id: An optional identifier.
/// - extraChild: A view with custom content.
public func alertDialog(
visible: Binding<Bool>,
heading: String,
@ -60,6 +62,30 @@ extension AnyView {
)
}
/// Add an alert dialog to the parent window.
/// - Parameters:
/// - visible: Whether the dialog is presented.
/// - heading: The heading.
/// - body: The body text.
/// - id: An optional identifier.
/// - extraChild: A view with custom content.
public func alertDialog(
visible: Binding<Bool>,
heading: String,
body: String = "",
id: String? = nil,
@ViewBuilder extraChild: () -> Body
) -> AlertDialog {
.init(
visible: visible,
child: self,
id: id ?? "no-id",
heading: heading,
body: body,
extraChild: extraChild()
)
}
/// Add a dialog to the parent window.
/// - Parameters:
/// - visible: Whether the dialog is presented.

View File

@ -28,6 +28,8 @@ public struct AlertDialog: AdwaitaWidget {
var heading: String
/// The body text.
var body: String
/// The body view.
var extraChild: Body?
/// The available responses.
var responses: [Response] = []
/// The child view.
@ -40,18 +42,21 @@ public struct AlertDialog: AdwaitaWidget {
/// - id: A unique identifier for dialogs on the view.
/// - heading: The heading.
/// - body: The body text.
/// - extraChild: The body view.
public init(
visible: Binding<Bool>,
child: AnyView,
id: String,
heading: String,
body: String
body: String,
extraChild: Body? = nil
) {
self._visible = visible
self.child = child
self.id = id
self.heading = heading
self.body = body
self.extraChild = extraChild
}
/// Information about a response.
@ -126,7 +131,7 @@ public struct AlertDialog: AdwaitaWidget {
if visible {
var present = false
if storage.content[Self.dialogID + id]?.first == nil {
createDialog(storage: storage)
createDialog(storage: storage, data: data)
present = true
}
let pointer = storage.content[Self.dialogID + id]?.first?.opaquePointer
@ -198,11 +203,18 @@ public struct AlertDialog: AdwaitaWidget {
}
/// Create a new instance of the dialog.
/// - Parameter storage: The wrapped view's storage.
func createDialog(storage: ViewStorage) {
/// - Parameters:
/// - storage: The wrapped view's storage.
/// - data: The widget data.
func createDialog(storage: ViewStorage, data: WidgetData) {
let pointer = adw_alert_dialog_new(nil, nil)
let dialog = ViewStorage(pointer?.opaque())
storage.content[Self.dialogID + id] = [dialog]
if let extraChild {
let child = extraChild.storage(data: data, type: AdwaitaMainView.self)
let childPointer = child.pointer as? OpaquePointer
adw_alert_dialog_set_extra_child(pointer?.cast(), childPointer?.cast())
}
}
/// Get the identifier of a response which is combined with the dialog's id.