use crate::{theme::color, tokens::ColorRole};
use gpui_kit::{
AnyElement, App, Bounds, DispatchPhase, ElementId, FocusHandle, Hitbox, HitboxBehavior,
InteractiveElement, IntoElement, KeyDownEvent, MouseButton, MouseDownEvent, ParentElement,
Pixels, Point, RenderOnce, ScrollEvent, ScrollHandle, StatefulInteractiveElement,
StyleRefinement, Styled, Window, canvas,
component::scroll::{
Scrollbar, ScrollbarAxis, ScrollbarHandle, ScrollbarMode, ScrollbarStyles,
},
div, point,
prelude::FluentBuilder as _,
px,
};
pub const THICKNESS: f32 = 8.;
pub const MIN_THUMB: f32 = THICKNESS;
pub const LINE_STEP: f32 = 40.;
pub const PAGE_FRACTION: f32 = 0.875;
pub const FOCUS_WIDTH: f32 = 2.;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ScrollAxes {
#[default]
Both,
Vertical,
Horizontal,
}
impl ScrollAxes {
pub const ALL: [ScrollAxes; 3] = [Self::Both, Self::Vertical, Self::Horizontal];
pub const fn name(self) -> &'static str {
match self {
Self::Both => "both",
Self::Vertical => "vertical",
Self::Horizontal => "horizontal",
}
}
fn scrollbar_axis(self) -> ScrollbarAxis {
match self {
Self::Both => ScrollbarAxis::Both,
Self::Vertical => ScrollbarAxis::Vertical,
Self::Horizontal => ScrollbarAxis::Horizontal,
}
}
const fn vertical(self) -> bool {
matches!(self, Self::Both | Self::Vertical)
}
const fn horizontal(self) -> bool {
matches!(self, Self::Both | Self::Horizontal)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ScrollFocusRing {
#[default]
FocusVisible,
Focus,
Hidden,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScrollbarLook {
pub thumb: gpui_kit::Hsla,
pub track: gpui_kit::Hsla,
}
impl ScrollbarLook {
pub fn current() -> Self {
Self {
thumb: color(ColorRole::BorderStrong01),
track: gpui_kit::Hsla::transparent_black(),
}
}
}
pub fn carbon_scrollbar_styles(styles: ScrollbarStyles) -> ScrollbarStyles {
let look = ScrollbarLook::current();
styles
.track(|t| {
t.bg(look.track)
.border_color(look.track)
.width(px(THICKNESS))
})
.track_hover(|t| t.bg(look.track).border_color(look.track))
.track_active(|t| t.bg(look.track).border_color(look.track))
.thumb(|t| {
t.bg(look.thumb)
.width(px(THICKNESS))
.inset(px(0.))
.radius(px(0.))
.min_length(px(MIN_THUMB))
})
}
pub fn carbon_scrollbar<H: ScrollbarHandle + Clone>(
id: impl Into<ElementId>,
handle: &H,
axes: ScrollAxes,
) -> Scrollbar {
Scrollbar::new(handle)
.id(id)
.axis(axes.scrollbar_axis())
.mode(ScrollbarMode::Always)
.viewport_from_layout()
.styles(carbon_scrollbar_styles)
}
pub fn keyboard_scroll(
key: &str,
shift: bool,
offset: Point<f32>,
max: Point<f32>,
viewport: Point<f32>,
axes: ScrollAxes,
) -> Option<Point<f32>> {
let (mut x, mut y) = (offset.x, offset.y);
let page = viewport.y * PAGE_FRACTION;
match key {
"up" if axes.vertical() => y += LINE_STEP,
"down" if axes.vertical() => y -= LINE_STEP,
"left" if axes.horizontal() => x += LINE_STEP,
"right" if axes.horizontal() => x -= LINE_STEP,
"pageup" if axes.vertical() => y += page,
"pagedown" if axes.vertical() => y -= page,
"space" if axes.vertical() => y += if shift { page } else { -page },
"home" if axes.vertical() => y = 0.,
"end" if axes.vertical() => y = -max.y,
"home" => x = 0.,
"end" => x = -max.x,
_ => return None,
}
let next = point(x.clamp(-max.x.max(0.), 0.), y.clamp(-max.y.max(0.), 0.));
(next != offset).then_some(next)
}
pub fn thumb_span(track: f32, viewport: f32, content: f32, offset: f32) -> (f32, f32) {
if content <= 0. {
return (0., track);
}
let length = (viewport / content * track).max(MIN_THUMB).min(track);
let extent = content - viewport;
let start = if extent > 0. {
(-offset / extent).clamp(0., 1.) * (track - length)
} else {
0.
};
(start, length)
}
pub fn track_page(at: f32, thumb: (f32, f32), offset: f32, viewport: f32, max: f32) -> Option<f32> {
let next = if at < thumb.0 {
offset + viewport
} else if at > thumb.0 + thumb.1 {
offset - viewport
} else {
return None;
};
Some(next.clamp(-max.max(0.), 0.))
}
pub fn shift_wheel(shift: bool, delta: Point<f32>, offset_x: f32, max_x: f32) -> Option<f32> {
if !shift || delta.x != 0. || delta.y == 0. || max_x <= 0. {
return None;
}
let next = (offset_x + delta.y).clamp(-max_x, 0.);
(next != offset_x).then_some(next)
}
fn bars(axes: ScrollAxes, max: Point<Pixels>) -> (bool, bool) {
(
axes.vertical() && max.y > px(0.),
axes.horizontal() && max.x > px(0.),
)
}
#[derive(IntoElement)]
pub struct ScrollArea {
id: ElementId,
style: StyleRefinement,
children: Vec<AnyElement>,
axes: ScrollAxes,
focusable: bool,
focus_handle: Option<FocusHandle>,
scroll_handle: Option<ScrollHandle>,
restrict_wheel: bool,
focus_ring: ScrollFocusRing,
}
impl ScrollArea {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
style: StyleRefinement::default(),
children: Vec::new(),
axes: ScrollAxes::Both,
focusable: true,
focus_handle: None,
scroll_handle: None,
restrict_wheel: false,
focus_ring: ScrollFocusRing::FocusVisible,
}
}
pub fn focus_ring(mut self, ring: ScrollFocusRing) -> Self {
self.focus_ring = ring;
self
}
pub fn restrict_wheel_to_axis(mut self) -> Self {
self.restrict_wheel = true;
self
}
pub fn axes(mut self, axes: ScrollAxes) -> Self {
self.axes = axes;
self
}
pub fn focusable(mut self, focusable: bool) -> Self {
self.focusable = focusable;
self
}
pub fn track_focus(mut self, handle: &FocusHandle) -> Self {
self.focus_handle = Some(handle.clone());
self
}
pub fn track_scroll(mut self, handle: &ScrollHandle) -> Self {
self.scroll_handle = Some(handle.clone());
self
}
}
impl Styled for ScrollArea {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl ParentElement for ScrollArea {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
fn to_f32(p: Point<Pixels>) -> Point<f32> {
point(f32::from(p.x), f32::from(p.y))
}
impl RenderOnce for ScrollArea {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let handle = self.scroll_handle.clone().unwrap_or_else(|| {
window
.use_keyed_state((id.clone(), "scroll"), cx, |_, _| ScrollHandle::default())
.read(cx)
.clone()
});
let focus = self.focus_handle.clone().unwrap_or_else(|| {
window
.use_keyed_state((id.clone(), "focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone()
});
let axes = self.axes;
let (v_bar, h_bar) = bars(axes, handle.max_offset());
let focus_visible = self.focusable
&& focus.is_focused(window)
&& match self.focus_ring {
ScrollFocusRing::FocusVisible => crate::focus::focus_visible_mode(window),
ScrollFocusRing::Focus => true,
ScrollFocusRing::Hidden => false,
};
let content = div()
.flex()
.flex_col()
.flex_none()
.when(axes.horizontal(), |this| this.min_w_full())
.when(axes == ScrollAxes::Horizontal, |this| this.min_h_full())
.when(v_bar, |this| this.pr(px(THICKNESS)))
.when(h_bar, |this| this.pb(px(THICKNESS)))
.children(self.children);
let viewport = div()
.id((id.clone(), "viewport"))
.flex()
.flex_col()
.when(axes.horizontal(), |this| this.items_start())
.flex_grow_1()
.flex_shrink_1()
.min_h_0()
.w_full()
.track_scroll(&handle)
.map(|this| match axes {
ScrollAxes::Both => this.overflow_scroll(),
ScrollAxes::Vertical => this.overflow_y_scroll(),
ScrollAxes::Horizontal => this.overflow_x_scroll(),
})
.when(self.restrict_wheel, |this| this.restrict_scroll_to_axis())
.child(content);
let on_key = {
let handle = handle.clone();
move |event: &KeyDownEvent, window: &mut Window, cx: &mut App| {
let bounds = handle.bounds();
let next = keyboard_scroll(
event.keystroke.key.as_str(),
event.keystroke.modifiers.shift,
to_f32(handle.offset()),
to_f32(handle.max_offset()),
point(f32::from(bounds.size.width), f32::from(bounds.size.height)),
axes,
);
if let Some(next) = next {
handle.set_offset(point(px(next.x), px(next.y)));
cx.stop_propagation();
window.refresh();
}
}
};
let bar_axes = match (v_bar, h_bar) {
(true, true) => ScrollAxes::Both,
(true, false) => ScrollAxes::Vertical,
(false, true) => ScrollAxes::Horizontal,
(false, false) => axes,
};
let behavior = {
let measured = handle.clone();
canvas(
move |bounds, window, _| {
if bars(axes, measured.max_offset()) != (v_bar, h_bar) {
window.on_next_frame(|window, _| window.refresh());
}
window.insert_hitbox(bounds, HitboxBehavior::Normal)
},
{
let handle = handle.clone();
move |_, hitbox, window, _| {
install_pointer(&handle, hitbox, axes, (v_bar, h_bar), window)
}
},
)
.absolute()
.top_0()
.left_0()
.size_full()
};
let mut root = div()
.id(id.clone())
.relative()
.flex()
.flex_col()
.overflow_hidden();
gpui_kit::Refineable::refine(root.style(), &self.style);
root.when(self.focusable, |this| {
this.track_focus(&focus.clone().tab_stop(true))
.on_key_down(on_key)
})
.child(viewport)
.child(behavior)
.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.child(carbon_scrollbar(
(id.clone(), "scrollbar"),
&handle,
bar_axes,
)),
)
.when(focus_visible, |this| {
this.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.border(px(FOCUS_WIDTH))
.border_color(color(ColorRole::Focus)),
)
})
}
}
fn install_pointer(
handle: &ScrollHandle,
hitbox: Hitbox,
axes: ScrollAxes,
shown: (bool, bool),
window: &mut Window,
) {
let bounds = hitbox.bounds;
window.on_mouse_event({
let handle = handle.clone();
let hitbox = hitbox.clone();
move |event: &MouseDownEvent, phase, window, cx| {
if phase != DispatchPhase::Capture
|| event.button != MouseButton::Left
|| !hitbox.is_hovered(window)
{
return;
}
if let Some(next) = track_press(&handle, bounds, shown, event.position) {
handle.set_offset(next);
cx.stop_propagation();
window.refresh();
}
}
});
if !axes.horizontal() {
return;
}
window.on_mouse_event({
let handle = handle.clone();
move |event: &ScrollEvent, phase, window, cx| {
if phase != DispatchPhase::Bubble || !hitbox.should_handle_scroll(window) {
return;
}
let delta = event.delta.pixel_delta(window.line_height());
let offset = handle.offset();
if let Some(x) = shift_wheel(
event.modifiers.shift,
to_f32(delta),
f32::from(offset.x),
f32::from(handle.max_offset().x),
) {
handle.set_offset(point(px(x), offset.y));
cx.stop_propagation();
window.refresh();
}
}
});
}
fn track_press(
handle: &ScrollHandle,
bounds: Bounds<Pixels>,
(v_bar, h_bar): (bool, bool),
at: Point<Pixels>,
) -> Option<Point<Pixels>> {
let offset = to_f32(handle.offset());
let max = to_f32(handle.max_offset());
let origin = to_f32(bounds.origin);
let size = point(f32::from(bounds.size.width), f32::from(bounds.size.height));
let local = point(f32::from(at.x) - origin.x, f32::from(at.y) - origin.y);
if v_bar && local.x >= size.x - THICKNESS {
let thumb = thumb_span(size.y, size.y, size.y + max.y, offset.y);
let y = track_page(local.y, thumb, offset.y, size.y, max.y)?;
return Some(point(px(offset.x), px(y)));
}
if h_bar && local.y >= size.y - THICKNESS {
let track = size.x - if v_bar { THICKNESS } else { 0. };
let thumb = thumb_span(track, size.x, size.x + max.x, offset.x);
if local.x > track {
return None;
}
let x = track_page(local.x, thumb, offset.x, size.x, max.x)?;
return Some(point(px(x), px(offset.y)));
}
None
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ScrollBarOrientation {
#[default]
Vertical,
Horizontal,
}
impl ScrollBarOrientation {
pub const ALL: [ScrollBarOrientation; 2] = [Self::Vertical, Self::Horizontal];
pub const fn name(self) -> &'static str {
match self {
Self::Vertical => "vertical",
Self::Horizontal => "horizontal",
}
}
}
#[derive(IntoElement, Default)]
pub struct ScrollBar {
orientation: ScrollBarOrientation,
}
impl ScrollBar {
pub fn new() -> Self {
Self::default()
}
pub fn orientation(mut self, orientation: ScrollBarOrientation) -> Self {
self.orientation = orientation;
self
}
}
impl RenderOnce for ScrollBar {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
gpui_kit::Empty
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
#[test]
fn scrollbar_look_follows_custom_css() {
crate::theme::tests_set(CarbonTheme::White);
let look = ScrollbarLook::current();
assert_eq!(look.thumb, color(ColorRole::BorderStrong01));
assert_eq!(look.track.a, 0.);
assert_eq!(THICKNESS, 8.);
assert_eq!(FOCUS_WIDTH, 2.);
}
#[test]
fn keyboard_scrolls_like_a_focused_browser_scroller() {
let max = point(100., 800.);
let view = point(280., 180.);
let at = |x, y| point(x, y);
let step =
|key, shift, offset| keyboard_scroll(key, shift, offset, max, view, ScrollAxes::Both);
assert_eq!(step("down", false, at(0., 0.)), Some(at(0., -40.)));
assert_eq!(step("up", false, at(0., 0.)), None);
assert_eq!(step("pagedown", false, at(0., 0.)), Some(at(0., -157.5)));
assert_eq!(step("space", false, at(0., 0.)), Some(at(0., -157.5)));
assert_eq!(step("space", true, at(0., -157.5)), Some(at(0., 0.)));
assert_eq!(step("end", false, at(0., 0.)), Some(at(0., -800.)));
assert_eq!(step("down", false, at(0., -790.)), Some(at(0., -800.)));
assert_eq!(step("home", false, at(0., -300.)), Some(at(0., 0.)));
assert_eq!(step("right", false, at(0., 0.)), Some(at(-40., 0.)));
assert_eq!(step("a", false, at(0., 0.)), None);
assert_eq!(
keyboard_scroll("right", false, at(0., 0.), max, view, ScrollAxes::Vertical),
None
);
}
#[test]
fn horizontal_keys_scroll_a_wide_area() {
let max = point(360., 0.);
let view = point(280., 180.);
let step = |key, offset, axes| keyboard_scroll(key, false, offset, max, view, axes);
assert_eq!(
step("right", point(0., 0.), ScrollAxes::Horizontal),
Some(point(-40., 0.))
);
assert_eq!(
step("left", point(-40., 0.), ScrollAxes::Horizontal),
Some(point(0., 0.))
);
assert_eq!(
step("end", point(0., 0.), ScrollAxes::Horizontal),
Some(point(-360., 0.))
);
assert_eq!(step("right", point(-360., 0.), ScrollAxes::Both), None);
}
#[test]
fn thumb_follows_base_geometry() {
assert_eq!(thumb_span(180., 180., 720., 0.), (0., 45.));
assert_eq!(thumb_span(180., 180., 720., -540.), (135., 45.));
assert_eq!(thumb_span(100., 100., 100_000., 0.).1, MIN_THUMB);
}
#[test]
fn a_track_press_pages_one_viewport_toward_the_pointer() {
let thumb = thumb_span(180., 180., 720., -270.);
assert_eq!(track_page(170., thumb, -270., 180., 540.), Some(-450.));
assert_eq!(track_page(5., thumb, -270., 180., 540.), Some(-90.));
assert_eq!(track_page(thumb.0 + 1., thumb, -270., 180., 540.), None);
assert_eq!(track_page(170., thumb, -500., 180., 540.), Some(-540.));
assert_eq!(track_page(0., (50., 45.), -20., 180., 540.), Some(0.));
}
#[test]
fn shift_turns_a_vertical_wheel_sideways() {
let wheel = point(0., -30.);
assert_eq!(shift_wheel(true, wheel, 0., 360.), Some(-30.));
assert_eq!(shift_wheel(false, wheel, 0., 360.), None);
assert_eq!(shift_wheel(true, point(-30., 0.), 0., 360.), None);
assert_eq!(shift_wheel(true, wheel, 0., 0.), None);
assert_eq!(shift_wheel(true, wheel, -360., 360.), None);
assert_eq!(shift_wheel(true, point(0., 50.), -20., 360.), Some(0.));
}
#[test]
fn bars_show_only_on_overflowing_scroll_axes() {
let max = point(px(360.), px(0.));
assert_eq!(bars(ScrollAxes::Both, max), (false, true));
assert_eq!(bars(ScrollAxes::Horizontal, max), (false, true));
assert_eq!(bars(ScrollAxes::Vertical, max), (false, false));
let both = point(px(10.), px(10.));
assert_eq!(bars(ScrollAxes::Both, both), (true, true));
}
}