Skip to content

Commit

Permalink
ListView: fix continuous redraw with fluent style
Browse files Browse the repository at this point in the history
Because we set the viewport-width several time to different value during
the layouting of the ListView, it will cause this property to always be
dirty. In particular, this causes permanent restart of the fluent's
scrollbar animation on the size of the handle, causing continous
repaints.
  • Loading branch information
ogoffart committed Jan 21, 2025
1 parent 5806b02 commit 7e37f19
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 36 deletions.
5 changes: 3 additions & 2 deletions api/cpp/include/slint.h
Original file line number Diff line number Diff line change
Expand Up @@ -1237,12 +1237,13 @@ class Repeater
float listview_width, float viewport_y) const
{
float offset = viewport_y;
viewport_width->set(listview_width);
auto vp_width = listview_width;
if (!inner)
return offset;
for (auto &x : inner->data) {
(*x.ptr)->listview_layout(&offset, viewport_width);
vp_width = std::max(vp_width, (*x.ptr)->listview_layout(&offset));
}
viewport_width->set(vp_width);
return offset;
}

Expand Down
12 changes: 3 additions & 9 deletions internal/compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2493,18 +2493,12 @@ fn generate_repeated_component(
Access::Public, // Because Repeater accesses it
Declaration::Function(Function {
name: "listview_layout".into(),
signature:
"(float *offset_y, const slint::private_api::Property<float> *viewport_width) const -> void"
.to_owned(),
signature: "(float *offset_y) const -> float".to_owned(),
statements: Some(vec![
"[[maybe_unused]] auto self = this;".into(),
"float vp_w = viewport_width->get();".to_owned(),

format!("{}.set(*offset_y);", p_y), // FIXME: shouldn't that be handled by apply layout?
format!("{}.set(*offset_y);", p_y),
format!("*offset_y += {}.get();", p_height),
format!("float w = {}.get();", p_width),
"if (vp_w < w)".to_owned(),
" viewport_width->set(w);".to_owned(),
format!("return {}.get();", p_width),
]),
..Function::default()
}),
Expand Down
9 changes: 2 additions & 7 deletions internal/compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1773,16 +1773,11 @@ fn generate_repeated_component(
fn listview_layout(
self: core::pin::Pin<&Self>,
offset_y: &mut sp::LogicalLength,
viewport_width: core::pin::Pin<&sp::Property<sp::LogicalLength>>,
) {
) -> sp::LogicalLength {
let _self = self;
let vp_w = viewport_width.get();
#p_y.set(*offset_y);
*offset_y += #p_height.get();
let w = #p_width.get();
if vp_w < w {
viewport_width.set(w);
}
#p_width.get()
}
}
} else {
Expand Down
18 changes: 9 additions & 9 deletions internal/core/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,10 @@ pub trait RepeatedItemTree:
///
/// offset_y is the `y` position where this item should be placed.
/// it should be updated to be to the y position of the next item.
fn listview_layout(
self: Pin<&Self>,
_offset_y: &mut LogicalLength,
_viewport_width: Pin<&Property<LogicalLength>>,
) {
///
/// Returns the item width
fn listview_layout(self: Pin<&Self>, _offset_y: &mut LogicalLength) -> LogicalLength {
LogicalLength::default()
}

/// Returns what's needed to perform the layout if this ItemTrees is in a box layout
Expand Down Expand Up @@ -971,15 +970,15 @@ impl<C: RepeatedItemTree + 'static> Repeater<C> {
self.data().project_ref().is_dirty.get();
self.data().project_ref().is_dirty.set(false);

viewport_width.set(listview_width);
let mut vp_width = listview_width;
let model = self.model();
let row_count = model.row_count();
let zero = LogicalLength::zero();
if row_count == 0 {
self.0.inner.borrow_mut().instances.clear();
viewport_height.set(zero);
viewport_y.set(zero);

viewport_width.set(vp_width);
return;
}

Expand Down Expand Up @@ -1136,7 +1135,7 @@ impl<C: RepeatedItemTree + 'static> Repeater<C> {
c.0 = RepeatedInstanceState::Clean;
}
if let Some(x) = c.1.as_ref() {
x.as_pin_ref().listview_layout(&mut y, viewport_width);
vp_width = vp_width.max(x.as_pin_ref().listview_layout(&mut y));
}
idx += 1;
if y >= listview_height {
Expand All @@ -1150,7 +1149,7 @@ impl<C: RepeatedItemTree + 'static> Repeater<C> {
if let Some(data) = model.row_data(idx) {
new_instance.update(idx, data);
}
new_instance.as_pin_ref().listview_layout(&mut y, viewport_width);
vp_width = vp_width.max(new_instance.as_pin_ref().listview_layout(&mut y));
indices_to_init.push(inner.instances.len());
inner.instances.push((RepeatedInstanceState::Clean, Some(new_instance)));
idx += 1;
Expand Down Expand Up @@ -1190,6 +1189,7 @@ impl<C: RepeatedItemTree + 'static> Repeater<C> {
inner.cached_item_height = (y - new_offset_y) / inner.instances.len() as Coord;
inner.anchor_y = inner.cached_item_height * inner.offset as Coord;
viewport_height.set(inner.cached_item_height * row_count as Coord);
viewport_width.set(vp_width);
let new_viewport_y = -inner.anchor_y + new_offset_y;
viewport_y.set(new_viewport_y);
inner.previous_viewport_y = new_viewport_y;
Expand Down
11 changes: 2 additions & 9 deletions internal/interpreter/dynamic_item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ impl RepeatedItemTree for ErasedItemTreeBox {
self.run_setup_code();
}

fn listview_layout(
self: Pin<&Self>,
offset_y: &mut LogicalLength,
viewport_width: Pin<&Property<LogicalLength>>,
) {
fn listview_layout(self: Pin<&Self>, offset_y: &mut LogicalLength) -> LogicalLength {
generativity::make_guard!(guard);
let s = self.unerase(guard);

Expand Down Expand Up @@ -177,10 +173,7 @@ impl RepeatedItemTree for ErasedItemTreeBox {
let h = LogicalLength::new(h);
let w = LogicalLength::new(w);
*offset_y += h;
let vp_w = viewport_width.get();
if vp_w < w {
viewport_width.set(w);
}
w
}

fn box_layout_data(self: Pin<&Self>, o: Orientation) -> BoxLayoutCellData {
Expand Down

0 comments on commit 7e37f19

Please sign in to comment.