feat: update gpui-components

This commit is contained in:
2024-12-30 07:52:26 +07:00
parent 1c752accb2
commit 4628340459
9 changed files with 397 additions and 518 deletions

View File

@@ -1,18 +1,10 @@
use gpui::{
px, relative, AnyView, Bounds, ContentMask, Corners, Edges, Element, ElementId,
px, relative, Axis, Bounds, ContentMask, Corners, Edges, Element, ElementId, EntityId,
GlobalElementId, Hitbox, Hsla, IntoElement, IsZero as _, LayoutId, PaintQuad, Pixels, Point,
Position, ScrollHandle, ScrollWheelEvent, Style, WindowContext,
Position, ScrollHandle, ScrollWheelEvent, Size, Style, WindowContext,
};
/// The scroll axis direction.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollableAxis {
/// Horizontal scroll.
Horizontal,
/// Vertical scroll.
Vertical,
}
use crate::AxisExt;
/// Make a scrollable mask element to cover the parent view with the mouse wheel event listening.
///
@@ -20,21 +12,17 @@ pub enum ScrollableAxis {
/// You can use this `scroll_handle` to control what you want to scroll.
/// This is only can handle once axis scrolling.
pub struct ScrollableMask {
view: AnyView,
axis: ScrollableAxis,
view_id: EntityId,
axis: Axis,
scroll_handle: ScrollHandle,
debug: Option<Hsla>,
}
impl ScrollableMask {
/// Create a new scrollable mask element.
pub fn new(
view: impl Into<AnyView>,
axis: ScrollableAxis,
scroll_handle: &ScrollHandle,
) -> Self {
pub fn new(view_id: EntityId, axis: Axis, scroll_handle: &ScrollHandle) -> Self {
Self {
view: view.into(),
view_id,
scroll_handle: scroll_handle.clone(),
axis,
debug: None,
@@ -75,7 +63,7 @@ impl Element for ScrollableMask {
position: Position::Absolute,
flex_grow: 1.0,
flex_shrink: 1.0,
size: gpui::Size {
size: Size {
width: relative(1.).into(),
height: relative(1.).into(),
},
@@ -127,32 +115,37 @@ impl Element for ScrollableMask {
}
cx.on_mouse_event({
let view_id = self.view_id;
let is_horizontal = self.axis.is_horizontal();
let scroll_handle = self.scroll_handle.clone();
let hitbox = hitbox.clone();
let mouse_position = cx.mouse_position();
let scroll_handle = self.scroll_handle.clone();
let old_offset = scroll_handle.offset();
let view_id = self.view.entity_id();
let is_horizontal = self.axis == ScrollableAxis::Horizontal;
let last_offset = scroll_handle.offset();
move |event: &ScrollWheelEvent, phase, cx| {
if bounds.contains(&mouse_position) && phase.bubble() && hitbox.is_hovered(cx) {
let delta = event.delta.pixel_delta(line_height);
let mut offset = scroll_handle.offset();
let mut delta = event.delta.pixel_delta(line_height);
if is_horizontal && !delta.x.is_zero() {
// When is horizontal scroll, move the horizontal scroll handle to make scrolling.
let mut offset = scroll_handle.offset();
// Limit for only one way scrolling at same time.
// When use MacBook touchpad we may get both x and y delta,
// only allows the one that more to scroll.
if !delta.x.is_zero() && !delta.y.is_zero() {
if delta.x.abs() > delta.y.abs() {
delta.y = px(0.);
} else {
delta.x = px(0.);
}
}
if is_horizontal {
offset.x += delta.x;
scroll_handle.set_offset(offset);
}
if !is_horizontal && !delta.y.is_zero() {
// When is vertical scroll, move the vertical scroll handle to make scrolling.
let mut offset = scroll_handle.offset();
} else {
offset.y += delta.y;
scroll_handle.set_offset(offset);
}
if old_offset != scroll_handle.offset() {
if last_offset != offset {
scroll_handle.set_offset(offset);
cx.notify(Some(view_id));
cx.stop_propagation();
}