1use crate::theme::MacTheme;
7use egui::{
8 Align, CornerRadius, CursorIcon, Id, Layout, Pos2, Rect, Stroke, StrokeKind, Ui, UiBuilder,
9 Vec2,
10};
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
14pub enum MacSplitOrientation {
15 #[default]
17 Horizontal,
18 Vertical,
20}
21
22pub struct MacSplitView<'a> {
47 id: Id,
48 orientation: MacSplitOrientation,
49 pos_override: Option<&'a mut f32>,
50 theme: Option<&'a MacTheme>,
51 min_first: Option<f32>,
52 max_first: Option<f32>,
53 min_second: Option<f32>,
54 collapsed_first: bool,
55 collapsed_second: bool,
56 show_handle: bool,
57 default_ratio: Option<f32>,
58}
59
60impl<'a> MacSplitView<'a> {
61 pub fn new(
63 id_source: impl std::hash::Hash + std::fmt::Debug,
64 orientation: MacSplitOrientation,
65 ) -> Self {
66 let id = Id::NULL.with(id_source);
67 Self {
68 id,
69 orientation,
70 pos_override: None,
71 theme: None,
72 min_first: None,
73 max_first: None,
74 min_second: None,
75 collapsed_first: false,
76 collapsed_second: false,
77 show_handle: true,
78 default_ratio: None,
79 }
80 }
81
82 pub fn with_pos(
84 id_source: impl std::hash::Hash + std::fmt::Debug,
85 orientation: MacSplitOrientation,
86 pos: &'a mut f32,
87 ) -> Self {
88 let mut view = Self::new(id_source, orientation);
89 view.pos_override = Some(pos);
90 view
91 }
92
93 pub fn theme(mut self, theme: &'a MacTheme) -> Self {
95 self.theme = Some(theme);
96 self
97 }
98
99 pub fn min_first(mut self, min: f32) -> Self {
101 self.min_first = Some(min);
102 self
103 }
104
105 pub fn max_first(mut self, max: f32) -> Self {
107 self.max_first = Some(max);
108 self
109 }
110
111 pub fn min_second(mut self, min: f32) -> Self {
113 self.min_second = Some(min);
114 self
115 }
116
117 pub fn collapsed_first(mut self, collapsed: bool) -> Self {
119 self.collapsed_first = collapsed;
120 self
121 }
122
123 pub fn collapsed_second(mut self, collapsed: bool) -> Self {
125 self.collapsed_second = collapsed;
126 self
127 }
128
129 pub fn show_handle(mut self, show: bool) -> Self {
131 self.show_handle = show;
132 self
133 }
134
135 pub fn default_ratio(mut self, ratio: f32) -> Self {
137 self.default_ratio = Some(ratio.clamp(0.0, 1.0));
138 self
139 }
140
141 pub fn split_panes(&mut self, ui: &mut Ui) -> (Rect, Rect) {
144 let fallback_theme;
145 let theme = match self.theme {
146 Some(t) => t,
147 None => {
148 fallback_theme = MacTheme::from_ctx(ui.ctx());
149 &fallback_theme
150 }
151 };
152
153 let metrics = theme.split_view();
154 let divider_thickness = metrics.divider_thickness.points();
155 let hit_thickness = metrics.hit_thickness.points();
156 let min_pane_size = metrics.min_pane_size.points();
157 let handle_len = metrics.handle_length.points();
158 let handle_th = metrics.handle_thickness.points();
159
160 let container_rect = ui.available_rect_before_wrap();
161 let total_span = match self.orientation {
162 MacSplitOrientation::Horizontal => container_rect.width(),
163 MacSplitOrientation::Vertical => container_rect.height(),
164 };
165
166 let default_ratio = self
167 .default_ratio
168 .unwrap_or_else(|| metrics.default_ratio.points());
169 let mut pos = if let Some(ref p) = self.pos_override {
171 **p
172 } else {
173 ui.data_mut(|d| *d.get_temp_mut_or_insert_with(self.id, || total_span * default_ratio))
174 };
175
176 let min_1 = self.min_first.unwrap_or(min_pane_size);
177 let min_2 = self.min_second.unwrap_or(min_pane_size);
178 let max_1 = self
179 .max_first
180 .unwrap_or(total_span - min_2)
181 .min(total_span - min_2);
182 pos = pos.clamp(min_1, max_1.max(min_1));
183
184 let (first_rect, second_rect, divider_line, handle_rect) = if self.collapsed_first {
185 let r2 = container_rect;
186 let r1 = Rect::from_min_size(container_rect.min, Vec2::ZERO);
187 (r1, r2, None, None)
188 } else if self.collapsed_second {
189 let r1 = container_rect;
190 let r2 = Rect::from_min_size(container_rect.max, Vec2::ZERO);
191 (r1, r2, None, None)
192 } else {
193 match self.orientation {
194 MacSplitOrientation::Horizontal => {
195 let hit_rect = Rect::from_min_max(
196 Pos2::new(
197 container_rect.left() + pos - hit_thickness * 0.5,
198 container_rect.top(),
199 ),
200 Pos2::new(
201 container_rect.left() + pos + hit_thickness * 0.5,
202 container_rect.bottom(),
203 ),
204 );
205 let hit_resp = ui.interact(
206 hit_rect,
207 self.id.with("split_drag"),
208 egui::Sense::click_and_drag(),
209 );
210 if hit_resp.hovered() || hit_resp.dragged() {
211 ui.output_mut(|o| o.cursor_icon = CursorIcon::ResizeHorizontal);
212 }
213 if hit_resp.dragged() {
214 pos += hit_resp.drag_delta().x;
215 pos = pos.clamp(min_1, max_1.max(min_1));
216 }
217
218 let split_x = container_rect.left() + pos;
219 let divider_center_x = split_x + divider_thickness * 0.5;
220 let r1 = Rect::from_min_max(
221 container_rect.min,
222 Pos2::new(split_x.min(container_rect.right()), container_rect.bottom()),
223 );
224 let r2_left = (split_x + divider_thickness).min(container_rect.right());
225 let r2 = Rect::from_min_max(
226 Pos2::new(r2_left, container_rect.top()),
227 container_rect.max,
228 );
229
230 let line = (
231 Pos2::new(divider_center_x, container_rect.top()),
232 Pos2::new(divider_center_x, container_rect.bottom()),
233 );
234 let h_rect = if self.show_handle {
235 let center_y = container_rect.center().y;
236 Some(Rect::from_center_size(
237 Pos2::new(divider_center_x, center_y),
238 Vec2::new(handle_th, handle_len),
239 ))
240 } else {
241 None
242 };
243
244 (r1, r2, Some(line), h_rect)
245 }
246 MacSplitOrientation::Vertical => {
247 let hit_rect = Rect::from_min_max(
248 Pos2::new(
249 container_rect.left(),
250 container_rect.top() + pos - hit_thickness * 0.5,
251 ),
252 Pos2::new(
253 container_rect.right(),
254 container_rect.top() + pos + hit_thickness * 0.5,
255 ),
256 );
257 let hit_resp = ui.interact(
258 hit_rect,
259 self.id.with("split_drag"),
260 egui::Sense::click_and_drag(),
261 );
262 if hit_resp.hovered() || hit_resp.dragged() {
263 ui.output_mut(|o| o.cursor_icon = CursorIcon::ResizeVertical);
264 }
265 if hit_resp.dragged() {
266 pos += hit_resp.drag_delta().y;
267 pos = pos.clamp(min_1, max_1.max(min_1));
268 }
269
270 let split_y = container_rect.top() + pos;
271 let divider_center_y = split_y + divider_thickness * 0.5;
272 let r1 = Rect::from_min_max(
273 container_rect.min,
274 Pos2::new(container_rect.right(), split_y.min(container_rect.bottom())),
275 );
276 let r2_top = (split_y + divider_thickness).min(container_rect.bottom());
277 let r2 = Rect::from_min_max(
278 Pos2::new(container_rect.left(), r2_top),
279 container_rect.max,
280 );
281
282 let line = (
283 Pos2::new(container_rect.left(), divider_center_y),
284 Pos2::new(container_rect.right(), divider_center_y),
285 );
286 let h_rect = if self.show_handle {
287 let center_x = container_rect.center().x;
288 Some(Rect::from_center_size(
289 Pos2::new(center_x, divider_center_y),
290 Vec2::new(handle_len, handle_th),
291 ))
292 } else {
293 None
294 };
295
296 (r1, r2, Some(line), h_rect)
297 }
298 }
299 };
300
301 if let Some(ref mut p) = self.pos_override {
303 **p = pos;
304 } else {
305 ui.data_mut(|d| d.insert_temp(self.id, pos));
306 }
307
308 let painter = ui.painter_at(container_rect);
310 if let Some((p1, p2)) = divider_line {
311 painter.line_segment([p1, p2], Stroke::new(divider_thickness, theme.separator()));
312 }
313 if let Some(hr) = handle_rect {
314 painter.rect(
315 hr,
316 CornerRadius::same((handle_th * 0.5) as u8),
317 theme.control(),
318 Stroke::NONE,
319 StrokeKind::Inside,
320 );
321 }
322
323 (first_rect, second_rect)
324 }
325
326 pub fn show<R1, R2>(
328 mut self,
329 ui: &mut Ui,
330 first: impl FnOnce(&mut Ui) -> R1,
331 second: impl FnOnce(&mut Ui) -> R2,
332 ) -> (R1, R2) {
333 let container_rect = ui.available_rect_before_wrap();
334 let (first_rect, second_rect) = self.split_panes(ui);
335
336 let mut ui1 = ui.new_child(
338 UiBuilder::new()
339 .max_rect(first_rect)
340 .layout(Layout::top_down(Align::Min)),
341 );
342 let r1 = first(&mut ui1);
343
344 let mut ui2 = ui.new_child(
345 UiBuilder::new()
346 .max_rect(second_rect)
347 .layout(Layout::top_down(Align::Min)),
348 );
349 let r2 = second(&mut ui2);
350
351 ui.advance_cursor_after_rect(container_rect);
352
353 (r1, r2)
354 }
355}