mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 14:21:16 +00:00
Implement sixtyfps::Weak
This is the counter-part to the strongly referencing type the compiler generates for components.
This commit is contained in:
parent
ff1d752bc6
commit
dd64226dc1
2 changed files with 53 additions and 0 deletions
|
@ -305,6 +305,48 @@ impl<T> ComponentWeakHandle<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// This trait describes the conversion of a strongly referenced SixtyFPS component,
|
||||
/// held by a [vtable::VRc] into a weak reference.
|
||||
pub trait IntoWeak {
|
||||
/// The type of the generated component.
|
||||
type Inner;
|
||||
/// Returns a new weak pointer.
|
||||
fn as_weak(&self) -> Weak<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
/// Internal function used when upgrading a weak reference to a strong one.
|
||||
fn from_inner(_: vtable::VRc<re_exports::ComponentVTable, Self::Inner>) -> Self;
|
||||
}
|
||||
|
||||
/// Struct that's used to hold weak references for SixtyFPS components.
|
||||
pub struct Weak<T: IntoWeak> {
|
||||
inner: vtable::VWeak<re_exports::ComponentVTable, T::Inner>,
|
||||
}
|
||||
|
||||
impl<T: IntoWeak> Weak<T> {
|
||||
#[doc(hidden)]
|
||||
pub fn new(rc: &vtable::VRc<re_exports::ComponentVTable, T::Inner>) -> Self {
|
||||
Self { inner: vtable::VRc::downgrade(&rc) }
|
||||
}
|
||||
|
||||
/// Returns a new strongly referenced component if some other instance still
|
||||
/// holds a strong reference. Otherwise, returns None.
|
||||
pub fn upgrade(&self) -> Option<T>
|
||||
where
|
||||
T: IntoWeak,
|
||||
{
|
||||
self.inner.upgrade().map(|inner| T::from_inner(inner))
|
||||
}
|
||||
|
||||
/// Convenience function that returns a new stronlyg referenced component if
|
||||
/// some other instance still holds a strong reference. Otherwise, this function
|
||||
/// panics.
|
||||
pub fn unwrap(&self) -> T {
|
||||
self.upgrade().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
/// This module contains functions useful for unit tests
|
||||
pub mod testing {
|
||||
/// This trait gives access to the underyling Window of a component for the
|
||||
|
|
|
@ -107,6 +107,7 @@ pub fn generate(doc: &Document, diag: &mut BuildDiagnostics) -> Option<TokenStre
|
|||
const _THE_SAME_VERSION_MUST_BE_USED_FOR_THE_COMPILER_AND_THE_RUNTIME : sixtyfps::#version_check = sixtyfps::#version_check;
|
||||
}
|
||||
pub use #compo_module::{#compo_id, #public_compo_id #(,#structs_ids)* };
|
||||
pub use sixtyfps::IntoWeak;
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -810,6 +811,16 @@ fn generate_component(
|
|||
#run_fun
|
||||
}
|
||||
|
||||
impl sixtyfps::IntoWeak for #public_struct {
|
||||
type Inner = #component_id;
|
||||
fn as_weak(&self) -> sixtyfps::Weak<Self> {
|
||||
sixtyfps::Weak::new(&self.0)
|
||||
}
|
||||
|
||||
fn from_inner(inner: vtable::VRc<sixtyfps::re_exports::ComponentVTable, #component_id>) -> Self {
|
||||
Self(inner)
|
||||
}
|
||||
}
|
||||
))
|
||||
} else {
|
||||
None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue