use gpui::{
AnyElement, Rgba, ScrollHandle, div, linear_color_stop, linear_gradient, point, prelude::*, px,
};
use crate::state::{ControlState, SWITCH_SLIDE};
pub const SCROLLBAR_MIN_THUMB: f32 = 24.0;
pub const SCROLLBAR_THICKNESS: f32 = 10.0;
pub const THUMB_THICKNESS: f32 = 4.0;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ScrollAxis {
Vertical,
Horizontal,
}
impl ScrollAxis {
pub fn other(self) -> Self {
match self {
ScrollAxis::Vertical => ScrollAxis::Horizontal,
ScrollAxis::Horizontal => ScrollAxis::Vertical,
}
}
}
pub struct ScrollDrag {
pub handle: ScrollHandle,
pub axis: ScrollAxis,
pub grab: f32,
}
pub struct ScrollbarGeometry {
pub track_start: f32,
pub track_len: f32,
pub track_end_inset: f32,
pub thumb_len: f32,
pub thumb_pos: f32,
pub max_offset: f32,
}
fn axis_metrics(handle: &ScrollHandle, axis: ScrollAxis) -> (f32, f32, f32, f32) {
let bounds = handle.bounds();
match axis {
ScrollAxis::Vertical => (
f32::from(bounds.top()),
f32::from(bounds.size.height),
f32::from(handle.max_offset().y),
f32::from(handle.offset().y),
),
ScrollAxis::Horizontal => (
f32::from(bounds.left()),
f32::from(bounds.size.width),
f32::from(handle.max_offset().x),
f32::from(handle.offset().x),
),
}
}
fn axis_scrollable(handle: &ScrollHandle, axis: ScrollAxis) -> bool {
let (_, viewport_len, max_offset, _) = axis_metrics(handle, axis);
max_offset > 0.5 && viewport_len > 0.0
}
pub fn scrollbar_geometry(handle: &ScrollHandle, axis: ScrollAxis) -> Option<ScrollbarGeometry> {
let (track_start, viewport_len, max_offset, offset) = axis_metrics(handle, axis);
if max_offset <= 0.5 || viewport_len <= 0.0 {
return None;
}
let track_end_inset = if axis_scrollable(handle, axis.other()) {
SCROLLBAR_THICKNESS
} else {
0.0
};
let track_len = (viewport_len - track_end_inset).max(1.0);
let content_len = viewport_len + max_offset;
let thumb_len = (track_len * viewport_len / content_len)
.max(SCROLLBAR_MIN_THUMB)
.min(track_len);
let usable = (track_len - thumb_len).max(0.0);
let fraction = (-offset / max_offset).clamp(0.0, 1.0);
Some(ScrollbarGeometry {
track_start,
track_len,
track_end_inset,
thumb_len,
thumb_pos: fraction * usable,
max_offset,
})
}
pub fn apply_scroll_drag(drag: &ScrollDrag, position: f32) {
let Some(geometry) = scrollbar_geometry(&drag.handle, drag.axis) else {
return;
};
let usable = (geometry.track_len - geometry.thumb_len).max(1.0);
let thumb_pos = (position - geometry.track_start - drag.grab).clamp(0.0, usable);
let fraction = thumb_pos / usable;
let target = -fraction * geometry.max_offset;
let current = drag.handle.offset();
match drag.axis {
ScrollAxis::Vertical => drag.handle.set_offset(point(current.x, px(target))),
ScrollAxis::Horizontal => drag.handle.set_offset(point(px(target), current.y)),
}
}
pub const SCROLL_FADE: f32 = 28.0;
pub fn scroll_fades(
state: &ControlState,
id: &str,
handle: &ScrollHandle,
axis: ScrollAxis,
start: Rgba,
end: Rgba,
) -> Vec<AnyElement> {
let (offset, max) = match axis {
ScrollAxis::Vertical => (
f32::from(handle.offset().y),
f32::from(handle.max_offset().y),
),
ScrollAxis::Horizontal => (
f32::from(handle.offset().x),
f32::from(handle.max_offset().x),
),
};
let scrollable = max > 0.5;
let wanted = |shown: bool| if scrollable && shown { 1.0 } else { 0.0 };
let at_start = state.tween(
format!("{id}-fade-start"),
wanted(offset < -0.5),
SWITCH_SLIDE,
);
let at_end = state.tween(
format!("{id}-fade-end"),
wanted(offset > -max + 0.5),
SWITCH_SLIDE,
);
let mut fades = Vec::new();
if at_start > 0.01 {
fades.push(edge_fade(axis, true, start, at_start));
}
if at_end > 0.01 {
fades.push(edge_fade(axis, false, end, at_end));
}
fades
}
fn edge_fade(axis: ScrollAxis, at_start: bool, surface: Rgba, opacity: f32) -> AnyElement {
let clear = crate::color::with_alpha(surface, 0.0);
let (near, far) = if at_start {
(surface, clear)
} else {
(clear, surface)
};
let angle = match axis {
ScrollAxis::Vertical => 180.0,
ScrollAxis::Horizontal => 90.0,
};
let background = linear_gradient(
angle,
linear_color_stop(near, 0.0),
linear_color_stop(far, 1.0),
);
let fade = div().absolute().opacity(opacity).bg(background);
match (axis, at_start) {
(ScrollAxis::Vertical, true) => fade.top_0().left_0().right_0().h(px(SCROLL_FADE)),
(ScrollAxis::Vertical, false) => fade.bottom_0().left_0().right_0().h(px(SCROLL_FADE)),
(ScrollAxis::Horizontal, true) => fade.left_0().top_0().bottom_0().w(px(SCROLL_FADE)),
(ScrollAxis::Horizontal, false) => fade.right_0().top_0().bottom_0().w(px(SCROLL_FADE)),
}
.into_any_element()
}