1use crate::controls::MacInsets;
8use crate::layout::subpixel_snap_rect;
9use crate::theme::MacTheme;
10use egui::{
11 CornerRadius, FontId, InnerResponse, Pos2, Rect, Response, Sense, Stroke, StrokeKind, Ui,
12 UiBuilder, Vec2, WidgetText,
13};
14
15pub struct MacBox<'a> {
36 title: Option<WidgetText>,
37 theme: Option<&'a MacTheme>,
38 fixed_rect: Option<Rect>,
39}
40
41impl<'a> MacBox<'a> {
42 pub fn new(title: impl Into<WidgetText>) -> Self {
44 Self {
45 title: Some(title.into()),
46 theme: None,
47 fixed_rect: None,
48 }
49 }
50
51 pub fn untitled() -> Self {
53 Self {
54 title: None,
55 theme: None,
56 fixed_rect: None,
57 }
58 }
59
60 pub fn theme(mut self, theme: &'a MacTheme) -> Self {
62 self.theme = Some(theme);
63 self
64 }
65
66 pub fn fixed_rect(mut self, rect: Rect) -> Self {
68 self.fixed_rect = Some(rect);
69 self
70 }
71
72 pub fn content_insets(&self) -> MacInsets {
74 let theme_cow = MacTheme::resolve_or_default(self.theme);
75 let m = theme_cow.box_view();
76 MacInsets {
77 top: m.inset_top.points(),
78 bottom: m.inset_bottom.points(),
79 left: m.inset_left.points(),
80 right: m.inset_right.points(),
81 }
82 }
83
84 pub fn corner_radius(&self) -> f32 {
86 let theme_cow = MacTheme::resolve_or_default(self.theme);
87 theme_cow.box_view().corner_radius.points()
88 }
89
90 pub fn border_width(&self) -> f32 {
92 let theme_cow = MacTheme::resolve_or_default(self.theme);
93 theme_cow.box_view().border_width.points()
94 }
95
96 pub fn paint_at(&self, ui: &mut Ui, rect: Rect) {
98 let theme_cow = MacTheme::resolve(self.theme, ui.ctx());
99 let theme: &MacTheme = &theme_cow;
100 let insets = self.content_insets();
101 let corner_r = self.corner_radius();
102 let border_w = self.border_width();
103 let m = theme.box_view();
104 let snapped = subpixel_snap_rect(rect, ui.ctx().pixels_per_point());
105
106 ui.painter().add(egui::epaint::RectShape::new(
107 snapped,
108 CornerRadius::same(corner_r as u8),
109 theme.box_background(),
110 Stroke::new(border_w, theme.box_border()),
111 StrokeKind::Inside,
112 ));
113
114 if let Some(ref title) = self.title {
115 let font_id = FontId::proportional(m.title_font_size.points());
116 let galley = theme.layout_tracked_galley(
117 ui,
118 title.text(),
119 font_id,
120 theme.label(),
121 snapped.width(),
122 );
123 let title_pos = Pos2::new(
124 snapped.min.x + insets.left,
125 snapped.min.y + insets.top * 0.5 - galley.size().y * 0.5,
126 );
127 ui.painter().galley(title_pos, galley, theme.label());
128 }
129 }
130
131 pub fn show<R>(
133 self,
134 ui: &mut Ui,
135 add_contents: impl FnOnce(&mut Ui) -> R,
136 ) -> InnerResponse<R> {
137 let theme_cow = MacTheme::resolve(self.theme, ui.ctx());
138 let theme: &MacTheme = &theme_cow;
139 let insets = self.content_insets();
140 let corner_r = self.corner_radius();
141 let border_w = self.border_width();
142 let title_font_sz = theme.box_view().title_font_size.points();
143
144 let (start_pos, available_w, fixed_box_rect) = if let Some(fixed) = self.fixed_rect {
145 (fixed.min, fixed.width(), Some(fixed))
146 } else {
147 (ui.cursor().min, ui.available_width(), None)
148 };
149
150 let content_max_y = fixed_box_rect.map_or(f32::INFINITY, |r| r.max.y - insets.bottom);
151 let content_rect = Rect::from_min_max(
152 Pos2::new(start_pos.x + insets.left, start_pos.y + insets.top),
153 Pos2::new(start_pos.x + available_w - insets.right, content_max_y),
154 );
155
156 let bg_shape_idx = ui.painter().add(egui::Shape::Noop);
157 let box_scope_id = ui.next_auto_id().with("mac_box_content");
158
159 let mut child_ui = ui.new_child(
160 UiBuilder::new()
161 .id_salt(box_scope_id)
162 .max_rect(content_rect),
163 );
164 let inner = add_contents(&mut child_ui);
165 let content_min_rect = child_ui.min_rect();
166
167 let (rect, response) = if let Some(fixed) = fixed_box_rect {
168 let r = subpixel_snap_rect(fixed, ui.ctx().pixels_per_point());
169 let resp = ui.interact(r, box_scope_id.with("fixed_box"), Sense::hover());
170 (r, resp)
171 } else {
172 let box_h = insets.top + content_min_rect.height() + insets.bottom;
173 let box_rect = Rect::from_min_size(start_pos, Vec2::new(available_w, box_h));
174 let box_rect = subpixel_snap_rect(box_rect, ui.ctx().pixels_per_point());
175 ui.allocate_exact_size(box_rect.size(), Sense::hover())
176 };
177
178 ui.painter().set(
179 bg_shape_idx,
180 egui::epaint::RectShape::new(
181 rect,
182 CornerRadius::same(corner_r as u8),
183 theme.box_background(),
184 Stroke::new(border_w, theme.box_border()),
185 StrokeKind::Inside,
186 ),
187 );
188
189 if let Some(title) = self.title {
190 let galley = theme.layout_tracked_galley(
191 ui,
192 title.text(),
193 FontId::proportional(title_font_sz),
194 theme.label(),
195 available_w,
196 );
197 let title_pos = Pos2::new(
198 rect.min.x + insets.left,
199 rect.min.y + insets.top * 0.5 - galley.size().y * 0.5,
200 );
201 ui.painter().galley(title_pos, galley, theme.label());
202 }
203
204 InnerResponse::new(inner, response)
205 }
206
207 pub fn separator(ui: &mut Ui) -> Response {
209 let theme = MacTheme::resolve(None, ui.ctx());
210 let frame_h = theme.box_view().separator_frame_height.points();
211 let border_w = theme.box_view().border_width.points();
212
213 let (rect, response) =
214 ui.allocate_exact_size(Vec2::new(ui.available_width(), frame_h), Sense::hover());
215 let painter = ui.painter_at(rect);
216 let cy = rect.center().y;
217
218 painter.line_segment(
219 [Pos2::new(rect.min.x, cy), Pos2::new(rect.max.x, cy)],
220 Stroke::new(border_w, theme.separator()),
221 );
222
223 response
224 }
225}