-
Ok, trying to write something with slint in Rust, and utterly frustrated with it.
so in my main code I have something resembling: main.rs: ...
let ui = AppWindow::new();
let ui_1 = ui.as_weak();
let ui_2 = ui.as_weak();
...
ui.on_request_name_accepted(move || { let ui = ui_1.unwrap(); let model = ui.get_model_property(); println!("{model:?}"); });
ui.on_request_start_accepted(move || { let ui = ui_2.unwrap(); let model = ui.get_model_property(); println!("{model:?}"); });
ui.on_request_end_accepted(move || { let ui = ui_3.unwrap().let model = ui.get_model_property(); println!("{model:?}"); });
... appwindow.slint: struct Model {
name: string,
start: string,
end: string,
}
export component AppWindow inherits Window {
callback request_name_accepted();
callback request_start_accepted();
callback request_end_accepted();
in-out property<Model> model_property;
...
LineEdit {
placeholder-text: "Unnamed";
text: root.model_property.name;
accepted => { root.request-name-accepted(); }
}
... The callback in my rust code is being called, but all three fields of model are always "". |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
This is a one way binding. The text is assigned to the model, but not the other way arround. You need to update it in the accepted, for example
|
Beta Was this translation helpful? Give feedback.
-
I see this mostly as a shortcoming of Rust, since it appears lambdas can only use references (the default) or move values (when writing ui.on_request_name_accepted({ let ui = ui.as_weak(); move || { let model = ui.unwrap().get_model_property(); println!("{model:?}"); }});
ui.on_request_start_accepted({ let ui = ui.as_weak(); move || { let model = ui.unwrap().get_model_property(); println!("{model:?}"); }});
ui.on_request_end_accepted({ let ui = ui.as_weak(); move || { let model = ui.unwrap().get_model_property(); println!("{model:?}"); }}); It narrows the scope of the weak It might have been nice if the UI callbacks automatically received a reference to the UI as a parameter, but maybe there was a good reason to omit this. |
Beta Was this translation helpful? Give feedback.
I see this mostly as a shortcoming of Rust, since it appears lambdas can only use references (the default) or move values (when writing
move ||
, whereas C++ provides[=] {
to create a lambda based on copies. To avoid the annoying creation of variables likeui_1
,ui_2
, etc. I have gotten into the habit of using this nice Rust feature (not sure what to call it):