use std::any::{Any, TypeId};
use std::fmt::{self, Debug};
use std::hash::{Hash, Hasher};
use std::rc::Rc;
use waft_foundation::{App, Key};
use waft_rendering::{
AnyRenderObject, ContainerRenderObjectMixin, ParentData, RenderHandle, RenderObject,
RenderObjectWithChildMixin,
};
use super::element::{AnyElement, BuildContext};
use super::elements::{
InheritedElement, LeafRenderObjectElement, MultiChildRenderObjectElement, ParentDataElement,
SingleChildRenderObjectElement, StatefulElement, StatelessElement,
};
use super::state::State;
pub type KeyRef = Rc<dyn Key>;
pub type WidgetRef = Rc<dyn Widget>;
pub trait Widget: Any + Debug {
fn key(&self) -> Option<&KeyRef>;
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement;
fn as_any(&self) -> &dyn Any;
fn widget_type(&self) -> TypeId;
fn kind(&self) -> WidgetKind;
}
#[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 trait IntoWidget<Kind> {
fn into_widget(self) -> WidgetRef;
}
pub struct WidgetRefKind;
impl IntoWidget<WidgetRefKind> for WidgetRef {
fn into_widget(self) -> WidgetRef {
self
}
}
pub trait StatelessWidget: Debug + 'static {
fn key(&self) -> Option<&KeyRef> {
None
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef;
}
pub struct StatelessKind;
pub struct Stateless<W: StatelessWidget>(pub W);
impl<W: StatelessWidget> IntoWidget<StatelessKind> for W {
fn into_widget(self) -> WidgetRef {
Rc::new(Stateless(self))
}
}
impl<W: StatelessWidget> Widget for Stateless<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
StatelessElement::<W>::create(app, this)
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Stateless
}
}
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 create_state(&self) -> Self::State;
}
pub struct StatefulKind;
pub struct Stateful<W: StatefulWidget>(pub W);
impl<W: StatefulWidget> IntoWidget<StatefulKind> for W {
fn into_widget(self) -> WidgetRef {
Rc::new(Stateful(self))
}
}
impl<W: StatefulWidget> Widget for Stateful<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
StatefulElement::<W>::create(app, this)
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Stateful
}
}
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 child(&self) -> &WidgetRef;
fn update_should_notify(&self, old_widget: &Self) -> bool;
}
pub struct InheritedKind;
pub struct Inherited<W: InheritedWidget>(pub W);
impl<W: InheritedWidget> IntoWidget<InheritedKind> for W {
fn into_widget(self) -> WidgetRef {
Rc::new(Inherited(self))
}
}
impl<W: InheritedWidget> Widget for Inherited<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
InheritedElement::<W>::create(app, this)
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
}
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 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);
impl<W: ParentDataWidget> IntoWidget<ParentDataKind> for W {
fn into_widget(self) -> WidgetRef {
Rc::new(ParentDataWrapper(self))
}
}
impl<W: ParentDataWidget> Widget for ParentDataWrapper<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
ParentDataElement::<W>::create(app, this)
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
}
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 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);
impl<W: LeafRenderObjectWidget> IntoWidget<LeafRenderObjectKind> for W {
fn into_widget(self) -> WidgetRef {
Rc::new(LeafRenderObject(self))
}
}
impl<W: LeafRenderObjectWidget> Widget for LeafRenderObject<W> {
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement {
LeafRenderObjectElement::<W>::create(app, this)
}
fn as_any(&self) -> &dyn Any {
&self.0
}
fn widget_type(&self) -> TypeId {
TypeId::of::<W>()
}
fn kind(&self) -> WidgetKind {
WidgetKind::Other
}
}
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::<Self>::create(app, this)
}
}
pub struct SingleChildRenderObjectKind;
pub struct SingleChildRenderObject<W: SingleChildRenderObjectWidget>(pub W)
where
W::RenderObject: RenderObjectWithChildMixin;
impl<W: SingleChildRenderObjectWidget> IntoWidget<SingleChildRenderObjectKind> for W
where
W::RenderObject: RenderObjectWithChildMixin,
{
fn into_widget(self) -> WidgetRef {
Rc::new(SingleChildRenderObject(self))
}
}
impl<W: SingleChildRenderObjectWidget> Widget for SingleChildRenderObject<W>
where
W::RenderObject: RenderObjectWithChildMixin,
{
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
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 kind(&self) -> WidgetKind {
WidgetKind::Other
}
}
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::<Self>::create(app, this)
}
}
pub struct MultiChildRenderObjectKind;
pub struct MultiChildRenderObject<W: MultiChildRenderObjectWidget>(pub W)
where
W::RenderObject: ContainerRenderObjectMixin;
impl<W: MultiChildRenderObjectWidget> IntoWidget<MultiChildRenderObjectKind> for W
where
W::RenderObject: ContainerRenderObjectMixin,
{
fn into_widget(self) -> WidgetRef {
Rc::new(MultiChildRenderObject(self))
}
}
impl<W: MultiChildRenderObjectWidget> Widget for MultiChildRenderObject<W>
where
W::RenderObject: ContainerRenderObjectMixin,
{
fn key(&self) -> Option<&KeyRef> {
self.0.key()
}
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 kind(&self) -> WidgetKind {
WidgetKind::Other
}
}
impl<W: MultiChildRenderObjectWidget> Debug for MultiChildRenderObject<W>
where
W::RenderObject: ContainerRenderObjectMixin,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Clone)]
pub struct GlobalKey {
id: Rc<GlobalKeyIdentity>,
debug_label: Option<String>,
}
struct GlobalKeyIdentity;
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 {
id: Rc::new(GlobalKeyIdentity),
debug_label: None,
}
}
pub fn labeled(debug_label: impl Into<String>) -> GlobalKey {
GlobalKey {
id: Rc::new(GlobalKeyIdentity),
debug_label: Some(debug_label.into()),
}
}
pub fn identity(&self) -> GlobalKeyId {
GlobalKeyId(Rc::as_ptr(&self.id) as usize)
}
pub fn current_context(&self, app: &mut App) -> Option<BuildContext> {
self.current_element(app)
}
pub fn current_widget(&self, app: &mut App) -> Option<WidgetRef> {
let element = self.current_element(app)?;
Some(element.widget(app).clone())
}
pub fn current_state<S: State>(&self, app: &mut App) -> Option<waft_foundation::Handle<S>> {
let element = self.current_element(app)?;
element.state_handle::<S>(app)
}
fn current_element(&self, app: &mut App) -> Option<AnyElement> {
let owner = crate::binding::current_build_owner(app)?;
owner.global_key_element(app, self.identity())
}
}
impl Key for GlobalKey {
fn eq_key(&self, other: &dyn Key) -> bool {
(other as &dyn Any)
.downcast_ref::<GlobalKey>()
.is_some_and(|other| Rc::ptr_eq(&self.id, &other.id))
}
fn hash_key(&self, mut state: &mut dyn Hasher) {
self.identity().hash(&mut state);
}
}
impl Debug for GlobalKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.debug_label {
Some(label) => write!(
f,
"[GlobalKey#{:05x} {label}]",
self.identity().0 & 0xF_FFFF
),
None => write!(f, "[GlobalKey#{:05x}]", self.identity().0 & 0xF_FFFF),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct GlobalKeyId(usize);
pub fn global_key_id(key: &KeyRef) -> Option<GlobalKeyId> {
((&**key) as &dyn Any)
.downcast_ref::<GlobalKey>()
.map(GlobalKey::identity)
}