use egui::epaint::Shadow;
use egui::{Color32, Context, Frame, Id, Key, Margin, Modal, RichText, Stroke, Ui};
use crate::{close_glyph, primary_button, secondary_button, text_field};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PromptAction {
Submit,
Cancel,
}
pub fn dialog_frame(ctx: &Context) -> Frame {
let visuals = ctx.style_of(ctx.theme()).visuals.clone();
Frame::new()
.fill(visuals.window_fill)
.stroke(Stroke::new(
crate::style::get(ctx).border_width,
visuals.window_stroke.color,
))
.corner_radius(crate::style::get(ctx).dialog())
.inner_margin(Margin::same(18))
.shadow(Shadow {
offset: [0, 8],
blur: 24,
spread: 0,
color: Color32::from_black_alpha(60),
})
}
pub fn popover_frame(ui: &Ui) -> Frame {
Frame::new()
.fill(ui.visuals().window_fill)
.stroke(crate::style::get(ui.ctx()).border(ui.visuals().window_stroke.color))
.corner_radius(crate::style::get(ui.ctx()).container())
.inner_margin(Margin::same(10))
.shadow(Shadow {
offset: [0, 6],
blur: 18,
spread: 0,
color: Color32::from_black_alpha(48),
})
}
pub fn show_popover<R>(ctx: &Context, id_salt: &str, width: f32, add: impl FnOnce(&mut Ui) -> R) -> R {
let screen = ctx.content_rect();
let width = width.min(screen.width() - 24.0);
let position = egui::pos2(
(screen.center().x - width * 0.5).max(screen.left() + 12.0),
screen.top() + 40.0,
);
egui::Area::new(Id::unique(id_salt))
.order(egui::Order::Foreground)
.fixed_pos(position)
.show(ctx, |ui| {
popover_frame(ui)
.show(ui, |ui| {
ui.set_width(width - 20.0);
add(ui)
})
.inner
})
.inner
}
pub fn dialog_header(ui: &mut Ui, title: &str, size: f32, close_tooltip: &str) -> bool {
let mut close = false;
ui.horizontal(|ui| {
ui.label(
RichText::new(title)
.size(size)
.strong()
.color(ui.visuals().text_color()),
);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
close = close_glyph(ui, 14.0, close_tooltip);
});
});
ui.add_space(8.0);
close
}
pub fn dialog_footer(ui: &mut Ui, confirm: &str, cancel: &str, danger: bool) -> Option<PromptAction> {
let mut action = None;
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if primary_button(ui, confirm, danger).clicked() {
action = Some(PromptAction::Submit);
}
ui.add_space(8.0);
if secondary_button(ui, cancel).clicked() {
action = Some(PromptAction::Cancel);
}
});
action
}
pub struct NamePrompt<'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>,
}
pub fn show_name_prompt(ctx: &Context, prompt: &mut NamePrompt<'_>) -> Option<PromptAction> {
let mut action = None;
let dialog = Modal::new(Id::unique("workbench_name_prompt"))
.frame(dialog_frame(ctx))
.show(ctx, |ui| {
ui.set_width(380.0);
if dialog_header(ui, prompt.title, 17.0, prompt.cancel) {
action = Some(PromptAction::Cancel);
}
if !prompt.description.is_empty() {
ui.label(RichText::new(prompt.description).small().weak());
ui.add_space(10.0);
}
let label = ui.label(
RichText::new(prompt.label)
.small()
.color(ui.visuals().weak_text_color()),
);
ui.add_space(3.0);
let response = text_field(ui, prompt.value, "", 344.0).labelled_by(label.id);
if ctx.memory(|memory| memory.focused()).is_none() {
response.request_focus();
}
if let Some(error) = prompt.error {
ui.add_space(5.0);
ui.label(RichText::new(error).small().color(ui.visuals().error_fg_color));
}
ui.add_space(14.0);
action = action.or(dialog_footer(ui, prompt.confirm, prompt.cancel, false));
if ui.input(|input| input.key_pressed(Key::Enter)) {
action = Some(PromptAction::Submit);
}
});
if dialog.backdrop_response.clicked() || ctx.input(|input| input.key_pressed(Key::Escape)) {
action = Some(PromptAction::Cancel);
}
action
}
pub struct ConfirmPrompt<'a> {
pub title: &'a str,
pub message: &'a str,
pub confirm: &'a str,
pub cancel: &'a str,
pub danger: bool,
}
pub fn show_confirm_prompt(ctx: &Context, prompt: &ConfirmPrompt<'_>) -> Option<PromptAction> {
let mut action = None;
let dialog = Modal::new(Id::unique("workbench_confirm_prompt"))
.frame(dialog_frame(ctx))
.show(ctx, |ui| {
ui.set_width(340.0);
if dialog_header(ui, prompt.title, 17.0, prompt.cancel) {
action = Some(PromptAction::Cancel);
}
ui.label(prompt.message);
ui.add_space(14.0);
action = action.or(dialog_footer(ui, prompt.confirm, prompt.cancel, prompt.danger));
});
if dialog.backdrop_response.clicked() || ctx.input(|input| input.key_pressed(Key::Escape)) {
action = Some(PromptAction::Cancel);
}
action
}
pub fn show_choice_window(
ctx: &Context,
title: &str,
message: &str,
buttons: &[&str],
open: &mut bool,
) -> Option<usize> {
let mut choice = None;
if *open && ctx.input(|input| input.key_pressed(Key::Escape)) && !buttons.is_empty() {
choice = Some(buttons.len() - 1);
}
if !*open {
return choice;
}
Modal::new(Id::unique(("workbench_choice_window", title)))
.frame(dialog_frame(ctx))
.show(ctx, |ui| {
ui.set_width(dialog_width(ctx, 380.0));
if dialog_header(ui, title, 17.0, "Close") {
*open = false;
}
ui.add(egui::Label::new(message).wrap());
ui.add_space(16.0);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.spacing_mut().item_spacing.x = 8.0;
for (index, label) in buttons.iter().enumerate() {
let response = if index == 0 {
primary_button(ui, label, false)
} else {
secondary_button(ui, label)
};
if response.clicked() {
choice = Some(index);
}
}
});
});
choice
}
pub fn show_progress(ctx: &Context, title: &str, detail: &str, progress: f32, cancel: &str) -> bool {
let mut cancelled = false;
let progress = progress.clamp(0.0, 1.0);
Modal::new(Id::unique(("workbench_progress", title)))
.frame(dialog_frame(ctx))
.show(ctx, |ui| {
let width = dialog_width(ctx, 400.0);
ui.set_width(width);
ui.label(
RichText::new(title)
.size(crate::style::get(ui.ctx()).font_size(17.0))
.strong()
.color(ui.visuals().text_color()),
);
ui.add_space(8.0);
ui.horizontal(|ui| {
let percent = format!("{:.0}%", progress * 100.0);
ui.add(egui::Label::new(RichText::new(detail).weak()).truncate());
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.label(RichText::new(percent).small().weak());
});
});
ui.add_space(6.0);
ui.scope(|ui| {
let track = ui.visuals().widgets.inactive.bg_stroke.color;
ui.visuals_mut().extreme_bg_color = track;
let fill = ui.visuals().widgets.active.bg_fill;
ui.add(
egui::ProgressBar::new(progress)
.desired_width(width)
.desired_height(6.0)
.corner_radius(crate::style::get(ui.ctx()).tight())
.fill(fill),
);
});
ui.add_space(16.0);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
cancelled = secondary_button(ui, cancel).clicked();
});
});
cancelled
}
fn dialog_width(ctx: &Context, preferred: f32) -> f32 {
preferred
.min(ctx.content_rect().width() - 2.0 * (18.0 + 16.0))
.max(160.0)
}
pub fn banner(ui: &mut Ui, color: Color32, message: &str, actions: impl FnOnce(&mut Ui)) {
Frame::new()
.fill(color.gamma_multiply(0.12))
.stroke(crate::style::get(ui.ctx()).border(color.gamma_multiply(0.35)))
.corner_radius(crate::style::get(ui.ctx()).control())
.inner_margin(Margin::symmetric(10, 5))
.show(ui, |ui| {
ui.set_min_width(ui.available_width());
ui.horizontal(|ui| {
ui.set_min_height(24.0);
ui.label(RichText::new(message).color(color));
ui.add_space(4.0);
actions(ui);
});
});
}