-
This minimal example essentially expresses my needs. [dependencies]
slint = "1.8.0"
async-compat = "0.2.4"
tokio = { version = "1.42.0", features = ["full"] } slint::slint! {
import { LineEdit, Button } from "std-widgets.slint";
export component AppMain inherits Window {
in property <string> username;
width: 100px;
height: 100px;
Text {
text: "Hello, " + username + "!";
}
}
export component AppLogin inherits Window {
property <string> user_id <=> l.text;
in property <bool> enabled <=> b.enabled;
callback login(string);
VerticalLayout {
padding: 10px;
spacing: 10px;
l := LineEdit { }
b := Button {
text: "Login";
clicked => {
login(user_id);
}
}
}
}
}
use tokio::time::{sleep, Duration};
fn main() {
let app_login = AppLogin::new().unwrap();
let app_main = AppMain::new().unwrap();
let main_weak = app_main.as_weak();
let login_weak = app_login.as_weak();
app_login.on_login(move |user_id| {
// clone ?
let main_weak_1 = main_weak.clone();
let login_weak_1 = login_weak.clone();
login_weak.upgrade().unwrap().set_enabled(false);
let f = async move {
if let Some(username) = login_req(&user_id).await {
let main_w = main_weak_1.upgrade().unwrap();
main_w.set_username(username.into());
main_w.show().unwrap();
login_weak_1.upgrade().unwrap().hide().unwrap();
} else {
login_weak_1.upgrade().unwrap().set_enabled(true);
}
};
slint::spawn_local(async_compat::Compat::new(f)).unwrap();
});
app_login.run().unwrap();
}
async fn login_req(user_id: &str) -> Option<String> {
println!("{} try Logging in...", user_id);
sleep(Duration::from_secs(2)).await;
match user_id {
"root" => Some("Administrator".into()),
_ => None,
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
ogoffart
Dec 19, 2024
Replies: 1 comment
-
Yes, this is correct. you indeed need to clone the weak. There is no way around it, it is unfortunate that Rust is so verbose with clone. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mcitem
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, this is correct. you indeed need to clone the weak. There is no way around it, it is unfortunate that Rust is so verbose with clone.