use std::cell::Cell;
use std::rc::Rc;
use std::time::{Duration, Instant};
use gpui::{
AnyElement, Context, DispatchPhase, Div, ElementId, FocusHandle, FontWeight, KeyDownEvent,
MouseButton, MouseDownEvent, PathBuilder, Rgba, ScrollHandle, ScrollWheelEvent, SharedString,
Window, canvas, deferred, div, point, prelude::*, px,
};
use crate::controls::{
ButtonVariant, CONTROL_RADIUS, WidgetContext, button_focused, caption, scrollbar,
};
use crate::keyboard::{self, Dismiss, Key, Orientation};
use crate::lighting;
use crate::palette::Palette;
use crate::scroll::{SCROLL_FADE, ScrollAxis, scroll_fades};
use crate::state::{
ComboId, ControlHost, ControlState, MOVE, SWITCH_SLIDE, TabDrag, Tag, TrackAxis,
};
#[derive(Clone, Debug)]
pub struct Tab {
pub id: SharedString,
pub label: SharedString,
pub badge: Option<SharedString>,
pub closable: bool,
}
impl Tab {
pub fn new(label: impl Into<SharedString>) -> Self {
let label = label.into();
Self {
id: label.clone(),
label,
badge: None,
closable: false,
}
}
pub fn id(mut self, id: impl Into<SharedString>) -> Self {
self.id = id.into();
self
}
pub fn badge(mut self, badge: impl Into<SharedString>) -> Self {
self.badge = Some(badge.into());
self
}
pub fn closable(mut self) -> Self {
self.closable = true;
self
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TabBarLayout {
Horizontal,
HorizontalUniform { width: f32 },
Vertical,
}
impl TabBarLayout {
fn vertical(self) -> bool {
matches!(self, Self::Vertical)
}
fn orientation(self) -> Orientation {
if self.vertical() {
Orientation::Vertical
} else {
Orientation::Horizontal
}
}
fn tab_width(self) -> Option<f32> {
match self {
Self::HorizontalUniform { width } => Some(if width.is_finite() {
width.clamp(96.0, 320.0)
} else {
160.0
}),
_ => None,
}
}
fn tab_size(self, measured: Option<f32>) -> Option<f32> {
match self {
Self::Vertical => Some(TAB_HEIGHT),
Self::HorizontalUniform { .. } => self.tab_width(),
Self::Horizontal => measured,
}
}
fn tab_sizes(self, measured: impl IntoIterator<Item = Option<f32>>) -> Option<Vec<f32>> {
measured
.into_iter()
.map(|measured| self.tab_size(measured))
.collect()
}
}
fn tab_scroll_kind(layout: TabBarLayout) -> (bool, bool) {
(layout.vertical(), layout.tab_width().is_some())
}
const TAB_HEIGHT: f32 = 26.0;
const TAB_GAP: f32 = 2.0;
const TAB_PAD: f32 = 2.0;
const WELL_BORDER: f32 = 1.0;
const TAB_INSET: f32 = WELL_BORDER + TAB_PAD;
#[allow(clippy::too_many_arguments)]
pub fn tab_bar<V: ControlHost>(
id: ComboId,
tabs: &[Tab],
selected: usize,
ctx: WidgetContext<'_, '_, '_, V>,
on_select: impl Fn(&mut V, usize, &mut Window, &mut Context<V>) + 'static,
on_close: impl Fn(&mut V, usize, &mut Window, &mut Context<V>) + 'static,
) -> impl IntoElement {
tab_bar_layout(
id,
tabs,
selected,
TabBarLayout::Horizontal,
ctx,
on_select,
on_close,
)
}
#[allow(clippy::too_many_arguments)]
pub fn tab_bar_layout<V: ControlHost>(
id: ComboId,
tabs: &[Tab],
selected: usize,
layout: TabBarLayout,
ctx: WidgetContext<'_, '_, '_, V>,
on_select: impl Fn(&mut V, usize, &mut Window, &mut Context<V>) + 'static,
on_close: impl Fn(&mut V, usize, &mut Window, &mut Context<V>) + 'static,
) -> impl IntoElement {
let WidgetContext { palette, view, cx } = ctx;
let dark = palette.is_dark;
let vertical = layout.vertical();
let uniform_width = layout.tab_width();
let on_select = Rc::new(on_select);
let on_close = Rc::new(on_close);
let geometry = (vertical, uniform_width.map(f32::to_bits));
let scroll_kind = tab_scroll_kind(layout);
let scroll = view.control_state().scroll((id, "tab-scroll", scroll_kind));
let focus = view.control_state().focus(id, cx);
let count = tabs.len();
let empty_focus = (count == 0).then(|| (focus.clone(), view.control_state().root_focus(cx)));
let mut arrows = (count > 0).then(|| {
let on_select = on_select.clone();
cx.listener(move |this, event: &KeyDownEvent, window, cx| {
let Some(key) = keyboard::key(event) else {
return;
};
if let Some(moved) = keyboard::step(key, layout.orientation(), selected, count) {
cx.stop_propagation();
on_select(this, moved, window, cx);
cx.notify();
}
})
});
let drag_valid = view.control_state().note_tab_axis(id, track_axis(vertical));
let dragging_here: Option<TabDrag> = view
.control_state()
.tab_drag()
.filter(|drag| drag_valid && drag.bar == id && drag.axis == track_axis(vertical))
.copied();
let weak = cx.entity().downgrade();
let order: Vec<usize> = match &dragging_here {
Some(drag) if drag.moved => {
let mut order: Vec<usize> = (0..tabs.len()).collect();
if drag.from < order.len() && drag.to < order.len() {
let moved = order.remove(drag.from);
order.insert(drag.to, moved);
}
order
}
_ => (0..tabs.len()).collect(),
};
let state = view.control_state();
let sizes: Option<Vec<f32>> = layout.tab_sizes(tabs.iter().map(|tab| {
state
.bounds(Tag::new((id, "tab", geometry, &tab.id)))
.map(|bounds| f32::from(bounds.size.width))
}));
let places: Option<Vec<f32>> = sizes
.as_ref()
.map(|sizes| tab_places(sizes, &order, TAB_GAP));
if let Some(tab) = tabs.get(selected)
&& state.tab_reveal_pending(id, (&tab.id, geometry))
&& let Some((places, sizes)) = places.as_ref().zip(sizes.as_ref())
&& let Some(slot) = order.iter().position(|&index| index == selected)
&& reveal_tab(&scroll, vertical, places, sizes, &order, slot)
{
state.finish_tab_reveal(id, (&tab.id, geometry));
}
let held_slot = dragging_here
.as_ref()
.filter(|drag| drag.moved)
.map(|drag| drag.to);
let well = state.group(id);
let held_offset = held_slot.and_then(|slot| {
let drag = dragging_here.as_ref()?;
let places = places.as_ref()?;
let sizes = sizes.as_ref()?;
let well = well?;
let size = *sizes.get(*order.get(slot)?)?;
let (origin, end) = if vertical {
(f32::from(well.top()), f32::from(well.bottom()))
} else {
(f32::from(well.left()), f32::from(well.right()))
};
let laid_out = origin + TAB_PAD + places[slot];
let lowest = origin + TAB_PAD;
let highest = (end - TAB_PAD - size).max(lowest);
let wanted = (drag.pointer - drag.grab).clamp(lowest, highest);
Some(wanted - laid_out)
});
let pill = places
.as_ref()
.zip(sizes.as_ref())
.and_then(|(places, sizes)| {
let slot = order.iter().position(|&index| index == selected)?;
Some((slot, TAB_PAD + places[slot], *sizes.get(selected)?))
})
.map(|(slot, pos, size)| {
match held_offset.filter(|_| held_slot == Some(slot)) {
Some(offset) => (
state.snap((id, "pill-pos", geometry), pos + offset),
state.snap((id, "pill-size", geometry), size),
),
None => (
state.tween((id, "pill-pos", geometry), pos, MOVE),
state.tween((id, "pill-size", geometry), size, MOVE),
),
}
});
let mut floating: Option<AnyElement> = None;
let mut rendered: Vec<AnyElement> = Vec::with_capacity(tabs.len());
for (slot, &index) in order.iter().enumerate() {
let cx: &mut Context<V> = &mut *cx;
let Some(tab) = tabs.get(index) else { continue };
let on_select = on_select.clone();
let on_close = on_close.clone();
let active = index == selected;
let tab_arrows = if active { arrows.take() } else { None };
let click_focus = focus.clone();
let held = held_slot == Some(slot);
let weak = weak.clone();
let fill = palette.control_fill;
let key = |what: &'static str| Tag::new((id, what, &tab.id));
let element = |what: &str| ElementId::Name(format!("{id}-{what}-{}", tab.id).into());
let on = state.blend(key("tab-on"), active, SWITCH_SLIDE);
let slide = Tag::new((id, "tab-slide", geometry, &tab.id));
let offset = match (places.as_ref(), held_offset.filter(|_| held)) {
(Some(places), Some(offset)) => state.snap(slide, places[slot] + offset) - places[slot],
(Some(places), None) => {
let place = places[slot];
state.tween(slide, place, MOVE) - place
}
(None, _) => 0.0,
};
if held && let Some(places) = places.as_ref() {
let float = tab_face(tab, active, palette, layout)
.absolute()
.when(vertical, |el| {
el.top(px(TAB_PAD + places[slot] + offset))
.left(px(TAB_PAD))
.right(px(TAB_PAD))
})
.when(!vertical, |el| {
el.top(px(TAB_PAD))
.left(px(TAB_PAD + places[slot] + offset))
});
floating = Some(
float
.opacity(0.85)
.bg(lighting::lit(
if active { fill } else { palette.field_surface },
0.08,
))
.shadow(lighting::panel(dark))
.into_any_element(),
);
}
let tab_id = tab.id.clone();
rendered.push(
div()
.id(element("tab"))
.h(px(TAB_HEIGHT))
.flex_none()
.when_some(uniform_width, |el, width| el.w(px(width)))
.when(vertical, |el| el.w_full())
.px(px(10.0))
.flex()
.items_center()
.gap(px(6.0))
.rounded(px(CONTROL_RADIUS))
.cursor_pointer()
.text_size(px(12.0))
.font_weight(if active {
FontWeight::MEDIUM
} else {
FontWeight::NORMAL
})
.text_color(crate::color::lerp(
palette.text_secondary,
palette.control_label,
on,
))
.whitespace_nowrap()
.relative()
.when(vertical, |el| el.top(px(offset)))
.when(!vertical, |el| el.left(px(offset)))
.when(active && pill.is_none(), |el| {
el.bg(lighting::lit(fill, 0.08))
.shadow(lighting::raised(dark))
})
.when(held && floating.is_some(), |el| el.opacity(0.0))
.when(active, |el| {
el.child(
keyboard::ring_for(&focus, CONTROL_RADIUS, palette)
.when_some(tab_arrows, |el, arrows| el.on_key_down(arrows)),
)
})
.when(!active, |el| {
el.hover(move |style| style.bg(palette.row_hover))
})
.when(held && floating.is_none(), |el| {
el.opacity(0.75).shadow(lighting::panel(dark))
})
.child(
canvas(
move |bounds, _window, cx| {
let mut laid_out = bounds;
if vertical {
laid_out.origin.y -= px(offset);
} else {
laid_out.origin.x -= px(offset);
}
if let Some(host) = weak.upgrade() {
host.update(cx, |host, _cx| {
let state = host.control_state_mut();
state.record_slot(id, slot, laid_out);
state.record_bounds(
Tag::new((id, "tab", geometry, &tab_id)),
laid_out,
);
});
}
},
|_bounds, _state, _window, _cx| {},
)
.absolute()
.top_0()
.left_0()
.right_0()
.bottom_0(),
)
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, event: &MouseDownEvent, _window, cx| {
let pointer = if vertical {
f32::from(event.position.y)
} else {
f32::from(event.position.x)
};
let grab = this
.control_state()
.slot(id, slot)
.map(|bounds| {
pointer
- if vertical {
f32::from(bounds.top())
} else {
f32::from(bounds.left())
}
})
.unwrap_or(0.0);
this.control_state_mut().begin_tab_drag(TabDrag {
bar: id,
axis: track_axis(vertical),
from: slot,
to: slot,
moved: false,
grab,
pressed: pointer,
pointer,
});
cx.notify();
}),
)
.on_mouse_up(
MouseButton::Left,
cx.listener(move |this, _event, window, cx| {
let clicked = drag_valid
&& this.control_state().tab_drag().is_some_and(|drag| {
drag.bar == id && drag.from == slot && !drag.moved
});
if clicked {
window.focus(&click_focus, cx);
on_select(this, index, window, cx);
}
cx.notify();
}),
)
.child(
div()
.when(vertical || uniform_width.is_some(), |el| {
el.flex_1().min_w(px(0.0)).overflow_hidden().text_ellipsis()
})
.child(tab.label.clone()),
)
.children(tab_badge(tab, palette))
.when(tab.closable, move |el| {
let on_close = on_close.clone();
el.child(
tab_close_glyph()
.id(element("tab-close"))
.hover(move |style| style.bg(palette.row_hover))
.on_mouse_down(MouseButton::Left, |_event, _window, cx| {
cx.stop_propagation()
})
.on_click(cx.listener(move |this, _event, window, cx| {
on_close(this, index, window, cx);
cx.notify();
})),
)
})
.into_any_element(),
);
}
let wheel_from: Rc<Cell<Option<f32>>> = Rc::default();
let well_probe = {
let weak = weak.clone();
let scroll = scroll.clone();
let wheel_from = wheel_from.clone();
canvas(
move |bounds, _window, cx| {
if let Some(host) = weak.upgrade() {
host.update(cx, |host, _cx| {
host.control_state_mut().record_group(id, bounds);
});
}
},
move |_bounds, _state, window, _cx| {
window.on_mouse_event(move |_: &ScrollWheelEvent, phase, _window, _cx| {
if phase == DispatchPhase::Bubble {
wheel_from.set(Some(scroll_along(&scroll, vertical)));
}
});
},
)
.absolute()
.top_0()
.left_0()
.right_0()
.bottom_0()
};
let axis = if vertical {
ScrollAxis::Vertical
} else {
ScrollAxis::Horizontal
};
let fades = div()
.absolute()
.when(vertical, |el| {
el.top_0()
.bottom_0()
.left(px(WELL_BORDER))
.right(px(WELL_BORDER))
})
.when(!vertical, |el| {
el.left_0()
.right_0()
.top(px(WELL_BORDER))
.bottom(px(WELL_BORDER))
})
.children(scroll_fades(
state,
id,
&scroll,
axis,
palette.field_surface,
palette.field_surface,
));
div()
.relative()
.flex()
.min_w(px(0.0))
.when(vertical || uniform_width.is_some(), |el| el.w_full())
.when(vertical, |el| el.h_full().min_h(px(0.0)).flex_col())
.child(
div()
.id(id)
.flex()
.flex_grow(1.0)
.min_w(px(0.0))
.when(vertical, |el| {
el.min_h(px(0.0))
.flex_col()
.overflow_y_scroll()
.restrict_scroll_to_axis()
})
.when(!vertical, |el| el.overflow_x_scroll())
.track_scroll(&scroll)
.on_scroll_wheel({
let scroll = scroll.clone();
move |_event, _window, cx| {
let max = max_scroll_along(&scroll, vertical);
if let Some(from) = wheel_from.take()
&& wheel_moved(from, scroll_along(&scroll, vertical), max)
{
cx.stop_propagation();
}
}
})
.child(
div()
.relative()
.flex()
.when(vertical, |el| el.w_full().flex_col().flex_none())
.when(!vertical, |el| el.items_center())
.gap(px(TAB_GAP))
.p(px(TAB_PAD))
.rounded(px(CONTROL_RADIUS))
.bg(palette.field_surface)
.border_1()
.border_color(palette.field_border)
.shadow(lighting::recessed(dark))
.child(well_probe)
.when_some(pill, |el, (pos, size)| {
el.child(
div()
.absolute()
.when(vertical, |el| {
el.top(px(pos))
.left(px(TAB_PAD))
.right(px(TAB_PAD))
.h(px(size))
})
.when(!vertical, |el| {
el.top(px(TAB_PAD))
.left(px(pos))
.w(px(size))
.h(px(TAB_HEIGHT))
})
.rounded(px(CONTROL_RADIUS))
.bg(lighting::lit(palette.control_fill, 0.08))
.shadow(lighting::raised(dark)),
)
})
.children(rendered)
.children(floating),
),
)
.child(fades)
.when_some(empty_focus, |el, (focus, root)| {
el.child(
canvas(
move |_bounds, window, cx| {
if focus.is_focused(window) {
window.focus(&root, cx);
}
},
|_bounds, _state, _window, _cx| {},
)
.absolute()
.size_full(),
)
})
}
fn tab_face(tab: &Tab, active: bool, palette: Palette, layout: TabBarLayout) -> Div {
div()
.h(px(TAB_HEIGHT))
.flex_none()
.when_some(layout.tab_width(), |el, width| el.w(px(width)))
.px(px(10.0))
.flex()
.items_center()
.gap(px(6.0))
.rounded(px(CONTROL_RADIUS))
.text_size(px(12.0))
.font_weight(if active {
FontWeight::MEDIUM
} else {
FontWeight::NORMAL
})
.text_color(if active {
palette.control_label
} else {
palette.text_secondary
})
.whitespace_nowrap()
.child(
div()
.when(layout.vertical() || layout.tab_width().is_some(), |el| {
el.flex_1().min_w(px(0.0)).overflow_hidden().text_ellipsis()
})
.child(tab.label.clone()),
)
.children(tab_badge(tab, palette))
.when(tab.closable, |el| el.child(tab_close_glyph()))
}
fn tab_badge(tab: &Tab, palette: Palette) -> Option<Div> {
tab.badge.clone().map(|badge| {
div()
.flex_none()
.text_size(px(10.5))
.text_color(palette.text_secondary)
.child(badge)
})
}
fn tab_close_glyph() -> Div {
div()
.w(px(14.0))
.h(px(14.0))
.flex_none()
.flex()
.items_center()
.justify_center()
.rounded(px(3.0))
.text_size(px(10.0))
.child("\u{2715}")
}
fn track_axis(vertical: bool) -> TrackAxis {
if vertical {
TrackAxis::Vertical
} else {
TrackAxis::Horizontal
}
}
fn scroll_along(scroll: &ScrollHandle, vertical: bool) -> f32 {
let offset = scroll.offset();
f32::from(if vertical { offset.y } else { offset.x })
}
fn max_scroll_along(scroll: &ScrollHandle, vertical: bool) -> f32 {
let max = scroll.max_offset();
f32::from(if vertical { max.y } else { max.x })
}
fn wheel_moved(from: f32, to: f32, max: f32) -> bool {
let clamp = |offset: f32| offset.clamp(-max.max(0.0), 0.0);
(clamp(from) - clamp(to)).abs() > 0.01
}
fn tab_places(sizes: &[f32], order: &[usize], gap: f32) -> Vec<f32> {
let mut along = 0.0;
order
.iter()
.map(|&index| {
let here = along;
along += sizes[index] + gap;
here
})
.collect()
}
fn reveal_delta(view_start: f32, view_end: f32, tab_start: f32, tab_end: f32) -> f32 {
if tab_start < view_start || tab_end - tab_start > view_end - view_start {
view_start - tab_start
} else if tab_end > view_end {
view_end - tab_end
} else {
0.0
}
}
fn reveal_scroll(
scrolled: f32,
view: f32,
places: &[f32],
sizes: &[f32],
order: &[usize],
slot: usize,
) -> f32 {
let content = tab_content_len(places, sizes, order);
let start = TAB_INSET + places[slot];
let end = start + sizes[order[slot]];
let delta = reveal_delta(
scrolled,
scrolled + view,
(start - SCROLL_FADE).max(0.0),
(end + SCROLL_FADE).min(content),
);
(scrolled - delta).clamp(0.0, (content - view).max(0.0))
}
fn tab_content_len(places: &[f32], sizes: &[f32], order: &[usize]) -> f32 {
let tabs = match (places.last(), order.last()) {
(Some(place), Some(&index)) => place + sizes[index],
_ => 0.0,
};
2.0 * TAB_INSET + tabs
}
fn reveal_tab(
scroll: &ScrollHandle,
vertical: bool,
places: &[f32],
sizes: &[f32],
order: &[usize],
slot: usize,
) -> bool {
let bounds = scroll.bounds();
if bounds.size.width <= px(0.0) || bounds.size.height <= px(0.0) {
return false;
}
let mut offset = scroll.offset();
let (view, scrolled) = if vertical {
(f32::from(bounds.size.height), -f32::from(offset.y))
} else {
(f32::from(bounds.size.width), -f32::from(offset.x))
};
let target = reveal_scroll(scrolled, view, places, sizes, order, slot);
if (target - scrolled).abs() > 0.5 {
if vertical {
offset.y = px(-target);
} else {
offset.x = px(-target);
}
scroll.set_offset(offset);
}
true
}
pub fn split_handle<V: ControlHost>(
id: ComboId,
fraction: f32,
vertical: bool,
ctx: WidgetContext<'_, '_, '_, V>,
) -> impl IntoElement {
let WidgetContext { palette, view, cx } = ctx;
const GRAB: f32 = 9.0;
let dragging = view.control_state().is_dragging(id);
let axis = if vertical {
TrackAxis::Horizontal
} else {
TrackAxis::Vertical
};
let mut rule: gpui::Hsla = crate::color::to_hsla(palette.field_border);
if dragging {
rule = crate::color::to_hsla(palette.accent);
}
let orientation = if vertical {
Orientation::Horizontal
} else {
Orientation::Vertical
};
div()
.id(id)
.relative()
.flex_none()
.when(vertical, |el| el.w(px(GRAB)).h_full().cursor_col_resize())
.when(!vertical, |el| el.h(px(GRAB)).w_full().cursor_row_resize())
.flex()
.items_center()
.justify_center()
.rounded(px(2.0))
.child(
keyboard::ring(ElementId::Name(format!("{id}-ring").into()), 2.0, palette).on_key_down(
cx.listener(move |this, event: &KeyDownEvent, _window, cx| {
let Some(key) = keyboard::key(event) else {
return;
};
if let Some(moved) = keyboard::nudge(key, orientation, fraction, 0.02) {
cx.stop_propagation();
let at = if vertical {
point(moved, 0.0)
} else {
point(0.0, moved)
};
this.track_dragged(id, at, cx);
cx.notify();
}
}),
),
)
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, event: &MouseDownEvent, _window, cx| {
this.control_state_mut().begin_track_drag(id, axis, None);
if let Some((_, at)) = this.control_state().track_ratio_at(event.position) {
this.track_dragged(id, at, cx);
}
cx.notify();
}),
)
.child(
div()
.when(vertical, |el| el.w(px(1.0)).h_full())
.when(!vertical, |el| el.h(px(1.0)).w_full())
.bg(rule),
)
}
pub fn split_area<V: ControlHost>(id: ComboId, cx: &mut Context<V>) -> impl IntoElement {
crate::controls::track_probe::<V>(id, cx.entity().downgrade())
}
#[allow(clippy::too_many_arguments)]
pub fn collapsible<V: ControlHost>(
id: &'static str,
title: &str,
expanded: bool,
ctx: WidgetContext<'_, '_, '_, V>,
body: impl IntoElement,
on_toggle: impl Fn(&mut V, bool, &mut Window, &mut Context<V>) + 'static,
) -> impl IntoElement {
let WidgetContext { palette, view, cx } = ctx;
let turned = view
.control_state()
.blend((id, "disclosure"), expanded, SWITCH_SLIDE);
let body_key = Tag::new((id, "body"));
let body_height = view
.control_state()
.measured(body_key)
.map(|bounds| f32::from(bounds.size.height));
let weak = cx.entity().downgrade();
let chevron: gpui::Hsla = crate::color::to_hsla(palette.text_secondary);
let on_toggle = Rc::new(on_toggle);
let on_toggle_key = on_toggle.clone();
let header_ring = ElementId::Name(format!("{id}-header-ring").into());
div()
.flex()
.flex_col()
.child(
div()
.id(ElementId::Name(format!("{id}-header").into()))
.relative()
.h(px(28.0))
.flex()
.items_center()
.gap(px(6.0))
.cursor_pointer()
.text_size(px(12.5))
.font_weight(FontWeight::MEDIUM)
.text_color(palette.text_primary)
.rounded(px(5.0))
.on_click(cx.listener(move |this, _event, window, cx| {
on_toggle(this, !expanded, window, cx);
cx.notify();
}))
.child(
div().w(px(12.0)).h(px(12.0)).flex_none().child(
canvas(
|_bounds, _window, _cx| {},
move |bounds, _state, window, _cx| {
let origin = bounds.origin;
let angle = turned * std::f32::consts::FRAC_PI_2;
let (sin, cos) = angle.sin_cos();
let centre = (6.0f32, 6.0f32);
let rotate = |x: f32, y: f32| {
point(
origin.x + px(centre.0 + x * cos - y * sin),
origin.y + px(centre.1 + x * sin + y * cos),
)
};
let mut builder = PathBuilder::stroke(px(1.5));
builder.move_to(rotate(-1.8, -3.6));
builder.line_to(rotate(1.8, 0.0));
builder.line_to(rotate(-1.8, 3.6));
if let Ok(path) = builder.build() {
window.paint_path(path, chevron);
}
},
)
.size_full(),
),
)
.child(SharedString::from(title.to_string()))
.child(
keyboard::ring(header_ring, 5.0, palette).on_key_down(cx.listener(
move |this, event: &KeyDownEvent, window, cx| {
let Some(key) = keyboard::key(event) else {
return;
};
let wanted = match key {
Key::Activate => !expanded,
Key::Left => false,
Key::Right => true,
_ => return,
};
cx.stop_propagation();
if wanted != expanded {
on_toggle_key(this, wanted, window, cx);
cx.notify();
}
},
)),
),
)
.when(turned > 0.01, |el| {
el.child(
div()
.overflow_hidden()
.when_some(body_height, |el, height| el.h(px(height * turned)))
.child(
div()
.relative()
.pl(px(18.0))
.pb(px(4.0))
.opacity(turned)
.child(crate::controls::measure_probe(body_key, weak))
.child(body),
),
)
})
}
pub struct DialogButton<V> {
pub id: ElementId,
pub label: SharedString,
pub variant: ButtonVariant,
pub enabled: bool,
#[allow(clippy::type_complexity)]
pub on_click: Box<dyn Fn(&mut V, &mut Window, &mut Context<V>) + 'static>,
}
impl<V: ControlHost> DialogButton<V> {
pub fn new(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
variant: ButtonVariant,
on_click: impl Fn(&mut V, &mut Window, &mut Context<V>) + 'static,
) -> Self {
Self {
id: id.into(),
label: label.into(),
variant,
enabled: true,
on_click: Box::new(on_click),
}
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
}
#[allow(clippy::too_many_arguments)]
pub fn dialog<V: ControlHost>(
id: ComboId,
title: &str,
width: f32,
ctx: WidgetContext<'_, '_, '_, V>,
body: impl IntoElement,
buttons: Vec<DialogButton<V>>,
on_dismiss: impl Fn(&mut V, &mut Window, &mut Context<V>) + 'static,
) -> Option<impl IntoElement> {
let WidgetContext { palette, view, cx } = ctx;
let state = view.control_state();
let Some((opacity, _)) = state.dialog_fade(id) else {
state.clear_dialog_trap(id);
return None;
};
let closing = !state.is_dialog_open(id);
let opening = state.dialog.as_ref().map(|dialog| dialog.opened_at);
let dark = palette.is_dark;
let opacity = opacity.clamp(0.0, 1.0);
let handles: Vec<FocusHandle> = (0..buttons.len())
.map(|n| state.dialog_button_focus(id, n, cx))
.collect();
let default = dialog_default(buttons.iter().map(|spec| (spec.variant, spec.enabled)));
let panel_focus = state.focus((id, "panel"), cx).tab_stop(false);
if closing {
state.clear_dialog_trap(id);
} else {
state.set_dialog_buttons(
id,
buttons
.iter()
.enumerate()
.filter_map(|(index, spec)| spec.enabled.then_some(index))
.collect(),
);
}
let on_dismiss: Press<V> = Rc::new(on_dismiss);
let dismiss_from_scrim = on_dismiss.clone();
let dismiss_from_key = on_dismiss.clone();
let mut footer: Vec<AnyElement> = Vec::with_capacity(buttons.len());
let mut presses: Vec<Press<V>> = Vec::with_capacity(buttons.len());
for (n, spec) in buttons.into_iter().enumerate() {
let cx: &mut Context<V> = &mut *cx;
let press: Press<V> = Rc::from(spec.on_click);
presses.push(press.clone());
footer.push(
div()
.flex_none()
.min_w(px(88.0))
.child(button_focused(
spec.id,
&spec.label,
spec.variant,
spec.enabled && !closing,
&handles[n],
palette,
cx,
move |host, window, cx| finish(host, opening, window, cx, &*press),
))
.into_any_element(),
);
}
let weak = cx.entity().downgrade();
let grab_panel = panel_focus.clone();
let grab_default = default.and_then(|index| handles.get(index)).cloned();
let enter_press = default.and_then(|index| presses.get(index)).cloned();
Some(
deferred(
div()
.id(ElementId::Name(format!("{id}-scrim").into()))
.absolute()
.inset_0()
.flex()
.items_center()
.justify_center()
.bg(crate::color::with_alpha(
palette.scrim(),
palette.scrim().alpha * opacity,
))
.occlude()
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, _event: &MouseDownEvent, window, cx| {
if closing {
return;
}
finish(this, opening, window, cx, &*dismiss_from_scrim);
cx.notify();
}),
)
.child(
div()
.id(ElementId::Name(format!("{id}-panel").into()))
.track_focus(&panel_focus)
.w(px(width))
.max_h(gpui::relative(0.86))
.opacity(opacity)
.flex()
.flex_col()
.gap(px(14.0))
.p(px(18.0))
.rounded(px(10.0))
.bg(if dark {
palette.soft_fill
} else {
palette.field_surface
})
.border_1()
.border_color(palette.field_border)
.shadow(lighting::faded(lighting::panel(dark), opacity))
.occlude()
.on_key_down(cx.listener(move |this, event: &KeyDownEvent, window, cx| {
if event.keystroke.key != "enter"
|| keyboard::key(event) != Some(Key::Activate)
|| closing
{
return;
}
let Some(press) = enter_press.clone() else {
return;
};
cx.stop_propagation();
finish(this, opening, window, cx, &*press);
cx.notify();
}))
.on_action(cx.listener(move |this, _: &Dismiss, window, cx| {
if closing {
cx.propagate();
return;
}
finish(this, opening, window, cx, &*dismiss_from_key);
cx.notify();
}))
.child(
canvas(
move |_bounds, window, cx| {
if closing || grab_panel.contains_focused(window, cx) {
return;
}
if let Some(host) = weak.upgrade() {
host.update(cx, |host, cx| {
let state = host.control_state_mut();
state.restore_menu_focus(window, cx);
if state.dialog_return_focus.is_none() {
state.dialog_return_focus = window.focused(cx);
}
});
}
match &grab_default {
Some(handle) => window.focus(handle, cx),
None => window.focus(&grab_panel, cx),
}
},
|_bounds, _state, _window, _cx| {},
)
.absolute()
.size_full(),
)
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::SEMIBOLD)
.text_color(palette.text_primary)
.child(SharedString::from(title.to_string())),
)
.child(div().flex_1().overflow_hidden().child(body))
.child(div().flex().justify_end().gap(px(8.0)).children(footer)),
),
)
.with_priority(150),
)
}
fn dialog_default(buttons: impl IntoIterator<Item = (ButtonVariant, bool)>) -> Option<usize> {
let mut first = None;
for (index, (variant, enabled)) in buttons.into_iter().enumerate() {
if enabled {
first.get_or_insert(index);
if variant == ButtonVariant::Primary {
return Some(index);
}
}
}
first
}
fn finish<V: ControlHost>(
host: &mut V,
opening: Option<Instant>,
window: &mut Window,
cx: &mut Context<V>,
act: &dyn Fn(&mut V, &mut Window, &mut Context<V>),
) {
host.control_state_mut().restore_dialog_focus(window, cx);
act(host, window, cx);
let state = host.control_state_mut();
if state.dialog.as_ref().map(|dialog| dialog.opened_at) == opening {
state.close_dialog();
}
}
type Press<V> = Rc<dyn Fn(&mut V, &mut Window, &mut Context<V>)>;
pub const DIALOG_ENTER: Duration = crate::easing::MODAL_ENTER;
pub const DIALOG_EXIT: Duration = crate::easing::MODAL_EXIT;
pub const CARD_LIFT: f32 = 0.04;
pub fn card(palette: Palette, title: &str, body: impl IntoElement) -> Div {
div()
.flex()
.flex_col()
.gap(px(12.0))
.flex_none()
.p(px(16.0))
.rounded(px(10.0))
.bg(lighting::lit(palette.area_surface, CARD_LIFT))
.border_1()
.border_color(palette.area_border)
.shadow(lighting::panel(palette.is_dark))
.child(caption(palette, title))
.child(body)
}
pub fn card_at(palette: Palette, fraction: f32) -> Rgba {
let (top, bottom) = lighting::lit_stops(palette.area_surface, CARD_LIFT);
crate::color::lerp(top, bottom, fraction.clamp(0.0, 1.0))
}
pub fn labelled(palette: Palette, label: &str, control: impl IntoElement) -> Div {
div()
.flex()
.flex_col()
.gap(px(6.0))
.child(caption(palette, label))
.child(control)
}
pub fn row() -> Div {
div().flex().flex_none().items_center().gap(px(10.0))
}
pub fn column() -> Div {
div().flex().flex_none().flex_col().gap(px(14.0))
}
pub fn arriving(
id: &'static str,
key: u64,
state: &ControlState,
content: impl IntoElement,
) -> Div {
let arrived = crate::easing::ease_out_cubic(state.transition(id, key, MOVE));
div()
.opacity(arrived)
.relative()
.top(px(8.0 * (1.0 - arrived)))
.child(content)
}
#[allow(clippy::too_many_arguments)]
pub fn scroll_area<V: ControlHost>(
id: &'static str,
handle: &ScrollHandle,
axis: ScrollAxis,
ctx: WidgetContext<'_, '_, '_, V>,
window: &Window,
content: impl IntoElement,
) -> Div {
let palette = ctx.palette;
let window_height = f32::from(window.viewport_size().height).max(1.0);
let bounds = handle.bounds();
let ground_at =
|y: gpui::Pixels| lighting::ground_at(palette.backdrop, f32::from(y) / window_height);
let (start, end) = match axis {
ScrollAxis::Vertical => (ground_at(bounds.top()), ground_at(bounds.bottom())),
ScrollAxis::Horizontal => {
let middle = ground_at(bounds.center().y);
(middle, middle)
}
};
div()
.relative()
.overflow_hidden()
.child(
div()
.id(id)
.size_full()
.flex()
.flex_col()
.when(axis == ScrollAxis::Vertical, |el| el.overflow_y_scroll())
.when(axis == ScrollAxis::Horizontal, |el| el.overflow_x_scroll())
.restrict_scroll_to_axis()
.track_scroll(handle)
.child(content),
)
.children(scroll_fades(ctx.state(), id, handle, axis, start, end))
.child(scrollbar(id, handle, axis, ctx))
}
pub fn reorder<T>(items: &mut Vec<T>, selected: &mut usize, from: usize, to: usize) {
if from >= items.len() || to >= items.len() {
return;
}
let item = items.remove(from);
items.insert(to, item);
*selected = moved_selection(*selected, from, to);
}
fn moved_selection(selected: usize, from: usize, to: usize) -> usize {
if selected == from {
to
} else if from < selected && selected <= to {
selected - 1
} else if to <= selected && selected < from {
selected + 1
} else {
selected
}
}
#[cfg(test)]
mod layout_tests {
use super::{
ButtonVariant, TAB_HEIGHT, TAB_INSET, TabBarLayout, dialog_default, moved_selection,
reorder, reveal_delta, reveal_scroll, tab_content_len, tab_places, tab_scroll_kind,
wheel_moved,
};
use crate::keyboard::Orientation;
use crate::scroll::SCROLL_FADE;
#[test]
fn resizing_uniform_tabs_reuses_their_scroll_handle() {
assert_eq!(
tab_scroll_kind(TabBarLayout::HorizontalUniform { width: 96.0 }),
tab_scroll_kind(TabBarLayout::HorizontalUniform { width: 320.0 })
);
assert_ne!(
tab_scroll_kind(TabBarLayout::Horizontal),
tab_scroll_kind(TabBarLayout::HorizontalUniform { width: 160.0 })
);
assert_ne!(
tab_scroll_kind(TabBarLayout::Vertical),
tab_scroll_kind(TabBarLayout::HorizontalUniform { width: 160.0 })
);
}
#[test]
fn only_uniform_tabs_keep_the_next_close_target_under_the_pointer() {
let gap = 2.0;
let natural = [90.0, 140.0, 70.0, 110.0];
let before = tab_places(&natural, &[0, 1, 2, 3], gap);
let after = tab_places(&[90.0, 70.0, 110.0], &[0, 1, 2], gap);
assert_eq!(before, [0.0, 92.0, 234.0, 306.0]);
assert_eq!(after[1], before[1], "the next tab takes the closed slot");
assert_eq!(
before[3] - after[2],
140.0 + gap,
"later tabs move by what was closed"
);
let close_target = |places: &[f32], sizes: &[f32], slot: usize| places[slot] + sizes[slot];
assert_ne!(
close_target(&after, &[90.0, 70.0, 110.0], 1),
close_target(&before, &natural, 1)
);
let width = TabBarLayout::HorizontalUniform { width: 158.0 }
.tab_width()
.unwrap();
let before = tab_places(&[width; 4], &[0, 1, 2, 3], gap);
let after = tab_places(&[width; 3], &[0, 1, 2], gap);
assert_eq!(
close_target(&after, &[width; 3], 1),
close_target(&before, &[width; 4], 1)
);
}
#[test]
fn closing_the_last_tab_scrolls_back_to_the_new_end() {
let gap = 2.0;
let view = 200.0;
let before = [90.0, 140.0, 70.0, 110.0];
let order = [0, 1, 2, 3];
let places = tab_places(&before, &order, gap);
let content = tab_content_len(&places, &before, &order);
assert_eq!(content, 2.0 * TAB_INSET + 410.0 + 3.0 * gap);
let at_end = content - view;
assert_eq!(
reveal_scroll(0.0, view, &places, &before, &order, 3),
at_end
);
let after = [90.0, 140.0, 70.0];
let order = [0, 1, 2];
let places = tab_places(&after, &order, gap);
let content = tab_content_len(&places, &after, &order);
let scrolled = reveal_scroll(at_end, view, &places, &after, &order, 2);
assert_eq!(scrolled, content - view);
assert!(scrolled < at_end, "the viewport moved");
let start = TAB_INSET + places[2];
assert!(scrolled <= start && start + after[2] <= scrolled + view);
}
#[test]
fn a_revealed_tab_clears_the_edge_fades() {
let sizes = [100.0; 8];
let order: Vec<usize> = (0..8).collect();
let places = tab_places(&sizes, &order, 2.0);
let view = 250.0;
let scrolled = reveal_scroll(0.0, view, &places, &sizes, &order, 4);
let end = TAB_INSET + places[4] + 100.0;
assert_eq!(scrolled + view, end + SCROLL_FADE);
let back = reveal_scroll(scrolled, view, &places, &sizes, &order, 0);
assert_eq!(back, 0.0);
}
#[test]
fn a_reveal_reads_the_tab_in_its_planned_slot() {
let sizes = [100.0, 60.0, 80.0];
let order = [1, 2, 0];
let places = tab_places(&sizes, &order, 2.0);
let view = 200.0;
let content = tab_content_len(&places, &sizes, &order);
assert_eq!(
reveal_scroll(0.0, view, &places, &sizes, &order, 2),
content - view
);
let order = [0, 1, 2];
let places = tab_places(&sizes, &order, 2.0);
assert_eq!(reveal_scroll(0.0, view, &places, &sizes, &order, 0), 0.0);
}
#[test]
fn only_a_natural_width_bar_waits_for_its_first_paint() {
assert_eq!(TabBarLayout::Horizontal.tab_sizes([None, None]), None);
assert_eq!(
TabBarLayout::Horizontal.tab_sizes([Some(80.0), None]),
None,
"one unmeasured tab holds the plan back"
);
assert_eq!(
TabBarLayout::Horizontal.tab_sizes([Some(80.0), Some(64.0)]),
Some(vec![80.0, 64.0])
);
assert_eq!(
TabBarLayout::HorizontalUniform { width: 150.0 }.tab_sizes([None, Some(80.0)]),
Some(vec![150.0, 150.0])
);
assert_eq!(
TabBarLayout::Vertical.tab_sizes([None, Some(200.0)]),
Some(vec![TAB_HEIGHT, TAB_HEIGHT])
);
}
#[test]
fn the_arrows_run_along_the_bar() {
assert_eq!(
TabBarLayout::Horizontal.orientation(),
Orientation::Horizontal
);
assert_eq!(
TabBarLayout::HorizontalUniform { width: 158.0 }.orientation(),
Orientation::Horizontal
);
assert_eq!(TabBarLayout::Vertical.orientation(), Orientation::Vertical);
}
#[test]
fn constrained_tab_widths_stay_usable() {
assert_eq!(
TabBarLayout::HorizontalUniform { width: 12.0 }.tab_width(),
Some(96.0)
);
assert_eq!(
TabBarLayout::HorizontalUniform { width: 900.0 }.tab_width(),
Some(320.0)
);
assert_eq!(
TabBarLayout::HorizontalUniform { width: f32::NAN }.tab_width(),
Some(160.0)
);
}
#[test]
fn arrow_selection_reveals_only_the_hidden_part_of_a_tab() {
assert_eq!(reveal_delta(20.0, 120.0, 40.0, 100.0), 0.0);
assert_eq!(reveal_delta(20.0, 120.0, 8.0, 68.0), 12.0);
assert_eq!(reveal_delta(20.0, 120.0, 75.0, 146.0), -26.0);
}
#[test]
fn a_strip_keeps_the_wheel_only_while_it_moves() {
let max = 300.0;
assert!(wheel_moved(-100.0, -140.0, max), "mid-strip");
assert!(wheel_moved(-290.0, -330.0, max), "arriving at the end");
assert!(!wheel_moved(-300.0, -340.0, max), "already at the end");
assert!(
!wheel_moved(-340.0, -380.0, max),
"a second event before the strip is clamped"
);
assert!(!wheel_moved(0.0, 40.0, max), "already at the start");
assert!(wheel_moved(-300.0, -260.0, max), "back from the end");
assert!(
!wheel_moved(0.0, -40.0, 0.0),
"a strip that fits never moves"
);
}
#[test]
fn a_tab_longer_than_the_view_settles_on_its_start() {
assert_eq!(reveal_delta(20.0, 120.0, 0.0, 150.0), 20.0);
assert_eq!(reveal_delta(20.0, 120.0, 40.0, 190.0), -20.0);
assert_eq!(reveal_delta(20.0, 120.0, 20.0, 170.0), 0.0);
}
#[test]
fn a_dialog_only_defaults_to_an_enabled_button() {
use ButtonVariant::{Primary, Soft};
assert_eq!(dialog_default([(Soft, true), (Primary, true)]), Some(1));
assert_eq!(dialog_default([(Soft, true), (Primary, false)]), Some(0));
assert_eq!(dialog_default([(Soft, false), (Soft, true)]), Some(1));
assert_eq!(dialog_default([(Primary, false)]), None);
assert_eq!(dialog_default([]), None);
}
#[test]
fn a_reorder_keeps_the_selection_on_its_item() {
let mut items = vec!["a", "b", "c", "d"];
let mut selected = 2; reorder(&mut items, &mut selected, 0, 3); assert_eq!(items, ["b", "c", "d", "a"]);
assert_eq!(items[selected], "c");
reorder(&mut items, &mut selected, 1, 0); assert_eq!(items, ["c", "b", "d", "a"]);
assert_eq!(selected, 0);
reorder(&mut items, &mut selected, 9, 0);
assert_eq!(items, ["c", "b", "d", "a"]);
}
#[test]
fn a_selection_outside_the_moved_span_does_not_move() {
assert_eq!(moved_selection(0, 2, 3), 0);
assert_eq!(moved_selection(3, 0, 1), 3);
assert_eq!(moved_selection(2, 0, 3), 1);
assert_eq!(moved_selection(1, 3, 0), 2);
}
}