Unresponsive UI on task #3986
-
Hey. Sorry for the maybe dumb question, but I am trying out slint for the first time (and also a rust beginner). However looking at the docs, I am not sure how the communication should work in detail. Because most examples are quite basic. Currently I have everything in the callback I defined in the #[derive(Debug)]
enum Keytype {
Userkey,
Masterkey,
}
fn read_key_file<P: AsRef<Path>>(path: P) -> Result<KeyFile, Box<dyn Error>> {
let file = File::open(path).expect("Failed to open keyfile");
let reader = BufReader::new(file);
let u = serde_json::from_reader(reader)?;
Ok(u)
}
fn get_keytype(input: &str) -> Keytype {
match input {
"User" => Keytype::Userkey,
"Master" => Keytype::Masterkey,
_ => panic!("Unexpected keytype {}", input),
}
}
fn get_key_path(input: Keytype) -> String {
match input {
Keytype::Userkey => String::from("user_keys.json"),
Keytype::Masterkey => String::from("master_keys.json"),
}
}
fn main() -> Result<(), slint::PlatformError> {
let ui = AppUi::new()?;
let ui_handle = ui.as_weak();
ui.on_start_decrypt(move | password | {
let ui = ui_handle.unwrap();
let key = ui.get_current_key();
let key_type = get_keytype(&key);
let key_path = get_key_path(key_type);
let kf = read_key_file(key_path).expect("Failed to read keyfile.");
let wallet_result = Wallet::unlock(kf, &password);
match wallet_result {
Ok(w) => {
let _ = process_reports("reports.json", w).expect("Failed process reports");
}
Err(e) => ui.invoke_show_error_popup(),
};
});
ui.run()
}
Are there any examples on how to handle longer running tasks? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Since your task appears to be CPU-bound, I think you'll want to spawn a thread to do the work and eventually move the result back to the main thread to show in the UI. Slint provides |
Beta Was this translation helpful? Give feedback.
Since your task appears to be CPU-bound, I think you'll want to spawn a thread to do the work and eventually move the result back to the main thread to show in the UI. Slint provides
slint::invoke_from_event_loop
for this, and I think the example provided in the documentation should help.