wgui: interactable components, rename TextLabel -> WidgetLabel

This commit is contained in:
Aleksander
2025-08-10 11:46:01 +02:00
parent 91e584383f
commit 93a3fee349
26 changed files with 294 additions and 174 deletions

View File

@@ -1,3 +1,5 @@
use std::rc::Rc;
use crate::{any::AnyTrait, event::EventAlterables, layout::LayoutState};
pub mod button;
@@ -8,6 +10,26 @@ pub struct InitData<'a> {
pub alterables: &'a mut EventAlterables,
}
pub trait Component: AnyTrait {
pub trait ComponentTrait: AnyTrait {
fn init(&self, data: &mut InitData);
}
#[derive(Clone)]
pub struct Component(pub Rc<dyn ComponentTrait>);
pub type ComponentWeak = std::rc::Weak<dyn ComponentTrait>;
impl Component {
pub fn weak(&self) -> ComponentWeak {
Rc::downgrade(&self.0)
}
pub fn try_cast<T: 'static>(&self) -> anyhow::Result<Rc<T>> {
if !(*self.0).as_any().is::<T>() {
anyhow::bail!("try_cast: type not matching");
}
// safety: we already checked it above, should be safe to directly cast it
unsafe { Ok(Rc::from_raw(Rc::into_raw(self.0.clone()) as _)) }
}
}