use super::dialog::{Dialog, DialogSize};
use gpui_kit::{
AnyElement, App, ElementId, FocusHandle, IntoElement, ParentElement, RenderOnce, SharedString,
Window,
};
use std::rc::Rc;
type Action = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct AlertDialog {
dialog: Dialog,
action_text: SharedString,
cancel_text: SharedString,
on_action: Option<Action>,
danger: bool,
}
impl AlertDialog {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
dialog: Dialog::new(id).alert(true).close_on_backdrop(false),
action_text: SharedString::default(),
cancel_text: SharedString::default(),
on_action: None,
danger: true,
}
}
pub fn action_text(mut self, text: impl Into<SharedString>) -> Self {
self.action_text = text.into();
self
}
pub fn cancel_text(mut self, text: impl Into<SharedString>) -> Self {
self.cancel_text = text.into();
self
}
pub fn on_action(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_action = Some(Rc::new(f));
self
}
pub fn danger(mut self, danger: bool) -> Self {
self.danger = danger;
self
}
pub fn open(mut self, open: bool) -> Self {
self.dialog = self.dialog.open(open);
self
}
pub fn default_open(mut self, open: bool) -> Self {
self.dialog = self.dialog.default_open(open);
self
}
pub fn on_open_change(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.dialog = self.dialog.on_open_change(f);
self
}
pub fn trigger(mut self, trigger: impl IntoElement) -> Self {
self.dialog = self.dialog.trigger(trigger);
self
}
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.dialog = self.dialog.title(title);
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.dialog = self.dialog.label(label);
self
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.dialog = self.dialog.description(description);
self
}
pub fn size(mut self, size: DialogSize) -> Self {
self.dialog = self.dialog.size(size);
self
}
pub fn close_label(mut self, label: impl Into<SharedString>) -> Self {
self.dialog = self.dialog.close_label(label);
self
}
pub fn initial_focus(mut self, handle: &FocusHandle) -> Self {
self.dialog = self.dialog.initial_focus(handle);
self
}
fn into_dialog(self) -> Dialog {
let mut dialog = self
.dialog
.danger(self.danger)
.primary_text(self.action_text)
.secondary_text(self.cancel_text);
if let Some(action) = self.on_action {
dialog = dialog.on_primary(move |window, cx| action(window, cx));
}
dialog
}
}
impl ParentElement for AlertDialog {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.dialog.extend(elements);
}
}
impl RenderOnce for AlertDialog {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
self.into_dialog()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn alert_defaults_follow_index_jsx() {
let alert = AlertDialog::new("a").action_text("Delete bucket");
assert!(alert.danger);
let dialog = alert.into_dialog();
assert!(dialog.is_danger());
assert!(dialog.is_alert());
assert!(!dialog.closes_on_backdrop());
}
}