Save last layout width in List to avoid unnecessary re-renders

This commit is contained in:
Antonio Scandurra 2021-09-01 12:20:11 +02:00
parent 477740360d
commit 0a22dee764

View file

@ -17,7 +17,7 @@ pub struct List {
#[derive(Clone)] #[derive(Clone)]
pub struct ListState(Rc<RefCell<StateInner>>); pub struct ListState(Rc<RefCell<StateInner>>);
#[derive(Clone, Copy, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Orientation { pub enum Orientation {
Top, Top,
Bottom, Bottom,
@ -47,6 +47,16 @@ enum ListItem {
Removed(f32), Removed(f32),
} }
impl std::fmt::Debug for ListItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unrendered => write!(f, "Unrendered"),
Self::Rendered(_) => f.debug_tuple("Rendered").finish(),
Self::Removed(height) => f.debug_tuple("Removed").field(height).finish(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
struct ListItemSummary { struct ListItemSummary {
count: usize, count: usize,
@ -228,7 +238,8 @@ impl Element for List {
drop(cursor); drop(cursor);
state.items = new_items; state.items = new_items;
state.rendered_range = new_rendered_range_start..new_rendered_range_end; state.rendered_range = new_rendered_range_start..new_rendered_range_end;
(constraint.max, scroll_top) state.last_layout_width = Some(size.x());
(size, scroll_top)
} }
fn paint(&mut self, bounds: RectF, scroll_top: &mut ScrollTop, cx: &mut PaintContext) { fn paint(&mut self, bounds: RectF, scroll_top: &mut ScrollTop, cx: &mut PaintContext) {