use std::{rc::Rc, sync::Arc};
use gpui::{
AnyElement, App, AppContext as _, Axis, Context, Div, Element, Empty, InteractiveElement as _,
IntoElement, MouseMoveEvent, MouseUpEvent, ParentElement as _, Pixels, Render, Stateful, Style,
Styled as _, Window, div, px,
};
use gpui_base::{
HandleEdge, ResizeHandleContext,
dock::{
CLOSED_BOTTOM_STRIP, DockAreaRenderer, DockContext, DockEvent, DockPlacement, NodeId,
PanelState, PanelView, TabGroupRenderer,
},
};
use crate::{
ActiveTheme as _,
dock::{
DockSkin, SkinShared, invalid_panel::InvalidPanel, panel_handle, report_user_toggle,
tab_panel::TabGroupSkin,
},
resizable::render_resize_handle_with,
resize_handle,
};
#[derive(Clone)]
struct ResizePanel;
impl Render for ResizePanel {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
Empty
}
}
impl DockAreaRenderer for DockSkin {
fn closed_dock_extent(&self, placement: DockPlacement) -> Pixels {
match placement {
DockPlacement::Bottom if self.shared().collapsed_tab_bar_visible.get() => {
CLOSED_BOTTOM_STRIP
}
_ => px(0.),
}
}
fn frame(&self, _: &mut Window, _: &mut App) -> Stateful<Div> {
div().id("dock-area")
}
fn center_frame(&self, _: &mut Window, _: &mut App) -> Stateful<Div> {
div().id("dock-area-center")
}
fn render_split_handle(
&self,
handle: &ResizeHandleContext,
window: &mut Window,
cx: &mut App,
) -> Option<AnyElement> {
let line = self.shared().split_divider_line_visible.get();
Some(render_resize_handle_with(handle, line, window, cx))
}
fn split_frame(&self, node: NodeId, _: Axis, _: &mut Window, cx: &mut App) -> Stateful<Div> {
div()
.id(("dock-split-frame", node.as_u64()))
.bg(cx.theme().tokens.tab_bar)
}
fn render_dock(
&self,
dock: &DockContext,
content: AnyElement,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
div()
.flex()
.size_full()
.relative()
.child(content)
.child(self.render_resize_handle(dock, window, cx))
.child(DockResizeTracker {
dock: dock.clone(),
shared: self.shared().clone(),
})
.into_any_element()
}
fn build_placeholder(
&self,
state: &PanelState,
_: &mut Window,
cx: &mut App,
) -> Option<Arc<dyn PanelView>> {
let state = state.clone();
Some(panel_handle(cx.new(|cx| InvalidPanel::new(state, cx))))
}
fn tab_group_renderer(&self) -> Rc<dyn TabGroupRenderer> {
Rc::new(TabGroupSkin::new(self.shared().clone()))
}
}
impl DockSkin {
fn render_resize_handle(
&self,
dock: &DockContext,
_: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let placement = dock.placement();
let physical = placement.physical(gpui_base::LayoutDirection::global(cx));
let open = dock.is_open();
let shared = self.shared().clone();
let id = match placement {
DockPlacement::Left => "resize-handle-left",
DockPlacement::Right => "resize-handle-right",
DockPlacement::Bottom => "resize-handle-bottom",
DockPlacement::Center => "resize-handle-center",
};
let edge = match physical {
DockPlacement::Left => HandleEdge::Trailing,
_ => HandleEdge::Leading,
};
resize_handle(id, placement.axis()).inside(edge).on_drag(
ResizePanel,
move |info, _, _, cx| {
cx.stop_propagation();
shared.resizing_dock().set(Some(placement));
shared.resize_started_open().set(open);
cx.new(|_| info.clone())
},
)
}
}
struct DockResizeTracker {
dock: DockContext,
shared: Rc<SkinShared>,
}
impl Element for DockResizeTracker {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<gpui::ElementId> {
None
}
fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&gpui::GlobalElementId>,
_: Option<&gpui::InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (gpui::LayoutId, Self::RequestLayoutState) {
(window.request_layout(Style::default(), None, cx), ())
}
fn prepaint(
&mut self,
_: Option<&gpui::GlobalElementId>,
_: Option<&gpui::InspectorElementId>,
_: gpui::Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
_: &mut Window,
_: &mut App,
) -> Self::PrepaintState {
}
fn paint(
&mut self,
_: Option<&gpui::GlobalElementId>,
_: Option<&gpui::InspectorElementId>,
_: gpui::Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
_: &mut Self::PrepaintState,
window: &mut Window,
_: &mut App,
) {
let placement = self.dock.placement();
window.on_mouse_event({
let dock = self.dock.clone();
let shared = self.shared.clone();
move |event: &MouseMoveEvent, phase, window, cx| {
if !phase.bubble() || shared.resizing_dock().get() != Some(placement) {
return;
}
dock.resize_to(event.position, window, cx);
}
});
window.on_mouse_event({
let dock = self.dock.clone();
let shared = self.shared.clone();
move |_: &MouseUpEvent, phase, window, cx| {
if !phase.bubble() || shared.resizing_dock().get() != Some(placement) {
return;
}
shared.resizing_dock().set(None);
dock.end_resize(window, cx);
let was_open = shared.resize_started_open().get();
_ = shared.area().update(cx, |area, cx| {
cx.emit(DockEvent::LayoutChanged);
report_user_toggle(area, placement, was_open, cx);
});
}
});
}
}
#[cfg(test)]
mod tests {
use std::rc::Rc;
use gpui::{
App, Entity, IntoElement as _, Modifiers, MouseButton, TestAppContext, VisualTestContext,
Window, point, px, size,
};
use std::cell::Cell;
use gpui_base::dock::DockAreaRenderer;
use gpui_base::PANEL_MIN_SIZE;
use crate::dock::{
CLOSED_BOTTOM_STRIP, DockArea, DockLayout, DockPlacement, DockSkin,
test_support::{MeasuredProbe, SizedProbe},
};
struct ChromelessDockSkin;
impl DockAreaRenderer for ChromelessDockSkin {
fn tab_group_renderer(&self) -> Rc<dyn gpui_base::dock::TabGroupRenderer> {
Rc::new(ChromelessTabs)
}
}
struct ChromelessTabs;
impl gpui_base::dock::TabGroupRenderer for ChromelessTabs {
fn render_tab_bar(
&self,
_: &gpui_base::dock::TabGroupContext,
_: &mut Window,
_: &mut App,
) -> gpui::AnyElement {
gpui::Empty.into_any_element()
}
}
#[gpui::test]
fn a_dock_is_its_own_width_under_a_renderer_that_draws_no_chrome(cx: &mut TestAppContext) {
cx.update(|cx| crate::init(cx));
let measured = Rc::new(Cell::new(gpui::Size::default()));
let probe = measured.clone();
let centre = Rc::new(Cell::new(gpui::Size::default()));
let centre_probe = centre.clone();
let (area, cx) = cx.add_window_view(|window, cx| {
DockArea::new("test", None, window, cx).with_renderer(Rc::new(ChromelessDockSkin))
});
cx.simulate_resize(size(px(800.), px(600.)));
cx.update(|window, cx| {
area.update(cx, |area, cx| {
area.set_center(
DockLayout::tabs().panel(SizedProbe::new(centre_probe, cx)),
window,
cx,
);
area.set_dock(
DockPlacement::Right,
DockLayout::tabs().panel(SizedProbe::new(probe, cx)),
window,
cx,
);
area.set_dock_size(DockPlacement::Right, px(200.), window, cx);
});
});
cx.run_until_parked();
cx.update(|window, cx| {
area.update(cx, |area, cx| {
if !area.is_dock_open(DockPlacement::Right) {
area.toggle_dock(DockPlacement::Right, window, cx);
}
});
});
cx.run_until_parked();
cx.update(|window, cx| window.draw(cx).clear(cx));
let dock = measured.get().width;
assert_eq!(
dock,
px(200.),
"the right dock has to be its own width, not the area's: got {dock:?}"
);
let middle = centre.get().width;
assert_eq!(
middle,
px(600.),
"the centre has to be what the docks leave: got {middle:?}"
);
let tall = centre.get().height;
assert_eq!(
tall,
px(600.),
"a group has to fill its slot, or its panel gets no height: got {tall:?}"
);
}
fn area_with_side_docks(cx: &mut TestAppContext) -> (Entity<DockArea>, &mut VisualTestContext) {
cx.update(|cx| crate::init(cx));
let (area, cx) = cx.add_window_view(|window, cx| {
DockArea::new("test", None, window, cx).with_renderer(DockSkin::new(cx))
});
cx.simulate_resize(size(px(800.), px(600.)));
cx.update(|window, cx| {
area.update(cx, |area, cx| {
area.set_center(
DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
window,
cx,
);
area.set_dock(
DockPlacement::Left,
DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
window,
cx,
);
area.set_dock(
DockPlacement::Right,
DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
window,
cx,
);
});
});
cx.run_until_parked();
(area, cx)
}
#[gpui::test]
fn dragging_the_left_handle_resizes_only_the_left_dock(cx: &mut TestAppContext) {
let (area, cx) = area_with_side_docks(cx);
cx.update(|window, cx| window.draw(cx).clear(cx));
cx.simulate_mouse_down(
point(px(198.5), px(300.)),
MouseButton::Left,
Modifiers::none(),
);
cx.simulate_mouse_move(
point(px(204.), px(300.)),
MouseButton::Left,
Modifiers::none(),
);
cx.simulate_mouse_move(
point(px(240.), px(300.)),
MouseButton::Left,
Modifiers::none(),
);
cx.simulate_mouse_up(
point(px(240.), px(300.)),
MouseButton::Left,
Modifiers::none(),
);
cx.run_until_parked();
let (left, right) = cx.update(|_, cx| {
let area = area.read(cx);
(
area.dock_size(DockPlacement::Left),
area.dock_size(DockPlacement::Right),
)
});
assert_eq!(
right,
Some(px(200.)),
"the right dock must not move when the left handle is dragged"
);
assert_eq!(left, Some(px(240.)), "the left dock follows the pointer");
}
#[gpui::test]
fn dragging_the_bottom_dock_closed_and_open_reports_each_toggle(cx: &mut TestAppContext) {
cx.update(|cx| crate::init(cx));
let (area, cx) = cx.add_window_view(|window, cx| {
DockArea::new("test", None, window, cx).with_renderer(DockSkin::new(cx))
});
cx.simulate_resize(size(px(800.), px(600.)));
let events = Rc::new(std::cell::RefCell::new(Vec::new()));
cx.update(|window, cx| {
area.update(cx, |area, cx| {
area.set_center(
DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
window,
cx,
);
area.set_dock(
DockPlacement::Bottom,
DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
window,
cx,
);
area.set_dock_size(DockPlacement::Bottom, px(200.), window, cx);
});
let events = events.clone();
cx.subscribe(&area, move |_, event: &crate::dock::DockToggleEvent, _| {
events
.borrow_mut()
.push((event.placement(), event.is_open()));
})
.detach();
});
cx.run_until_parked();
let drag = |cx: &mut VisualTestContext, ys: &[f32]| {
cx.update(|window, cx| window.draw(cx).clear(cx));
let (first, rest) = ys.split_first().unwrap();
cx.simulate_mouse_down(
point(px(400.), px(*first)),
MouseButton::Left,
Modifiers::none(),
);
for y in rest {
cx.simulate_mouse_move(
point(px(400.), px(*y)),
MouseButton::Left,
Modifiers::none(),
);
}
cx.simulate_mouse_up(
point(px(400.), px(*ys.last().unwrap())),
MouseButton::Left,
Modifiers::none(),
);
cx.run_until_parked();
};
drag(cx, &[400.5, 380., 350.]);
assert!(events.borrow().is_empty(), "a plain resize is not reported");
drag(cx, &[350.5, 356., 580., 540.]);
drag(cx, &[571.5, 565., 520.]);
assert_eq!(
*events.borrow(),
vec![
(DockPlacement::Bottom, false),
(DockPlacement::Bottom, true)
]
);
}
#[gpui::test]
fn dragging_the_bottom_handle_below_the_minimum_snaps_to_the_nearer_end(
cx: &mut TestAppContext,
) {
cx.update(|cx| crate::init(cx));
let (area, cx) = cx.add_window_view(|window, cx| {
DockArea::new("test", None, window, cx).with_renderer(DockSkin::new(cx))
});
cx.simulate_resize(size(px(800.), px(600.)));
cx.update(|window, cx| {
area.update(cx, |area, cx| {
area.set_center(
DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
window,
cx,
);
area.set_dock(
DockPlacement::Bottom,
DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
window,
cx,
);
area.set_dock_size(DockPlacement::Bottom, px(200.), window, cx);
});
});
cx.run_until_parked();
let drag = |cx: &mut VisualTestContext, ys: &[f32]| {
cx.update(|window, cx| window.draw(cx).clear(cx));
let (first, rest) = ys.split_first().unwrap();
cx.simulate_mouse_down(
point(px(400.), px(*first)),
MouseButton::Left,
Modifiers::none(),
);
for y in rest {
cx.simulate_mouse_move(
point(px(400.), px(*y)),
MouseButton::Left,
Modifiers::none(),
);
}
cx.simulate_mouse_up(
point(px(400.), px(*ys.last().unwrap())),
MouseButton::Left,
Modifiers::none(),
);
cx.run_until_parked();
};
let bottom = |cx: &mut VisualTestContext| {
cx.update(|_, cx| {
let area = area.read(cx);
(
area.is_dock_open(DockPlacement::Bottom),
area.dock_size(DockPlacement::Bottom),
)
})
};
drag(cx, &[400.5, 406., 580., 540.]);
assert!(!bottom(cx).0, "released nearer the strip, the dock closes");
drag(cx, &[571.5, 565., 520.]);
assert_eq!(
bottom(cx),
(true, Some(PANEL_MIN_SIZE)),
"released nearer the minimum, the dock opens at the minimum"
);
}
#[gpui::test]
fn a_closed_bottom_dock_takes_no_space_without_its_tab_bar(cx: &mut TestAppContext) {
cx.update(|cx| crate::init(cx));
let mut skin = None;
let (area, cx) = cx.add_window_view(|window, cx| {
let this = DockSkin::new(cx);
skin = Some(this.clone());
DockArea::new("test", None, window, cx).with_renderer(this)
});
let skin = skin.unwrap();
cx.simulate_resize(size(px(800.), px(600.)));
let center = Rc::new(std::cell::Cell::new(px(0.)));
cx.update(|window, cx| {
area.update(cx, |area, cx| {
area.set_center(
DockLayout::tabs().panel(MeasuredProbe::new(center.clone(), cx)),
window,
cx,
);
area.set_dock(
DockPlacement::Bottom,
DockLayout::tabs().panel(MeasuredProbe::new(Rc::default(), cx)),
window,
cx,
);
area.toggle_dock(DockPlacement::Bottom, window, cx);
});
});
let draw = |cx: &mut VisualTestContext| {
cx.run_until_parked();
cx.update(|window, cx| window.draw(cx).clear(cx));
};
draw(cx);
assert!(!cx.update(|_, cx| area.read(cx).is_dock_open(DockPlacement::Bottom)));
let with_strip = center.get();
assert!(skin.is_collapsed_tab_bar_visible());
cx.update(|_, cx| skin.set_collapsed_tab_bar_visible(false, cx));
draw(cx);
assert_eq!(center.get() - with_strip, CLOSED_BOTTOM_STRIP);
}
}