use egui::{Color32, Context, Frame, Id, Key, Modal, RichText, Role, ScrollArea, Sense, Ui, WidgetInfo};
use crate::{close_glyph, dialog_frame, primary_button, switch};
#[derive(Clone, Copy, Debug)]
pub struct SettingsDialog<'a> {
pub title: &'a str,
pub tabs: &'a [&'a str],
pub note: &'a str,
pub done: &'a str,
pub close: &'a str,
}
pub fn show_settings_dialog<R>(
ctx: &Context,
open: &mut bool,
dialog: &SettingsDialog<'_>,
body: impl FnOnce(&mut Ui, usize) -> R,
) -> Option<R> {
if !*open {
return None;
}
let tab_id = Id::unique(("workbench_settings_tab", dialog.title));
let mut tab: usize = ctx.data_mut(|data| *data.get_temp_mut_or_default(tab_id));
let mut keep_open = true;
let mut result = None;
let response = Modal::new(Id::unique("workbench_settings"))
.frame(dialog_frame(ctx))
.show(ctx, |ui| {
let available = ctx.content_rect().size();
let width = (available.x - 48.0)
.clamp(560.0, 620.0)
.min((available.x - 2.0 * (18.0 + 16.0)).max(280.0));
let height = (available.y - 150.0)
.clamp(240.0, 352.0)
.min((available.y - 2.0 * (18.0 + 16.0) - 110.0).max(120.0));
ui.set_min_width(width);
ui.set_max_width(width);
ui.horizontal(|ui| {
ui.label(
RichText::new(dialog.title)
.size(crate::style::get(ui.ctx()).font_size(18.0))
.strong()
.color(ui.visuals().text_color()),
);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if close_glyph(ui, 18.0, dialog.close) {
keep_open = false;
}
});
});
ui.add_space(10.0);
let rail = 128.0;
let content = width - rail - 36.0;
ui.allocate_ui_with_layout(
egui::vec2(width, height),
egui::Layout::left_to_right(egui::Align::Min),
|ui| {
ui.allocate_ui_with_layout(
egui::vec2(rail, height),
egui::Layout::top_down(egui::Align::Min),
|ui| {
ui.set_width(rail);
ui.spacing_mut().item_spacing.y = 2.0;
for (index, label) in dialog.tabs.iter().enumerate() {
if tab_button(ui, label, index == tab) {
tab = index;
}
}
},
);
ui.separator();
ui.add_space(10.0);
ui.allocate_ui_with_layout(
egui::vec2(content, height),
egui::Layout::top_down(egui::Align::Min),
|ui| {
ui.set_width(content);
ScrollArea::vertical()
.auto_shrink([false, false])
.max_width(content)
.max_height(height)
.show(ui, |ui| {
ui.set_width(content - 10.0);
result = Some(body(ui, tab));
});
},
);
},
);
ui.add_space(8.0);
ui.separator();
ui.add_space(8.0);
ui.horizontal(|ui| {
ui.label(RichText::new(dialog.note).small().weak());
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if primary_button(ui, dialog.done, false).clicked() {
keep_open = false;
}
});
});
});
ctx.data_mut(|data| data.insert_temp(tab_id, tab));
if response.backdrop_response.clicked() || ctx.input(|input| input.key_pressed(Key::Escape)) {
keep_open = false;
}
*open = keep_open;
result
}
pub fn settings_section(ui: &mut Ui, title: &str, description: &str) {
ui.label(
RichText::new(title)
.size(crate::style::get(ui.ctx()).font_size(15.0))
.strong()
.color(ui.visuals().text_color()),
);
ui.label(RichText::new(description).small().weak());
ui.add_space(10.0);
}
pub fn settings_row(ui: &mut Ui, title: &str, description: &str, control: impl FnOnce(&mut Ui)) {
Frame::new().inner_margin(egui::Margin::symmetric(0, 7)).show(ui, |ui| {
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.set_width(260.0);
ui.set_max_width(260.0);
ui.label(RichText::new(title).size(crate::style::get(ui.ctx()).font_size(12.5)));
ui.label(RichText::new(description).small().weak());
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), control);
});
});
}
pub fn settings_switch(ui: &mut Ui, title: &str, description: &str, value: &mut bool) -> bool {
let mut changed = false;
settings_row(ui, title, description, |ui| changed = switch(ui, value, title));
changed
}
fn tab_button(ui: &mut Ui, label: &str, selected: bool) -> bool {
let width = ui.available_width().clamp(96.0, 126.0);
let (rect, response) = ui.allocate_exact_size(egui::vec2(width, 28.0), Sense::click());
response.widget_info(|| WidgetInfo::selected(Role::Tab, true, selected, label));
let visuals = ui.visuals();
let fill = if selected {
visuals.selection.bg_fill
} else if response.hovered() {
visuals.widgets.hovered.weak_bg_fill
} else {
Color32::TRANSPARENT
};
ui.painter().rect_filled(rect, crate::style::get(ui.ctx()).item(), fill);
if selected {
let underline = egui::Rect::from_min_max(
egui::pos2(rect.left() + 5.0, rect.bottom() - 2.0),
egui::pos2(rect.right() - 5.0, rect.bottom()),
);
ui.painter().rect_filled(
underline,
crate::style::get(ui.ctx()).mark(),
visuals.selection.stroke.color,
);
}
let color = if selected {
visuals.selection.stroke.color
} else {
visuals.text_color()
};
ui.painter().text(
egui::pos2(rect.right() - 14.0, rect.center().y),
egui::Align2::RIGHT_CENTER,
label,
crate::style::get(ui.ctx()).font(12.5),
color,
);
response.clicked()
}