diff --git a/Sources/Adwaita/AnyView+.swift b/Sources/Adwaita/AnyView+.swift index a60005f..c455690 100644 --- a/Sources/Adwaita/AnyView+.swift +++ b/Sources/Adwaita/AnyView+.swift @@ -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, 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, + 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. diff --git a/Sources/Core/View/Dialogs/AlertDialog.swift b/Sources/Core/View/Dialogs/AlertDialog.swift index 293d788..0a3e95d 100644 --- a/Sources/Core/View/Dialogs/AlertDialog.swift +++ b/Sources/Core/View/Dialogs/AlertDialog.swift @@ -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, 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.