Update scroll view (#66)

This commit is contained in:
Jason Lee 2024-07-24 15:41:20 +08:00 committed by GitHub
parent 4edf1e3427
commit 22a15c7dcc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 10 deletions

View file

@ -7,7 +7,7 @@ use gpui::{
}; };
use ui::button::Button; use ui::button::Button;
use ui::divider::Divider; use ui::divider::Divider;
use ui::scroll::{scroll_view, Scrollbar, ScrollbarAxis, ScrollbarState}; use ui::scroll::{Scrollable, Scrollbar, ScrollbarAxis, ScrollbarState};
use ui::theme::ActiveTheme; use ui::theme::ActiveTheme;
use ui::{h_flex, v_flex, Clickable}; use ui::{h_flex, v_flex, Clickable};
@ -171,7 +171,6 @@ impl Render for ScrollableStory {
), ),
) )
.child({ .child({
let view = cx.view().clone();
let items = self.items.clone(); let items = self.items.clone();
let test_width = self.test_width; let test_width = self.test_width;
@ -181,13 +180,18 @@ impl Render for ScrollableStory {
.border_color(cx.theme().border) .border_color(cx.theme().border)
.w_full() .w_full()
.h(px(200.)) .h(px(200.))
.child(scroll_view("scrollview-1", view).content(move |cx| { .child(
v_flex().m_3().w(test_width).gap_1().children( v_flex()
items .m_3()
.iter() .w(test_width)
.map(|s| div().bg(cx.theme().card).child(s.clone())), .gap_1()
) .children(
})) items
.iter()
.map(|s| div().bg(cx.theme().card).child(s.clone())),
)
.scrollable("scroll-view1", cx.view().clone()),
)
}) })
} }
} }

View file

@ -2,6 +2,19 @@ mod scroll_view;
mod scrollable; mod scrollable;
mod scrollbar; mod scrollbar;
use gpui::{AnyElement, AnyView, Div, Element, ElementId};
pub use scroll_view::*; pub use scroll_view::*;
pub use scrollable::*; pub use scrollable::*;
pub use scrollbar::*; pub use scrollbar::*;
pub trait Scrollable: Element + Sized {
/// Wraps the element in a ScrollView.
///
/// Current this is only have a vertical scrollbar.
fn scrollable(self, id: impl Into<ElementId>, view: impl Into<AnyView>) -> ScrollView {
ScrollView::new(id.into(), view, ScrollbarAxis::Vertical).content(move |_| self)
}
}
impl Scrollable for AnyElement {}
impl Scrollable for Div {}

View file

@ -21,7 +21,11 @@ pub struct ScrollView {
} }
impl ScrollView { impl ScrollView {
fn new(id: impl Into<ElementId>, view: impl Into<AnyView>, axis: ScrollbarAxis) -> Self { pub(super) fn new(
id: impl Into<ElementId>,
view: impl Into<AnyView>,
axis: ScrollbarAxis,
) -> Self {
let view: AnyView = view.into(); let view: AnyView = view.into();
Self { Self {
id: ElementId::Name(SharedString::from(format!( id: ElementId::Name(SharedString::from(format!(