use std::any::{Any, TypeId};
use std::fmt::{self, Debug};
use std::hash::{Hash, Hasher};
use std::panic::Location;
use std::rc::Rc;
use waft_foundation::{
App, DiagnosticPropertiesBuilder, DiagnosticableNode, DiagnosticsTreeStyle, HandleId,
IntoPropertyName, Key,
};
use waft_rendering::{
AnyRenderObject, ContainerRenderObjectMixin, ErasedRenderObject, ParentData, RenderHandle,
RenderObject, RenderObjectWithChildMixin,
};
use super::element::{AnyElement, BuildContext, Element};
use super::elements::{
InheritedElement, LeafRenderObjectElement, MultiChildRenderObjectElement, ParentDataElement,
SingleChildRenderObjectElement, StatefulElement, StatelessElement,
};
use super::state::{AnyState, State};
use crate::widgets::inherited_theme::InheritedTheme;
use crate::widgets::preferred_size::PreferredSizeWidget;
#[derive(Clone)]
pub enum KeyRef {
Local(Rc<dyn Key>),
Global(GlobalKey),
}
impl KeyRef {
pub fn key(&self) -> &dyn Key {
match self {
KeyRef::Local(key) => &**key,
KeyRef::Global(global) => global.key(),
}
}
}
impl PartialEq for KeyRef {
fn eq(&self, other: &KeyRef) -> bool {
match (self, other) {
(KeyRef::Local(a), KeyRef::Local(b)) => **a == **b,
(KeyRef::Global(a), KeyRef::Global(b)) => a == b,
_ => false,
}
}
}
impl Eq for KeyRef {}
impl Hash for KeyRef {
fn hash<H: Hasher>(&self, state: &mut H) {
std::mem::discriminant(self).hash(state);
self.key().hash(state);
}
}
impl Debug for KeyRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
KeyRef::Local(key) => key.fmt(f),
KeyRef::Global(global) => global.fmt(f),
}
}
}
impl<K: Key> From<K> for KeyRef {
fn from(key: K) -> KeyRef {
KeyRef::Local(Rc::new(key))
}
}
impl From<GlobalKey> for KeyRef {
fn from(key: GlobalKey) -> KeyRef {
KeyRef::Global(key)
}
}
pub type WidgetRef = Rc<dyn Widget>;
impl PartialEq for dyn Widget {
fn eq(&self, other: &dyn Widget) -> bool {
std::ptr::addr_eq(self as *const dyn Widget, other as *const dyn Widget)
}
}
pub trait Widget: Any + Debug {
fn key(&self) -> Option<&KeyRef>;
fn creation_location(&self) -> Option<&'static Location<'static>> {
None
}
fn record_creation_location(&mut self, location: CreationLocation) {
let _ = location;
}
fn to_string_short(&self) -> String {
describe_widget(self.type_name(), self.key())
}
fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
let _ = properties;
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement;
fn as_any(&self) -> &dyn Any;
fn widget_type(&self) -> TypeId;
fn type_name(&self) -> &'static str {
short_type_name::<Self>()
}
fn kind(&self) -> WidgetKind;
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
None
}
fn as_render_object(&self) -> Option<&dyn AnyRenderObjectWidget> {
None
}
fn as_single_child_render_object(&self) -> Option<&dyn AnySingleChildRenderObjectWidget> {
None
}
fn as_multi_child_render_object(&self) -> Option<&dyn AnyMultiChildRenderObjectWidget> {
None
}
fn as_parent_data(&self) -> Option<&dyn AnyParentDataWidget> {
None
}
fn as_stateful(&self) -> Option<&dyn AnyStatefulWidget> {
None
}
fn as_inherited(&self) -> Option<&dyn AnyInheritedWidget> {
None
}
fn as_stateless(&self) -> Option<&dyn StatelessWidget> {
None
}
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget> {
None
}
}
impl dyn Widget {
pub fn to_diagnostics_node(
&self,
app: &App,
name: impl IntoPropertyName,
style: Option<DiagnosticsTreeStyle>,
) -> DiagnosticableNode {
let mut properties = DiagnosticPropertiesBuilder::new();
properties.default_diagnostics_tree_style = DiagnosticsTreeStyle::Dense;
self.debug_fill_properties(app, &mut properties);
let node = DiagnosticableNode::from_parts(name, self.to_string_short(), properties);
match style {
Some(style) => node.with_style(style),
None => node,
}
}
}
pub fn describe_widget(type_name: &str, key: Option<&KeyRef>) -> String {
match key {
Some(key) => format!("{type_name}-{key:?}"),
None => type_name.to_string(),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum WidgetKind {
Stateful,
Stateless,
Other,
}
pub fn can_update(old_widget: &dyn Widget, new_widget: &dyn Widget) -> bool {
old_widget.widget_type() == new_widget.widget_type()
&& keys_equal(old_widget.key(), new_widget.key())
}
pub fn keys_equal(a: Option<&KeyRef>, b: Option<&KeyRef>) -> bool {
match (a, b) {
(None, None) => true,
(Some(a), Some(b)) => a == b,
_ => false,
}
}
pub fn same_widget(a: &WidgetRef, b: &WidgetRef) -> bool {
Rc::ptr_eq(a, b)
}
pub fn downcast_widget<W: 'static>(widget: &dyn Widget) -> Option<&W> {
widget.as_any().downcast_ref::<W>()
}
pub fn short_type_name<W: ?Sized>() -> &'static str {
without_module_path(std::any::type_name::<W>())
}
fn without_module_path(name: &'static str) -> &'static str {
let type_arguments = name.find('<').unwrap_or(name.len());
let own_name = name[..type_arguments]
.rfind("::")
.map_or(0, |path_end| path_end + 2);
&name[own_name..]
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CreationLocation {
#[cfg(debug_assertions)]
location: &'static Location<'static>,
}
impl CreationLocation {
#[cfg_attr(debug_assertions, track_caller)]
pub fn of_caller() -> CreationLocation {
CreationLocation {
#[cfg(debug_assertions)]
location: Location::caller(),
}
}
pub fn location(self) -> Option<&'static Location<'static>> {
#[cfg(debug_assertions)]
return Some(self.location);
#[cfg(not(debug_assertions))]
return None;
}
}
pub trait IntoWidget<Kind> {
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(self) -> WidgetRef;
}
pub struct WidgetRefKind;
impl IntoWidget<WidgetRefKind> for WidgetRef {
fn into_widget(self) -> WidgetRef {
self
}
}
pub struct TreeNodeKind;
impl<W: Widget> IntoWidget<TreeNodeKind> for W {
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(mut self) -> WidgetRef {
self.record_creation_location(CreationLocation::of_caller());
Rc::new(self)
}
}
pub trait StatelessWidget: Debug + 'static {
fn key(&self) -> Option<&KeyRef> {
None
}
fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
let _ = properties;
}
fn debug_check_constructor(&self) {}
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget> {
None
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef;
}
pub struct StatelessKind;
pub struct Stateless<W: StatelessWidget>(pub W, pub(crate) CreationLocation);
impl<W: StatelessWidget> IntoWidget<StatelessKind> for W {
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(self) -> WidgetRef {
if cfg!(debug_assertions) {
self.debug_check_constructor();
}
Rc::new(Stateless(self, CreationLocation::of_caller()))
}
}
impl<W: StatelessWidget> Widget for Stateless<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn creation_location(&self) -> Option<&'static Location<'static>> {
self.1.location()
}
fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
self.0.debug_fill_properties(app, properties);
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
StatelessElement::new(app, this).as_element()
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn type_name(&self) -> &'static str {
short_type_name::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Stateless
}
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget> {
self.0.as_preferred_size_widget()
}
fn as_stateless(&self) -> Option<&dyn StatelessWidget> {
Some(&self.0)
}
}
impl<W: StatelessWidget> Debug for Stateless<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait StatefulWidget: Debug + 'static {
type State: State<Widget = Self>;
fn key(&self) -> Option<&KeyRef> {
None
}
fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
let _ = properties;
}
fn debug_check_constructor(&self) {}
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget> {
None
}
fn type_name(&self) -> &'static str {
short_type_name::<Self>()
}
fn create_state(&self) -> Self::State;
}
pub struct StatefulKind;
pub struct Stateful<W: StatefulWidget>(pub W, pub(crate) CreationLocation);
impl<W: StatefulWidget> IntoWidget<StatefulKind> for W {
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(self) -> WidgetRef {
if cfg!(debug_assertions) {
self.debug_check_constructor();
}
Rc::new(Stateful(self, CreationLocation::of_caller()))
}
}
impl<W: StatefulWidget> Widget for Stateful<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn creation_location(&self) -> Option<&'static Location<'static>> {
self.1.location()
}
fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
self.0.debug_fill_properties(app, properties);
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
StatefulElement::new(app, this).as_element()
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn type_name(&self) -> &'static str {
StatefulWidget::type_name(&self.0)
}
fn kind(&self) -> WidgetKind {
WidgetKind::Stateful
}
fn as_stateful(&self) -> Option<&dyn AnyStatefulWidget> {
Some(self)
}
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget> {
self.0.as_preferred_size_widget()
}
}
impl<W: StatefulWidget> AnyStatefulWidget for Stateful<W> {
fn create_state(&self, app: &mut App) -> AnyState {
let state = app.create(self.0.create_state()).into_tree_node(app);
AnyState::of(state)
}
}
pub trait AnyStatefulWidget {
fn create_state(&self, app: &mut App) -> AnyState;
}
impl<W: StatefulWidget> Debug for Stateful<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait InheritedWidget: Debug + 'static {
fn key(&self) -> Option<&KeyRef> {
None
}
fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
let _ = properties;
}
fn debug_check_constructor(&self) {}
fn child(&self) -> &WidgetRef;
fn update_should_notify(&self, old_widget: &Self) -> bool;
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
None
}
}
pub struct InheritedKind;
pub struct Inherited<W: InheritedWidget>(pub W, pub(crate) CreationLocation);
impl<W: InheritedWidget> IntoWidget<InheritedKind> for W {
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(self) -> WidgetRef {
if cfg!(debug_assertions) {
self.debug_check_constructor();
}
Rc::new(Inherited(self, CreationLocation::of_caller()))
}
}
impl<W: InheritedWidget> Widget for Inherited<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn creation_location(&self) -> Option<&'static Location<'static>> {
self.1.location()
}
fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
self.0.debug_fill_properties(app, properties);
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
InheritedElement::new(app, this).as_element()
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn type_name(&self) -> &'static str {
short_type_name::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
self.0.as_inherited_theme()
}
fn as_inherited(&self) -> Option<&dyn AnyInheritedWidget> {
Some(self)
}
}
impl<W: InheritedWidget> AnyInheritedWidget for Inherited<W> {
fn child(&self) -> &WidgetRef {
self.0.child()
}
fn update_should_notify(&self, old_widget: &dyn Widget) -> bool {
let old_widget =
downcast_widget::<W>(old_widget).expect("the old widget has the same type");
self.0.update_should_notify(old_widget)
}
}
pub trait AnyInheritedWidget {
fn child(&self) -> &WidgetRef;
fn update_should_notify(&self, old_widget: &dyn Widget) -> bool;
}
impl<W: InheritedWidget> Debug for Inherited<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait ParentDataWidget: Debug + 'static {
type ParentData: ParentData + 'static;
fn key(&self) -> Option<&KeyRef> {
None
}
fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
let _ = properties;
}
fn debug_check_constructor(&self) {}
fn child(&self) -> &WidgetRef;
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject);
fn debug_is_valid_render_object(
&self,
app: &App,
render_object: ::waft_rendering::AnyRenderObject,
) -> bool {
render_object.parent_data_is::<Self::ParentData>(app)
}
fn debug_can_apply_out_of_turn(&self) -> bool {
false
}
}
pub struct ParentDataKind;
pub struct ParentDataWrapper<W: ParentDataWidget>(pub W, pub(crate) CreationLocation);
impl<W: ParentDataWidget> IntoWidget<ParentDataKind> for W {
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(self) -> WidgetRef {
if cfg!(debug_assertions) {
self.debug_check_constructor();
}
Rc::new(ParentDataWrapper(self, CreationLocation::of_caller()))
}
}
impl<W: ParentDataWidget> Widget for ParentDataWrapper<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn creation_location(&self) -> Option<&'static Location<'static>> {
self.1.location()
}
fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
self.0.debug_fill_properties(app, properties);
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
ParentDataElement::new(app, this).as_element()
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn type_name(&self) -> &'static str {
short_type_name::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
fn as_parent_data(&self) -> Option<&dyn AnyParentDataWidget> {
Some(self)
}
}
impl<W: ParentDataWidget> AnyParentDataWidget for ParentDataWrapper<W> {
fn child(&self) -> &WidgetRef {
self.0.child()
}
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject) {
self.0.apply_parent_data(app, render_object);
}
fn debug_is_valid_render_object(&self, app: &App, render_object: AnyRenderObject) -> bool {
self.0.debug_is_valid_render_object(app, render_object)
}
fn parent_data_type_name(&self) -> &'static str {
std::any::type_name::<W::ParentData>()
}
}
pub trait AnyParentDataWidget: Debug {
fn child(&self) -> &WidgetRef;
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject);
fn debug_is_valid_render_object(&self, app: &App, render_object: AnyRenderObject) -> bool;
fn parent_data_type_name(&self) -> &'static str;
}
impl<W: ParentDataWidget> Debug for ParentDataWrapper<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait RenderObjectWidget: Debug + 'static {
type RenderObject: RenderObject;
fn key(&self) -> Option<&KeyRef> {
None
}
fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
let _ = properties;
}
fn to_string_short(&self) -> String {
describe_widget(short_type_name::<Self>(), self.key())
}
fn debug_check_constructor(&self) {}
fn debug_visit_onstage_children(
&self,
app: &App,
children: &[AnyElement],
visitor: &mut dyn FnMut(AnyElement),
) {
let _ = app;
children.iter().for_each(|child| visitor(*child));
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject;
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<Self::RenderObject>,
) {
let _ = (app, context, render_object);
}
fn did_unmount_render_object(
&self,
app: &mut App,
render_object: RenderHandle<Self::RenderObject>,
) {
let _ = (app, render_object);
}
}
pub trait LeafRenderObjectWidget: RenderObjectWidget {}
pub struct LeafRenderObjectKind;
pub struct LeafRenderObject<W: LeafRenderObjectWidget>(pub W, pub(crate) CreationLocation);
impl<W: LeafRenderObjectWidget> IntoWidget<LeafRenderObjectKind> for W {
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(self) -> WidgetRef {
if cfg!(debug_assertions) {
RenderObjectWidget::debug_check_constructor(&self);
}
Rc::new(LeafRenderObject(self, CreationLocation::of_caller()))
}
}
impl<W: LeafRenderObjectWidget> Widget for LeafRenderObject<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn creation_location(&self) -> Option<&'static Location<'static>> {
self.1.location()
}
fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
self.0.debug_fill_properties(app, properties);
}
fn to_string_short(&self) -> String {
self.0.to_string_short()
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
LeafRenderObjectElement::new(app, this).as_element()
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn type_name(&self) -> &'static str {
short_type_name::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
fn as_render_object(&self) -> Option<&dyn AnyRenderObjectWidget> {
Some(self)
}
}
impl<W: LeafRenderObjectWidget> Debug for LeafRenderObject<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait SingleChildRenderObjectWidget: RenderObjectWidget
where
Self::RenderObject: RenderObjectWithChildMixin,
{
fn child(&self) -> Option<&WidgetRef>;
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement
where
Self: Sized,
{
SingleChildRenderObjectElement::new(app, this).as_element()
}
}
pub struct SingleChildRenderObjectKind;
pub struct SingleChildRenderObject<W: SingleChildRenderObjectWidget>(
pub W,
pub(crate) CreationLocation,
)
where
W::RenderObject: RenderObjectWithChildMixin;
impl<W: SingleChildRenderObjectWidget> IntoWidget<SingleChildRenderObjectKind> for W
where
W::RenderObject: RenderObjectWithChildMixin,
{
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(self) -> WidgetRef {
if cfg!(debug_assertions) {
RenderObjectWidget::debug_check_constructor(&self);
}
Rc::new(SingleChildRenderObject(self, CreationLocation::of_caller()))
}
}
impl<W: SingleChildRenderObjectWidget> Widget for SingleChildRenderObject<W>
where
W::RenderObject: RenderObjectWithChildMixin,
{
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn creation_location(&self) -> Option<&'static Location<'static>> {
self.1.location()
}
fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
self.0.debug_fill_properties(app, properties);
}
fn to_string_short(&self) -> String {
self.0.to_string_short()
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
SingleChildRenderObjectWidget::create_element(&self.0, app, this)
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn type_name(&self) -> &'static str {
short_type_name::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
fn as_render_object(&self) -> Option<&dyn AnyRenderObjectWidget> {
Some(self)
}
fn as_single_child_render_object(&self) -> Option<&dyn AnySingleChildRenderObjectWidget> {
Some(self)
}
}
impl<W: SingleChildRenderObjectWidget> Debug for SingleChildRenderObject<W>
where
W::RenderObject: RenderObjectWithChildMixin,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait MultiChildRenderObjectWidget: RenderObjectWidget
where
Self::RenderObject: ContainerRenderObjectMixin,
{
fn children(&self) -> &[WidgetRef];
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement
where
Self: Sized,
{
MultiChildRenderObjectElement::new(app, this).as_element()
}
}
pub struct MultiChildRenderObjectKind;
pub struct MultiChildRenderObject<W: MultiChildRenderObjectWidget>(
pub W,
pub(crate) CreationLocation,
)
where
W::RenderObject: ContainerRenderObjectMixin;
impl<W: MultiChildRenderObjectWidget> IntoWidget<MultiChildRenderObjectKind> for W
where
W::RenderObject: ContainerRenderObjectMixin,
{
#[cfg_attr(debug_assertions, track_caller)]
fn into_widget(self) -> WidgetRef {
if cfg!(debug_assertions) {
RenderObjectWidget::debug_check_constructor(&self);
}
Rc::new(MultiChildRenderObject(self, CreationLocation::of_caller()))
}
}
impl<W: MultiChildRenderObjectWidget> Widget for MultiChildRenderObject<W>
where
W::RenderObject: ContainerRenderObjectMixin,
{
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn creation_location(&self) -> Option<&'static Location<'static>> {
self.1.location()
}
fn debug_fill_properties(&self, app: &App, properties: &mut DiagnosticPropertiesBuilder) {
self.0.debug_fill_properties(app, properties);
}
fn to_string_short(&self) -> String {
self.0.to_string_short()
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
MultiChildRenderObjectWidget::create_element(&self.0, app, this)
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn type_name(&self) -> &'static str {
short_type_name::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
fn as_render_object(&self) -> Option<&dyn AnyRenderObjectWidget> {
Some(self)
}
fn as_multi_child_render_object(&self) -> Option<&dyn AnyMultiChildRenderObjectWidget> {
Some(self)
}
}
impl<W: MultiChildRenderObjectWidget> Debug for MultiChildRenderObject<W>
where
W::RenderObject: ContainerRenderObjectMixin,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub trait AnyRenderObjectWidget {
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject;
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: AnyRenderObject,
);
fn did_unmount_render_object(&self, app: &mut App, render_object: AnyRenderObject);
fn debug_visit_onstage_children(
&self,
app: &App,
children: &[AnyElement],
visitor: &mut dyn FnMut(AnyElement),
);
}
pub trait AnySingleChildRenderObjectWidget: AnyRenderObjectWidget {
fn child(&self) -> Option<&WidgetRef>;
fn render_object_child(
&self,
app: &App,
render_object: AnyRenderObject,
) -> Option<AnyRenderObject>;
fn set_render_object_child(
&self,
app: &mut App,
render_object: AnyRenderObject,
child: Option<AnyRenderObject>,
);
}
pub trait AnyMultiChildRenderObjectWidget: AnyRenderObjectWidget {
fn children(&self) -> &[WidgetRef];
fn insert_render_object_child(
&self,
app: &mut App,
render_object: AnyRenderObject,
child: AnyRenderObject,
after: Option<AnyRenderObject>,
);
fn move_render_object_child(
&self,
app: &mut App,
render_object: AnyRenderObject,
child: AnyRenderObject,
after: Option<AnyRenderObject>,
);
fn remove_render_object_child(
&self,
app: &mut App,
render_object: AnyRenderObject,
child: AnyRenderObject,
);
}
macro_rules! any_render_object_widget_calls {
() => {
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
self.0.create_render_object(app, context)
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: AnyRenderObject,
) {
let render_object = typed_render_object::<W>(app, render_object);
self.0.update_render_object(app, context, render_object);
}
fn did_unmount_render_object(&self, app: &mut App, render_object: AnyRenderObject) {
let render_object = typed_render_object::<W>(app, render_object);
self.0.did_unmount_render_object(app, render_object);
}
fn debug_visit_onstage_children(
&self,
app: &App,
children: &[AnyElement],
visitor: &mut dyn FnMut(AnyElement),
) {
self.0.debug_visit_onstage_children(app, children, visitor);
}
};
}
fn typed_render_object<W: RenderObjectWidget>(
app: &App,
render_object: AnyRenderObject,
) -> RenderHandle<W::RenderObject> {
render_object
.downcast(app)
.expect("the render object this widget created")
}
impl<W: LeafRenderObjectWidget> AnyRenderObjectWidget for LeafRenderObject<W> {
any_render_object_widget_calls!();
}
impl<W: SingleChildRenderObjectWidget> AnyRenderObjectWidget for SingleChildRenderObject<W>
where
W::RenderObject: RenderObjectWithChildMixin,
{
any_render_object_widget_calls!();
}
impl<W: SingleChildRenderObjectWidget> AnySingleChildRenderObjectWidget
for SingleChildRenderObject<W>
where
W::RenderObject: RenderObjectWithChildMixin,
{
fn child(&self) -> Option<&WidgetRef> {
self.0.child()
}
fn render_object_child(
&self,
app: &App,
render_object: AnyRenderObject,
) -> Option<AnyRenderObject> {
typed_render_object::<W>(app, render_object)
.child(app)
.map(ErasedRenderObject::as_object)
}
fn set_render_object_child(
&self,
app: &mut App,
render_object: AnyRenderObject,
child: Option<AnyRenderObject>,
) {
typed_render_object::<W>(app, render_object)
.set_child(app, child.map(ErasedRenderObject::from_object));
}
}
impl<W: MultiChildRenderObjectWidget> AnyRenderObjectWidget for MultiChildRenderObject<W>
where
W::RenderObject: ContainerRenderObjectMixin,
{
any_render_object_widget_calls!();
}
impl<W: MultiChildRenderObjectWidget> AnyMultiChildRenderObjectWidget for MultiChildRenderObject<W>
where
W::RenderObject: ContainerRenderObjectMixin,
{
fn children(&self) -> &[WidgetRef] {
self.0.children()
}
fn insert_render_object_child(
&self,
app: &mut App,
render_object: AnyRenderObject,
child: AnyRenderObject,
after: Option<AnyRenderObject>,
) {
typed_render_object::<W>(app, render_object).insert(
app,
ErasedRenderObject::from_object(child),
after.map(ErasedRenderObject::from_object),
);
}
fn move_render_object_child(
&self,
app: &mut App,
render_object: AnyRenderObject,
child: AnyRenderObject,
after: Option<AnyRenderObject>,
) {
typed_render_object::<W>(app, render_object).move_child(
app,
ErasedRenderObject::from_object(child),
after.map(ErasedRenderObject::from_object),
);
}
fn remove_render_object_child(
&self,
app: &mut App,
render_object: AnyRenderObject,
child: AnyRenderObject,
) {
typed_render_object::<W>(app, render_object)
.remove(app, ErasedRenderObject::from_object(child));
}
}
#[derive(Clone)]
pub struct GlobalKey {
key: Rc<dyn Key>,
}
impl GlobalKey {
#[expect(
clippy::new_without_default,
reason = "each call creates a distinct key; Default would suggest a neutral value"
)]
pub fn new() -> GlobalKey {
GlobalKey::wrapping(Rc::new(LabeledGlobalKeyValue::new(None)))
}
pub fn labeled(debug_label: impl Into<String>) -> GlobalKey {
GlobalKey::wrapping(Rc::new(LabeledGlobalKeyValue::new(Some(
debug_label.into(),
))))
}
pub fn object(value: impl Into<HandleId>) -> GlobalKey {
GlobalKey::wrapping(Rc::new(GlobalObjectKeyValue {
value: value.into(),
}))
}
pub fn wrapping(key: Rc<dyn Key>) -> GlobalKey {
GlobalKey { key }
}
pub fn key(&self) -> &dyn Key {
&*self.key
}
pub fn current_context(&self, app: &App) -> Option<BuildContext> {
self.current_element(app)
}
pub fn current_widget(&self, app: &App) -> Option<WidgetRef> {
let element = self.current_element(app)?;
Some(element.widget(app).clone())
}
pub fn current_state<S: State>(&self, app: &App) -> Option<waft_foundation::Handle<S>> {
let element = self.current_element(app)?;
element.state_handle::<S>(app)
}
fn current_element(&self, app: &App) -> Option<AnyElement> {
let owner = crate::binding::current_build_owner(app)?;
owner.global_key_element(app, self)
}
}
impl PartialEq for GlobalKey {
fn eq(&self, other: &GlobalKey) -> bool {
*self.key == *other.key
}
}
impl Eq for GlobalKey {}
impl Hash for GlobalKey {
fn hash<H: Hasher>(&self, state: &mut H) {
self.key.hash(state);
}
}
impl Debug for GlobalKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.key.fmt(f)
}
}
struct LabeledGlobalKeyValue {
unique: waft_foundation::UniqueKey,
debug_label: Option<String>,
}
impl LabeledGlobalKeyValue {
fn new(debug_label: Option<String>) -> LabeledGlobalKeyValue {
LabeledGlobalKeyValue {
unique: waft_foundation::UniqueKey::new(),
debug_label,
}
}
}
impl Key for LabeledGlobalKeyValue {
fn eq_key(&self, other: &dyn Key) -> bool {
(other as &dyn Any)
.downcast_ref::<LabeledGlobalKeyValue>()
.is_some_and(|other| other.unique == self.unique)
}
fn hash_key(&self, mut state: &mut dyn Hasher) {
TypeId::of::<LabeledGlobalKeyValue>().hash(&mut state);
self.unique.hash(&mut state);
}
}
impl Debug for LabeledGlobalKeyValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let id = std::ptr::from_ref(self) as usize & 0xF_FFFF;
match &self.debug_label {
Some(label) => write!(f, "[GlobalKey#{id:05x} {label}]"),
None => write!(f, "[GlobalKey#{id:05x}]"),
}
}
}
struct GlobalObjectKeyValue {
value: HandleId,
}
impl Key for GlobalObjectKeyValue {
fn eq_key(&self, other: &dyn Key) -> bool {
(other as &dyn Any)
.downcast_ref::<GlobalObjectKeyValue>()
.is_some_and(|other| other.value == self.value)
}
fn hash_key(&self, mut state: &mut dyn Hasher) {
TypeId::of::<GlobalObjectKeyValue>().hash(&mut state);
self.value.hash(&mut state);
}
}
impl Debug for GlobalObjectKeyValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[GlobalObjectKey {:?}]", self.value)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct ObjectKey {
pub value: Option<HandleId>,
}
impl ObjectKey {
pub const fn new(value: Option<HandleId>) -> ObjectKey {
ObjectKey { value }
}
}
impl Key for ObjectKey {
fn eq_key(&self, other: &dyn Key) -> bool {
(other as &dyn Any)
.downcast_ref::<ObjectKey>()
.is_some_and(|other| other.value == self.value)
}
fn hash_key(&self, mut state: &mut dyn Hasher) {
TypeId::of::<ObjectKey>().hash(&mut state);
self.value.hash(&mut state);
}
}
impl waft_foundation::LocalKey for ObjectKey {}
impl Debug for ObjectKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.value {
Some(value) => write!(f, "[{value:?}]"),
None => f.write_str("[null]"),
}
}
}