Keep the component alive when the window is visible

show() now let's Slint maintain a strong reference to the component, so
that it's easy to create new windows without having to do an awkward
dance around keeping the component alive.

Closes #3328
This commit is contained in:
Simon Hausmann 2023-08-25 13:37:22 +02:00 committed by Simon Hausmann
parent 4dda627d14
commit de58d5e83c
4 changed files with 46 additions and 1 deletions

View file

@ -167,6 +167,7 @@ log = { version = "0.4.17", optional = true }
[dev-dependencies]
slint-build = { path = "../build" }
i-slint-backend-testing = { path = "../../../internal/backends/testing" }
i-slint-backend-winit = { path = "../../../internal/backends/winit", features = ["wayland", "x11", "renderer-software"] }
serde_json = "1.0.96"
serde = { version = "1.0.163", features = ["derive"] }

View file

@ -0,0 +1,34 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
use ::slint::slint;
// Sorry, can't test with rust test harness and multiple threads.
#[cfg(not(any(target_arch = "wasm32", target_os = "macos", target_os = "ios")))]
#[test]
fn show_maintains_strong_reference() {
slint::platform::set_platform(Box::new(i_slint_backend_winit::Backend::new())).unwrap();
slint!(export component TestWindow inherits Window {
callback root-clicked();
TouchArea {
clicked => { root.root-clicked(); }
}
});
let window = TestWindow::new().unwrap();
let window_weak = window.as_weak();
let window_weak_2 = window_weak.clone();
slint::Timer::single_shot(std::time::Duration::from_millis(20), move || {
window_weak_2.upgrade().unwrap().hide().unwrap();
slint::quit_event_loop().unwrap();
});
window.show().unwrap();
drop(window);
slint::run_event_loop().unwrap();
assert!(window_weak.upgrade().is_none());
}