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 title_fg = theme.label_on(theme.box_background());
117 let galley = theme.layout_tracked_galley(
118 ui,
119 title.text(),
120 font_id,
121 title_fg,
122 snapped.width(),
123 );
124 let title_pos = Pos2::new(
125 snapped.min.x + insets.left,
126 snapped.min.y + insets.top * 0.5 - galley.size().y * 0.5,
127 );
128 ui.painter().galley(title_pos, galley, title_fg);
129 }
130 }
131
132 pub fn show<R>(
134 self,
135 ui: &mut Ui,
136 add_contents: impl FnOnce(&mut Ui) -> R,
137 ) -> InnerResponse<R> {
138 let theme_cow = MacTheme::resolve(self.theme, ui.ctx());
139 let theme: &MacTheme = &theme_cow;
140 let insets = self.content_insets();
141 let corner_r = self.corner_radius();
142 let border_w = self.border_width();
143 let title_font_sz = theme.box_view().title_font_size.points();
144
145 let (start_pos, available_w, fixed_box_rect) = if let Some(fixed) = self.fixed_rect {
146 (fixed.min, fixed.width(), Some(fixed))
147 } else {
148 (ui.cursor().min, ui.available_width(), None)
149 };
150
151 let content_max_y = fixed_box_rect.map_or(f32::INFINITY, |r| r.max.y - insets.bottom);
152 let content_rect = Rect::from_min_max(
153 Pos2::new(start_pos.x + insets.left, start_pos.y + insets.top),
154 Pos2::new(start_pos.x + available_w - insets.right, content_max_y),
155 );
156
157 let bg_shape_idx = ui.painter().add(egui::Shape::Noop);
158 let box_scope_id = ui.next_auto_id().with("mac_box_content");
159
160 let mut child_ui = ui.new_child(
161 UiBuilder::new()
162 .id_salt(box_scope_id)
163 .max_rect(content_rect),
164 );
165 let box_bg = theme.box_background();
166 let inner = theme.with_surface(box_bg, || add_contents(&mut child_ui));
167 let content_min_rect = child_ui.min_rect();
168
169 let (rect, response) = if let Some(fixed) = fixed_box_rect {
170 let r = subpixel_snap_rect(fixed, ui.ctx().pixels_per_point());
171 let resp = ui.interact(r, box_scope_id.with("fixed_box"), Sense::hover());
172 (r, resp)
173 } else {
174 let box_h = insets.top + content_min_rect.height() + insets.bottom;
175 let box_rect = Rect::from_min_size(start_pos, Vec2::new(available_w, box_h));
176 let box_rect = subpixel_snap_rect(box_rect, ui.ctx().pixels_per_point());
177 ui.allocate_exact_size(box_rect.size(), Sense::hover())
178 };
179
180 ui.painter().set(
181 bg_shape_idx,
182 egui::epaint::RectShape::new(
183 rect,
184 CornerRadius::same(corner_r as u8),
185 box_bg,
186 Stroke::new(border_w, theme.box_border()),
187 StrokeKind::Inside,
188 ),
189 );
190
191 if let Some(title) = self.title {
192 let title_fg = theme.label_on(box_bg);
193 let galley = theme.layout_tracked_galley(
194 ui,
195 title.text(),
196 FontId::proportional(title_font_sz),
197 title_fg,
198 available_w,
199 );
200 let title_pos = Pos2::new(
201 rect.min.x + insets.left,
202 rect.min.y + insets.top * 0.5 - galley.size().y * 0.5,
203 );
204 ui.painter().galley(title_pos, galley, title_fg);
205 }
206
207 InnerResponse::new(inner, response)
208 }
209
210 pub fn separator(ui: &mut Ui) -> Response {
212 let theme = MacTheme::resolve(None, ui.ctx());
213 let frame_h = theme.box_view().separator_frame_height.points();
214 let border_w = theme.box_view().border_width.points();
215
216 let (rect, response) =
217 ui.allocate_exact_size(Vec2::new(ui.available_width(), frame_h), Sense::hover());
218 let painter = ui.painter_at(rect);
219 let cy = rect.center().y;
220
221 painter.line_segment(
222 [Pos2::new(rect.min.x, cy), Pos2::new(rect.max.x, cy)],
223 Stroke::new(border_w, theme.separator()),
224 );
225
226 response
227 }
228}