use std::rc::Rc;
use std::time::{Duration, Instant};
use gpui::{
AnyElement, Context, Div, ElementId, FocusHandle, FontWeight, KeyDownEvent, MouseButton,
MouseDownEvent, PathBuilder, ScrollHandle, SharedString, Window, canvas, deferred, div, point,
prelude::*, px,
};
use crate::controls::{ButtonVariant, CONTROL_RADIUS, button_focused, caption, scrollbar};
use crate::keyboard::{self, Dismiss, Key, Orientation};
use crate::lighting;
use crate::palette::Palette;
use crate::scroll::{ScrollAxis, scroll_fades};
use crate::state::{ComboId, ControlHost, ControlState, MOVE, SWITCH_SLIDE, TabDrag, 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
}
}
#[allow(clippy::too_many_arguments)]
pub fn tab_bar<V: ControlHost>(
id: ComboId,
tabs: &[Tab],
selected: usize,
palette: Palette,
view: &V,
cx: &mut Context<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 dark = palette.is_dark;
let on_select = Rc::new(on_select);
let on_close = Rc::new(on_close);
let focus = view.control_state().focus(id, cx);
let count = tabs.len();
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, Orientation::Horizontal, selected, count) {
cx.stop_propagation();
on_select(this, moved, window, cx);
cx.notify();
}
})
});
let drag: Option<TabDrag> = view.control_state().tab_drag.clone();
let dragging_here = drag.as_ref().filter(|drag| drag.bar == id).cloned();
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(),
};
const TAB_HEIGHT: f32 = 26.0;
const TAB_GAP: f32 = 2.0;
const TAB_PAD: f32 = 2.0;
let state = view.control_state();
let widths: Option<Vec<f32>> = state
.tab_widths
.get(id)
.and_then(|known| tabs.iter().map(|tab| known.get(&tab.id).copied()).collect());
let places: Option<Vec<f32>> = widths.as_ref().map(|widths| {
let mut x = 0.0;
order
.iter()
.map(|&index| {
let here = x;
x += widths[index] + TAB_GAP;
here
})
.collect()
});
let held_slot = dragging_here
.as_ref()
.filter(|drag| drag.moved)
.map(|drag| drag.to);
let well = state.group_bounds.get(id).copied();
let held_offset = held_slot.and_then(|slot| {
let drag = dragging_here.as_ref()?;
let places = places.as_ref()?;
let widths = widths.as_ref()?;
let well = well?;
let width = *widths.get(*order.get(slot)?)?;
let laid_out = f32::from(well.origin.x) + TAB_PAD + places[slot];
let lowest = f32::from(well.origin.x) + TAB_PAD;
let highest = (f32::from(well.right()) - TAB_PAD - width).max(lowest);
let wanted = (drag.pointer - drag.grab).clamp(lowest, highest);
Some(wanted - laid_out)
});
let pill = places
.as_ref()
.zip(widths.as_ref())
.and_then(|(places, widths)| {
let slot = order.iter().position(|&index| index == selected)?;
Some((slot, TAB_PAD + places[slot], *widths.get(selected)?))
})
.map(|(slot, x, width)| {
match held_offset.filter(|_| held_slot == Some(slot)) {
Some(offset) => (
state.snap(format!("{id}-pill-x"), x + offset),
state.snap(format!("{id}-pill-w"), width),
),
None => (
state.tween(format!("{id}-pill-x"), x, MOVE),
state.tween(format!("{id}-pill-w"), width, 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: &str| ElementId::Name(format!("{id}-{what}-{}", tab.id).into());
let on = state.blend(&key("tab-on"), active, SWITCH_SLIDE);
let slide = key("tab-x");
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() {
floating = Some(
tab_face(tab, active, palette)
.absolute()
.top(px(TAB_PAD))
.left(px(TAB_PAD + places[slot] + offset))
.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(key("tab"))
.h(px(TAB_HEIGHT))
.flex_none()
.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()
.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;
laid_out.origin.x -= px(offset);
if let Some(host) = weak.upgrade() {
host.update(cx, |host, _cx| {
let state = host.control_state_mut();
let slots = state.tab_bounds.entry(id).or_default();
if slots.len() <= slot {
slots.resize(slot + 1, laid_out);
}
slots[slot] = laid_out;
state
.tab_widths
.entry(id)
.or_default()
.insert(tab_id.clone(), f32::from(bounds.size.width));
});
}
},
|_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 = f32::from(event.position.x);
let grab = this
.control_state()
.tab_bounds
.get(id)
.and_then(|slots| slots.get(slot))
.map(|bounds| pointer - f32::from(bounds.left()))
.unwrap_or(0.0);
this.control_state_mut().tab_drag = Some(TabDrag {
bar: id,
from: slot,
to: slot,
moved: false,
grab,
pointer,
});
cx.notify();
}),
)
.on_mouse_up(
MouseButton::Left,
cx.listener(move |this, _event, window, cx| {
let was_reorder = this
.control_state()
.tab_drag
.as_ref()
.is_some_and(|drag| drag.moved);
if !was_reorder {
window.focus(&click_focus, cx);
on_select(this, index, window, cx);
}
cx.notify();
}),
)
.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(key("tab-close"))
.hover(move |style| style.bg(palette.row_hover))
.on_click(cx.listener(move |this, _event, window, cx| {
on_close(this, index, window, cx);
cx.notify();
})),
)
})
.into_any_element(),
);
}
let well_probe = {
let weak = weak.clone();
canvas(
move |bounds, _window, cx| {
if let Some(host) = weak.upgrade() {
host.update(cx, |host, _cx| {
host.control_state_mut().group_bounds.insert(id, bounds);
});
}
},
|_bounds, _state, _window, _cx| {},
)
.absolute()
.top_0()
.left_0()
.right_0()
.bottom_0()
};
div().id(id).flex().overflow_x_scroll().child(
div()
.relative()
.flex()
.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, (x, width)| {
el.child(
div()
.absolute()
.top(px(TAB_PAD))
.left(px(x))
.w(px(width))
.h(px(TAB_HEIGHT))
.rounded(px(CONTROL_RADIUS))
.bg(lighting::lit(palette.control_fill, 0.08))
.shadow(lighting::raised(dark)),
)
})
.children(rendered)
.children(floating),
)
}
fn tab_face(tab: &Tab, active: bool, palette: Palette) -> Div {
div()
.h(px(26.0))
.flex_none()
.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(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()
.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()
.items_center()
.justify_center()
.rounded(px(3.0))
.text_size(px(10.0))
.child("\u{2715}")
}
pub fn split_handle<V: ControlHost>(
id: ComboId,
fraction: f32,
vertical: bool,
palette: Palette,
view: &V,
cx: &mut Context<V>,
) -> impl IntoElement {
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,
palette: Palette,
view: &V,
cx: &mut Context<V>,
body: impl IntoElement,
on_toggle: impl Fn(&mut V, bool, &mut Window, &mut Context<V>) + 'static,
) -> impl IntoElement {
let element_id: ElementId = ElementId::Name(format!("{id}-disclosure").into());
let turned = view
.control_state()
.blend(&element_id, expanded, SWITCH_SLIDE);
let body_key = ElementId::Name(format!("{id}-body").into());
let body_height = view
.control_state()
.measured
.get(&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 o = 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(
o.x + px(centre.0 + x * cos - y * sin),
o.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,
palette: Palette,
view: &V,
cx: &mut Context<V>,
body: impl IntoElement,
buttons: Vec<DialogButton<V>>,
on_dismiss: impl Fn(&mut V, &mut Window, &mut Context<V>) + 'static,
) -> Option<impl IntoElement> {
let state = view.control_state();
let Some((opacity, _)) = state.dialog_fade(id) else {
state.set_dialog_trap(None);
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.focus(ElementId::Name(format!("{id}-button-{n}").into()), cx))
.collect();
let default = buttons
.iter()
.position(|spec| matches!(spec.variant, ButtonVariant::Primary))
.unwrap_or(0);
let panel_focus = state
.focus(ElementId::Name(format!("{id}-panel").into()), cx)
.tab_stop(false);
state.set_dialog_trap((!closing).then(|| handles.clone()));
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,
&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 = handles.get(default).cloned();
let enter_press = presses.get(default).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" || 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;
}
let previous = window.focused(cx);
if let Some(host) = weak.upgrade() {
host.update(cx, |host, _cx| {
let state = host.control_state_mut();
if state.dialog_return_focus.is_none() {
state.dialog_return_focus = previous;
}
});
}
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 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 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, 0.04))
.border_1()
.border_color(palette.area_border)
.shadow(lighting::panel(palette.is_dark))
.child(caption(palette, title))
.child(body)
}
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(&ElementId::Name(id.into()), 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,
palette: Palette,
view: &V,
window: &Window,
cx: &mut Context<V>,
content: impl IntoElement,
) -> Div {
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(
view.control_state(),
id,
handle,
axis,
start,
end,
))
.child(scrollbar(id, handle, axis, palette, view, cx))
}
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::{moved_selection, reorder};
#[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);
}
}