use egui::{Context, Id, Key, Modal, Ui};
use crate::widgets::dialogs::{
ConfirmPrompt, NamePrompt, dialog_frame, dialog_header, show_confirm_prompt, show_name_prompt, show_progress,
};
pub struct Dialog<'a> {
id: &'a str,
title: &'a str,
open: &'a mut bool,
width: f32,
close: &'a str,
body_size: f32,
}
impl<'a> Dialog<'a> {
pub fn new(id: &'a str, title: &'a str, open: &'a mut bool) -> Self {
Self {
id,
title,
open,
width: 420.0,
close: "Close",
body_size: 17.0,
}
}
pub fn width(mut self, width: f32) -> Self {
self.width = width;
self
}
pub fn close(mut self, close: &'a str) -> Self {
self.close = close;
self
}
pub fn title_size(mut self, size: f32) -> Self {
self.body_size = size;
self
}
pub fn show<R>(self, ctx: &Context, body: impl FnOnce(&mut Ui) -> R) -> Option<R> {
if !*self.open {
return None;
}
let mut keep_open = true;
let mut result = None;
let modal = Modal::new(Id::unique(self.id))
.frame(dialog_frame(ctx))
.show(ctx, |ui| {
let fit = (ctx.content_rect().width() - 2.0 * (18.0 + 16.0)).max(200.0);
ui.set_width(self.width.min(fit));
if dialog_header(ui, self.title, self.body_size, self.close) {
keep_open = false;
}
result = Some(body(ui));
});
if modal.backdrop_response.clicked() || ctx.input(|input| input.key_pressed(Key::Escape)) {
keep_open = false;
}
*self.open = keep_open;
result
}
}
pub struct ConfirmDialog<'a> {
pub title: &'a str,
pub message: &'a str,
pub confirm: &'a str,
pub cancel: &'a str,
pub danger: bool,
}
impl<'a> ConfirmDialog<'a> {
pub fn new(title: &'a str, message: &'a str) -> Self {
Self {
title,
message,
confirm: "OK",
cancel: "Cancel",
danger: false,
}
}
pub fn confirm(mut self, confirm: &'a str) -> Self {
self.confirm = confirm;
self
}
pub fn cancel(mut self, cancel: &'a str) -> Self {
self.cancel = cancel;
self
}
pub fn danger(mut self, danger: bool) -> Self {
self.danger = danger;
self
}
pub fn show(self, ctx: &Context) -> Option<PromptAction> {
show_confirm_prompt(
ctx,
&ConfirmPrompt {
title: self.title,
message: self.message,
confirm: self.confirm,
cancel: self.cancel,
danger: self.danger,
},
)
}
}
pub struct NameDialog<'a> {
pub title: &'a str,
pub description: &'a str,
pub label: &'a str,
pub value: &'a mut String,
pub confirm: &'a str,
pub cancel: &'a str,
pub error: Option<&'a str>,
}
impl<'a> NameDialog<'a> {
pub fn new(title: &'a str, label: &'a str, value: &'a mut String) -> Self {
Self {
title,
description: "",
label,
value,
confirm: "OK",
cancel: "Cancel",
error: None,
}
}
pub fn description(mut self, description: &'a str) -> Self {
self.description = description;
self
}
pub fn confirm(mut self, confirm: &'a str) -> Self {
self.confirm = confirm;
self
}
pub fn cancel(mut self, cancel: &'a str) -> Self {
self.cancel = cancel;
self
}
pub fn error(mut self, error: Option<&'a str>) -> Self {
self.error = error;
self
}
pub fn show(self, ctx: &Context) -> Option<PromptAction> {
let mut prompt = NamePrompt {
title: self.title,
description: self.description,
label: self.label,
value: self.value,
confirm: self.confirm,
cancel: self.cancel,
error: self.error,
};
show_name_prompt(ctx, &mut prompt)
}
}
pub use crate::widgets::dialogs::PromptAction;
pub struct ProgressDialog<'a> {
pub title: &'a str,
pub detail: &'a str,
pub progress: f32,
pub cancel: &'a str,
}
impl<'a> ProgressDialog<'a> {
pub fn new(title: &'a str, detail: &'a str, progress: f32) -> Self {
Self {
title,
detail,
progress,
cancel: "Cancel",
}
}
pub fn cancel(mut self, cancel: &'a str) -> Self {
self.cancel = cancel;
self
}
pub fn show(self, ctx: &Context) -> bool {
show_progress(ctx, self.title, self.detail, self.progress, self.cancel)
}
}