1use std::{rc::Rc, sync::Arc};
5
6use gpui::{
7 AnyElement, App, AppContext as _, Axis, Context, Div, Element, Empty, InteractiveElement as _,
8 IntoElement, MouseMoveEvent, MouseUpEvent, ParentElement as _, Pixels, Render, Stateful, Style,
9 Styled as _, Window, div, px,
10};
11use gpui_base::{
12 HandleEdge, ResizeHandleContext,
13 dock::{
14 CLOSED_BOTTOM_STRIP, DockAreaRenderer, DockContext, DockEvent, DockPlacement, NodeId,
15 PanelState, PanelView, TabGroupRenderer,
16 },
17};
18
19use crate::{
20 ActiveTheme as _,
21 dock::{
22 DockSkin, SkinShared, invalid_panel::InvalidPanel, panel_handle, report_user_toggle,
23 tab_panel::TabGroupSkin,
24 },
25 resizable::render_resize_handle,
26 resize_handle,
27};
28
29#[derive(Clone)]
32struct ResizePanel;
33
34impl Render for ResizePanel {
35 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
36 Empty
37 }
38}
39
40impl DockAreaRenderer for DockSkin {
41 fn closed_dock_extent(&self, placement: DockPlacement) -> Pixels {
42 match placement {
43 DockPlacement::Bottom if self.shared().collapsed_tab_bar_visible.get() => {
44 CLOSED_BOTTOM_STRIP
45 }
46 _ => px(0.),
47 }
48 }
49
50 fn frame(&self, _: &mut Window, _: &mut App) -> Stateful<Div> {
54 div().id("dock-area")
55 }
56
57 fn center_frame(&self, _: &mut Window, _: &mut App) -> Stateful<Div> {
58 div().id("dock-area-center")
59 }
60
61 fn render_split_handle(
62 &self,
63 handle: &ResizeHandleContext,
64 window: &mut Window,
65 cx: &mut App,
66 ) -> Option<AnyElement> {
67 Some(render_resize_handle(handle, window, cx))
68 }
69
70 fn split_frame(&self, node: NodeId, _: Axis, _: &mut Window, cx: &mut App) -> Stateful<Div> {
71 div()
74 .id(("dock-split-frame", node.as_u64()))
75 .bg(cx.theme().tokens.tab_bar)
76 }
77
78 fn render_dock(
79 &self,
80 dock: &DockContext,
81 content: AnyElement,
82 window: &mut Window,
83 cx: &mut App,
84 ) -> AnyElement {
85 div()
90 .flex()
91 .size_full()
92 .relative()
93 .child(content)
94 .child(self.render_resize_handle(dock, window, cx))
95 .child(DockResizeTracker {
96 dock: dock.clone(),
97 shared: self.shared().clone(),
98 })
99 .into_any_element()
100 }
101
102 fn build_placeholder(
107 &self,
108 state: &PanelState,
109 _: &mut Window,
110 cx: &mut App,
111 ) -> Option<Arc<dyn PanelView>> {
112 let state = state.clone();
113 Some(panel_handle(cx.new(|cx| InvalidPanel::new(state, cx))))
114 }
115
116 fn tab_group_renderer(&self) -> Rc<dyn TabGroupRenderer> {
117 Rc::new(TabGroupSkin::new(self.shared().clone()))
118 }
119}
120
121impl DockSkin {
122 fn render_resize_handle(
123 &self,
124 dock: &DockContext,
125 _: &mut Window,
126 cx: &mut App,
127 ) -> impl IntoElement {
128 let placement = dock.placement();
129 let physical = placement.physical(gpui_base::LayoutDirection::global(cx));
130 let open = dock.is_open();
131 let shared = self.shared().clone();
132
133 let id = match placement {
138 DockPlacement::Left => "resize-handle-left",
139 DockPlacement::Right => "resize-handle-right",
140 DockPlacement::Bottom => "resize-handle-bottom",
141 DockPlacement::Center => "resize-handle-center",
142 };
143
144 let edge = match physical {
148 DockPlacement::Left => HandleEdge::Trailing,
149 _ => HandleEdge::Leading,
150 };
151
152 resize_handle(id, placement.axis()).inside(edge).on_drag(
153 ResizePanel,
154 move |info, _, _, cx| {
155 cx.stop_propagation();
156 shared.resizing_dock().set(Some(placement));
157 shared.resize_started_open().set(open);
158 cx.new(|_| info.clone())
159 },
160 )
161 }
162}
163
164struct DockResizeTracker {
173 dock: DockContext,
174 shared: Rc<SkinShared>,
175}
176
177#[cfg(not(feature = "gpui-fast"))]
178impl IntoElement for DockResizeTracker {
179 type Element = Self;
180
181 fn into_element(self) -> Self::Element {
182 self
183 }
184}
185
186impl Element for DockResizeTracker {
187 type RequestLayoutState = ();
188 type PrepaintState = ();
189
190 fn id(&self) -> Option<gpui::ElementId> {
191 None
192 }
193
194 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
195 None
196 }
197
198 fn request_layout(
199 &mut self,
200 _: Option<&gpui::GlobalElementId>,
201 _: Option<&gpui::InspectorElementId>,
202 window: &mut Window,
203 cx: &mut App,
204 ) -> (gpui::LayoutId, Self::RequestLayoutState) {
205 (window.request_layout(Style::default(), None, cx), ())
206 }
207
208 fn prepaint(
209 &mut self,
210 _: Option<&gpui::GlobalElementId>,
211 _: Option<&gpui::InspectorElementId>,
212 _: gpui::Bounds<Pixels>,
213 _: &mut Self::RequestLayoutState,
214 _: &mut Window,
215 _: &mut App,
216 ) -> Self::PrepaintState {
217 }
218
219 fn paint(
220 &mut self,
221 _: Option<&gpui::GlobalElementId>,
222 _: Option<&gpui::InspectorElementId>,
223 _: gpui::Bounds<Pixels>,
224 _: &mut Self::RequestLayoutState,
225 _: &mut Self::PrepaintState,
226 window: &mut Window,
227 _: &mut App,
228 ) {
229 let placement = self.dock.placement();
230
231 window.on_mouse_event({
232 let dock = self.dock.clone();
233 let shared = self.shared.clone();
234 move |event: &MouseMoveEvent, phase, window, cx| {
235 if !phase.bubble() || shared.resizing_dock().get() != Some(placement) {
236 return;
237 }
238 dock.resize_to(event.position, window, cx);
241 }
242 });
243
244 window.on_mouse_event({
245 let dock = self.dock.clone();
246 let shared = self.shared.clone();
247 move |_: &MouseUpEvent, phase, window, cx| {
248 if !phase.bubble() || shared.resizing_dock().get() != Some(placement) {
249 return;
250 }
251 shared.resizing_dock().set(None);
252 dock.end_resize(window, cx);
253 let was_open = shared.resize_started_open().get();
254 _ = shared.area().update(cx, |area, cx| {
257 cx.emit(DockEvent::LayoutChanged);
258 report_user_toggle(area, placement, was_open, cx);
259 });
260 }
261 });
262 }
263}
264
265#[cfg(test)]
266mod tests {
267 use std::rc::Rc;
268
269 use gpui::{
270 App, Entity, IntoElement as _, Modifiers, MouseButton, TestAppContext, VisualTestContext,
271 Window, point, px, size,
272 };
273
274 use std::cell::Cell;
275
276 use gpui_base::dock::DockAreaRenderer;
277
278 use gpui_base::PANEL_MIN_SIZE;
279
280 use crate::dock::{
281 CLOSED_BOTTOM_STRIP, DockArea, DockLayout, DockPlacement, DockSkin,
282 test_support::{MeasuredProbe, SizedProbe},
283 };
284
285 struct ChromelessDockSkin;
291
292 impl DockAreaRenderer for ChromelessDockSkin {
293 fn tab_group_renderer(&self) -> Rc<dyn gpui_base::dock::TabGroupRenderer> {
294 Rc::new(ChromelessTabs)
295 }
296 }
297
298 struct ChromelessTabs;
299 impl gpui_base::dock::TabGroupRenderer for ChromelessTabs {
300 fn render_tab_bar(
304 &self,
305 _: &gpui_base::dock::TabGroupContext,
306 _: &mut Window,
307 _: &mut App,
308 ) -> gpui::AnyElement {
309 gpui::Empty.into_any_element()
310 }
311 }
312
313 #[gpui::test]
322 fn a_dock_is_its_own_width_under_a_renderer_that_draws_no_chrome(cx: &mut TestAppContext) {
323 cx.update(|cx| crate::init(cx));
324 let measured = Rc::new(Cell::new(gpui::Size::default()));
325 let probe = measured.clone();
326 let centre = Rc::new(Cell::new(gpui::Size::default()));
327 let centre_probe = centre.clone();
328 let (area, cx) = cx.add_window_view(|window, cx| {
329 DockArea::new("test", None, window, cx).with_renderer(Rc::new(ChromelessDockSkin))
330 });
331 cx.simulate_resize(size(px(800.), px(600.)));
332 cx.update(|window, cx| {
333 area.update(cx, |area, cx| {
334 area.set_center(
335 DockLayout::tabs().panel(SizedProbe::new(centre_probe, cx)),
336 window,
337 cx,
338 );
339 area.set_dock(
340 DockPlacement::Right,
341 DockLayout::tabs().panel(SizedProbe::new(probe, cx)),
342 window,
343 cx,
344 );
345 area.set_dock_size(DockPlacement::Right, px(200.), window, cx);
346 });
347 });
348 cx.run_until_parked();
349 cx.update(|window, cx| {
350 area.update(cx, |area, cx| {
351 if !area.is_dock_open(DockPlacement::Right) {
352 area.toggle_dock(DockPlacement::Right, window, cx);
353 }
354 });
355 });
356 cx.run_until_parked();
357 cx.update(|window, cx| window.draw(cx).clear(cx));
358
359 let dock = measured.get().width;
360 assert_eq!(
361 dock,
362 px(200.),
363 "the right dock has to be its own width, not the area's: got {dock:?}"
364 );
365 let middle = centre.get().width;
369 assert_eq!(
370 middle,
371 px(600.),
372 "the centre has to be what the docks leave: got {middle:?}"
373 );
374 let tall = centre.get().height;
380 assert_eq!(
381 tall,
382 px(600.),
383 "a group has to fill its slot, or its panel gets no height: got {tall:?}"
384 );
385 }
386
387 fn area_with_side_docks(cx: &mut TestAppContext) -> (Entity<DockArea>, &mut VisualTestContext) {
388 cx.update(|cx| crate::init(cx));
389 let (area, cx) = cx.add_window_view(|window, cx| {
390 DockArea::new("test", None, window, cx).with_renderer(DockSkin::new(cx))
391 });
392 cx.simulate_resize(size(px(800.), px(600.)));
393 cx.update(|window, cx| {
394 area.update(cx, |area, cx| {
395 area.set_center(
396 DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
397 window,
398 cx,
399 );
400 area.set_dock(
401 DockPlacement::Left,
402 DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
403 window,
404 cx,
405 );
406 area.set_dock(
407 DockPlacement::Right,
408 DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
409 window,
410 cx,
411 );
412 });
413 });
414 cx.run_until_parked();
415 (area, cx)
416 }
417
418 #[gpui::test]
425 fn dragging_the_left_handle_resizes_only_the_left_dock(cx: &mut TestAppContext) {
426 let (area, cx) = area_with_side_docks(cx);
427 cx.update(|window, cx| window.draw(cx).clear(cx));
428
429 cx.simulate_mouse_down(
431 point(px(198.5), px(300.)),
432 MouseButton::Left,
433 Modifiers::none(),
434 );
435 cx.simulate_mouse_move(
437 point(px(204.), px(300.)),
438 MouseButton::Left,
439 Modifiers::none(),
440 );
441 cx.simulate_mouse_move(
443 point(px(240.), px(300.)),
444 MouseButton::Left,
445 Modifiers::none(),
446 );
447 cx.simulate_mouse_up(
448 point(px(240.), px(300.)),
449 MouseButton::Left,
450 Modifiers::none(),
451 );
452 cx.run_until_parked();
453
454 let (left, right) = cx.update(|_, cx| {
455 let area = area.read(cx);
456 (
457 area.dock_size(DockPlacement::Left),
458 area.dock_size(DockPlacement::Right),
459 )
460 });
461 assert_eq!(
462 right,
463 Some(px(200.)),
464 "the right dock must not move when the left handle is dragged"
465 );
466 assert_eq!(left, Some(px(240.)), "the left dock follows the pointer");
467 }
468 #[gpui::test]
469 fn dragging_the_bottom_dock_closed_and_open_reports_each_toggle(cx: &mut TestAppContext) {
470 cx.update(|cx| crate::init(cx));
471 let (area, cx) = cx.add_window_view(|window, cx| {
472 DockArea::new("test", None, window, cx).with_renderer(DockSkin::new(cx))
473 });
474 cx.simulate_resize(size(px(800.), px(600.)));
475 let events = Rc::new(std::cell::RefCell::new(Vec::new()));
476 cx.update(|window, cx| {
477 area.update(cx, |area, cx| {
478 area.set_center(
479 DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
480 window,
481 cx,
482 );
483 area.set_dock(
484 DockPlacement::Bottom,
485 DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
486 window,
487 cx,
488 );
489 area.set_dock_size(DockPlacement::Bottom, px(200.), window, cx);
490 });
491 let events = events.clone();
492 cx.subscribe(&area, move |_, event: &crate::dock::DockToggleEvent, _| {
493 events
494 .borrow_mut()
495 .push((event.placement(), event.is_open()));
496 })
497 .detach();
498 });
499 cx.run_until_parked();
500
501 let drag = |cx: &mut VisualTestContext, ys: &[f32]| {
502 cx.update(|window, cx| window.draw(cx).clear(cx));
503 let (first, rest) = ys.split_first().unwrap();
504 cx.simulate_mouse_down(
505 point(px(400.), px(*first)),
506 MouseButton::Left,
507 Modifiers::none(),
508 );
509 for y in rest {
510 cx.simulate_mouse_move(
511 point(px(400.), px(*y)),
512 MouseButton::Left,
513 Modifiers::none(),
514 );
515 }
516 cx.simulate_mouse_up(
517 point(px(400.), px(*ys.last().unwrap())),
518 MouseButton::Left,
519 Modifiers::none(),
520 );
521 cx.run_until_parked();
522 };
523
524 drag(cx, &[400.5, 380., 350.]);
526 assert!(events.borrow().is_empty(), "a plain resize is not reported");
527
528 drag(cx, &[350.5, 356., 580., 540.]);
530 drag(cx, &[571.5, 565., 520.]);
531 assert_eq!(
532 *events.borrow(),
533 vec![
534 (DockPlacement::Bottom, false),
535 (DockPlacement::Bottom, true)
536 ]
537 );
538 }
539
540 #[gpui::test]
541 fn dragging_the_bottom_handle_below_the_minimum_snaps_to_the_nearer_end(
542 cx: &mut TestAppContext,
543 ) {
544 cx.update(|cx| crate::init(cx));
545 let (area, cx) = cx.add_window_view(|window, cx| {
546 DockArea::new("test", None, window, cx).with_renderer(DockSkin::new(cx))
547 });
548 cx.simulate_resize(size(px(800.), px(600.)));
549 cx.update(|window, cx| {
550 area.update(cx, |area, cx| {
551 area.set_center(
552 DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
553 window,
554 cx,
555 );
556 area.set_dock(
557 DockPlacement::Bottom,
558 DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
559 window,
560 cx,
561 );
562 area.set_dock_size(DockPlacement::Bottom, px(200.), window, cx);
563 });
564 });
565 cx.run_until_parked();
566
567 let drag = |cx: &mut VisualTestContext, ys: &[f32]| {
568 cx.update(|window, cx| window.draw(cx).clear(cx));
569 let (first, rest) = ys.split_first().unwrap();
570 cx.simulate_mouse_down(
571 point(px(400.), px(*first)),
572 MouseButton::Left,
573 Modifiers::none(),
574 );
575 for y in rest {
576 cx.simulate_mouse_move(
577 point(px(400.), px(*y)),
578 MouseButton::Left,
579 Modifiers::none(),
580 );
581 }
582 cx.simulate_mouse_up(
583 point(px(400.), px(*ys.last().unwrap())),
584 MouseButton::Left,
585 Modifiers::none(),
586 );
587 cx.run_until_parked();
588 };
589 let bottom = |cx: &mut VisualTestContext| {
590 cx.update(|_, cx| {
591 let area = area.read(cx);
592 (
593 area.is_dock_open(DockPlacement::Bottom),
594 area.dock_size(DockPlacement::Bottom),
595 )
596 })
597 };
598
599 drag(cx, &[400.5, 406., 580., 540.]);
603 assert!(!bottom(cx).0, "released nearer the strip, the dock closes");
604
605 drag(cx, &[571.5, 565., 520.]);
608 assert_eq!(
609 bottom(cx),
610 (true, Some(PANEL_MIN_SIZE)),
611 "released nearer the minimum, the dock opens at the minimum"
612 );
613 }
614
615 #[gpui::test]
618 fn a_closed_bottom_dock_takes_no_space_without_its_tab_bar(cx: &mut TestAppContext) {
619 cx.update(|cx| crate::init(cx));
620 let mut skin = None;
621 let (area, cx) = cx.add_window_view(|window, cx| {
622 let this = DockSkin::new(cx);
623 skin = Some(this.clone());
624 DockArea::new("test", None, window, cx).with_renderer(this)
625 });
626 let skin = skin.unwrap();
627 cx.simulate_resize(size(px(800.), px(600.)));
628 let center = Rc::new(std::cell::Cell::new(px(0.)));
629 cx.update(|window, cx| {
630 area.update(cx, |area, cx| {
631 area.set_center(
632 DockLayout::tabs().panel(MeasuredProbe::new(center.clone(), cx)),
633 window,
634 cx,
635 );
636 area.set_dock(
637 DockPlacement::Bottom,
638 DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
639 window,
640 cx,
641 );
642 area.toggle_dock(DockPlacement::Bottom, window, cx);
643 });
644 });
645 let draw = |cx: &mut VisualTestContext| {
646 cx.run_until_parked();
647 cx.update(|window, cx| window.draw(cx).clear(cx));
648 };
649 draw(cx);
650 assert!(!cx.update(|_, cx| area.read(cx).is_dock_open(DockPlacement::Bottom)));
651 let with_strip = center.get();
652
653 assert!(skin.is_collapsed_tab_bar_visible());
654 cx.update(|_, cx| skin.set_collapsed_tab_bar_visible(false, cx));
655 draw(cx);
656 assert_eq!(center.get() - with_strip, CLOSED_BOTTOM_STRIP);
657 }
658}