@@ -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<AbsoluteLength>) -> 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());
@@ -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<ScrollHandle: ScrollableHandle> Scrollbars<ScrollHandle> {
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<T: ScrollableHandle> ScrollbarState<T> {
fn space_to_reserve_for(&self, axis: ScrollbarAxis) -> Option<Pixels> {
(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())
}