diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rs index 2b4a3c84e8111796bf7ce32a4c6ad83854ded6fd..2529c425dafe818b5e0ecb82bb6df5c8abc23d8b 100644 --- a/crates/gpui/src/elements/div.rs +++ b/crates/gpui/src/elements/div.rs @@ -16,12 +16,12 @@ //! constructed by combining these two systems into an all-in-one element. use crate::{ - AbsoluteLength, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, App, Bounds, ClickEvent, - DispatchPhase, Display, Element, ElementId, Entity, FocusHandle, Global, GlobalElementId, - Hitbox, HitboxBehavior, HitboxId, InspectorElementId, IntoElement, IsZero, KeyContext, - KeyDownEvent, KeyUpEvent, KeyboardButton, KeyboardClickEvent, LayoutId, ModifiersChangedEvent, - MouseButton, MouseClickEvent, MouseDownEvent, MouseMoveEvent, MousePressureEvent, MouseUpEvent, - Overflow, ParentElement, Pixels, Point, Render, ScrollWheelEvent, SharedString, Size, Style, + Action, AnyDrag, AnyElement, AnyTooltip, AnyView, App, Bounds, ClickEvent, DispatchPhase, + Display, Element, ElementId, Entity, FocusHandle, Global, GlobalElementId, Hitbox, + HitboxBehavior, HitboxId, InspectorElementId, IntoElement, IsZero, KeyContext, KeyDownEvent, + KeyUpEvent, KeyboardButton, KeyboardClickEvent, LayoutId, ModifiersChangedEvent, MouseButton, + MouseClickEvent, MouseDownEvent, MouseMoveEvent, MousePressureEvent, MouseUpEvent, Overflow, + ParentElement, Pixels, Point, Render, ScrollWheelEvent, SharedString, Size, Style, StyleRefinement, Styled, Task, TooltipId, Visibility, Window, WindowControlArea, point, px, size, }; @@ -1146,15 +1146,6 @@ pub trait StatefulInteractiveElement: InteractiveElement { self } - /// Set the space to be reserved for rendering the scrollbar. - /// - /// This will only affect the layout of the element when overflow for this element is set to - /// `Overflow::Scroll`. - fn scrollbar_width(mut self, width: impl Into) -> Self { - self.interactivity().base_style.scrollbar_width = Some(width.into()); - self - } - /// Track the scroll state of this element with the given handle. fn track_scroll(mut self, scroll_handle: &ScrollHandle) -> Self { self.interactivity().tracked_scroll_handle = Some(scroll_handle.clone()); diff --git a/crates/gpui/src/styled.rs b/crates/gpui/src/styled.rs index 3d0b86a9523f5ac05e51941c826e32379368c464..873e248488fe3740d21e26556c4387780590541a 100644 --- a/crates/gpui/src/styled.rs +++ b/crates/gpui/src/styled.rs @@ -61,6 +61,15 @@ pub trait Styled: Sized { self } + /// Set the space to be reserved for rendering the scrollbar. + /// + /// This will only affect the layout of the element when overflow for this element is set to + /// `Overflow::Scroll`. + fn scrollbar_width(mut self, width: impl Into) -> Self { + self.style().scrollbar_width = Some(width.into()); + self + } + /// Sets the whitespace of the element to `normal`. /// [Docs](https://tailwindcss.com/docs/whitespace#normal) fn whitespace_normal(mut self) -> Self { diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rs index eaba1f22682a759d8cfce42e555ca692cee9ada6..652755a3cf7444d121df8b0cfa6b89db51e12777 100644 --- a/crates/terminal_view/src/terminal_view.rs +++ b/crates/terminal_view/src/terminal_view.rs @@ -1260,7 +1260,7 @@ impl Render for TerminalView { div.custom_scrollbars( Scrollbars::for_settings::() .show_along(ScrollAxes::Vertical) - .with_track_along( + .with_stable_track_along( ScrollAxes::Vertical, cx.theme().colors().editor_background, ) diff --git a/crates/ui/src/components/scrollbar.rs b/crates/ui/src/components/scrollbar.rs index 8e8e89be9c0580a7820685b5690a996dfd2dade0..e7c04ff9c7e85ba702f65f6c4347de304412f452 100644 --- a/crates/ui/src/components/scrollbar.rs +++ b/crates/ui/src/components/scrollbar.rs @@ -343,6 +343,7 @@ enum ReservedSpace { None, Thumb, Track, + StableTrack, } impl ReservedSpace { @@ -353,6 +354,14 @@ impl ReservedSpace { fn needs_scroll_track(&self) -> bool { *self == ReservedSpace::Track } + + fn needs_space_reserved(&self, max_offset: Pixels) -> bool { + match self { + Self::StableTrack => true, + Self::Track => !max_offset.is_zero(), + _ => false, + } + } } #[derive(Debug, Default, Clone, Copy)] @@ -479,6 +488,12 @@ impl Scrollbars { self } + pub fn with_stable_track_along(mut self, along: ScrollAxes, background_color: Hsla) -> Self { + self.visibility = along.apply_to(self.visibility, ReservedSpace::StableTrack); + self.track_color = Some(background_color); + self + } + pub fn width_sm(mut self) -> Self { self.scrollbar_width = ScrollbarWidth::Small; self @@ -713,13 +728,10 @@ impl ScrollbarState { fn space_to_reserve_for(&self, axis: ScrollbarAxis) -> Option { (self.show_state.is_disabled().not() - && self.visibility.along(axis).needs_scroll_track() && self - .scroll_handle() - .max_offset() + .visibility .along(axis) - .is_zero() - .not()) + .needs_space_reserved(self.scroll_handle().max_offset().along(axis))) .then(|| self.space_to_reserve()) }