Closed AllenDang closed 3 years ago
Yes it's ok. Widget clones are cheap. You can also scope them within the set_callback like so:
input_url.handle({
let mut output = output.clone();
move |i, evt| match evt {
enums::Event::KeyUp => match app::event_key() {
enums::Key::Enter => {
on_execute(i.value(), &mut output);
false
}
_ => false,
},
_ => false,
}
});
ui.btn_download.set_callback({
let mut output = output.clone();
move |_| {
on_execute(input_url.value(), &mut output);
}
});
There are also other event handling strategies like passing messages and defining your own Event types: https://github.com/fltk-rs/fltk-rs/wiki/Events
I indent to have a event handler which accept a reference of an Output widget, and use it to display some result log.
Idealy I could do it by:
on_execute
by passin&mut output
as parameter in every desired events.Compiler told me this is not allowed, because the reference of
&mut output
could not be borrowed multiple times.So I tried below code.
It works, but I'm not sure this is correct.
I have to clone output widget each time before invoking
on_execute
, is this the right pattern? Or I'm on the wrong track?Sorry to ask such a basic question, I already spent few hours on trying to understand closure and borrow, still not very clear about whether it's the right thing to do.