63 lines
2.3 KiB
Swift
63 lines
2.3 KiB
Swift
import Adw
|
|
|
|
extension WidgetView where Target: Adw.AlertDialog {
|
|
/// Adds one response button to the alert dialog.
|
|
///
|
|
/// `AdwAlertDialog` shows no buttons until responses are added.
|
|
///
|
|
/// - Parameters:
|
|
/// - id: The response identifier reported to ``onResponse(_:)``.
|
|
/// - label: The button label. An underscore marks the mnemonic character.
|
|
/// - appearance: Visual appearance. Defaults to ``ResponseAppearance/default``.
|
|
/// - isEnabled: Whether the button is clickable. Defaults to `true`.
|
|
/// - Returns: A copy of this view with the response added at mount time.
|
|
public func addResponse(
|
|
id: String,
|
|
label: String,
|
|
appearance: ResponseAppearance = .default,
|
|
isEnabled: Bool = true
|
|
) -> Self {
|
|
appending { w, _ in
|
|
w.addResponse(id: id, label: label)
|
|
if appearance != .default {
|
|
w.setResponseAppearance(response: id, appearance: appearance)
|
|
}
|
|
if !isEnabled {
|
|
w.setResponseEnabled(response: id, enabled: false)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Adds every response in `responses`, in order.
|
|
///
|
|
/// - Parameter responses: The response buttons to add.
|
|
/// - Returns: A copy of this view with the responses added at mount time.
|
|
public func responses(_ responses: [AlertResponse]) -> Self {
|
|
var result = self
|
|
for response in responses {
|
|
result = result.addResponse(
|
|
id: response.id,
|
|
label: response.label,
|
|
appearance: response.appearance,
|
|
isEnabled: response.isEnabled
|
|
)
|
|
}
|
|
return result
|
|
}
|
|
|
|
/// Runs `handler` with the response identifier each time the dialog emits
|
|
/// `::response`.
|
|
///
|
|
/// Fires for button activations and, on dismissal, for the close response.
|
|
/// The signal handler is disconnected on subtree teardown.
|
|
///
|
|
/// - Parameter handler: Receives the activated response identifier.
|
|
/// - Returns: A copy of this view with the handler connected at mount time.
|
|
public func onResponse(_ handler: @escaping (String) -> Void) -> Self {
|
|
appending { w, ctx in
|
|
ctx.registry.add(w.connectResponse(detail: nil) { _, response in
|
|
handler(response)
|
|
})
|
|
}
|
|
}
|
|
}
|