use std::ops::Deref;
use kurbo::Point;
use smallvec::SmallVec;
use crate::core::{FromDynWidget, PropertiesRef, Property, QueryCtx, Widget, WidgetId};
pub struct WidgetRef<'w, W: Widget + ?Sized> {
pub(crate) ctx: QueryCtx<'w>,
pub(crate) widget: &'w W,
}
impl<W: Widget + ?Sized> Clone for WidgetRef<'_, W> {
fn clone(&self) -> Self {
*self
}
}
impl<W: Widget + ?Sized> Copy for WidgetRef<'_, W> {}
impl<W: Widget + ?Sized> std::fmt::Debug for WidgetRef<'_, W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let widget_name = self.widget.short_type_name();
let display_name = if let Some(debug_text) = self.widget.get_debug_text() {
format!("{widget_name}<{debug_text}>").into()
} else {
std::borrow::Cow::Borrowed(widget_name)
};
let children = self.children();
if children.is_empty() {
f.write_str(&display_name)
} else {
let mut f_tuple = f.debug_tuple(&display_name);
for child in children {
f_tuple.field(&child);
}
f_tuple.finish()
}
}
}
impl<W: Widget + ?Sized> Deref for WidgetRef<'_, W> {
type Target = W;
fn deref(&self) -> &Self::Target {
self.widget
}
}
impl<'w, W: Widget + ?Sized> WidgetRef<'w, W> {
pub fn ctx(&self) -> &'_ QueryCtx<'w> {
&self.ctx
}
pub fn inner(self) -> &'w W {
self.widget
}
pub fn id(&self) -> WidgetId {
self.ctx.widget_state.id
}
pub fn contains_prop<T: Property>(&self) -> bool {
self.ctx.properties.contains::<T>()
}
pub fn get_prop<T: Property>(&self) -> &T {
self.ctx
.properties
.get_without_saving::<T>(self.ctx.property_cache())
}
pub fn downcast<W2: Widget + FromDynWidget + ?Sized>(&self) -> Option<WidgetRef<'w, W2>> {
Some(WidgetRef {
ctx: self.ctx,
widget: W2::from_dyn(self.widget.as_dyn())?,
})
}
pub fn children(&self) -> SmallVec<[WidgetRef<'w, dyn Widget>; 16]> {
let parent_id = self.ctx.widget_state.id;
self.widget
.children_ids()
.iter()
.map(|&id| {
let Some(node_ref) = self.ctx.children.item(id) else {
panic!(
"Error in '{}' #{parent_id}: child #{id} has not been added to tree",
self.widget.short_type_name()
);
};
let children = node_ref.children;
let widget = &*node_ref.item.widget;
let state = &node_ref.item.state;
let properties = &node_ref.item.properties;
let class_set = &node_ref.item.class_set;
let stack = self
.ctx
.property_arena
.get(state.property_stack_id, widget.type_id());
let ctx = QueryCtx {
global_state: self.ctx.global_state,
widget_state: state,
properties: PropertiesRef {
local: properties,
default_map: self.ctx.properties.default_map,
stack,
class_set,
},
children,
property_arena: self.ctx.property_arena,
};
WidgetRef { ctx, widget }
})
.collect()
}
}
impl<'w, W: Widget> WidgetRef<'w, W> {
pub fn as_dyn(&self) -> WidgetRef<'w, dyn Widget> {
WidgetRef {
ctx: self.ctx,
widget: self.widget,
}
}
}
impl WidgetRef<'_, dyn Widget> {
pub fn find_widget_by_id(&self, id: WidgetId) -> Option<Self> {
if self.ctx.widget_state.id == id {
Some(*self)
} else {
self.children()
.into_iter()
.find_map(|child| child.find_widget_by_id(id))
}
}
pub fn find_widget_under_pointer(&self, pos: Point) -> Option<Self> {
self.widget.find_widget_under_pointer(self.ctx, pos)
}
}