1use crate::{
35 App, ArenaBox, AvailableSpace, Bounds, Context, DispatchNodeId, ElementId, FocusHandle,
36 InspectorElementId, LayoutId, Pixels, Point, SharedString, Size, Style, Window,
37 util::FluentBuilder, window::with_element_arena,
38};
39use derive_more::{Deref, DerefMut};
40use std::{
41 any::{Any, type_name},
42 fmt::{self, Debug, Display},
43 mem, panic,
44 sync::Arc,
45};
46
47pub trait Element: 'static + IntoElement {
52 type RequestLayoutState: 'static;
55
56 type PrepaintState: 'static;
59
60 fn id(&self) -> Option<ElementId>;
66
67 fn source_location(&self) -> Option<&'static panic::Location<'static>>;
70
71 fn request_layout(
74 &mut self,
75 id: Option<&GlobalElementId>,
76 inspector_id: Option<&InspectorElementId>,
77 window: &mut Window,
78 cx: &mut App,
79 ) -> (LayoutId, Self::RequestLayoutState);
80
81 fn prepaint(
84 &mut self,
85 id: Option<&GlobalElementId>,
86 inspector_id: Option<&InspectorElementId>,
87 bounds: Bounds<Pixels>,
88 request_layout: &mut Self::RequestLayoutState,
89 window: &mut Window,
90 cx: &mut App,
91 ) -> Self::PrepaintState;
92
93 fn paint(
96 &mut self,
97 id: Option<&GlobalElementId>,
98 inspector_id: Option<&InspectorElementId>,
99 bounds: Bounds<Pixels>,
100 request_layout: &mut Self::RequestLayoutState,
101 prepaint: &mut Self::PrepaintState,
102 window: &mut Window,
103 cx: &mut App,
104 );
105
106 fn into_any(self) -> AnyElement {
108 AnyElement::new(self)
109 }
110}
111
112pub trait IntoElement: Sized {
114 type Element: Element;
117
118 fn into_element(self) -> Self::Element;
120
121 fn into_any_element(self) -> AnyElement {
123 self.into_element().into_any()
124 }
125}
126
127impl<T: IntoElement> FluentBuilder for T {}
128
129pub trait Render: 'static + Sized {
132 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement;
134}
135
136impl Render for Empty {
137 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
138 Empty
139 }
140}
141
142pub trait RenderOnce: 'static {
148 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement;
152}
153
154pub trait ParentElement {
157 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>);
159
160 fn child(mut self, child: impl IntoElement) -> Self
162 where
163 Self: Sized,
164 {
165 self.extend(std::iter::once(child.into_element().into_any()));
166 self
167 }
168
169 fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self
171 where
172 Self: Sized,
173 {
174 self.extend(children.into_iter().map(|child| child.into_any_element()));
175 self
176 }
177}
178
179#[doc(hidden)]
182pub struct Component<C: RenderOnce> {
183 component: Option<C>,
184 #[cfg(debug_assertions)]
185 source: &'static core::panic::Location<'static>,
186}
187
188impl<C: RenderOnce> Component<C> {
189 #[track_caller]
191 pub fn new(component: C) -> Self {
192 Component {
193 component: Some(component),
194 #[cfg(debug_assertions)]
195 source: core::panic::Location::caller(),
196 }
197 }
198}
199
200fn prepaint_component(
201 (element, name): &mut (AnyElement, &'static str),
202 window: &mut Window,
203 cx: &mut App,
204) {
205 window.with_id(ElementId::Name(SharedString::new_static(name)), |window| {
206 element.prepaint(window, cx);
207 })
208}
209
210fn paint_component(
211 (element, name): &mut (AnyElement, &'static str),
212 window: &mut Window,
213 cx: &mut App,
214) {
215 window.with_id(ElementId::Name(SharedString::new_static(name)), |window| {
216 element.paint(window, cx);
217 })
218}
219impl<C: RenderOnce> Element for Component<C> {
220 type RequestLayoutState = (AnyElement, &'static str);
221 type PrepaintState = ();
222
223 fn id(&self) -> Option<ElementId> {
224 None
225 }
226
227 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
228 #[cfg(debug_assertions)]
229 return Some(self.source);
230
231 #[cfg(not(debug_assertions))]
232 return None;
233 }
234
235 fn request_layout(
236 &mut self,
237 _id: Option<&GlobalElementId>,
238 _inspector_id: Option<&InspectorElementId>,
239 window: &mut Window,
240 cx: &mut App,
241 ) -> (LayoutId, Self::RequestLayoutState) {
242 window.with_id(ElementId::Name(type_name::<C>().into()), |window| {
243 let mut element = self
244 .component
245 .take()
246 .unwrap()
247 .render(window, cx)
248 .into_any_element();
249
250 let layout_id = element.request_layout(window, cx);
251 (layout_id, (element, type_name::<C>()))
252 })
253 }
254
255 fn prepaint(
256 &mut self,
257 _id: Option<&GlobalElementId>,
258 _inspector_id: Option<&InspectorElementId>,
259 _: Bounds<Pixels>,
260 state: &mut Self::RequestLayoutState,
261 window: &mut Window,
262 cx: &mut App,
263 ) {
264 prepaint_component(state, window, cx);
265 }
266
267 fn paint(
268 &mut self,
269 _id: Option<&GlobalElementId>,
270 _inspector_id: Option<&InspectorElementId>,
271 _: Bounds<Pixels>,
272 state: &mut Self::RequestLayoutState,
273 _: &mut Self::PrepaintState,
274 window: &mut Window,
275 cx: &mut App,
276 ) {
277 paint_component(state, window, cx);
278 }
279}
280
281impl<C: RenderOnce> IntoElement for Component<C> {
282 type Element = Self;
283
284 fn into_element(self) -> Self::Element {
285 self
286 }
287}
288
289#[derive(Deref, DerefMut, Clone, Default, Debug, Eq, PartialEq, Hash)]
291pub struct GlobalElementId(pub(crate) Arc<[ElementId]>);
292
293impl Display for GlobalElementId {
294 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
295 for (i, element_id) in self.0.iter().enumerate() {
296 if i > 0 {
297 write!(f, ".")?;
298 }
299 write!(f, "{}", element_id)?;
300 }
301 Ok(())
302 }
303}
304
305trait ElementObject {
306 fn inner_element(&mut self) -> &mut dyn Any;
307
308 fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId;
309
310 fn prepaint(&mut self, window: &mut Window, cx: &mut App);
311
312 fn paint(&mut self, window: &mut Window, cx: &mut App);
313
314 fn layout_as_root(
315 &mut self,
316 available_space: Size<AvailableSpace>,
317 window: &mut Window,
318 cx: &mut App,
319 ) -> Size<Pixels>;
320}
321
322pub struct Drawable<E: Element> {
324 pub element: E,
326 phase: ElementDrawPhase<E::RequestLayoutState, E::PrepaintState>,
327}
328
329#[derive(Default)]
330enum ElementDrawPhase<RequestLayoutState, PrepaintState> {
331 #[default]
332 Start,
333 RequestLayout {
334 layout_id: LayoutId,
335 global_id: Option<GlobalElementId>,
336 inspector_id: Option<InspectorElementId>,
337 request_layout: RequestLayoutState,
338 },
339 LayoutComputed {
340 layout_id: LayoutId,
341 global_id: Option<GlobalElementId>,
342 inspector_id: Option<InspectorElementId>,
343 available_space: Size<AvailableSpace>,
344 request_layout: RequestLayoutState,
345 },
346 Prepaint {
347 node_id: DispatchNodeId,
348 global_id: Option<GlobalElementId>,
349 inspector_id: Option<InspectorElementId>,
350 bounds: Bounds<Pixels>,
351 request_layout: RequestLayoutState,
352 prepaint: PrepaintState,
353 },
354 Painted,
355}
356
357impl<E: Element> Drawable<E> {
359 pub(crate) fn new(element: E) -> Self {
360 Drawable {
361 element,
362 phase: ElementDrawPhase::Start,
363 }
364 }
365
366 fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
367 match mem::take(&mut self.phase) {
368 ElementDrawPhase::Start => {
369 let global_id = self.element.id().map(|element_id| {
370 window.element_id_stack.push(element_id);
371 GlobalElementId(Arc::from(&*window.element_id_stack))
372 });
373
374 let inspector_id;
375 #[cfg(debug_assertions)]
376 {
377 inspector_id = self.element.source_location().map(|source| {
378 let path = crate::InspectorElementPath {
379 global_id: GlobalElementId(Arc::from(&*window.element_id_stack)),
380 source_location: source,
381 };
382 window.build_inspector_element_id(path)
383 });
384 }
385 #[cfg(not(debug_assertions))]
386 {
387 inspector_id = None;
388 }
389
390 let (layout_id, request_layout) = self.element.request_layout(
391 global_id.as_ref(),
392 inspector_id.as_ref(),
393 window,
394 cx,
395 );
396
397 if global_id.is_some() {
398 window.element_id_stack.pop();
399 }
400
401 self.phase = ElementDrawPhase::RequestLayout {
402 layout_id,
403 global_id,
404 inspector_id,
405 request_layout,
406 };
407 layout_id
408 }
409 _ => panic!("must call request_layout only once"),
410 }
411 }
412
413 pub(crate) fn prepaint(&mut self, window: &mut Window, cx: &mut App) {
414 match mem::take(&mut self.phase) {
415 ElementDrawPhase::RequestLayout {
416 layout_id,
417 global_id,
418 inspector_id,
419 mut request_layout,
420 }
421 | ElementDrawPhase::LayoutComputed {
422 layout_id,
423 global_id,
424 inspector_id,
425 mut request_layout,
426 ..
427 } => {
428 if let Some(element_id) = self.element.id() {
429 window.element_id_stack.push(element_id);
430 debug_assert_eq!(&*global_id.as_ref().unwrap().0, &*window.element_id_stack);
431 }
432
433 let bounds = window.layout_bounds(layout_id);
434 let node_id = window.next_frame.dispatch_tree.push_node();
435 let prepaint = self.element.prepaint(
436 global_id.as_ref(),
437 inspector_id.as_ref(),
438 bounds,
439 &mut request_layout,
440 window,
441 cx,
442 );
443 window.next_frame.dispatch_tree.pop_node();
444
445 if global_id.is_some() {
446 window.element_id_stack.pop();
447 }
448
449 self.phase = ElementDrawPhase::Prepaint {
450 node_id,
451 global_id,
452 inspector_id,
453 bounds,
454 request_layout,
455 prepaint,
456 };
457 }
458 _ => panic!("must call request_layout before prepaint"),
459 }
460 }
461
462 pub(crate) fn paint(
463 &mut self,
464 window: &mut Window,
465 cx: &mut App,
466 ) -> (E::RequestLayoutState, E::PrepaintState) {
467 match mem::take(&mut self.phase) {
468 ElementDrawPhase::Prepaint {
469 node_id,
470 global_id,
471 inspector_id,
472 bounds,
473 mut request_layout,
474 mut prepaint,
475 ..
476 } => {
477 if let Some(element_id) = self.element.id() {
478 window.element_id_stack.push(element_id);
479 debug_assert_eq!(&*global_id.as_ref().unwrap().0, &*window.element_id_stack);
480 }
481
482 window.next_frame.dispatch_tree.set_active_node(node_id);
483 self.element.paint(
484 global_id.as_ref(),
485 inspector_id.as_ref(),
486 bounds,
487 &mut request_layout,
488 &mut prepaint,
489 window,
490 cx,
491 );
492
493 if global_id.is_some() {
494 window.element_id_stack.pop();
495 }
496
497 self.phase = ElementDrawPhase::Painted;
498 (request_layout, prepaint)
499 }
500 _ => panic!("must call prepaint before paint"),
501 }
502 }
503
504 pub(crate) fn layout_as_root(
505 &mut self,
506 available_space: Size<AvailableSpace>,
507 window: &mut Window,
508 cx: &mut App,
509 ) -> Size<Pixels> {
510 if matches!(&self.phase, ElementDrawPhase::Start) {
511 self.request_layout(window, cx);
512 }
513
514 let layout_id = match mem::take(&mut self.phase) {
515 ElementDrawPhase::RequestLayout {
516 layout_id,
517 global_id,
518 inspector_id,
519 request_layout,
520 } => {
521 window.compute_layout(layout_id, available_space, cx);
522 self.phase = ElementDrawPhase::LayoutComputed {
523 layout_id,
524 global_id,
525 inspector_id,
526 available_space,
527 request_layout,
528 };
529 layout_id
530 }
531 ElementDrawPhase::LayoutComputed {
532 layout_id,
533 global_id,
534 inspector_id,
535 available_space: prev_available_space,
536 request_layout,
537 } => {
538 if available_space != prev_available_space {
539 window.compute_layout(layout_id, available_space, cx);
540 }
541 self.phase = ElementDrawPhase::LayoutComputed {
542 layout_id,
543 global_id,
544 inspector_id,
545 available_space,
546 request_layout,
547 };
548 layout_id
549 }
550 _ => panic!("cannot measure after painting"),
551 };
552
553 window.layout_bounds(layout_id).size
554 }
555}
556
557impl<E> ElementObject for Drawable<E>
558where
559 E: Element,
560 E::RequestLayoutState: 'static,
561{
562 fn inner_element(&mut self) -> &mut dyn Any {
563 &mut self.element
564 }
565
566 #[inline]
567 fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
568 Drawable::request_layout(self, window, cx)
569 }
570
571 #[inline]
572 fn prepaint(&mut self, window: &mut Window, cx: &mut App) {
573 Drawable::prepaint(self, window, cx);
574 }
575
576 #[inline]
577 fn paint(&mut self, window: &mut Window, cx: &mut App) {
578 Drawable::paint(self, window, cx);
579 }
580
581 #[inline]
582 fn layout_as_root(
583 &mut self,
584 available_space: Size<AvailableSpace>,
585 window: &mut Window,
586 cx: &mut App,
587 ) -> Size<Pixels> {
588 Drawable::layout_as_root(self, available_space, window, cx)
589 }
590}
591
592pub struct AnyElement(ArenaBox<dyn ElementObject>);
594
595impl AnyElement {
596 pub(crate) fn new<E>(element: E) -> Self
597 where
598 E: 'static + Element,
599 E::RequestLayoutState: Any,
600 {
601 let element = with_element_arena(|arena| arena.alloc(|| Drawable::new(element)))
602 .map(|element| element as &mut dyn ElementObject);
603 AnyElement(element)
604 }
605
606 pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
608 self.0.inner_element().downcast_mut::<T>()
609 }
610
611 pub fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
614 self.0.request_layout(window, cx)
615 }
616
617 pub fn prepaint(&mut self, window: &mut Window, cx: &mut App) -> Option<FocusHandle> {
620 let focus_assigned = window.next_frame.focus.is_some();
621
622 self.0.prepaint(window, cx);
623
624 if !focus_assigned && let Some(focus_id) = window.next_frame.focus {
625 return FocusHandle::for_id(focus_id, &cx.focus_handles);
626 }
627
628 None
629 }
630
631 pub fn paint(&mut self, window: &mut Window, cx: &mut App) {
633 self.0.paint(window, cx);
634 }
635
636 pub fn layout_as_root(
638 &mut self,
639 available_space: Size<AvailableSpace>,
640 window: &mut Window,
641 cx: &mut App,
642 ) -> Size<Pixels> {
643 self.0.layout_as_root(available_space, window, cx)
644 }
645
646 pub fn prepaint_at(
649 &mut self,
650 origin: Point<Pixels>,
651 window: &mut Window,
652 cx: &mut App,
653 ) -> Option<FocusHandle> {
654 window.with_absolute_element_offset(origin, |window| self.prepaint(window, cx))
655 }
656
657 pub fn prepaint_as_root(
660 &mut self,
661 origin: Point<Pixels>,
662 available_space: Size<AvailableSpace>,
663 window: &mut Window,
664 cx: &mut App,
665 ) -> Option<FocusHandle> {
666 self.layout_as_root(available_space, window, cx);
667 window.with_absolute_element_offset(origin, |window| self.prepaint(window, cx))
668 }
669}
670
671impl Element for AnyElement {
672 type RequestLayoutState = ();
673 type PrepaintState = ();
674
675 fn id(&self) -> Option<ElementId> {
676 None
677 }
678
679 fn source_location(&self) -> Option<&'static panic::Location<'static>> {
680 None
681 }
682
683 fn request_layout(
684 &mut self,
685 _: Option<&GlobalElementId>,
686 _inspector_id: Option<&InspectorElementId>,
687 window: &mut Window,
688 cx: &mut App,
689 ) -> (LayoutId, Self::RequestLayoutState) {
690 let layout_id = self.request_layout(window, cx);
691 (layout_id, ())
692 }
693
694 fn prepaint(
695 &mut self,
696 _: Option<&GlobalElementId>,
697 _inspector_id: Option<&InspectorElementId>,
698 _: Bounds<Pixels>,
699 _: &mut Self::RequestLayoutState,
700 window: &mut Window,
701 cx: &mut App,
702 ) {
703 self.prepaint(window, cx);
704 }
705
706 fn paint(
707 &mut self,
708 _: Option<&GlobalElementId>,
709 _inspector_id: Option<&InspectorElementId>,
710 _: Bounds<Pixels>,
711 _: &mut Self::RequestLayoutState,
712 _: &mut Self::PrepaintState,
713 window: &mut Window,
714 cx: &mut App,
715 ) {
716 self.paint(window, cx);
717 }
718}
719
720impl IntoElement for AnyElement {
721 type Element = Self;
722
723 fn into_element(self) -> Self::Element {
724 self
725 }
726
727 fn into_any_element(self) -> AnyElement {
728 self
729 }
730}
731
732pub struct Empty;
734
735impl IntoElement for Empty {
736 type Element = Self;
737
738 fn into_element(self) -> Self::Element {
739 self
740 }
741}
742
743impl Element for Empty {
744 type RequestLayoutState = ();
745 type PrepaintState = ();
746
747 fn id(&self) -> Option<ElementId> {
748 None
749 }
750
751 fn source_location(&self) -> Option<&'static panic::Location<'static>> {
752 None
753 }
754
755 fn request_layout(
756 &mut self,
757 _id: Option<&GlobalElementId>,
758 _inspector_id: Option<&InspectorElementId>,
759 window: &mut Window,
760 cx: &mut App,
761 ) -> (LayoutId, Self::RequestLayoutState) {
762 (
763 window.request_layout(
764 Style {
765 display: crate::Display::None,
766 ..Default::default()
767 },
768 None,
769 cx,
770 ),
771 (),
772 )
773 }
774
775 fn prepaint(
776 &mut self,
777 _id: Option<&GlobalElementId>,
778 _inspector_id: Option<&InspectorElementId>,
779 _bounds: Bounds<Pixels>,
780 _state: &mut Self::RequestLayoutState,
781 _window: &mut Window,
782 _cx: &mut App,
783 ) {
784 }
785
786 fn paint(
787 &mut self,
788 _id: Option<&GlobalElementId>,
789 _inspector_id: Option<&InspectorElementId>,
790 _bounds: Bounds<Pixels>,
791 _request_layout: &mut Self::RequestLayoutState,
792 _prepaint: &mut Self::PrepaintState,
793 _window: &mut Window,
794 _cx: &mut App,
795 ) {
796 }
797}