chore: reduce component overhead (#3)
Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
+20
-301
@@ -1,17 +1,13 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
//! A scrollable container with an overlay scrollbar.
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, Bounds, DispatchPhase, ElementId, Entity, EntityId, InteractiveElement as _, IntoElement,
|
||||
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, RenderOnce, ScrollEvent,
|
||||
ScrollHandle, SharedString, StatefulInteractiveElement as _, Styled, Task, Window, canvas, div,
|
||||
fill, point,
|
||||
App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce, ScrollHandle,
|
||||
SharedString, StatefulInteractiveElement as _, Styled, Window, div,
|
||||
};
|
||||
use gpui_native_theme::ActiveTheme as _;
|
||||
|
||||
use crate::common::StyledExt;
|
||||
use crate::scrollbar::{Scrollbar, ScrollbarVisibility};
|
||||
|
||||
/// Which directions the content scrolls in.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
|
||||
@@ -25,108 +21,6 @@ pub enum ScrollAxes {
|
||||
Both,
|
||||
}
|
||||
|
||||
/// When an overlay scrollbar shows.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum ScrollbarVisibility {
|
||||
/// The running platform's behavior, from the theme.
|
||||
#[default]
|
||||
Platform,
|
||||
Always,
|
||||
Autohide,
|
||||
}
|
||||
|
||||
/// How long an autohide thumb stays fully visible before its fade begins.
|
||||
const AUTOHIDE_DELAY: Duration = Duration::from_millis(700);
|
||||
/// How long the autohide fade runs.
|
||||
const AUTOHIDE_FADE: Duration = Duration::from_millis(250);
|
||||
/// How often the fade repaints while it runs.
|
||||
const AUTOHIDE_FADE_STEP: Duration = Duration::from_millis(50);
|
||||
|
||||
/// The shared autohide timeline for an overlay scrollbar.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Autohide {
|
||||
view: EntityId,
|
||||
state: Entity<AutohideState>,
|
||||
}
|
||||
|
||||
struct AutohideState {
|
||||
last_activity: Option<Instant>,
|
||||
hide_task: Option<Task<()>>,
|
||||
}
|
||||
|
||||
impl Autohide {
|
||||
pub(crate) fn new(key: ElementId, window: &mut Window, cx: &mut App) -> Self {
|
||||
let view = window.current_view();
|
||||
let state = window.use_keyed_state(key, cx, |_, _| AutohideState {
|
||||
last_activity: None,
|
||||
hide_task: None,
|
||||
});
|
||||
Self { view, state }
|
||||
}
|
||||
|
||||
/// Marks interaction.
|
||||
pub(crate) fn touch(&self, window: &mut Window, cx: &mut App) {
|
||||
let view = self.view;
|
||||
let reduce_motion = cx.theme().accessibility.reduce_motion;
|
||||
|
||||
let task = window.spawn(cx, async move |cx| {
|
||||
if reduce_motion {
|
||||
cx.background_executor()
|
||||
.timer(AUTOHIDE_DELAY + AUTOHIDE_FADE)
|
||||
.await;
|
||||
cx.update(|_, cx| cx.notify(view)).ok();
|
||||
return;
|
||||
}
|
||||
|
||||
let steps = (AUTOHIDE_FADE.as_millis() / AUTOHIDE_FADE_STEP.as_millis()).max(1);
|
||||
cx.background_executor().timer(AUTOHIDE_DELAY).await;
|
||||
|
||||
for _ in 0..steps {
|
||||
cx.update(|_, cx| cx.notify(view)).ok();
|
||||
cx.background_executor().timer(AUTOHIDE_FADE_STEP).await;
|
||||
}
|
||||
|
||||
cx.update(|_, cx| cx.notify(view)).ok();
|
||||
});
|
||||
|
||||
self.state.update(cx, |state, _| {
|
||||
state.last_activity = Some(Instant::now());
|
||||
state.hide_task = Some(task);
|
||||
});
|
||||
}
|
||||
|
||||
/// The thumb's opacity.
|
||||
pub(crate) fn alpha(&self, cx: &App, dragging: bool) -> f32 {
|
||||
if dragging {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
let Some(activity) = self.state.read(cx).last_activity else {
|
||||
return 0.0;
|
||||
};
|
||||
|
||||
let idle = activity.elapsed();
|
||||
if idle <= AUTOHIDE_DELAY {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
let fade = idle.saturating_sub(AUTOHIDE_DELAY);
|
||||
if fade >= AUTOHIDE_FADE {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
1.0 - fade.as_secs_f32() / AUTOHIDE_FADE.as_secs_f32()
|
||||
}
|
||||
}
|
||||
|
||||
/// An overlay scrollbar thumb's style for one paint: `None` hides it.
|
||||
pub(crate) fn thumb_style(color: gpui::Hsla, alpha: f32) -> Option<gpui::Hsla> {
|
||||
(alpha > 0.0).then_some(gpui::Hsla {
|
||||
a: color.a * alpha,
|
||||
..color
|
||||
})
|
||||
}
|
||||
|
||||
/// A scrollable container with an overlay scrollbar.
|
||||
#[derive(IntoElement)]
|
||||
pub struct ScrollArea {
|
||||
@@ -196,32 +90,8 @@ impl ScrollArea {
|
||||
}
|
||||
}
|
||||
|
||||
/// Names keyed state under the scrollbar's own id.
|
||||
pub fn scrollbar_state_key(name: &'static str, id: &ElementId) -> ElementId {
|
||||
ElementId::NamedChild(Arc::new(id.clone()), SharedString::from(name))
|
||||
}
|
||||
|
||||
/// Pure thumb geometry for an overlay scrollbar.
|
||||
pub(crate) fn thumb_geometry(
|
||||
viewport: Pixels,
|
||||
content: Pixels,
|
||||
offset: Pixels,
|
||||
track: Pixels,
|
||||
min_thumb: Pixels,
|
||||
) -> (Pixels, Pixels) {
|
||||
if content <= viewport || content <= Pixels::ZERO {
|
||||
return (track, Pixels::ZERO);
|
||||
}
|
||||
|
||||
let length = (viewport / content * track).max(min_thumb).min(track);
|
||||
let overflow = content - viewport;
|
||||
let progress = (-offset / overflow).clamp(0., 1.);
|
||||
let travel = track - length;
|
||||
(length, travel * progress)
|
||||
}
|
||||
|
||||
impl RenderOnce for ScrollArea {
|
||||
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
|
||||
let mut pane_style = self.style.clone();
|
||||
pane_style.padding = Default::default();
|
||||
|
||||
@@ -252,175 +122,24 @@ impl RenderOnce for ScrollArea {
|
||||
|
||||
if let Some(handle) = self.handle {
|
||||
let id = self.id.clone();
|
||||
let autohide = match self.visibility {
|
||||
ScrollbarVisibility::Platform => cx.theme().scrollbar.autohide,
|
||||
ScrollbarVisibility::Always => false,
|
||||
ScrollbarVisibility::Autohide => true,
|
||||
};
|
||||
|
||||
wrapper.extend(std::iter::once(
|
||||
canvas(|bounds, _, _| bounds, {
|
||||
let handle = handle.clone();
|
||||
move |overlay, _, window, cx| {
|
||||
paint_scrollbar(&handle, &id, autohide, overlay, window, cx);
|
||||
}
|
||||
})
|
||||
.absolute()
|
||||
.inset_0()
|
||||
.into_any_element(),
|
||||
));
|
||||
if self.axes != ScrollAxes::Horizontal {
|
||||
wrapper.extend(std::iter::once(
|
||||
Scrollbar::vertical((id.clone(), "v-scrollbar"), handle.clone())
|
||||
.visibility(self.visibility)
|
||||
.into_any_element(),
|
||||
));
|
||||
}
|
||||
|
||||
if self.axes != ScrollAxes::Vertical {
|
||||
wrapper.extend(std::iter::once(
|
||||
Scrollbar::horizontal((id, "h-scrollbar"), handle)
|
||||
.visibility(self.visibility)
|
||||
.into_any_element(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
wrapper
|
||||
}
|
||||
}
|
||||
|
||||
/// Paints the overlay scrollbar thumb.
|
||||
fn paint_scrollbar(
|
||||
handle: &ScrollHandle,
|
||||
key: &ElementId,
|
||||
autohide: bool,
|
||||
overlay: Bounds<Pixels>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
let theme = cx.theme();
|
||||
let view = window.current_view();
|
||||
|
||||
let color = theme.colors.scrollbar_thumb;
|
||||
let width = theme.scrollbar.width;
|
||||
let inset = theme.scrollbar.inset;
|
||||
let min_thumb = theme.scrollbar.min_thumb;
|
||||
|
||||
let viewport = handle.bounds();
|
||||
let max_offset = handle.max_offset().y;
|
||||
|
||||
if max_offset <= Pixels::ZERO || viewport.size.height <= Pixels::ZERO {
|
||||
return;
|
||||
}
|
||||
|
||||
let autohide_state =
|
||||
autohide.then(|| Autohide::new(scrollbar_state_key("autohide", key), window, cx));
|
||||
let drag = window.use_keyed_state(scrollbar_state_key("thumb-drag", key), cx, |_, _| {
|
||||
Option::<(Pixels, Pixels)>::None
|
||||
});
|
||||
let dragging = drag.read(cx).is_some();
|
||||
|
||||
let alpha = match (&autohide_state, autohide) {
|
||||
(Some(state), true) => state.alpha(cx, dragging),
|
||||
_ => 1.0,
|
||||
};
|
||||
|
||||
let track = overlay.size.height - inset * 2.0;
|
||||
let (thumb_length, thumb_top) = thumb_geometry(
|
||||
viewport.size.height,
|
||||
viewport.size.height + max_offset,
|
||||
handle.offset().y,
|
||||
track,
|
||||
min_thumb,
|
||||
);
|
||||
|
||||
let thumb_x = overlay.right() - width - inset;
|
||||
let thumb_bounds = Bounds::from_corners(
|
||||
point(thumb_x, overlay.top() + inset + thumb_top),
|
||||
point(
|
||||
thumb_x + width,
|
||||
overlay.top() + inset + thumb_top + thumb_length,
|
||||
),
|
||||
);
|
||||
|
||||
if let Some(color) = thumb_style(color, alpha) {
|
||||
window.paint_quad(fill(thumb_bounds, color).corner_radii(width / 2.0));
|
||||
}
|
||||
|
||||
if let Some(state) = &autohide_state {
|
||||
window.on_mouse_event({
|
||||
let state = state.clone();
|
||||
move |event: &ScrollEvent, phase, window, cx| {
|
||||
if phase != DispatchPhase::Bubble || !overlay.contains(&event.position) {
|
||||
return;
|
||||
}
|
||||
state.touch(window, cx);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
window.on_mouse_event({
|
||||
let handle = handle.clone();
|
||||
let drag = drag.clone();
|
||||
let autohide_state = autohide_state.clone();
|
||||
move |event: &MouseDownEvent, phase, window, cx| {
|
||||
if phase != DispatchPhase::Bubble
|
||||
|| event.button != gpui::MouseButton::Left
|
||||
|| !thumb_bounds.contains(&event.position)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
drag.update(cx, |drag, _| {
|
||||
*drag = Some((event.position.y, handle.offset().y));
|
||||
});
|
||||
|
||||
if let Some(state) = &autohide_state {
|
||||
state.touch(window, cx);
|
||||
}
|
||||
|
||||
cx.stop_propagation();
|
||||
}
|
||||
});
|
||||
|
||||
window.on_mouse_event({
|
||||
let drag = drag.clone();
|
||||
let autohide_state = autohide_state.clone();
|
||||
move |event: &MouseUpEvent, phase, window, cx| {
|
||||
if phase != DispatchPhase::Bubble || event.button != gpui::MouseButton::Left {
|
||||
return;
|
||||
}
|
||||
|
||||
if drag.read(cx).is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
drag.update(cx, |drag, _| {
|
||||
*drag = None;
|
||||
});
|
||||
|
||||
if let Some(state) = &autohide_state {
|
||||
state.touch(window, cx);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
window.on_mouse_event({
|
||||
let handle = handle.clone();
|
||||
let drag = drag.clone();
|
||||
let autohide_state = autohide_state.clone();
|
||||
move |event: &MouseMoveEvent, phase, window, cx| {
|
||||
if phase != DispatchPhase::Bubble {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(state) = &autohide_state
|
||||
&& drag.read(cx).is_some()
|
||||
{
|
||||
state.touch(window, cx);
|
||||
}
|
||||
|
||||
if event.pressed_button != Some(gpui::MouseButton::Left) {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some((start_y, start_offset)) = *drag.read(cx) else {
|
||||
return;
|
||||
};
|
||||
|
||||
if max_offset > Pixels::ZERO && track > thumb_length {
|
||||
let scale = max_offset / (track - thumb_length);
|
||||
let offset = (start_offset - (event.position.y - start_y) * scale)
|
||||
.clamp(-max_offset, Pixels::ZERO);
|
||||
handle.set_offset(point(handle.offset().x, offset));
|
||||
cx.notify(view);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user