use crate::{
icons::{CarbonIcon, Icon, IconSize},
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
AnyElement, App, AppContext as _, CursorStyle, ElementId, Empty, Hsla, InteractiveElement,
IntoElement, KeyDownEvent, MouseButton, MouseDownEvent, ParentElement, Pixels, Render,
RenderOnce, StatefulInteractiveElement, Styled, Window, deferred, div,
prelude::FluentBuilder as _, px, relative, rems,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ResizableOrientation {
#[default]
Horizontal,
Vertical,
}
impl ResizableOrientation {
pub const ALL: [ResizableOrientation; 2] = [Self::Horizontal, Self::Vertical];
pub const fn name(self) -> &'static str {
match self {
Self::Horizontal => "horizontal",
Self::Vertical => "vertical",
}
}
}
pub const DEFAULT_MIN_SIZE: f32 = 10.;
pub const KEY_STEP: f32 = 5.;
pub const HANDLE_THICKNESS: f32 = 1.;
pub const HIT_OVERHANG: f32 = 4.;
pub const GRIP: (f32, f32) = (16., 24.);
pub const MIN_HEIGHT: f32 = 160.;
pub fn drag_sizes(start: &[f32], i: usize, delta: f32, min_i: f32, min_next: f32) -> Vec<f32> {
let mut next = start.to_vec();
if i + 1 >= start.len() {
return next;
}
let pair = start[i] + start[i + 1];
let a = (start[i] + delta).max(min_i).min(pair - min_next);
next[i] = a;
next[i + 1] = pair - a;
next
}
pub fn key_step(key: &str) -> Option<f32> {
match key {
"left" | "up" => Some(-KEY_STEP),
"right" | "down" => Some(KEY_STEP),
_ => None,
}
}
pub fn key_sizes(
sizes: &[f32],
i: usize,
key: &str,
min_i: f32,
min_next: f32,
) -> Option<Vec<f32>> {
key_step(key).map(|step| drag_sizes(sizes, i, step, min_i, min_next))
}
pub fn handle_color(hovered: bool, focus_visible: bool, dragging: bool) -> Hsla {
color(if hovered || focus_visible || dragging {
ColorRole::BorderInteractive
} else {
ColorRole::BorderSubtle01
})
}
pub fn initial_sizes(defaults: &[Option<f32>]) -> Vec<f32> {
let even = 100. / defaults.len().max(1) as f32;
defaults.iter().map(|d| d.unwrap_or(even)).collect()
}
pub struct ResizablePanel {
default_size: Option<f32>,
min_size: Option<f32>,
children: Vec<AnyElement>,
}
impl Default for ResizablePanel {
fn default() -> Self {
Self::new()
}
}
impl ResizablePanel {
pub fn new() -> Self {
Self {
default_size: None,
min_size: None,
children: Vec::new(),
}
}
pub fn default_size(mut self, percent: f32) -> Self {
self.default_size = Some(percent);
self
}
pub fn min_size(mut self, percent: f32) -> Self {
self.min_size = Some(percent);
self
}
fn min(&self) -> f32 {
self.min_size.unwrap_or(DEFAULT_MIN_SIZE)
}
}
impl ParentElement for ResizablePanel {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
#[derive(Clone, Copy, Default)]
pub struct ResizableHandle {
with_handle: bool,
}
impl ResizableHandle {
pub fn new() -> Self {
Self::default()
}
pub fn with_handle(mut self, with_handle: bool) -> Self {
self.with_handle = with_handle;
self
}
}
enum Part {
Panel(ResizablePanel),
Handle(ResizableHandle),
}
#[derive(IntoElement)]
pub struct ResizablePanelGroup {
id: ElementId,
orientation: ResizableOrientation,
parts: Vec<Part>,
}
pub type Resizable = ResizablePanelGroup;
impl ResizablePanelGroup {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
orientation: ResizableOrientation::Horizontal,
parts: Vec::new(),
}
}
pub fn orientation(mut self, orientation: ResizableOrientation) -> Self {
self.orientation = orientation;
self
}
pub fn panel(mut self, panel: ResizablePanel) -> Self {
self.parts.push(Part::Panel(panel));
self
}
pub fn handle(mut self, handle: ResizableHandle) -> Self {
self.parts.push(Part::Handle(handle));
self
}
}
#[derive(Default)]
struct GroupState {
sizes: Vec<f32>,
defaults: Vec<Option<f32>>,
drag: Option<(Pixels, Vec<f32>)>,
dragging: Option<usize>,
}
#[derive(Default)]
struct HandlePointer {
hit: bool,
grip: bool,
}
#[derive(Clone)]
struct HandleDrag {
group: ElementId,
}
struct DragGhost;
impl Render for DragGhost {
fn render(&mut self, _: &mut Window, _: &mut gpui_kit::Context<Self>) -> impl IntoElement {
Empty
}
}
impl RenderOnce for ResizablePanelGroup {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let horizontal = self.orientation == ResizableOrientation::Horizontal;
let defaults: Vec<Option<f32>> = self
.parts
.iter()
.filter_map(|p| match p {
Part::Panel(p) => Some(p.default_size),
Part::Handle(_) => None,
})
.collect();
let mins: Vec<f32> = self
.parts
.iter()
.filter_map(|p| match p {
Part::Panel(p) => Some(p.min()),
Part::Handle(_) => None,
})
.collect();
let state = window.use_keyed_state((id.clone(), "sizes"), cx, |_, _| GroupState::default());
state.update(cx, |s, _| {
if s.defaults != defaults || s.sizes.len() != defaults.len() {
s.sizes = initial_sizes(&defaults);
s.defaults = defaults.clone();
}
});
let (sizes, dragging) = {
let s = state.read(cx);
(s.sizes.clone(), s.dragging)
};
let mins = std::rc::Rc::new(mins);
let along = move |p: gpui_kit::Point<Pixels>| if horizontal { p.x } else { p.y };
let mut panel_ix = 0usize;
let mut handle_ix = 0usize;
let mut children: Vec<AnyElement> = Vec::new();
for part in self.parts {
match part {
Part::Panel(panel) => {
let size = sizes.get(panel_ix).copied().unwrap_or(0.);
panel_ix += 1;
children.push(
div()
.flex()
.flex_col()
.flex_grow_0()
.flex_shrink_0()
.flex_basis(relative(size / 100.))
.min_w_0()
.min_h_0()
.overflow_hidden()
.children(panel.children)
.into_any_element(),
);
}
Part::Handle(handle) => {
let i = handle_ix;
handle_ix += 1;
let hid = ElementId::NamedChild(
std::sync::Arc::new(id.clone()),
format!("handle-{i}").into(),
);
children.push(render_handle(
hid,
i,
handle,
horizontal,
dragging == Some(i),
&state,
&mins,
along,
&id,
window,
cx,
));
}
}
}
let group = id.clone();
div()
.id(id.clone())
.relative()
.flex()
.when(!horizontal, |this| this.flex_col())
.size_full()
.min_h(rems(MIN_HEIGHT / 16.))
.on_drag_move::<HandleDrag>({
let state = state.clone();
let mins = mins.clone();
move |event, _, cx| {
if event.drag(cx).group != group {
return;
}
let total = f32::from(if horizontal {
event.bounds.size.width
} else {
event.bounds.size.height
});
let position = along(event.event.position);
state.update(cx, |s, cx| {
let (Some(i), Some((start, s0))) = (s.dragging, s.drag.as_ref()) else {
return;
};
if total <= 0. {
return;
}
let delta = f32::from(position - *start) / total * 100.;
let next = drag_sizes(s0, i, delta, mins[i], mins[i + 1]);
if next != s.sizes {
s.sizes = next;
cx.notify();
}
});
}
})
.children(children)
.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.border_1()
.border_color(color(ColorRole::BorderSubtle01)),
)
}
}
#[allow(clippy::too_many_arguments)]
fn render_handle(
hid: ElementId,
i: usize,
handle: ResizableHandle,
horizontal: bool,
dragging: bool,
state: &gpui_kit::Entity<GroupState>,
mins: &std::rc::Rc<Vec<f32>>,
along: impl Fn(gpui_kit::Point<Pixels>) -> Pixels + Copy + 'static,
group: &ElementId,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let pointer = window.use_keyed_state((hid.clone(), "pointer"), cx, |_, _| {
HandlePointer::default()
});
let focus = window
.use_keyed_state((hid.clone(), "focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let hovered = {
let p = pointer.read(cx);
p.hit || p.grip
};
let focus_visible = focus.is_focused(window) && crate::focus::focus_visible_mode(window);
let line = handle_color(hovered, focus_visible, dragging);
let cursor = if horizontal {
CursorStyle::ResizeLeftRight
} else {
CursorStyle::ResizeUpDown
};
let start_drag = {
let state = state.clone();
let focus = focus.clone();
move |event: &MouseDownEvent, window: &mut Window, cx: &mut App| {
window.focus(&focus, cx);
let position = along(event.position);
state.update(cx, |s, cx| {
s.drag = Some((position, s.sizes.clone()));
s.dragging = Some(i);
cx.notify();
});
}
};
let end_drag = {
let state = state.clone();
move |_: &gpui_kit::MouseUpEvent, _: &mut Window, cx: &mut App| {
state.update(cx, |s, cx| {
if s.dragging.take().is_some() {
s.drag = None;
cx.notify();
}
});
}
};
let set_pointer = |grip: bool| {
let pointer = pointer.clone();
move |over: &bool, _: &mut Window, cx: &mut App| {
pointer.update(cx, |p, cx| {
if grip {
p.grip = *over;
} else {
p.hit = *over;
}
cx.notify();
})
}
};
let payload = HandleDrag {
group: group.clone(),
};
let on_key = {
let state = state.clone();
let mins = mins.clone();
move |event: &KeyDownEvent, _: &mut Window, cx: &mut App| {
let key = event.keystroke.key.as_str();
if key_step(key).is_none() {
return;
}
cx.stop_propagation();
state.update(cx, |s, cx| {
if let Some(next) = key_sizes(&s.sizes, i, key, mins[i], mins[i + 1]) {
s.sizes = next;
cx.notify();
}
});
}
};
let hit = div()
.id((hid.clone(), "hit"))
.absolute()
.map(|this| {
if horizontal {
this.top_0()
.bottom_0()
.left(px(-HIT_OVERHANG))
.right(px(-HIT_OVERHANG))
} else {
this.left_0()
.right_0()
.top(px(-HIT_OVERHANG))
.bottom(px(-HIT_OVERHANG))
}
})
.cursor(cursor)
.on_hover(set_pointer(false))
.on_mouse_down(MouseButton::Left, start_drag.clone())
.on_mouse_up(MouseButton::Left, end_drag.clone())
.on_mouse_up_out(MouseButton::Left, end_drag.clone())
.on_drag(payload.clone(), |_, _, _, cx| cx.new(|_| DragGhost));
let grip = handle.with_handle.then(|| {
let (w, h) = if horizontal { GRIP } else { (GRIP.1, GRIP.0) };
div()
.id((hid.clone(), "grip"))
.absolute()
.map(|this| {
if horizontal {
this.left(px((HANDLE_THICKNESS - w) / 2.))
.top(relative(0.5))
.mt(px(-h / 2.))
} else {
this.top(px((HANDLE_THICKNESS - h) / 2.))
.left(relative(0.5))
.ml(px(-w / 2.))
}
})
.w(rems(w / 16.))
.h(rems(h / 16.))
.flex()
.items_center()
.justify_center()
.bg(color(ColorRole::LayerAccent01))
.cursor(cursor)
.on_hover(set_pointer(true))
.on_mouse_down(MouseButton::Left, start_drag)
.on_mouse_up(MouseButton::Left, end_drag.clone())
.on_mouse_up_out(MouseButton::Left, end_drag)
.on_drag(payload, |_, _, _, cx| cx.new(|_| DragGhost))
.child(
Icon::carbon(CarbonIcon::Draggable)
.size(IconSize::S16)
.color(color(ColorRole::IconSecondary))
.when(!horizontal, |icon| {
icon.rotate(gpui_kit::radians(std::f32::consts::FRAC_PI_2))
}),
)
});
div()
.id(hid.clone())
.track_focus(&focus.tab_stop(true))
.on_key_down(on_key)
.relative()
.flex_none()
.map(|this| {
if horizontal {
this.w(px(HANDLE_THICKNESS)).self_stretch()
} else {
this.h(px(HANDLE_THICKNESS)).self_stretch()
}
})
.bg(line)
.child(deferred(hit).with_priority(1))
.when_some(grip, |this, grip| {
this.child(deferred(grip).with_priority(1))
})
.into_any_element()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn drag_clamps_the_pair_to_min_sizes() {
let s0 = [30., 70.];
assert_eq!(drag_sizes(&s0, 0, 10., 10., 10.), vec![40., 60.]);
assert_eq!(drag_sizes(&s0, 0, -50., 10., 10.), vec![10., 90.]);
assert_eq!(drag_sizes(&s0, 0, 80., 10., 10.), vec![90., 10.]);
assert_eq!(drag_sizes(&s0, 0, 80., 10., 25.), vec![75., 25.]);
assert_eq!(
drag_sizes(&[20., 30., 50.], 1, 5., 10., 10.),
vec![20., 35., 45.]
);
}
#[test]
fn arrow_keys_step_five_percent() {
let s = [60., 40.];
assert_eq!(key_sizes(&s, 0, "right", 10., 10.), Some(vec![65., 35.]));
assert_eq!(key_sizes(&s, 0, "down", 10., 10.), Some(vec![65., 35.]));
assert_eq!(key_sizes(&s, 0, "left", 10., 10.), Some(vec![55., 45.]));
assert_eq!(key_sizes(&s, 0, "up", 10., 10.), Some(vec![55., 45.]));
assert_eq!(
key_sizes(&[12., 88.], 0, "left", 10., 10.),
Some(vec![10., 90.])
);
assert_eq!(key_sizes(&s, 0, "enter", 10., 10.), None);
}
#[test]
fn defaults_split_evenly() {
assert_eq!(initial_sizes(&[Some(30.), Some(70.)]), vec![30., 70.]);
assert_eq!(initial_sizes(&[None, None, None, None]), vec![25.; 4]);
}
#[test]
fn handle_colors_and_geometry_follow_custom_css() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(
handle_color(false, false, false),
color(ColorRole::BorderSubtle01)
);
assert_eq!(
handle_color(true, false, false),
color(ColorRole::BorderInteractive)
);
assert_eq!(
handle_color(false, true, false),
color(ColorRole::BorderInteractive)
);
assert_eq!((HANDLE_THICKNESS, HIT_OVERHANG), (1., 4.));
assert_eq!(GRIP, (16., 24.));
assert_eq!(MIN_HEIGHT, 160.);
}
}