forked from aparoksha/adwaita-swift
Add custom content to alert dialog
This commit is contained in:
parent
776b63f46a
commit
90beecaeb9
@ -45,6 +45,8 @@ extension AnyView {
|
|||||||
/// - visible: Whether the dialog is presented.
|
/// - visible: Whether the dialog is presented.
|
||||||
/// - heading: The heading.
|
/// - heading: The heading.
|
||||||
/// - body: The body text.
|
/// - body: The body text.
|
||||||
|
/// - id: An optional identifier.
|
||||||
|
/// - extraChild: A view with custom content.
|
||||||
public func alertDialog(
|
public func alertDialog(
|
||||||
visible: Binding<Bool>,
|
visible: Binding<Bool>,
|
||||||
heading: String,
|
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.
|
/// Add a dialog to the parent window.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - visible: Whether the dialog is presented.
|
/// - visible: Whether the dialog is presented.
|
||||||
|
|||||||
@ -28,6 +28,8 @@ public struct AlertDialog: AdwaitaWidget {
|
|||||||
var heading: String
|
var heading: String
|
||||||
/// The body text.
|
/// The body text.
|
||||||
var body: String
|
var body: String
|
||||||
|
/// The body view.
|
||||||
|
var extraChild: Body?
|
||||||
/// The available responses.
|
/// The available responses.
|
||||||
var responses: [Response] = []
|
var responses: [Response] = []
|
||||||
/// The child view.
|
/// The child view.
|
||||||
@ -40,18 +42,21 @@ public struct AlertDialog: AdwaitaWidget {
|
|||||||
/// - id: A unique identifier for dialogs on the view.
|
/// - id: A unique identifier for dialogs on the view.
|
||||||
/// - heading: The heading.
|
/// - heading: The heading.
|
||||||
/// - body: The body text.
|
/// - body: The body text.
|
||||||
|
/// - extraChild: The body view.
|
||||||
public init(
|
public init(
|
||||||
visible: Binding<Bool>,
|
visible: Binding<Bool>,
|
||||||
child: AnyView,
|
child: AnyView,
|
||||||
id: String,
|
id: String,
|
||||||
heading: String,
|
heading: String,
|
||||||
body: String
|
body: String,
|
||||||
|
extraChild: Body? = nil
|
||||||
) {
|
) {
|
||||||
self._visible = visible
|
self._visible = visible
|
||||||
self.child = child
|
self.child = child
|
||||||
self.id = id
|
self.id = id
|
||||||
self.heading = heading
|
self.heading = heading
|
||||||
self.body = body
|
self.body = body
|
||||||
|
self.extraChild = extraChild
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Information about a response.
|
/// Information about a response.
|
||||||
@ -126,7 +131,7 @@ public struct AlertDialog: AdwaitaWidget {
|
|||||||
if visible {
|
if visible {
|
||||||
var present = false
|
var present = false
|
||||||
if storage.content[Self.dialogID + id]?.first == nil {
|
if storage.content[Self.dialogID + id]?.first == nil {
|
||||||
createDialog(storage: storage)
|
createDialog(storage: storage, data: data)
|
||||||
present = true
|
present = true
|
||||||
}
|
}
|
||||||
let pointer = storage.content[Self.dialogID + id]?.first?.opaquePointer
|
let pointer = storage.content[Self.dialogID + id]?.first?.opaquePointer
|
||||||
@ -198,11 +203,18 @@ public struct AlertDialog: AdwaitaWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new instance of the dialog.
|
/// Create a new instance of the dialog.
|
||||||
/// - Parameter storage: The wrapped view's storage.
|
/// - Parameters:
|
||||||
func createDialog(storage: ViewStorage) {
|
/// - 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 pointer = adw_alert_dialog_new(nil, nil)
|
||||||
let dialog = ViewStorage(pointer?.opaque())
|
let dialog = ViewStorage(pointer?.opaque())
|
||||||
storage.content[Self.dialogID + id] = [dialog]
|
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.
|
/// Get the identifier of a response which is combined with the dialog's id.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user