Closed flukejones closed 1 year ago
The same is also true of a callback with array arg. Seems to not be possible to pass an array in from rust:
callback set-toolbar([Parameter]);
set-toolbar(parameters) => {
page-welding.parameters = parameters;
}
results in:
# [allow (dead_code)] pub fn set_parameters (& self , value : slint :: private_unstable_api :: re_exports :: ModelRc < r#Parameter >) {
And also not possible to pass nested arrays.
Uh... okay, so maybe the model documentation needs more work. I still don't fully understand it but on a whim I tried:
choices: VecModel::from_slice(&vec![ParameterChoice {
icon: Image::load_from_path(&PathBuf::from(
"gui/slint/ui/images/parameters/gas.png",
)).unwrap(),
index: 42,
label: "Test 1".to_string().into(),
},
ParameterChoice {
icon: Image::load_from_path(&PathBuf::from(
"gui/slint/ui/images/parameters/gas.png",
)).unwrap(),
index: 42,
label: "Test 2".to_string().into(),
}]),
VecModel
transforms to ModelRc
?
You've noticed correctly, arrays in .slint files map to models in Rust. I'll try to improve the docs on this. There's evidently something missing to make this obvious, especially that this also applies to fields of structs.
Regarding VecModel
and ModelRc
, there are three ways of instantiating VecModel
:
Default
traitFrom
trait for Vec<T>
&[T]
slice via from_slice
.The first two options create a VecModel<T>
and that still needs an explicit step to turn it into a Rc<dyn Model>
( aka ModelRc
) via ModelRc::new(m: impl Model<..>)
. But you have the VecModel<T>
around and can use its API to make further changes. The third option, creating a VecModel
from a slice, assumes that you may want to use this right away and not do any further mutations, so the concrete model type is erased right away and VecModel<T>::from_slice
already returns Rc<dyn Model>
(ModelRc
).
Finally, for a given ModelRc
, you can always try to go back to the concrete model via as_any()
and then downcast_ref()
.
I created https://github.com/slint-ui/slint/pull/2791 . I'm still wondering about whether we should offer From<&[T]>
for ModelRc
. On the one hand it's tempting, on the other hand it disguises the underlying VecModel
, so it's harder to understand that if you want to make changes later that's the type you need to downcast_ref
to.
@tronical I would say yes to including a From
conversion. As far as i can see in my own work the only time I will be touching ModelRc
is when I need to put data in the UI - and I will be using [<T>]
or Vec
up until that point.
I have two exported structs such:
Parameter
is used in a for loop, withchoices
being a nested for. The property is bound all the way to root for rust access.And I try to work with them as:
I noticed that trying to instantiate
choices
as an array was an error, and onlychoices: ModelRc::new(ParameterChoice {
would work. This looks to be due to the generated code being incorrect:where
choices
is aModelRc
for some reason. Use of the struct including the incorrect field works fine in a for loop in .slint still, just not correct.