Rewrite callback in a more future-proof and hackable way

This might be someone's first time dealing with callbacks and the dance
you have to do in Rust Slint code with weak handles. While the previous
code worked, if you try to expand the callback with multiple arguments
it quickly devolves into an unsolvable whack-a-mole game of confusing
compiler errors.

What other examples do and what actually scales is turning it into a
weak handle outside of the callback closure and having the top-level
closure move everything.
This commit is contained in:
Joshua Goins
2026-06-23 13:51:57 -04:00
committed by Olivier Goffart
parent d66f1cc74b
commit 0ce0692fe5

View File

@@ -8,12 +8,10 @@ slint::include_modules!();
fn main() -> Result<(), Box<dyn Error>> {
let ui = AppWindow::new()?;
ui.on_request_increase_value({
let ui_handle = ui.as_weak();
move || {
let ui = ui_handle.unwrap();
ui.set_counter(ui.get_counter() + 1);
}
let ui_handle = ui.as_weak();
ui.on_request_increase_value(move || {
let ui = ui_handle.unwrap();
ui.set_counter(ui.get_counter() + 1);
});
ui.run()?;