Skip to content

Commit

Permalink
ListView: adapt the viewport's width based on the minimum-width of de…
Browse files Browse the repository at this point in the history
…legate

(Instead of their `width` property)
  • Loading branch information
ogoffart committed Jan 21, 2025
1 parent 7e37f19 commit 2df0b8b
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 24 deletions.
3 changes: 1 addition & 2 deletions internal/compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2487,7 +2487,6 @@ fn generate_repeated_component(
if let Some(listview) = &repeated.listview {
let p_y = access_member(&listview.prop_y, &ctx);
let p_height = access_member(&listview.prop_height, &ctx);
let p_width = access_member(&listview.prop_width, &ctx);

repeater_struct.members.push((
Access::Public, // Because Repeater accesses it
Expand All @@ -2498,7 +2497,7 @@ fn generate_repeated_component(
"[[maybe_unused]] auto self = this;".into(),
format!("{}.set(*offset_y);", p_y),
format!("*offset_y += {}.get();", p_height),
format!("return {}.get();", p_width),
"return layout_info({&static_vtable, const_cast<void *>(static_cast<const void *>(this))}, slint::cbindgen_private::Orientation::Horizontal).min;".into(),
]),
..Function::default()
}),
Expand Down
5 changes: 2 additions & 3 deletions internal/compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1768,16 +1768,15 @@ fn generate_repeated_component(
let extra_fn = if let Some(listview) = &repeated.listview {
let p_y = access_member(&listview.prop_y, &ctx).unwrap();
let p_height = access_member(&listview.prop_height, &ctx).unwrap();
let p_width = access_member(&listview.prop_width, &ctx).unwrap();
quote! {
fn listview_layout(
self: core::pin::Pin<&Self>,
self: ::core::pin::Pin<&Self>,
offset_y: &mut sp::LogicalLength,
) -> sp::LogicalLength {
let _self = self;
#p_y.set(*offset_y);
*offset_y += #p_height.get();
#p_width.get()
sp::LogicalLength::new(self.as_ref().layout_info(sp::Orientation::Horizontal).min)
}
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions internal/compiler/llr/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ pub struct ListViewInfo {
// In the repeated component context
pub prop_y: PropertyReference,
// In the repeated component context
pub prop_width: PropertyReference,
// In the repeated component context
pub prop_height: PropertyReference,
}

Expand Down
1 change: 0 additions & 1 deletion internal/compiler/llr/lower_to_item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,6 @@ fn lower_repeated_component(
listview_height: ctx.map_property_reference(&lv.listview_height),
listview_width: ctx.map_property_reference(&lv.listview_width),
prop_y: sc.mapping.map_property_reference(&geom.y, ctx.state),
prop_width: sc.mapping.map_property_reference(&geom.width, ctx.state),
prop_height: sc.mapping.map_property_reference(&geom.height, ctx.state),
});

Expand Down
1 change: 0 additions & 1 deletion internal/compiler/llr/optim_passes/count_property_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ pub fn count_property_use(root: &CompilationUnit) {
Some(ParentCtx::new(ctx, Some(idx as u32))),
);
visit_property(&lv.prop_y, &rep_ctx);
visit_property(&lv.prop_width, &rep_ctx);
visit_property(&lv.prop_height, &rep_ctx);
}
for idx in r.data_prop.iter().chain(r.index_prop.iter()) {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ 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.
///
/// Returns the item width
/// Returns the minimum item width which will be used to compute the listview's viewport width
fn listview_layout(self: Pin<&Self>, _offset_y: &mut LogicalLength) -> LogicalLength {
LogicalLength::default()
}
Expand Down
15 changes: 2 additions & 13 deletions internal/interpreter/dynamic_item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl RepeatedItemTree for ErasedItemTreeBox {
)
.expect("cannot set y");

let h: f32 = crate::eval::load_property(
let h: LogicalLength = crate::eval::load_property(
s.borrow_instance(),
&geom.height.element(),
geom.height.name(),
Expand All @@ -161,19 +161,8 @@ impl RepeatedItemTree for ErasedItemTreeBox {
.try_into()
.expect("height not the right type");

let w: f32 = crate::eval::load_property(
s.borrow_instance(),
&geom.width.element(),
geom.width.name(),
)
.expect("missing width")
.try_into()
.expect("width not the right type");

let h = LogicalLength::new(h);
let w = LogicalLength::new(w);
*offset_y += h;
w
LogicalLength::new(self.borrow().as_ref().layout_info(Orientation::Horizontal).min)
}

fn box_layout_data(self: Pin<&Self>, o: Orientation) -> BoxLayoutCellData {
Expand Down
19 changes: 18 additions & 1 deletion tests/cases/elements/listview.slint
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ TestCase := Window {
width: 400px;
height: 540px;

property <string> value;
in-out property <string> value;
out property listview-viewport-height <=> listview.viewport-height;
out property listview-viewport-width <=> listview.viewport-width;
out property listview-preferred-height <=> listview.preferred-height;

out property <bool> test: true;



listview := ListView {
for data in [
Expand All @@ -27,6 +34,7 @@ TestCase := Window {
text: data.text;
color: data.color;
font_size: 20px ;
min-width: 1500px;
}
}
TouchArea { clicked => { value = data.text; } }
Expand All @@ -40,18 +48,27 @@ auto handle = TestCase::create();
const TestCase &instance = *handle;
slint_testing::send_mouse_click(&instance, 5., 205.);
assert_eq(instance.get_value(), "Green");
assert(instance.get_test());
assert_eq(instance.get_listview_viewport_height(), 8. * 100.);
assert_eq(instance.get_listview_viewport_width(), 1500.);
```
```rust
let instance = TestCase::new().unwrap();
slint_testing::send_mouse_click(&instance, 5., 205.);
assert_eq!(instance.get_value(), "Green");
assert!(instance.get_test());
assert_eq!(instance.get_listview_viewport_height(), 8. * 100.);
assert_eq!(instance.get_listview_viewport_width(), 1500.);
```
```js
var instance = new slint.TestCase();
slintlib.private_api.send_mouse_click(instance, 5., 205.);
assert.equal(instance.value, "Green");
assert(instance.test);
assert.equal(instance.listview_viewport_height, 8. * 100.);
assert.equal(instance.listview_viewport_width, 1500.);
```
*/

0 comments on commit 2df0b8b

Please sign in to comment.