1use std::any::{Any, TypeId};
10use std::collections::{HashMap, HashSet};
11use std::rc::Rc;
12
13use waft_foundation::{App, DiagnosticPropertiesBuilder, DiagnosticsProperty, Handle, HandleId};
14use waft_rendering::{AnyRenderObject, RenderHandle};
15
16use super::element::{
17 AnyElement, Element, ElementBase, ElementData, ElementLifecycle, IndexedSlot, Slot,
18 debug_check_owner_build_target_exists,
19};
20use super::state::{AnyState, State, StateLifecycle};
21use super::widget::{
22 AnyInheritedWidget, AnyMultiChildRenderObjectWidget, AnyParentDataWidget,
23 AnyRenderObjectWidget, AnySingleChildRenderObjectWidget, ParentDataWidget, RenderObjectWidget,
24 WidgetRef,
25};
26
27#[derive(Default)]
32pub struct ComponentElementData {
33 pub(crate) child: Option<AnyElement>,
34 pub(crate) debug_doing_build: bool,
35}
36
37pub trait ComponentElement: Element {
44 fn component_data(self: Handle<Self>, app: &App) -> &ComponentElementData;
46
47 fn component_data_mut(self: Handle<Self>, app: &mut App) -> &mut ComponentElementData;
49
50 fn build(self: Handle<Self>, app: &mut App) -> WidgetRef;
53
54 #[inline(never)]
56 fn build_tracked(self: Handle<Self>, app: &mut App) -> WidgetRef {
57 let element = self.as_element();
58 let (built, tracked) = app.entity_track(|app| self.build(app));
59 element.bind_entity_watches(app, tracked);
60 built
61 }
62
63 fn child(self: Handle<Self>, app: &App) -> Option<AnyElement> {
65 self.component_data(app).child
66 }
67
68 #[inline(always)]
70 fn mount(
71 self: Handle<Self>,
72 app: &mut App,
73 parent: Option<AnyElement>,
74 new_slot: Option<Slot>,
75 ) {
76 ElementBase::mount(self, app, parent, new_slot);
77 debug_assert!(self.child(app).is_none());
78 debug_assert!(self.as_element().lifecycle(app) == ElementLifecycle::Active);
79 self.first_build(app);
80 debug_assert!(self.child(app).is_some());
81 }
82
83 #[inline(always)]
85 fn first_build(self: Handle<Self>, app: &mut App) {
86 ElementBase::rebuild(self, app, false); }
88
89 #[inline(always)]
94 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
95 if cfg!(debug_assertions) {
96 self.component_data_mut(app).debug_doing_build = true;
97 }
98 let built = self.build_tracked(app);
99 if cfg!(debug_assertions) {
100 self.component_data_mut(app).debug_doing_build = false;
101 }
102 ElementBase::perform_rebuild(self, app); let child = self.child(app);
104 let slot = self.as_element().slot(app);
105 let child = self
106 .as_element()
107 .update_child(app, child, Some(built), slot);
108 debug_assert!(child.is_some());
109 self.component_data_mut(app).child = child;
110 }
111
112 fn visit_children_component(
114 self: Handle<Self>,
115 app: &App,
116 visitor: &mut dyn FnMut(AnyElement),
117 ) {
118 if let Some(child) = self.child(app) {
119 visitor(child);
120 }
121 }
122
123 fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
125 debug_assert!(self.child(app) == Some(child));
126 self.component_data_mut(app).child = None;
127 }
128}
129
130macro_rules! component_element_overrides {
133 () => {
134 fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
135 ComponentElement::visit_children_component(self, app, visitor);
136 }
137
138 fn render_object_attaching_child(self: Handle<Self>, app: &App) -> Option<AnyElement> {
139 ComponentElement::child(self, app)
140 }
141
142 fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
143 ComponentElement::forget_child(self, app, child);
144 }
145
146 fn debug_doing_build(self: Handle<Self>, app: &App) -> bool {
147 self.component_data(app).debug_doing_build
148 }
149 };
150}
151pub(crate) use component_element_overrides;
152
153pub struct StatelessElement {
158 element: ElementData,
159 component: ComponentElementData,
160}
161
162impl StatelessElement {
163 pub fn new(app: &mut App, widget: WidgetRef) -> Handle<Self> {
165 debug_assert!(widget.as_stateless().is_some());
166 app.create(StatelessElement {
167 element: ElementData::new(widget),
168 component: ComponentElementData::default(),
169 })
170 .into_tree_node(app)
171 }
172}
173
174impl ComponentElement for StatelessElement {
175 fn component_data(self: Handle<Self>, app: &App) -> &ComponentElementData {
176 &app.get(self).component
177 }
178
179 fn component_data_mut(self: Handle<Self>, app: &mut App) -> &mut ComponentElementData {
180 &mut app.get_mut(self).component
181 }
182
183 fn build(self: Handle<Self>, app: &mut App) -> WidgetRef {
184 let widget = self.as_element().widget(app).clone();
185 let stateless = widget
186 .as_stateless()
187 .expect("a StatelessElement holds a StatelessWidget");
188 stateless.build(app, self.as_element())
189 }
190}
191
192impl Element for StatelessElement {
193 crate::element_accessors!();
194 component_element_overrides!();
195
196 #[inline(always)]
197 fn mount(
198 self: Handle<Self>,
199 app: &mut App,
200 parent: Option<AnyElement>,
201 new_slot: Option<Slot>,
202 ) {
203 ComponentElement::mount(self, app, parent, new_slot);
204 }
205
206 #[inline(always)]
207 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
208 ElementBase::update(self, app, new_widget);
209 ElementBase::rebuild(self, app, true);
210 }
211
212 #[inline(always)]
213 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
214 ComponentElement::perform_rebuild(self, app);
215 }
216}
217
218pub struct StatefulElement {
223 element: ElementData,
224 component: ComponentElementData,
225 state: Option<AnyState>,
226 did_change_dependencies: bool,
227}
228
229impl StatefulElement {
230 pub fn new(app: &mut App, widget: WidgetRef) -> Handle<Self> {
233 let state = widget
234 .as_stateful()
235 .expect("a StatefulElement holds a StatefulWidget")
236 .create_state(app);
237 let this = app
238 .create(StatefulElement {
239 element: ElementData::new(widget.clone()),
240 component: ComponentElementData::default(),
241 state: Some(state),
242 did_change_dependencies: false,
243 })
244 .into_tree_node(app);
245 debug_assert!(state.element(app).is_none());
246 state.set_element(app, Some(this));
247 state.set_widget(app, widget);
248 debug_assert!(state.debug_lifecycle_state(app) == StateLifecycle::Created);
249 this
250 }
251
252 pub fn state<S: State>(self: Handle<Self>, app: &App) -> Option<Handle<S>> {
258 let state = app.get(self).state.expect("the element is unmounted");
259 (state.type_id() == TypeId::of::<S>()).then(|| Handle::from_id(state.id()))
260 }
261
262 fn any_state(self: Handle<Self>, app: &App) -> AnyState {
263 app.get(self).state.expect("the element is unmounted")
264 }
265}
266
267impl ComponentElement for StatefulElement {
268 fn component_data(self: Handle<Self>, app: &App) -> &ComponentElementData {
269 &app.get(self).component
270 }
271
272 fn component_data_mut(self: Handle<Self>, app: &mut App) -> &mut ComponentElementData {
273 &mut app.get_mut(self).component
274 }
275
276 fn build(self: Handle<Self>, app: &mut App) -> WidgetRef {
277 let state = self.any_state(app);
278 state.build(app, self.as_element())
279 }
280
281 #[inline(always)]
282 fn first_build(self: Handle<Self>, app: &mut App) {
283 let state = self.any_state(app);
284 debug_assert!(state.debug_lifecycle_state(app) == StateLifecycle::Created);
285 state.init_state(app);
286 if cfg!(debug_assertions) {
287 state.set_debug_lifecycle_state(app, StateLifecycle::Initialized);
288 }
289 state.did_change_dependencies(app);
290 if cfg!(debug_assertions) {
291 state.set_debug_lifecycle_state(app, StateLifecycle::Ready);
292 }
293 ElementBase::rebuild(self, app, false);
294 }
295
296 #[inline(always)]
297 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
298 if app.get(self).did_change_dependencies {
299 self.any_state(app).did_change_dependencies(app);
300 app.get_mut(self).did_change_dependencies = false;
301 }
302 if cfg!(debug_assertions) {
303 self.component_data_mut(app).debug_doing_build = true;
304 }
305 let built = self.build_tracked(app);
306 if cfg!(debug_assertions) {
307 self.component_data_mut(app).debug_doing_build = false;
308 }
309 ElementBase::perform_rebuild(self, app); let child = self.child(app);
311 let slot = self.as_element().slot(app);
312 let child = self
313 .as_element()
314 .update_child(app, child, Some(built), slot);
315 debug_assert!(child.is_some());
316 self.component_data_mut(app).child = child;
317 }
318}
319
320impl Element for StatefulElement {
321 crate::element_accessors!();
322 component_element_overrides!();
323
324 fn debug_fill_properties(
326 self: Handle<Self>,
327 app: &App,
328 properties: &mut DiagnosticPropertiesBuilder,
329 ) {
330 properties.add(
331 DiagnosticsProperty::<String>::new(
332 "state",
333 app.get(self).state.map(|state| state.to_string(app)),
334 )
335 .default_value(None),
336 );
337 }
338
339 #[inline(always)]
340 fn mount(
341 self: Handle<Self>,
342 app: &mut App,
343 parent: Option<AnyElement>,
344 new_slot: Option<Slot>,
345 ) {
346 ComponentElement::mount(self, app, parent, new_slot);
347 }
348
349 #[inline(always)]
350 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
351 let old_widget = self.as_element().widget(app).clone();
352 ElementBase::update(self, app, new_widget.clone());
353 let state = self.any_state(app);
354 state.set_widget(app, new_widget);
355 state.did_update_widget(app, &old_widget);
356 ElementBase::rebuild(self, app, true);
357 }
358
359 #[inline(always)]
360 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
361 ComponentElement::perform_rebuild(self, app);
362 }
363
364 fn activate(self: Handle<Self>, app: &mut App) {
365 ElementBase::activate(self, app);
366 self.any_state(app).activate(app);
367 debug_assert!(self.as_element().lifecycle(app) == ElementLifecycle::Active); self.as_element().mark_needs_build(app);
372 }
373
374 fn deactivate(self: Handle<Self>, app: &mut App) {
375 self.any_state(app).deactivate(app);
376 ElementBase::deactivate(self, app);
377 }
378
379 fn unmount(self: Handle<Self>, app: &mut App) {
380 ElementBase::unmount(self, app);
381 let state = self.any_state(app);
382 state.dispose(app);
383 if cfg!(debug_assertions) {
384 state.set_debug_lifecycle_state(app, StateLifecycle::Defunct);
385 }
386 state.set_element(app, None);
387 app.get_mut(self).state = None;
388 app.release_tree_node(state.id());
389 }
390
391 fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
392 ElementBase::did_change_dependencies(self, app);
393 app.get_mut(self).did_change_dependencies = true;
394 }
395
396 fn state_handle_id(self: Handle<Self>, app: &App) -> Option<(TypeId, HandleId)> {
397 app.get(self)
398 .state
399 .map(|state| (state.type_id(), state.id()))
400 }
401}
402
403pub trait ProxyElement: ComponentElement {
408 fn proxied_child(self: Handle<Self>, app: &App) -> WidgetRef;
410
411 fn updated(self: Handle<Self>, app: &mut App, old_widget: WidgetRef) {
417 self.notify_clients(app, old_widget);
418 }
419
420 fn notify_clients(self: Handle<Self>, app: &mut App, old_widget: WidgetRef);
425
426 #[inline(always)]
428 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
429 let old_widget = self.as_element().widget(app).clone();
430 debug_assert!(!Rc::ptr_eq(&old_widget, &new_widget));
431 ElementBase::update(self, app, new_widget);
432 self.updated(app, old_widget);
433 ElementBase::rebuild(self, app, true);
434 }
435}
436
437pub struct InheritedElement {
442 element: ElementData,
443 component: ComponentElementData,
444 dependents: HashMap<AnyElement, Option<Rc<dyn Any>>>,
445}
446
447impl InheritedElement {
448 pub fn new(app: &mut App, widget: WidgetRef) -> Handle<Self> {
450 debug_assert!(widget.as_inherited().is_some());
451 app.create(InheritedElement {
452 element: ElementData::new(widget),
453 component: ComponentElementData::default(),
454 dependents: HashMap::new(),
455 })
456 .into_tree_node(app)
457 }
458
459 pub fn widget(self: Handle<Self>, app: &App) -> &dyn AnyInheritedWidget {
461 self.as_element()
462 .widget(app)
463 .as_inherited()
464 .expect("an InheritedElement holds an InheritedWidget")
465 }
466
467 pub fn get_dependencies(
474 self: Handle<Self>,
475 app: &App,
476 dependent: AnyElement,
477 ) -> Option<Rc<dyn Any>> {
478 app.get(self).dependents.get(&dependent).cloned().flatten()
479 }
480
481 pub fn set_dependencies(
488 self: Handle<Self>,
489 app: &mut App,
490 dependent: AnyElement,
491 value: Option<Rc<dyn Any>>,
492 ) {
493 app.get_mut(self).dependents.insert(dependent, value);
494 }
495
496 pub fn notify_dependent(
504 self: Handle<Self>,
505 app: &mut App,
506 old_widget: &WidgetRef,
507 dependent: AnyElement,
508 ) {
509 let _ = (self, old_widget);
510 dependent.did_change_dependencies(app);
511 }
512}
513
514impl ComponentElement for InheritedElement {
515 fn component_data(self: Handle<Self>, app: &App) -> &ComponentElementData {
516 &app.get(self).component
517 }
518
519 fn component_data_mut(self: Handle<Self>, app: &mut App) -> &mut ComponentElementData {
520 &mut app.get_mut(self).component
521 }
522
523 fn build(self: Handle<Self>, app: &mut App) -> WidgetRef {
524 self.proxied_child(app)
525 }
526}
527
528impl ProxyElement for InheritedElement {
529 fn proxied_child(self: Handle<Self>, app: &App) -> WidgetRef {
530 self.widget(app).child().clone()
531 }
532
533 fn updated(self: Handle<Self>, app: &mut App, old_widget: WidgetRef) {
534 let should_notify = self.widget(app).update_should_notify(&*old_widget);
535 if should_notify {
536 self.notify_clients(app, old_widget);
537 }
538 }
539
540 fn notify_clients(self: Handle<Self>, app: &mut App, old_widget: WidgetRef) {
541 debug_assert!(debug_check_owner_build_target_exists(
542 self.as_element(),
543 app,
544 "notifyClients"
545 ));
546 let dependents: Vec<AnyElement> = app.get(self).dependents.keys().copied().collect();
547 for dependent in dependents {
548 if cfg!(debug_assertions) {
549 let mut ancestor = dependent.parent(app);
551 while let Some(current) = ancestor
552 && current != self.as_element()
553 {
554 ancestor = current.parent(app);
555 }
556 debug_assert!(ancestor == Some(self.as_element()));
557 debug_assert!(dependent.does_depend_on_inherited_element(app, self.as_element()));
559 }
560 self.notify_dependent(app, &old_widget, dependent);
561 }
562 }
563}
564
565impl Element for InheritedElement {
566 crate::element_accessors!();
567 component_element_overrides!();
568
569 #[inline(always)]
570 fn mount(
571 self: Handle<Self>,
572 app: &mut App,
573 parent: Option<AnyElement>,
574 new_slot: Option<Slot>,
575 ) {
576 ComponentElement::mount(self, app, parent, new_slot);
577 }
578
579 #[inline(always)]
580 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
581 ProxyElement::update(self, app, new_widget);
582 }
583
584 #[inline(always)]
585 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
586 ComponentElement::perform_rebuild(self, app);
587 }
588
589 fn update_inheritance(self: Handle<Self>, app: &mut App) {
590 debug_assert!(self.as_element().lifecycle(app) == ElementLifecycle::Active);
591 let incoming = self
592 .as_element()
593 .parent(app)
594 .and_then(|parent| parent.inherited_elements(app));
595 let mut widgets: HashMap<TypeId, AnyElement> = incoming
596 .map(|incoming| (*incoming).clone())
597 .unwrap_or_default();
598 let widget_type = self.as_element().widget(app).widget_type();
599 widgets.insert(widget_type, self.as_element());
600 self.as_element()
601 .set_inherited_elements(app, Some(Rc::new(widgets)));
602 }
603
604 fn debug_deactivated(self: Handle<Self>, app: &mut App) {
605 debug_assert!(app.get(self).dependents.is_empty());
606 debug_assert!(self.as_element().lifecycle(app) == ElementLifecycle::Inactive);
607 }
608
609 const IS_INHERITED_ELEMENT: bool = true;
610
611 fn update_dependencies(
612 self: Handle<Self>,
613 app: &mut App,
614 dependent: AnyElement,
615 aspect: Option<Rc<dyn Any>>,
616 ) {
617 let _ = aspect;
618 self.set_dependencies(app, dependent, None);
619 }
620
621 fn remove_dependent(self: Handle<Self>, app: &mut App, dependent: AnyElement) {
622 app.get_mut(self).dependents.remove(&dependent);
623 }
624}
625
626pub struct ParentDataElement {
631 element: ElementData,
632 component: ComponentElementData,
633}
634
635impl ParentDataElement {
636 pub fn new(app: &mut App, widget: WidgetRef) -> Handle<Self> {
638 debug_assert!(widget.as_parent_data().is_some());
639 app.create(ParentDataElement {
640 element: ElementData::new(widget),
641 component: ComponentElementData::default(),
642 })
643 .into_tree_node(app)
644 }
645
646 pub fn widget(self: Handle<Self>, app: &App) -> &dyn AnyParentDataWidget {
648 self.as_element()
649 .widget(app)
650 .as_parent_data()
651 .expect("a ParentDataElement holds a ParentDataWidget")
652 }
653
654 fn apply_parent_data_to_descendants(self: Handle<Self>, app: &mut App) {
655 let mut child = self.as_element().render_object_attaching_child(app);
656 while let Some(current) = child {
657 if current.is_render_object_element() {
658 current.update_parent_data(app, self.as_element());
659 return;
660 }
661 child = current.render_object_attaching_child(app);
662 }
663 }
664
665 pub fn apply_widget_out_of_turn<W: ParentDataWidget>(
688 self: Handle<Self>,
689 app: &mut App,
690 new_widget: &W,
691 ) {
692 debug_assert!(new_widget.debug_can_apply_out_of_turn());
693 debug_assert!(Rc::ptr_eq(new_widget.child(), self.widget(app).child()));
694 let mut child = self.as_element().render_object_attaching_child(app);
695 while let Some(current) = child {
696 if current.is_render_object_element() {
697 let render_object = current
698 .render_object(app)
699 .expect("a mounted RenderObjectElement has a render object");
700 new_widget.apply_parent_data(app, render_object);
701 return;
702 }
703 child = current.render_object_attaching_child(app);
704 }
705 }
706}
707
708impl ComponentElement for ParentDataElement {
709 fn component_data(self: Handle<Self>, app: &App) -> &ComponentElementData {
710 &app.get(self).component
711 }
712
713 fn component_data_mut(self: Handle<Self>, app: &mut App) -> &mut ComponentElementData {
714 &mut app.get_mut(self).component
715 }
716
717 fn build(self: Handle<Self>, app: &mut App) -> WidgetRef {
718 self.proxied_child(app)
719 }
720}
721
722impl ProxyElement for ParentDataElement {
723 fn proxied_child(self: Handle<Self>, app: &App) -> WidgetRef {
724 self.widget(app).child().clone()
725 }
726
727 fn notify_clients(self: Handle<Self>, app: &mut App, _old_widget: WidgetRef) {
728 self.apply_parent_data_to_descendants(app);
729 }
730}
731
732impl Element for ParentDataElement {
733 crate::element_accessors!();
734 component_element_overrides!();
735
736 #[inline(always)]
737 fn mount(
738 self: Handle<Self>,
739 app: &mut App,
740 parent: Option<AnyElement>,
741 new_slot: Option<Slot>,
742 ) {
743 ComponentElement::mount(self, app, parent, new_slot);
744 }
745
746 #[inline(always)]
747 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
748 ProxyElement::update(self, app, new_widget);
749 }
750
751 #[inline(always)]
752 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
753 ComponentElement::perform_rebuild(self, app);
754 }
755
756 const IS_PARENT_DATA_ELEMENT: bool = true;
757
758 fn apply_parent_data(
759 self: Handle<Self>,
760 app: &mut App,
761 render_object: AnyRenderObject,
762 ) -> bool {
763 let widget = self.as_element().widget(app).clone();
765 let parent_data_widget = widget
766 .as_parent_data()
767 .expect("a ParentDataElement holds a ParentDataWidget");
768 let valid = parent_data_widget.debug_is_valid_render_object(app, render_object);
769 debug_assert!(
770 valid,
771 "Incorrect use of ParentDataWidget. The ParentDataWidget {:?} wants to apply \
772 ParentData of type {} to a RenderObject which has been set up to accept ParentData \
773 of an incompatible type. Usually, this means that the widget has the wrong \
774 ancestor RenderObjectWidget.",
775 parent_data_widget,
776 parent_data_widget.parent_data_type_name()
777 );
778 if valid {
779 parent_data_widget.apply_parent_data(app, render_object);
780 }
781 valid
782 }
783}
784
785#[derive(Default)]
790pub struct RenderObjectElementData {
791 render_object: Option<AnyRenderObject>,
792 ancestor_render_object_element: Option<AnyElement>,
793 debug_doing_build: bool,
794}
795
796impl RenderObjectElementData {
797 pub fn render_object(&self) -> Option<AnyRenderObject> {
799 self.render_object
800 }
801
802 pub fn debug_doing_build(&self) -> bool {
804 self.debug_doing_build
805 }
806}
807
808pub trait RenderObjectElement: Element {
829 fn render_object_element_data(self: Handle<Self>, app: &App) -> &RenderObjectElementData;
831
832 fn render_object_element_data_mut(
834 self: Handle<Self>,
835 app: &mut App,
836 ) -> &mut RenderObjectElementData;
837
838 fn render_object(self: Handle<Self>, app: &App) -> AnyRenderObject {
840 self.render_object_element_data(app)
841 .render_object
842 .expect("unmounted")
843 }
844
845 fn typed_render_object(
847 self: Handle<Self>,
848 app: &App,
849 ) -> RenderHandle<
850 <<Self as RenderObjectElementWidget>::Widget as RenderObjectWidget>::RenderObject,
851 >
852 where
853 Self: RenderObjectElementWidget,
854 {
855 RenderObjectElement::render_object(self, app)
856 .downcast(app)
857 .expect("the render object this widget created")
858 }
859
860 fn find_ancestor_render_object_element(self: Handle<Self>, app: &App) -> Option<AnyElement> {
861 let slot = self.as_element().slot(app);
862 let mut ancestor = self.as_element().parent(app);
863 while let Some(current) = ancestor
864 && !current.is_render_object_element()
865 {
866 if cfg!(debug_assertions)
870 && !current.debug_expects_render_object_for_slot(app, slot.as_ref())
871 {
872 return None;
873 }
874 ancestor = current.parent(app);
875 }
876 if cfg!(debug_assertions)
877 && let Some(current) = ancestor
878 && !current.debug_expects_render_object_for_slot(app, slot.as_ref())
879 {
880 return None;
881 }
882 ancestor
883 }
884
885 fn find_ancestor_parent_data_elements(self: Handle<Self>, app: &App) -> Vec<AnyElement> {
886 let mut ancestor = self.as_element().parent(app);
887 let mut result = Vec::new();
888 while let Some(current) = ancestor
889 && !current.is_render_object_element()
890 {
891 if current.is_parent_data_element() {
892 result.push(current);
893 }
894 ancestor = current.parent(app);
895 }
896 result
897 }
898
899 #[inline(always)]
901 fn mount(self: Handle<Self>, app: &mut App, parent: Option<AnyElement>, new_slot: Option<Slot>)
902 where
903 Self: RenderObjectWidgetCalls,
904 {
905 ElementBase::mount(self, app, parent, new_slot.clone());
906 if cfg!(debug_assertions) {
907 self.render_object_element_data_mut(app).debug_doing_build = true;
908 }
909 let render_object = {
910 let widget = self.as_element().widget(app).clone();
911 RenderObjectWidgetCalls::create_render_object(self, app, &widget)
912 };
913 self.render_object_element_data_mut(app).render_object = Some(render_object);
914 if cfg!(debug_assertions) {
915 self.render_object_element_data_mut(app).debug_doing_build = false;
916 }
917 debug_assert!(self.as_element().slot(app) == new_slot);
918 self.as_element().attach_render_object(app, new_slot);
919 ElementBase::perform_rebuild(self, app); }
921
922 #[inline(always)]
924 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef)
925 where
926 Self: RenderObjectWidgetCalls,
927 {
928 ElementBase::update(self, app, new_widget);
929 RenderObjectElement::perform_rebuild(self, app); }
931
932 #[inline(always)]
934 fn perform_rebuild(self: Handle<Self>, app: &mut App)
935 where
936 Self: RenderObjectWidgetCalls,
937 {
938 if cfg!(debug_assertions) {
939 self.render_object_element_data_mut(app).debug_doing_build = true;
940 }
941 let widget = self.as_element().widget(app).clone();
942 RenderObjectWidgetCalls::update_render_object(self, app, &widget);
943 if cfg!(debug_assertions) {
944 self.render_object_element_data_mut(app).debug_doing_build = false;
945 }
946 ElementBase::perform_rebuild(self, app); }
948
949 fn deactivate(self: Handle<Self>, app: &mut App) {
951 ElementBase::deactivate(self, app);
952 debug_assert!(
953 !RenderObjectElement::render_object(self, app).attached(app),
954 "A RenderObject was still attached when attempting to deactivate its \
955 RenderObjectElement"
956 );
957 }
958
959 fn unmount(self: Handle<Self>, app: &mut App)
961 where
962 Self: RenderObjectWidgetCalls,
963 {
964 let old_widget = self.as_element().widget(app).clone();
965 ElementBase::unmount(self, app);
966 let render_object = RenderObjectElement::render_object(self, app);
967 debug_assert!(
968 !render_object.attached(app),
969 "A RenderObject was still attached when attempting to unmount its RenderObjectElement"
970 );
971 RenderObjectWidgetCalls::did_unmount_render_object(self, app, &old_widget, render_object);
972 render_object.dispose(app);
973 self.render_object_element_data_mut(app).render_object = None;
974 }
975
976 fn update_parent_data(self: Handle<Self>, app: &mut App, parent_data_element: AnyElement) {
978 let render_object = RenderObjectElement::render_object(self, app);
979 parent_data_element.apply_parent_data(app, render_object);
980 }
981
982 fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
984 let old_slot = self.as_element().slot(app);
985 debug_assert!(old_slot != new_slot);
986 ElementBase::update_slot(self, app, new_slot.clone());
987 debug_assert!(self.as_element().slot(app) == new_slot);
988 debug_assert!(
989 self.render_object_element_data(app)
990 .ancestor_render_object_element
991 == self.find_ancestor_render_object_element(app)
992 );
993 if let Some(ancestor) = self
994 .render_object_element_data(app)
995 .ancestor_render_object_element
996 {
997 let render_object = RenderObjectElement::render_object(self, app);
998 ancestor.move_render_object_child(app, render_object, old_slot, new_slot);
999 }
1000 }
1001
1002 fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
1004 debug_assert!(
1005 self.render_object_element_data(app)
1006 .ancestor_render_object_element
1007 .is_none()
1008 );
1009 self.as_element().set_slot(app, new_slot.clone());
1010 let ancestor = self.find_ancestor_render_object_element(app);
1011 self.render_object_element_data_mut(app)
1012 .ancestor_render_object_element = ancestor;
1013 debug_assert!(
1014 ancestor.is_some(),
1015 "The render object for {:?} cannot find ancestor render object to attach to. Try \
1016 wrapping your widget in a View widget or any other widget that is backed by a \
1017 RenderTreeRootElement to serve as the root of the render tree.",
1018 self.as_element()
1019 );
1020 if let Some(ancestor) = ancestor {
1021 let render_object = RenderObjectElement::render_object(self, app);
1022 ancestor.insert_render_object_child(app, render_object, new_slot);
1023 }
1024 for parent_data_element in self.find_ancestor_parent_data_elements(app) {
1025 RenderObjectElement::update_parent_data(self, app, parent_data_element);
1026 }
1027 }
1028
1029 fn detach_render_object(self: Handle<Self>, app: &mut App) {
1031 if let Some(ancestor) = self
1032 .render_object_element_data(app)
1033 .ancestor_render_object_element
1034 {
1035 let render_object = RenderObjectElement::render_object(self, app);
1036 let slot = self.as_element().slot(app);
1037 ancestor.remove_render_object_child(app, render_object, slot);
1038 self.render_object_element_data_mut(app)
1039 .ancestor_render_object_element = None;
1040 }
1041 self.as_element().set_slot(app, None);
1042 }
1043}
1044
1045pub trait RenderObjectElementWidget {
1050 type Widget: RenderObjectWidget;
1052
1053 fn widget_of(widget: &WidgetRef) -> &Self::Widget;
1055}
1056
1057pub trait RenderObjectWidgetCalls: RenderObjectElement {
1067 fn create_render_object(
1069 self: Handle<Self>,
1070 app: &mut App,
1071 widget: &WidgetRef,
1072 ) -> AnyRenderObject;
1073
1074 fn update_render_object(self: Handle<Self>, app: &mut App, widget: &WidgetRef);
1076
1077 fn did_unmount_render_object(
1079 self: Handle<Self>,
1080 app: &mut App,
1081 old_widget: &WidgetRef,
1082 render_object: AnyRenderObject,
1083 );
1084}
1085
1086impl<T: RenderObjectElement + RenderObjectElementWidget> RenderObjectWidgetCalls for T {
1087 fn create_render_object(
1088 self: Handle<Self>,
1089 app: &mut App,
1090 widget: &WidgetRef,
1091 ) -> AnyRenderObject {
1092 T::widget_of(widget).create_render_object(app, self.as_element())
1093 }
1094
1095 fn update_render_object(self: Handle<Self>, app: &mut App, widget: &WidgetRef) {
1096 let render_object = self.typed_render_object(app);
1097 T::widget_of(widget).update_render_object(app, self.as_element(), render_object);
1098 }
1099
1100 fn did_unmount_render_object(
1101 self: Handle<Self>,
1102 app: &mut App,
1103 old_widget: &WidgetRef,
1104 render_object: AnyRenderObject,
1105 ) {
1106 let render_object = render_object
1107 .downcast(app)
1108 .expect("the render object this widget created");
1109 T::widget_of(old_widget).did_unmount_render_object(app, render_object);
1110 }
1111}
1112
1113macro_rules! erased_render_object_widget_calls {
1116 ($element:ty, $widget_face:expr) => {
1117 impl RenderObjectWidgetCalls for $element {
1118 fn create_render_object(
1119 self: Handle<Self>,
1120 app: &mut App,
1121 widget: &WidgetRef,
1122 ) -> AnyRenderObject {
1123 $widget_face(widget).create_render_object(app, self.as_element())
1124 }
1125
1126 fn update_render_object(self: Handle<Self>, app: &mut App, widget: &WidgetRef) {
1127 let render_object = RenderObjectElement::render_object(self, app);
1128 $widget_face(widget).update_render_object(app, self.as_element(), render_object);
1129 }
1130
1131 fn did_unmount_render_object(
1132 self: Handle<Self>,
1133 app: &mut App,
1134 old_widget: &WidgetRef,
1135 render_object: AnyRenderObject,
1136 ) {
1137 $widget_face(old_widget).did_unmount_render_object(app, render_object);
1138 }
1139 }
1140 };
1141}
1142
1143macro_rules! render_object_element_overrides {
1145 () => {
1146 fn render_object(self: Handle<Self>, app: &App) -> Option<AnyRenderObject> {
1147 self.render_object_element_data(app).render_object
1148 }
1149
1150 fn render_object_attaching_child(self: Handle<Self>, _app: &App) -> Option<AnyElement> {
1151 None
1152 }
1153
1154 fn debug_doing_build(self: Handle<Self>, app: &App) -> bool {
1155 self.render_object_element_data(app).debug_doing_build
1156 }
1157
1158 const IS_RENDER_OBJECT_ELEMENT: bool = true;
1159
1160 #[inline(always)]
1161 fn mount(
1162 self: Handle<Self>,
1163 app: &mut App,
1164 parent: Option<AnyElement>,
1165 new_slot: Option<Slot>,
1166 ) {
1167 RenderObjectElement::mount(self, app, parent, new_slot);
1168 }
1169
1170 #[inline(always)]
1171 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
1172 RenderObjectElement::update(self, app, new_widget);
1173 }
1174
1175 #[inline(always)]
1176 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
1177 RenderObjectElement::perform_rebuild(self, app);
1178 }
1179
1180 fn deactivate(self: Handle<Self>, app: &mut App) {
1181 RenderObjectElement::deactivate(self, app);
1182 }
1183
1184 fn unmount(self: Handle<Self>, app: &mut App) {
1185 RenderObjectElement::unmount(self, app);
1186 }
1187
1188 fn update_parent_data(self: Handle<Self>, app: &mut App, parent_data_element: AnyElement) {
1189 RenderObjectElement::update_parent_data(self, app, parent_data_element);
1190 }
1191
1192 fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
1193 RenderObjectElement::update_slot(self, app, new_slot);
1194 }
1195
1196 fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
1197 RenderObjectElement::attach_render_object(self, app, new_slot);
1198 }
1199
1200 fn detach_render_object(self: Handle<Self>, app: &mut App) {
1201 RenderObjectElement::detach_render_object(self, app);
1202 }
1203 };
1204}
1205
1206pub struct LeafRenderObjectElement {
1211 element: ElementData,
1212 render_object_element: RenderObjectElementData,
1213}
1214
1215impl LeafRenderObjectElement {
1216 pub fn new(app: &mut App, widget: WidgetRef) -> Handle<Self> {
1218 debug_assert!(widget.as_render_object().is_some());
1219 app.create(LeafRenderObjectElement {
1220 element: ElementData::new(widget),
1221 render_object_element: RenderObjectElementData::default(),
1222 })
1223 .into_tree_node(app)
1224 }
1225}
1226
1227erased_render_object_widget_calls!(LeafRenderObjectElement, leaf_widget);
1228
1229fn leaf_widget(widget: &WidgetRef) -> &dyn AnyRenderObjectWidget {
1231 widget
1232 .as_render_object()
1233 .expect("a LeafRenderObjectElement holds a LeafRenderObjectWidget")
1234}
1235
1236impl RenderObjectElement for LeafRenderObjectElement {
1237 fn render_object_element_data(self: Handle<Self>, app: &App) -> &RenderObjectElementData {
1238 &app.get(self).render_object_element
1239 }
1240
1241 fn render_object_element_data_mut(
1242 self: Handle<Self>,
1243 app: &mut App,
1244 ) -> &mut RenderObjectElementData {
1245 &mut app.get_mut(self).render_object_element
1246 }
1247}
1248
1249impl Element for LeafRenderObjectElement {
1250 crate::element_accessors!();
1251 render_object_element_overrides!();
1252
1253 fn forget_child(self: Handle<Self>, _app: &mut App, _child: AnyElement) {
1254 unreachable!("a leaf render object element has no child to forget");
1255 }
1256
1257 fn insert_render_object_child(
1258 self: Handle<Self>,
1259 _app: &mut App,
1260 _child: AnyRenderObject,
1261 _slot: Option<Slot>,
1262 ) {
1263 unreachable!("a leaf render object element takes no child");
1264 }
1265
1266 fn move_render_object_child(
1267 self: Handle<Self>,
1268 _app: &mut App,
1269 _child: AnyRenderObject,
1270 _old_slot: Option<Slot>,
1271 _new_slot: Option<Slot>,
1272 ) {
1273 unreachable!("a leaf render object element takes no child");
1274 }
1275
1276 fn remove_render_object_child(
1277 self: Handle<Self>,
1278 _app: &mut App,
1279 _child: AnyRenderObject,
1280 _slot: Option<Slot>,
1281 ) {
1282 unreachable!("a leaf render object element takes no child");
1283 }
1284}
1285
1286#[derive(Default)]
1291pub struct SingleChildRenderObjectElementData {
1292 child: Option<AnyElement>,
1293}
1294
1295pub trait SingleChildRenderObjectElementBase:
1316 RenderObjectElement + RenderObjectWidgetCalls
1317{
1318 fn single_child_data(self: Handle<Self>, app: &App) -> &SingleChildRenderObjectElementData;
1320
1321 fn single_child_data_mut(
1323 self: Handle<Self>,
1324 app: &mut App,
1325 ) -> &mut SingleChildRenderObjectElementData;
1326
1327 fn child(self: Handle<Self>, app: &App) -> Option<AnyElement> {
1329 self.single_child_data(app).child
1330 }
1331
1332 #[inline(always)]
1335 fn update_child_from_widget(self: Handle<Self>, app: &mut App) {
1336 let child_widget = single_child_widget(self.as_element().widget(app))
1337 .child()
1338 .cloned();
1339 let child = self.single_child_data(app).child;
1340 let child = self
1341 .as_element()
1342 .update_child(app, child, child_widget, None);
1343 self.single_child_data_mut(app).child = child;
1344 }
1345
1346 fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
1348 if let Some(child) = self.single_child_data(app).child {
1349 visitor(child);
1350 }
1351 }
1352
1353 fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
1355 debug_assert!(self.single_child_data(app).child == Some(child));
1356 self.single_child_data_mut(app).child = None;
1357 }
1358
1359 #[inline(always)]
1361 fn mount(
1362 self: Handle<Self>,
1363 app: &mut App,
1364 parent: Option<AnyElement>,
1365 new_slot: Option<Slot>,
1366 ) {
1367 debug_assert!(
1368 self.as_element()
1369 .widget(app)
1370 .as_single_child_render_object()
1371 .is_some(),
1372 "{:?} uses SingleChildRenderObjectElementBase, but its widget did not reach the tree \
1373 through the framework's SingleChildRenderObjectWidget wrapper: turn it into a widget \
1374 with into_widget and give it its own create_element",
1375 self.as_element()
1376 );
1377 RenderObjectElement::mount(self, app, parent, new_slot);
1378 self.update_child_from_widget(app);
1379 }
1380
1381 #[inline(always)]
1383 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
1384 RenderObjectElement::update(self, app, new_widget);
1385 self.update_child_from_widget(app);
1386 }
1387
1388 fn insert_render_object_child(
1390 self: Handle<Self>,
1391 app: &mut App,
1392 child: AnyRenderObject,
1393 slot: Option<Slot>,
1394 ) {
1395 debug_assert!(slot.is_none());
1396 let render_object = RenderObjectElement::render_object(self, app);
1397 let widget = self.as_element().widget(app).clone();
1398 single_child_widget(&widget).set_render_object_child(app, render_object, Some(child));
1399 }
1400
1401 fn move_render_object_child(
1403 self: Handle<Self>,
1404 _app: &mut App,
1405 _child: AnyRenderObject,
1406 _old_slot: Option<Slot>,
1407 _new_slot: Option<Slot>,
1408 ) {
1409 unreachable!("a single child never moves slots");
1410 }
1411
1412 fn remove_render_object_child(
1414 self: Handle<Self>,
1415 app: &mut App,
1416 child: AnyRenderObject,
1417 slot: Option<Slot>,
1418 ) {
1419 debug_assert!(slot.is_none());
1420 let render_object = RenderObjectElement::render_object(self, app);
1421 let widget = self.as_element().widget(app).clone();
1422 let widget = single_child_widget(&widget);
1423 debug_assert!(widget.render_object_child(app, render_object) == Some(child));
1424 widget.set_render_object_child(app, render_object, None);
1425 }
1426}
1427
1428pub struct SingleChildRenderObjectElement {
1431 element: ElementData,
1432 render_object_element: RenderObjectElementData,
1433 single_child: SingleChildRenderObjectElementData,
1434}
1435
1436impl SingleChildRenderObjectElement {
1437 pub fn new(app: &mut App, widget: WidgetRef) -> Handle<Self> {
1439 debug_assert!(widget.as_single_child_render_object().is_some());
1440 app.create(SingleChildRenderObjectElement {
1441 element: ElementData::new(widget),
1442 render_object_element: RenderObjectElementData::default(),
1443 single_child: SingleChildRenderObjectElementData::default(),
1444 })
1445 .into_tree_node(app)
1446 }
1447}
1448
1449erased_render_object_widget_calls!(
1450 SingleChildRenderObjectElement,
1451 |widget| -> &dyn AnyRenderObjectWidget { single_child_widget(widget) }
1452);
1453
1454fn single_child_widget(widget: &WidgetRef) -> &dyn AnySingleChildRenderObjectWidget {
1456 widget
1457 .as_single_child_render_object()
1458 .expect("a single-child render object element holds a SingleChildRenderObjectWidget")
1459}
1460
1461impl RenderObjectElement for SingleChildRenderObjectElement {
1462 fn render_object_element_data(self: Handle<Self>, app: &App) -> &RenderObjectElementData {
1463 &app.get(self).render_object_element
1464 }
1465
1466 fn render_object_element_data_mut(
1467 self: Handle<Self>,
1468 app: &mut App,
1469 ) -> &mut RenderObjectElementData {
1470 &mut app.get_mut(self).render_object_element
1471 }
1472}
1473
1474impl SingleChildRenderObjectElementBase for SingleChildRenderObjectElement {
1475 fn single_child_data(self: Handle<Self>, app: &App) -> &SingleChildRenderObjectElementData {
1476 &app.get(self).single_child
1477 }
1478
1479 fn single_child_data_mut(
1480 self: Handle<Self>,
1481 app: &mut App,
1482 ) -> &mut SingleChildRenderObjectElementData {
1483 &mut app.get_mut(self).single_child
1484 }
1485}
1486
1487impl Element for SingleChildRenderObjectElement {
1488 crate::element_accessors!();
1489
1490 fn render_object(self: Handle<Self>, app: &App) -> Option<AnyRenderObject> {
1491 self.render_object_element_data(app).render_object
1492 }
1493
1494 fn render_object_attaching_child(self: Handle<Self>, _app: &App) -> Option<AnyElement> {
1495 None
1496 }
1497
1498 fn debug_doing_build(self: Handle<Self>, app: &App) -> bool {
1499 self.render_object_element_data(app).debug_doing_build
1500 }
1501
1502 const IS_RENDER_OBJECT_ELEMENT: bool = true;
1503
1504 fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
1505 SingleChildRenderObjectElementBase::visit_children(self, app, visitor);
1506 }
1507
1508 fn debug_visit_onstage_children(
1509 self: Handle<Self>,
1510 app: &App,
1511 visitor: &mut dyn FnMut(AnyElement),
1512 ) {
1513 let children: Vec<AnyElement> = self.child(app).into_iter().collect();
1514 single_child_widget(self.as_element().widget(app))
1515 .debug_visit_onstage_children(app, &children, visitor);
1516 }
1517
1518 fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
1519 SingleChildRenderObjectElementBase::forget_child(self, app, child);
1520 }
1521
1522 #[inline(always)]
1523 fn mount(
1524 self: Handle<Self>,
1525 app: &mut App,
1526 parent: Option<AnyElement>,
1527 new_slot: Option<Slot>,
1528 ) {
1529 SingleChildRenderObjectElementBase::mount(self, app, parent, new_slot);
1530 }
1531
1532 #[inline(always)]
1533 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
1534 SingleChildRenderObjectElementBase::update(self, app, new_widget);
1535 }
1536
1537 #[inline(always)]
1538 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
1539 RenderObjectElement::perform_rebuild(self, app);
1540 }
1541
1542 fn deactivate(self: Handle<Self>, app: &mut App) {
1543 RenderObjectElement::deactivate(self, app);
1544 }
1545
1546 fn unmount(self: Handle<Self>, app: &mut App) {
1547 RenderObjectElement::unmount(self, app);
1548 }
1549
1550 fn update_parent_data(self: Handle<Self>, app: &mut App, parent_data_element: AnyElement) {
1551 RenderObjectElement::update_parent_data(self, app, parent_data_element);
1552 }
1553
1554 fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
1555 RenderObjectElement::update_slot(self, app, new_slot);
1556 }
1557
1558 fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
1559 RenderObjectElement::attach_render_object(self, app, new_slot);
1560 }
1561
1562 fn detach_render_object(self: Handle<Self>, app: &mut App) {
1563 RenderObjectElement::detach_render_object(self, app);
1564 }
1565
1566 fn insert_render_object_child(
1567 self: Handle<Self>,
1568 app: &mut App,
1569 child: AnyRenderObject,
1570 slot: Option<Slot>,
1571 ) {
1572 SingleChildRenderObjectElementBase::insert_render_object_child(self, app, child, slot);
1573 }
1574
1575 fn move_render_object_child(
1576 self: Handle<Self>,
1577 app: &mut App,
1578 child: AnyRenderObject,
1579 old_slot: Option<Slot>,
1580 new_slot: Option<Slot>,
1581 ) {
1582 SingleChildRenderObjectElementBase::move_render_object_child(
1583 self, app, child, old_slot, new_slot,
1584 );
1585 }
1586
1587 fn remove_render_object_child(
1588 self: Handle<Self>,
1589 app: &mut App,
1590 child: AnyRenderObject,
1591 slot: Option<Slot>,
1592 ) {
1593 SingleChildRenderObjectElementBase::remove_render_object_child(self, app, child, slot);
1594 }
1595}
1596
1597#[derive(Default)]
1602pub struct MultiChildRenderObjectElementData {
1603 children: Vec<AnyElement>,
1604 forgotten_children: HashSet<AnyElement>,
1607}
1608
1609pub trait MultiChildRenderObjectElementBase: RenderObjectElement + RenderObjectWidgetCalls {
1636 fn multi_child_data(self: Handle<Self>, app: &App) -> &MultiChildRenderObjectElementData;
1638
1639 fn multi_child_data_mut(
1641 self: Handle<Self>,
1642 app: &mut App,
1643 ) -> &mut MultiChildRenderObjectElementData;
1644
1645 fn children(self: Handle<Self>, app: &App) -> Vec<AnyElement> {
1650 let this = self.multi_child_data(app);
1651 this.children
1652 .iter()
1653 .copied()
1654 .filter(|child| !this.forgotten_children.contains(child))
1655 .collect()
1656 }
1657
1658 fn after_render_object(app: &App, slot: Option<&Slot>) -> Option<AnyRenderObject> {
1661 let render_object = Self::indexed_slot(slot)
1662 .value?
1663 .render_object(app)
1664 .expect("the element before this slot has a render object");
1665 Some(render_object)
1666 }
1667
1668 fn indexed_slot(slot: Option<&Slot>) -> &IndexedSlot {
1670 match slot {
1671 Some(Slot::Indexed(slot)) => slot,
1672 _ => unreachable!("a multi-child element gives its children indexed slots"),
1673 }
1674 }
1675
1676 fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
1678 let this = self.multi_child_data(app);
1679 for child in &this.children {
1680 if !this.forgotten_children.contains(child) {
1681 visitor(*child);
1682 }
1683 }
1684 }
1685
1686 fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
1688 debug_assert!(self.multi_child_data(app).children.contains(&child));
1689 debug_assert!(
1690 !self
1691 .multi_child_data(app)
1692 .forgotten_children
1693 .contains(&child)
1694 );
1695 self.multi_child_data_mut(app)
1696 .forgotten_children
1697 .insert(child);
1698 }
1699
1700 #[inline(always)]
1702 fn mount(
1703 self: Handle<Self>,
1704 app: &mut App,
1705 parent: Option<AnyElement>,
1706 new_slot: Option<Slot>,
1707 ) {
1708 debug_assert!(
1709 self.as_element()
1710 .widget(app)
1711 .as_multi_child_render_object()
1712 .is_some(),
1713 "{:?} uses MultiChildRenderObjectElementBase, but its widget did not reach the tree \
1714 through the framework's MultiChildRenderObjectWidget wrapper: turn it into a widget \
1715 with into_widget and give it its own create_element",
1716 self.as_element()
1717 );
1718 RenderObjectElement::mount(self, app, parent, new_slot);
1719 let child_widgets = multi_child_widget(self.as_element().widget(app))
1720 .children()
1721 .to_vec();
1722 let mut children = Vec::with_capacity(child_widgets.len());
1723 let mut previous_child: Option<AnyElement> = None;
1724 for (index, child_widget) in child_widgets.into_iter().enumerate() {
1725 let new_child = self.as_element().inflate_widget(
1726 app,
1727 child_widget,
1728 Some(Slot::Indexed(IndexedSlot {
1729 index,
1730 value: previous_child,
1731 })),
1732 );
1733 children.push(new_child);
1734 previous_child = Some(new_child);
1735 }
1736 self.multi_child_data_mut(app).children = children;
1737 }
1738
1739 #[inline(always)]
1741 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
1742 RenderObjectElement::update(self, app, new_widget);
1743 let child_widgets = multi_child_widget(self.as_element().widget(app))
1744 .children()
1745 .to_vec();
1746 let old_children = self.multi_child_data(app).children.clone();
1747 let is_forgotten = |app: &App, child: AnyElement| {
1748 self.multi_child_data(app)
1749 .forgotten_children
1750 .contains(&child)
1751 };
1752 let children = self.as_element().update_children(
1753 app,
1754 &old_children,
1755 &child_widgets,
1756 Some(&is_forgotten),
1757 None,
1758 );
1759 let this = self.multi_child_data_mut(app);
1760 this.children = children;
1761 this.forgotten_children.clear();
1762 }
1763
1764 fn insert_render_object_child(
1766 self: Handle<Self>,
1767 app: &mut App,
1768 child: AnyRenderObject,
1769 slot: Option<Slot>,
1770 ) {
1771 let render_object = RenderObjectElement::render_object(self, app);
1772 let after = Self::after_render_object(app, slot.as_ref());
1773 let widget = self.as_element().widget(app).clone();
1774 multi_child_widget(&widget).insert_render_object_child(app, render_object, child, after);
1775 }
1776
1777 fn move_render_object_child(
1779 self: Handle<Self>,
1780 app: &mut App,
1781 child: AnyRenderObject,
1782 _old_slot: Option<Slot>,
1783 new_slot: Option<Slot>,
1784 ) {
1785 let render_object = RenderObjectElement::render_object(self, app);
1786 debug_assert!(child.parent(app) == Some(render_object));
1787 let after = Self::after_render_object(app, new_slot.as_ref());
1788 let widget = self.as_element().widget(app).clone();
1789 multi_child_widget(&widget).move_render_object_child(app, render_object, child, after);
1790 }
1791
1792 fn remove_render_object_child(
1794 self: Handle<Self>,
1795 app: &mut App,
1796 child: AnyRenderObject,
1797 _slot: Option<Slot>,
1798 ) {
1799 let render_object = RenderObjectElement::render_object(self, app);
1800 debug_assert!(child.parent(app) == Some(render_object));
1801 let widget = self.as_element().widget(app).clone();
1802 multi_child_widget(&widget).remove_render_object_child(app, render_object, child);
1803 }
1804}
1805
1806pub struct MultiChildRenderObjectElement {
1809 element: ElementData,
1810 render_object_element: RenderObjectElementData,
1811 multi_child: MultiChildRenderObjectElementData,
1812}
1813
1814impl MultiChildRenderObjectElement {
1815 pub fn new(app: &mut App, widget: WidgetRef) -> Handle<Self> {
1817 debug_assert!(widget.as_multi_child_render_object().is_some());
1818 app.create(MultiChildRenderObjectElement {
1819 element: ElementData::new(widget),
1820 render_object_element: RenderObjectElementData::default(),
1821 multi_child: MultiChildRenderObjectElementData::default(),
1822 })
1823 .into_tree_node(app)
1824 }
1825}
1826
1827erased_render_object_widget_calls!(
1828 MultiChildRenderObjectElement,
1829 |widget| -> &dyn AnyRenderObjectWidget { multi_child_widget(widget) }
1830);
1831
1832fn multi_child_widget(widget: &WidgetRef) -> &dyn AnyMultiChildRenderObjectWidget {
1834 widget
1835 .as_multi_child_render_object()
1836 .expect("a multi-child render object element holds a MultiChildRenderObjectWidget")
1837}
1838
1839impl RenderObjectElement for MultiChildRenderObjectElement {
1840 fn render_object_element_data(self: Handle<Self>, app: &App) -> &RenderObjectElementData {
1841 &app.get(self).render_object_element
1842 }
1843
1844 fn render_object_element_data_mut(
1845 self: Handle<Self>,
1846 app: &mut App,
1847 ) -> &mut RenderObjectElementData {
1848 &mut app.get_mut(self).render_object_element
1849 }
1850}
1851
1852impl MultiChildRenderObjectElementBase for MultiChildRenderObjectElement {
1853 fn multi_child_data(self: Handle<Self>, app: &App) -> &MultiChildRenderObjectElementData {
1854 &app.get(self).multi_child
1855 }
1856
1857 fn multi_child_data_mut(
1858 self: Handle<Self>,
1859 app: &mut App,
1860 ) -> &mut MultiChildRenderObjectElementData {
1861 &mut app.get_mut(self).multi_child
1862 }
1863}
1864
1865impl Element for MultiChildRenderObjectElement {
1866 crate::element_accessors!();
1867
1868 fn render_object(self: Handle<Self>, app: &App) -> Option<AnyRenderObject> {
1869 self.render_object_element_data(app).render_object
1870 }
1871
1872 fn render_object_attaching_child(self: Handle<Self>, _app: &App) -> Option<AnyElement> {
1873 None
1874 }
1875
1876 fn debug_doing_build(self: Handle<Self>, app: &App) -> bool {
1877 self.render_object_element_data(app).debug_doing_build
1878 }
1879
1880 const IS_RENDER_OBJECT_ELEMENT: bool = true;
1881
1882 #[inline(always)]
1883 fn perform_rebuild(self: Handle<Self>, app: &mut App) {
1884 RenderObjectElement::perform_rebuild(self, app);
1885 }
1886
1887 fn deactivate(self: Handle<Self>, app: &mut App) {
1888 RenderObjectElement::deactivate(self, app);
1889 }
1890
1891 fn unmount(self: Handle<Self>, app: &mut App) {
1892 RenderObjectElement::unmount(self, app);
1893 }
1894
1895 fn update_parent_data(self: Handle<Self>, app: &mut App, parent_data_element: AnyElement) {
1896 RenderObjectElement::update_parent_data(self, app, parent_data_element);
1897 }
1898
1899 fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
1900 RenderObjectElement::update_slot(self, app, new_slot);
1901 }
1902
1903 fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
1904 RenderObjectElement::attach_render_object(self, app, new_slot);
1905 }
1906
1907 fn detach_render_object(self: Handle<Self>, app: &mut App) {
1908 RenderObjectElement::detach_render_object(self, app);
1909 }
1910
1911 fn visit_children(self: Handle<Self>, app: &App, visitor: &mut dyn FnMut(AnyElement)) {
1912 MultiChildRenderObjectElementBase::visit_children(self, app, visitor);
1913 }
1914
1915 fn debug_visit_onstage_children(
1916 self: Handle<Self>,
1917 app: &App,
1918 visitor: &mut dyn FnMut(AnyElement),
1919 ) {
1920 let mut children = Vec::new();
1921 MultiChildRenderObjectElementBase::visit_children(self, app, &mut |child| {
1922 children.push(child);
1923 });
1924 multi_child_widget(self.as_element().widget(app))
1925 .debug_visit_onstage_children(app, &children, visitor);
1926 }
1927
1928 fn forget_child(self: Handle<Self>, app: &mut App, child: AnyElement) {
1929 MultiChildRenderObjectElementBase::forget_child(self, app, child);
1930 }
1931
1932 #[inline(always)]
1933 fn mount(
1934 self: Handle<Self>,
1935 app: &mut App,
1936 parent: Option<AnyElement>,
1937 new_slot: Option<Slot>,
1938 ) {
1939 MultiChildRenderObjectElementBase::mount(self, app, parent, new_slot);
1940 }
1941
1942 #[inline(always)]
1943 fn update(self: Handle<Self>, app: &mut App, new_widget: WidgetRef) {
1944 MultiChildRenderObjectElementBase::update(self, app, new_widget);
1945 }
1946
1947 fn insert_render_object_child(
1948 self: Handle<Self>,
1949 app: &mut App,
1950 child: AnyRenderObject,
1951 slot: Option<Slot>,
1952 ) {
1953 MultiChildRenderObjectElementBase::insert_render_object_child(self, app, child, slot);
1954 }
1955
1956 fn move_render_object_child(
1957 self: Handle<Self>,
1958 app: &mut App,
1959 child: AnyRenderObject,
1960 old_slot: Option<Slot>,
1961 new_slot: Option<Slot>,
1962 ) {
1963 MultiChildRenderObjectElementBase::move_render_object_child(
1964 self, app, child, old_slot, new_slot,
1965 );
1966 }
1967
1968 fn remove_render_object_child(
1969 self: Handle<Self>,
1970 app: &mut App,
1971 child: AnyRenderObject,
1972 slot: Option<Slot>,
1973 ) {
1974 MultiChildRenderObjectElementBase::remove_render_object_child(self, app, child, slot);
1975 }
1976}
1977
1978pub trait RenderTreeRootElement: RenderObjectElement {
1988 fn attach_render_object(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
1990 self.as_element().set_slot(app, new_slot);
1991 debug_assert!(self.debug_check_must_not_attach_render_object_to_ancestor(app));
1992 }
1993
1994 fn detach_render_object(self: Handle<Self>, app: &mut App) {
1996 self.as_element().set_slot(app, None);
1997 }
1998
1999 fn update_slot(self: Handle<Self>, app: &mut App, new_slot: Option<Slot>) {
2001 ElementBase::update_slot(self, app, new_slot);
2002 debug_assert!(self.debug_check_must_not_attach_render_object_to_ancestor(app));
2003 }
2004
2005 fn debug_check_must_not_attach_render_object_to_ancestor(
2006 self: Handle<Self>,
2007 app: &App,
2008 ) -> bool {
2009 if !cfg!(debug_assertions) {
2010 return true;
2011 }
2012 assert!(
2013 self.find_ancestor_render_object_element(app).is_none(),
2014 "The RenderObject for {:?} cannot maintain an independent render tree at its \
2015 current location. This RenderObject is the root of an independent render tree and \
2016 it cannot attach itself to an ancestor in an existing tree. The ancestor \
2017 RenderObject, however, expects that a child will be attached.",
2018 self.as_element()
2019 );
2020 true
2021 }
2022}