project panel: Sticky dragging + do not move thumb when it's clicked (#13460)

/cc @mrnugget 
Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-06-24 12:51:32 +02:00 committed by GitHub
parent 9ef9baef6f
commit 145cd798c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 77 additions and 42 deletions

View file

@ -1,27 +1,33 @@
use std::ops::Range;
use std::{cell::Cell, ops::Range, rc::Rc};
use gpui::{
point, Bounds, ContentMask, Hitbox, MouseDownEvent, MouseMoveEvent, ScrollWheelEvent, Style,
UniformListScrollHandle,
point, AnyView, Bounds, ContentMask, Hitbox, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
ScrollWheelEvent, Style, UniformListScrollHandle,
};
use ui::{prelude::*, px, relative, IntoElement};
pub(crate) struct ProjectPanelScrollbar {
thumb: Range<f32>,
scroll: UniformListScrollHandle,
is_dragging_scrollbar: Rc<Cell<bool>>,
item_count: usize,
view: AnyView,
}
impl ProjectPanelScrollbar {
pub(crate) fn new(
thumb: Range<f32>,
scroll: UniformListScrollHandle,
is_dragging_scrollbar: Rc<Cell<bool>>,
view: AnyView,
item_count: usize,
) -> Self {
Self {
thumb,
scroll,
is_dragging_scrollbar,
item_count,
view,
}
}
}
@ -68,7 +74,6 @@ impl gpui::Element for ProjectPanelScrollbar {
_prepaint: &mut Self::PrepaintState,
cx: &mut ui::WindowContext,
) {
let hitbox_id = _prepaint.id;
cx.with_content_mask(Some(ContentMask { bounds }), |cx| {
let colors = cx.theme().colors();
let scrollbar_background = colors.scrollbar_track_border;
@ -77,32 +82,38 @@ impl gpui::Element for ProjectPanelScrollbar {
let thumb_offset = self.thumb.start * bounds.size.height;
let thumb_end = self.thumb.end * bounds.size.height;
let thumb_upper_left = point(bounds.origin.x, bounds.origin.y + thumb_offset);
let thumb_lower_right = point(
bounds.origin.x + bounds.size.width,
bounds.origin.y + thumb_end,
);
let thumb_percentage_size = self.thumb.end - self.thumb.start;
cx.paint_quad(gpui::fill(
Bounds::from_corners(thumb_upper_left, thumb_lower_right),
thumb_background,
));
let thumb_bounds = {
let thumb_upper_left = point(bounds.origin.x, bounds.origin.y + thumb_offset);
let thumb_lower_right = point(
bounds.origin.x + bounds.size.width,
bounds.origin.y + thumb_end,
);
Bounds::from_corners(thumb_upper_left, thumb_lower_right)
};
cx.paint_quad(gpui::fill(thumb_bounds, thumb_background));
let scroll = self.scroll.clone();
let item_count = self.item_count;
cx.on_mouse_event({
let scroll = self.scroll.clone();
let is_dragging = self.is_dragging_scrollbar.clone();
move |event: &MouseDownEvent, phase, _cx| {
if phase.bubble() && bounds.contains(&event.position) {
let scroll = scroll.0.borrow();
if let Some(last_height) = scroll.last_item_height {
let max_offset = item_count as f32 * last_height;
let percentage =
(event.position.y - bounds.origin.y) / bounds.size.height;
if !thumb_bounds.contains(&event.position) {
let scroll = scroll.0.borrow();
if let Some(last_height) = scroll.last_item_height {
let max_offset = item_count as f32 * last_height;
let percentage =
(event.position.y - bounds.origin.y) / bounds.size.height;
let percentage = percentage.min(1. - thumb_percentage_size);
scroll
.base_handle
.set_offset(point(px(0.), -max_offset * percentage));
let percentage = percentage.min(1. - thumb_percentage_size);
scroll
.base_handle
.set_offset(point(px(0.), -max_offset * percentage));
}
} else {
is_dragging.set(true);
}
}
}
@ -119,24 +130,30 @@ impl gpui::Element for ProjectPanelScrollbar {
}
}
});
let is_dragging = self.is_dragging_scrollbar.clone();
let view_id = self.view.entity_id();
cx.on_mouse_event(move |event: &MouseMoveEvent, _, cx| {
if event.dragging() && is_dragging.get() {
let scroll = scroll.0.borrow();
if let Some(last_height) = scroll.last_item_height {
let max_offset = item_count as f32 * last_height;
let percentage = (event.position.y - bounds.origin.y) / bounds.size.height;
cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| {
if phase.bubble() && bounds.contains(&event.position) && hitbox_id.is_hovered(cx) {
if event.dragging() {
let scroll = scroll.0.borrow();
if let Some(last_height) = scroll.last_item_height {
let max_offset = item_count as f32 * last_height;
let percentage =
(event.position.y - bounds.origin.y) / bounds.size.height;
let percentage = percentage.min(1. - thumb_percentage_size);
scroll
.base_handle
.set_offset(point(px(0.), -max_offset * percentage));
}
} else {
cx.stop_propagation();
let percentage = percentage.min(1. - thumb_percentage_size);
scroll
.base_handle
.set_offset(point(px(0.), -max_offset * percentage));
cx.notify(view_id);
}
} else {
is_dragging.set(false);
}
});
let is_dragging = self.is_dragging_scrollbar.clone();
cx.on_mouse_event(move |_event: &MouseUpEvent, phase, cx| {
if phase.bubble() {
is_dragging.set(false);
cx.notify(view_id);
}
});
})