1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! Show method for Dialog — renders a modal overlay.
impl super::widget::Dialog {
/// Shows the dialog when `open` is true. Content closure receives a `&mut Ui`.
pub fn show(self, ctx: &egui::Context, open: &mut bool, content: impl FnOnce(&mut egui::Ui)) {
if !*open {
return;
}
let theme = crate::theme::shadcn_theme_ext::ShadcnThemeExt::shadcn_theme(ctx);
let spacing = crate::responsive::responsive_ext::ResponsiveExt::responsive_spacing(ctx);
// Backdrop
let backdrop_response = crate::widgets::overlay_common::paint_backdrop(
ctx,
"dialog_backdrop",
crate::widgets::overlay_common::BACKDROP_ALPHA,
);
if backdrop_response.clicked() {
*open = false;
ctx.request_repaint();
return;
}
// On mobile the dialog becomes a bottom sheet with side margins;
// on desktop it stays a centered window.
let on_mobile = spacing.is_mobile();
let (anchor, cr) = if on_mobile {
(
egui::Align2::CENTER_BOTTOM,
egui::CornerRadius {
nw: crate::utils::f32_to_u8_clamped(theme.radius + 2.0),
ne: crate::utils::f32_to_u8_clamped(theme.radius + 2.0),
sw: 0,
se: 0,
},
)
} else {
(
egui::Align2::CENTER_CENTER,
crate::paint::corner_radius(theme.radius + 2.0),
)
};
// Frame = content + 2*24 inner margin + 2*1 stroke, so the content
// width must leave room for the frame to stay inside the screen.
// Clamp on desktop as well so narrow viewports (e.g. mobile
// emulation reporting desktop due to DPI) never overflow.
let max_panel_width = crate::widgets::overlay_common::max_panel_width(ctx, &spacing);
let panel_width = if on_mobile {
max_panel_width
} else {
self.width.clamp(0.0, max_panel_width)
};
let _ = egui::Area::new(egui::Id::unique("dialog_panel"))
.order(egui::Order::Foreground)
.anchor(anchor, egui::Vec2::ZERO)
.show(ctx, |inner_ui| {
let frame = egui::Frame::NONE
.fill(theme.background)
.inner_margin(egui::Margin::same(24))
.corner_radius(cr)
.stroke(egui::Stroke::new(1.0, theme.border))
.shadow(egui::Shadow {
offset: [0, 8],
blur: 24,
spread: 0,
color: egui::Color32::from_black_alpha(12),
});
let _ = frame.show(inner_ui, |content_ui| {
content_ui.set_max_width(panel_width);
// Close button
crate::widgets::overlay_common::close_button(content_ui, ctx, &theme, open);
if let Some(title) = self.title {
let _ = content_ui.label(
egui::RichText::new(title)
.color(theme.foreground)
.size(18.0)
.strong(),
);
}
if let Some(desc) = self.description {
content_ui.add_space(4.0);
let _ = content_ui.label(
egui::RichText::new(desc)
.color(theme.muted_foreground)
.size(14.0),
);
}
content_ui.add_space(16.0);
let _ = content_ui.with_layout(
egui::Layout::from_main_dir_and_cross_align(
egui::Direction::TopDown,
egui::Align::Min,
)
.with_main_align(egui::Align::Min),
|ui| {
content(ui);
},
);
});
});
}
}