Hide scrollbar when scroll are is small than container. (#71)

This commit is contained in:
Jason Lee 2024-07-25 15:53:57 +08:00 committed by GitHub
parent f02c2a82b1
commit 2396c918f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 2 deletions

View file

@ -43,9 +43,12 @@ impl ScrollableStory {
} else if n == 1 { } else if n == 1 {
self.items = (0..100).map(|i| format!("Item {}", i)).collect::<Vec<_>>(); self.items = (0..100).map(|i| format!("Item {}", i)).collect::<Vec<_>>();
self.test_width = px(10000.); self.test_width = px(10000.);
} else { } else if n == 2 {
self.items = (0..500).map(|i| format!("Item {}", i)).collect::<Vec<_>>(); self.items = (0..500).map(|i| format!("Item {}", i)).collect::<Vec<_>>();
self.test_width = px(10000.); self.test_width = px(10000.);
} else {
self.items = (0..5).map(|i| format!("Item {}", i)).collect::<Vec<_>>();
self.test_width = px(10000.);
} }
self.scroll_state.set(ScrollbarState::default()); self.scroll_state.set(ScrollbarState::default());
cx.notify(); cx.notify();
@ -87,6 +90,13 @@ impl Render for ScrollableStory {
view.change_test_cases(2, cx); view.change_test_cases(2, cx);
})), })),
) )
.child(
Button::new("test-3", cx)
.label("Size 3")
.on_click(cx.listener(|view, _, cx| {
view.change_test_cases(3, cx);
})),
)
.child(Divider::vertical().px_2()) .child(Divider::vertical().px_2())
.child( .child(
Button::new("test-axis-both", cx) Button::new("test-axis-both", cx)

View file

@ -131,6 +131,8 @@ impl ScrollbarAxis {
match self { match self {
Self::Vertical => vec![Self::Vertical], Self::Vertical => vec![Self::Vertical],
Self::Horizontal => vec![Self::Horizontal], Self::Horizontal => vec![Self::Horizontal],
// This should keep vertical first, vertical is the primary axis
// if vertical not need display, then horizontal will not keep right margin.
Self::Both => vec![Self::Vertical, Self::Horizontal], Self::Both => vec![Self::Vertical, Self::Horizontal],
} }
} }
@ -293,7 +295,7 @@ impl Element for Scrollbar {
cx: &mut gpui::WindowContext, cx: &mut gpui::WindowContext,
) { ) {
let hitbox_bounds = hitbox.bounds; let hitbox_bounds = hitbox.bounds;
let has_both = self.axis.is_both(); let mut has_both = self.axis.is_both();
cx.with_content_mask( cx.with_content_mask(
Some(ContentMask { Some(ContentMask {
@ -323,6 +325,12 @@ impl Element for Scrollbar {
px(0.) px(0.)
}; };
// Hide scrollbar, if the scroll area is smaller than the container.
if scroll_area_size <= container_size {
has_both = false;
continue;
}
let thumb_length = (container_size / scroll_area_size * container_size) let thumb_length = (container_size / scroll_area_size * container_size)
.max(px(MIN_THUMB_SIZE)); .max(px(MIN_THUMB_SIZE));
let thumb_start = -(scroll_position / (scroll_area_size - container_size) let thumb_start = -(scroll_position / (scroll_area_size - container_size)