1//! Elements are the workhorses of GPUI. They are responsible for laying out and painting all of
2//! the contents of a window. Elements form a tree and are laid out according to the web layout
3//! standards as implemented by [taffy](https://github.com/DioxusLabs/taffy). Most of the time,
4//! you won't need to interact with this module or these APIs directly. Elements provide their
5//! own APIs and GPUI, or other element implementation, uses the APIs in this module to convert
6//! that element tree into the pixels you see on the screen.
7//!
8//! # Element Basics
9//!
10//! Elements are constructed by calling [`Render::render()`] on the root view of the window,
11//! which recursively constructs the element tree from the current state of the application,.
12//! These elements are then laid out by Taffy, and painted to the screen according to their own
13//! implementation of [`Element::paint()`]. Before the start of the next frame, the entire element
14//! tree and any callbacks they have registered with GPUI are dropped and the process repeats.
15//!
16//! But some state is too simple and voluminous to store in every view that needs it, e.g.
17//! whether a hover has been started or not. For this, GPUI provides the [`Element::PrepaintState`], associated type.
18//!
19//! # Implementing your own elements
20//!
21//! Elements are intended to be the low level, imperative API to GPUI. They are responsible for upholding,
22//! or breaking, GPUI's features as they deem necessary. As an example, most GPUI elements are expected
23//! to stay in the bounds that their parent element gives them. But with [`Window::with_content_mask`],
24//! you can ignore this restriction and paint anywhere inside of the window's bounds. This is useful for overlays
25//! and popups and anything else that shows up 'on top' of other elements.
26//! With great power, comes great responsibility.
27//!
28//! However, most of the time, you won't need to implement your own elements. GPUI provides a number of
29//! elements that should cover most common use cases out of the box and it's recommended that you use those
30//! to construct `components`, using the [`RenderOnce`] trait and the `#[derive(IntoElement)]` macro. Only implement
31//! elements when you need to take manual control of the layout and painting process, such as when using
32//! your own custom layout algorithm or rendering a code editor.
33
34use 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 accesskit::{Node, Role};
40use derive_more::{Deref, DerefMut};
41use std::{
42 any::{Any, type_name},
43 fmt::{self, Debug, Display},
44 hash::Hash,
45 mem, panic,
46 sync::Arc,
47};
48
49// div().id("hello").role(Role::Button)
50
51/// Implemented by types that participate in laying out and painting the contents of a window.
52/// Elements form a tree and are laid out according to web-based layout rules, as implemented by Taffy.
53/// You can create custom elements by implementing this trait, see the module-level documentation
54/// for more details.
55pub trait Element: 'static + IntoElement {
56 /// The type of state returned from [`Element::request_layout`]. A mutable reference to this state is subsequently
57 /// provided to [`Element::prepaint`] and [`Element::paint`].
58 type RequestLayoutState: 'static;
59
60 /// The type of state returned from [`Element::prepaint`]. A mutable reference to this state is subsequently
61 /// provided to [`Element::paint`].
62 type PrepaintState: 'static;
63
64 /// If this element has a unique identifier, return it here. This is used to track elements across frames, and
65 /// will cause a GlobalElementId to be passed to the request_layout, prepaint, and paint methods.
66 ///
67 /// The global id can in turn be used to access state that's connected to an element with the same id across
68 /// frames. This id must be unique among children of the first containing element with an id.
69 fn id(&self) -> Option<ElementId>;
70
71 /// Source location where this element was constructed, used to disambiguate elements in the
72 /// inspector and navigate to their source code.
73 fn source_location(&self) -> Option<&'static panic::Location<'static>>;
74
75 /// Before an element can be painted, we need to know where it's going to be and how big it is.
76 /// Use this method to request a layout from Taffy and initialize the element's state.
77 fn request_layout(
78 &mut self,
79 id: Option<&GlobalElementId>,
80 inspector_id: Option<&InspectorElementId>,
81 window: &mut Window,
82 cx: &mut App,
83 ) -> (LayoutId, Self::RequestLayoutState);
84
85 /// After laying out an element, we need to commit its bounds to the current frame for hitbox
86 /// purposes. The state argument is the same state that was returned from [`Element::request_layout()`].
87 fn prepaint(
88 &mut self,
89 id: Option<&GlobalElementId>,
90 inspector_id: Option<&InspectorElementId>,
91 bounds: Bounds<Pixels>,
92 request_layout: &mut Self::RequestLayoutState,
93 window: &mut Window,
94 cx: &mut App,
95 ) -> Self::PrepaintState;
96
97 /// Once layout has been completed, this method will be called to paint the element to the screen.
98 /// The state argument is the same state that was returned from [`Element::request_layout()`].
99 fn paint(
100 &mut self,
101 id: Option<&GlobalElementId>,
102 inspector_id: Option<&InspectorElementId>,
103 bounds: Bounds<Pixels>,
104 request_layout: &mut Self::RequestLayoutState,
105 prepaint: &mut Self::PrepaintState,
106 window: &mut Window,
107 cx: &mut App,
108 );
109
110 /// Convert this element into a dynamically-typed [`AnyElement`].
111 fn into_any(self) -> AnyElement {
112 AnyElement::new(self)
113 }
114
115 /// todo! document and maybe hide accesskit types from public API
116 fn a11y_role(&self) -> Option<Role> {
117 None
118 }
119
120 /// todo! document and maybe hide accesskit types from public API
121 fn write_a11y_info(&self, _node: &mut Node) {}
122}
123
124/// Implemented by any type that can be converted into an element.
125pub trait IntoElement: Sized {
126 /// The specific type of element into which the implementing type is converted.
127 /// Useful for converting other types into elements automatically, like Strings
128 type Element: Element;
129
130 /// Convert self into a type that implements [`Element`].
131 fn into_element(self) -> Self::Element;
132
133 /// Convert self into a dynamically-typed [`AnyElement`].
134 fn into_any_element(self) -> AnyElement {
135 self.into_element().into_any()
136 }
137}
138
139impl<T: IntoElement> FluentBuilder for T {}
140
141/// An object that can be drawn to the screen. This is the trait that distinguishes "views" from
142/// other entities. Views are `Entity`'s which `impl Render` and drawn to the screen.
143pub trait Render: 'static + Sized {
144 /// Render this view into an element tree.
145 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement;
146}
147
148impl Render for Empty {
149 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
150 Empty
151 }
152}
153
154/// You can derive [`IntoElement`] on any type that implements this trait.
155/// It is used to construct reusable `components` out of plain data. Think of
156/// components as a recipe for a certain pattern of elements. RenderOnce allows
157/// you to invoke this pattern, without breaking the fluent builder pattern of
158/// the element APIs.
159pub trait RenderOnce: 'static {
160 /// Render this component into an element tree. Note that this method
161 /// takes ownership of self, as compared to [`Render::render()`] method
162 /// which takes a mutable reference.
163 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement;
164}
165
166/// This is a helper trait to provide a uniform interface for constructing elements that
167/// can accept any number of any kind of child elements
168pub trait ParentElement {
169 /// Extend this element's children with the given child elements.
170 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>);
171
172 /// Add a single child element to this element.
173 fn child(mut self, child: impl IntoElement) -> Self
174 where
175 Self: Sized,
176 {
177 self.extend(std::iter::once(child.into_element().into_any()));
178 self
179 }
180
181 /// Add multiple child elements to this element.
182 fn children(mut self, children: impl IntoIterator<Item = impl IntoElement>) -> Self
183 where
184 Self: Sized,
185 {
186 self.extend(children.into_iter().map(|child| child.into_any_element()));
187 self
188 }
189}
190
191/// An element for rendering components. An implementation detail of the [`IntoElement`] derive macro
192/// for [`RenderOnce`]
193#[doc(hidden)]
194pub struct Component<C: RenderOnce> {
195 component: Option<C>,
196 #[cfg(debug_assertions)]
197 source: &'static core::panic::Location<'static>,
198}
199
200impl<C: RenderOnce> Component<C> {
201 /// Create a new component from the given RenderOnce type.
202 #[track_caller]
203 pub fn new(component: C) -> Self {
204 Component {
205 component: Some(component),
206 #[cfg(debug_assertions)]
207 source: core::panic::Location::caller(),
208 }
209 }
210}
211
212fn prepaint_component(
213 (element, name): &mut (AnyElement, &'static str),
214 window: &mut Window,
215 cx: &mut App,
216) {
217 window.with_id(ElementId::Name(SharedString::new_static(name)), |window| {
218 element.prepaint(window, cx);
219 })
220}
221
222fn paint_component(
223 (element, name): &mut (AnyElement, &'static str),
224 window: &mut Window,
225 cx: &mut App,
226) {
227 window.with_id(ElementId::Name(SharedString::new_static(name)), |window| {
228 element.paint(window, cx);
229 })
230}
231impl<C: RenderOnce> Element for Component<C> {
232 type RequestLayoutState = (AnyElement, &'static str);
233 type PrepaintState = ();
234
235 fn id(&self) -> Option<ElementId> {
236 None
237 }
238
239 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
240 #[cfg(debug_assertions)]
241 return Some(self.source);
242
243 #[cfg(not(debug_assertions))]
244 return None;
245 }
246
247 fn request_layout(
248 &mut self,
249 _id: Option<&GlobalElementId>,
250 _inspector_id: Option<&InspectorElementId>,
251 window: &mut Window,
252 cx: &mut App,
253 ) -> (LayoutId, Self::RequestLayoutState) {
254 window.with_id(ElementId::Name(type_name::<C>().into()), |window| {
255 let mut element = self
256 .component
257 .take()
258 .unwrap()
259 .render(window, cx)
260 .into_any_element();
261
262 let layout_id = element.request_layout(window, cx);
263 (layout_id, (element, type_name::<C>()))
264 })
265 }
266
267 fn prepaint(
268 &mut self,
269 _id: Option<&GlobalElementId>,
270 _inspector_id: Option<&InspectorElementId>,
271 _: Bounds<Pixels>,
272 state: &mut Self::RequestLayoutState,
273 window: &mut Window,
274 cx: &mut App,
275 ) {
276 prepaint_component(state, window, cx);
277 }
278
279 fn paint(
280 &mut self,
281 _id: Option<&GlobalElementId>,
282 _inspector_id: Option<&InspectorElementId>,
283 _: Bounds<Pixels>,
284 state: &mut Self::RequestLayoutState,
285 _: &mut Self::PrepaintState,
286 window: &mut Window,
287 cx: &mut App,
288 ) {
289 paint_component(state, window, cx);
290 }
291}
292
293impl<C: RenderOnce> IntoElement for Component<C> {
294 type Element = Self;
295
296 fn into_element(self) -> Self::Element {
297 self
298 }
299}
300
301/// A globally unique identifier for an element, used to track state across frames.
302#[derive(Deref, DerefMut, Clone, Default, Debug, Eq, PartialEq, Hash)]
303pub struct GlobalElementId(pub(crate) Arc<[ElementId]>);
304
305impl GlobalElementId {
306 /// Accesskit needs node IDs to be stable across frames.
307 pub(crate) fn accesskit_node_id(&self) -> accesskit::NodeId {
308 use std::hash::Hasher;
309 let mut hasher = std::hash::DefaultHasher::default();
310 self.hash(&mut hasher);
311 hasher.finish().into()
312 }
313}
314
315impl Display for GlobalElementId {
316 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
317 for (i, element_id) in self.0.iter().enumerate() {
318 if i > 0 {
319 write!(f, ".")?;
320 }
321 write!(f, "{}", element_id)?;
322 }
323 Ok(())
324 }
325}
326
327trait ElementObject {
328 fn inner_element(&mut self) -> &mut dyn Any;
329
330 fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId;
331
332 fn prepaint(&mut self, window: &mut Window, cx: &mut App);
333
334 fn paint(&mut self, window: &mut Window, cx: &mut App);
335
336 fn layout_as_root(
337 &mut self,
338 available_space: Size<AvailableSpace>,
339 window: &mut Window,
340 cx: &mut App,
341 ) -> Size<Pixels>;
342}
343
344/// A wrapper around an implementer of [`Element`] that allows it to be drawn in a window.
345pub struct Drawable<E: Element> {
346 /// The drawn element.
347 pub element: E,
348 phase: ElementDrawPhase<E::RequestLayoutState, E::PrepaintState>,
349}
350
351#[derive(Default)]
352enum ElementDrawPhase<RequestLayoutState, PrepaintState> {
353 #[default]
354 Start,
355 RequestLayout {
356 layout_id: LayoutId,
357 global_id: Option<GlobalElementId>,
358 inspector_id: Option<InspectorElementId>,
359 request_layout: RequestLayoutState,
360 },
361 LayoutComputed {
362 layout_id: LayoutId,
363 global_id: Option<GlobalElementId>,
364 inspector_id: Option<InspectorElementId>,
365 available_space: Size<AvailableSpace>,
366 request_layout: RequestLayoutState,
367 },
368 Prepaint {
369 node_id: DispatchNodeId,
370 global_id: Option<GlobalElementId>,
371 inspector_id: Option<InspectorElementId>,
372 bounds: Bounds<Pixels>,
373 request_layout: RequestLayoutState,
374 prepaint: PrepaintState,
375 },
376 Painted,
377}
378
379/// A wrapper around an implementer of [`Element`] that allows it to be drawn in a window.
380impl<E: Element> Drawable<E> {
381 pub(crate) fn new(element: E) -> Self {
382 Drawable {
383 element,
384 phase: ElementDrawPhase::Start,
385 }
386 }
387
388 fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
389 match mem::take(&mut self.phase) {
390 ElementDrawPhase::Start => {
391 let global_id = self.element.id().map(|element_id| {
392 window.element_id_stack.push(element_id);
393 GlobalElementId(Arc::from(&*window.element_id_stack))
394 });
395
396 let inspector_id;
397 #[cfg(any(feature = "inspector", debug_assertions))]
398 {
399 inspector_id = self.element.source_location().map(|source| {
400 let path = crate::InspectorElementPath {
401 global_id: GlobalElementId(Arc::from(&*window.element_id_stack)),
402 source_location: source,
403 };
404 window.build_inspector_element_id(path)
405 });
406 }
407 #[cfg(not(any(feature = "inspector", debug_assertions)))]
408 {
409 inspector_id = None;
410 }
411
412 let (layout_id, request_layout) = self.element.request_layout(
413 global_id.as_ref(),
414 inspector_id.as_ref(),
415 window,
416 cx,
417 );
418
419 if global_id.is_some() {
420 window.element_id_stack.pop();
421 }
422
423 self.phase = ElementDrawPhase::RequestLayout {
424 layout_id,
425 global_id,
426 inspector_id,
427 request_layout,
428 };
429 layout_id
430 }
431 _ => panic!("must call request_layout only once"),
432 }
433 }
434
435 fn push_a11y_node(
436 &self,
437 global_id: &GlobalElementId,
438 bounds: Bounds<Pixels>,
439 window: &mut Window,
440 ) -> bool {
441 if !window.is_a11y_active() {
442 return false;
443 }
444 if let Some(role) = self.element.a11y_role() {
445 let global_id = global_id.accesskit_node_id();
446
447 let mut node = accesskit::Node::new(role);
448
449 node.set_bounds(accesskit::Rect::new(
450 f64::from(bounds.origin.x),
451 f64::from(bounds.origin.y),
452 f64::from(bounds.origin.x + bounds.size.width),
453 f64::from(bounds.origin.y + bounds.size.height),
454 ));
455
456 if let Some((_parent_id, parent_node)) = window.a11y_nodes.peek_mut() {
457 parent_node.push_child(global_id);
458 }
459
460 self.element.write_a11y_info(&mut node);
461
462 window.a11y_nodes.push(global_id, node);
463 true
464 } else {
465 false
466 }
467 }
468
469 pub(crate) fn prepaint(&mut self, window: &mut Window, cx: &mut App) {
470 match mem::take(&mut self.phase) {
471 ElementDrawPhase::RequestLayout {
472 layout_id,
473 global_id,
474 inspector_id,
475 mut request_layout,
476 }
477 | ElementDrawPhase::LayoutComputed {
478 layout_id,
479 global_id,
480 inspector_id,
481 mut request_layout,
482 ..
483 } => {
484 if let Some(element_id) = self.element.id() {
485 window.element_id_stack.push(element_id);
486 debug_assert_eq!(&*global_id.as_ref().unwrap().0, &*window.element_id_stack);
487 }
488
489 let bounds = window.layout_bounds(layout_id);
490
491 let a11y_node_pushed = if let Some(global_id) = &global_id {
492 self.push_a11y_node(global_id, bounds, window)
493 } else {
494 false
495 };
496 let node_id = window.next_frame.dispatch_tree.push_node();
497 let prepaint = self.element.prepaint(
498 global_id.as_ref(),
499 inspector_id.as_ref(),
500 bounds,
501 &mut request_layout,
502 window,
503 cx,
504 );
505 window.next_frame.dispatch_tree.pop_node();
506
507 if global_id.is_some() {
508 window.element_id_stack.pop();
509 }
510 if a11y_node_pushed {
511 window.a11y_nodes.pop();
512 }
513
514 self.phase = ElementDrawPhase::Prepaint {
515 node_id,
516 global_id,
517 inspector_id,
518 bounds,
519 request_layout,
520 prepaint,
521 };
522 }
523 _ => panic!("must call request_layout before prepaint"),
524 }
525 }
526
527 pub(crate) fn paint(
528 &mut self,
529 window: &mut Window,
530 cx: &mut App,
531 ) -> (E::RequestLayoutState, E::PrepaintState) {
532 match mem::take(&mut self.phase) {
533 ElementDrawPhase::Prepaint {
534 node_id,
535 global_id,
536 inspector_id,
537 bounds,
538 mut request_layout,
539 mut prepaint,
540 ..
541 } => {
542 if let Some(element_id) = self.element.id() {
543 window.element_id_stack.push(element_id);
544 debug_assert_eq!(&*global_id.as_ref().unwrap().0, &*window.element_id_stack);
545 }
546
547 window.next_frame.dispatch_tree.set_active_node(node_id);
548 self.element.paint(
549 global_id.as_ref(),
550 inspector_id.as_ref(),
551 bounds,
552 &mut request_layout,
553 &mut prepaint,
554 window,
555 cx,
556 );
557
558 if global_id.is_some() {
559 window.element_id_stack.pop();
560 }
561
562 self.phase = ElementDrawPhase::Painted;
563 (request_layout, prepaint)
564 }
565 _ => panic!("must call prepaint before paint"),
566 }
567 }
568
569 pub(crate) fn layout_as_root(
570 &mut self,
571 available_space: Size<AvailableSpace>,
572 window: &mut Window,
573 cx: &mut App,
574 ) -> Size<Pixels> {
575 if matches!(&self.phase, ElementDrawPhase::Start) {
576 self.request_layout(window, cx);
577 }
578
579 let layout_id = match mem::take(&mut self.phase) {
580 ElementDrawPhase::RequestLayout {
581 layout_id,
582 global_id,
583 inspector_id,
584 request_layout,
585 } => {
586 window.compute_layout(layout_id, available_space, cx);
587 self.phase = ElementDrawPhase::LayoutComputed {
588 layout_id,
589 global_id,
590 inspector_id,
591 available_space,
592 request_layout,
593 };
594 layout_id
595 }
596 ElementDrawPhase::LayoutComputed {
597 layout_id,
598 global_id,
599 inspector_id,
600 available_space: prev_available_space,
601 request_layout,
602 } => {
603 if available_space != prev_available_space {
604 window.compute_layout(layout_id, available_space, cx);
605 }
606 self.phase = ElementDrawPhase::LayoutComputed {
607 layout_id,
608 global_id,
609 inspector_id,
610 available_space,
611 request_layout,
612 };
613 layout_id
614 }
615 _ => panic!("cannot measure after painting"),
616 };
617
618 window.layout_bounds(layout_id).size
619 }
620}
621
622impl<E> ElementObject for Drawable<E>
623where
624 E: Element,
625 E::RequestLayoutState: 'static,
626{
627 fn inner_element(&mut self) -> &mut dyn Any {
628 &mut self.element
629 }
630
631 #[inline]
632 fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
633 Drawable::request_layout(self, window, cx)
634 }
635
636 #[inline]
637 fn prepaint(&mut self, window: &mut Window, cx: &mut App) {
638 Drawable::prepaint(self, window, cx);
639 }
640
641 #[inline]
642 fn paint(&mut self, window: &mut Window, cx: &mut App) {
643 Drawable::paint(self, window, cx);
644 }
645
646 #[inline]
647 fn layout_as_root(
648 &mut self,
649 available_space: Size<AvailableSpace>,
650 window: &mut Window,
651 cx: &mut App,
652 ) -> Size<Pixels> {
653 Drawable::layout_as_root(self, available_space, window, cx)
654 }
655}
656
657/// A dynamically typed element that can be used to store any element type.
658pub struct AnyElement(ArenaBox<dyn ElementObject>);
659
660impl AnyElement {
661 pub(crate) fn new<E>(element: E) -> Self
662 where
663 E: 'static + Element,
664 E::RequestLayoutState: Any,
665 {
666 let element = with_element_arena(|arena| arena.alloc(|| Drawable::new(element)))
667 .map(|element| element as &mut dyn ElementObject);
668 AnyElement(element)
669 }
670
671 /// Attempt to downcast a reference to the boxed element to a specific type.
672 pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
673 self.0.inner_element().downcast_mut::<T>()
674 }
675
676 /// Request the layout ID of the element stored in this `AnyElement`.
677 /// Used for laying out child elements in a parent element.
678 pub fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
679 self.0.request_layout(window, cx)
680 }
681
682 /// Prepares the element to be painted by storing its bounds, giving it a chance to draw hitboxes and
683 /// request autoscroll before the final paint pass is confirmed.
684 pub fn prepaint(&mut self, window: &mut Window, cx: &mut App) -> Option<FocusHandle> {
685 let focus_assigned = window.next_frame.focus.is_some();
686
687 self.0.prepaint(window, cx);
688
689 if !focus_assigned && let Some(focus_id) = window.next_frame.focus {
690 return FocusHandle::for_id(focus_id, &cx.focus_handles);
691 }
692
693 None
694 }
695
696 /// Paints the element stored in this `AnyElement`.
697 pub fn paint(&mut self, window: &mut Window, cx: &mut App) {
698 self.0.paint(window, cx);
699 }
700
701 /// Performs layout for this element within the given available space and returns its size.
702 pub fn layout_as_root(
703 &mut self,
704 available_space: Size<AvailableSpace>,
705 window: &mut Window,
706 cx: &mut App,
707 ) -> Size<Pixels> {
708 self.0.layout_as_root(available_space, window, cx)
709 }
710
711 /// Prepaints this element at the given absolute origin.
712 /// If any element in the subtree beneath this element is focused, its FocusHandle is returned.
713 pub fn prepaint_at(
714 &mut self,
715 origin: Point<Pixels>,
716 window: &mut Window,
717 cx: &mut App,
718 ) -> Option<FocusHandle> {
719 window.with_absolute_element_offset(origin, |window| self.prepaint(window, cx))
720 }
721
722 /// Performs layout on this element in the available space, then prepaints it at the given absolute origin.
723 /// If any element in the subtree beneath this element is focused, its FocusHandle is returned.
724 pub fn prepaint_as_root(
725 &mut self,
726 origin: Point<Pixels>,
727 available_space: Size<AvailableSpace>,
728 window: &mut Window,
729 cx: &mut App,
730 ) -> Option<FocusHandle> {
731 self.layout_as_root(available_space, window, cx);
732 window.with_absolute_element_offset(origin, |window| self.prepaint(window, cx))
733 }
734}
735
736impl Element for AnyElement {
737 type RequestLayoutState = ();
738 type PrepaintState = ();
739
740 fn id(&self) -> Option<ElementId> {
741 None
742 }
743
744 fn source_location(&self) -> Option<&'static panic::Location<'static>> {
745 None
746 }
747
748 fn request_layout(
749 &mut self,
750 _: Option<&GlobalElementId>,
751 _inspector_id: Option<&InspectorElementId>,
752 window: &mut Window,
753 cx: &mut App,
754 ) -> (LayoutId, Self::RequestLayoutState) {
755 let layout_id = self.request_layout(window, cx);
756 (layout_id, ())
757 }
758
759 fn prepaint(
760 &mut self,
761 _: Option<&GlobalElementId>,
762 _inspector_id: Option<&InspectorElementId>,
763 _: Bounds<Pixels>,
764 _: &mut Self::RequestLayoutState,
765 window: &mut Window,
766 cx: &mut App,
767 ) {
768 self.prepaint(window, cx);
769 }
770
771 fn paint(
772 &mut self,
773 _: Option<&GlobalElementId>,
774 _inspector_id: Option<&InspectorElementId>,
775 _: Bounds<Pixels>,
776 _: &mut Self::RequestLayoutState,
777 _: &mut Self::PrepaintState,
778 window: &mut Window,
779 cx: &mut App,
780 ) {
781 self.paint(window, cx);
782 }
783}
784
785impl IntoElement for AnyElement {
786 type Element = Self;
787
788 fn into_element(self) -> Self::Element {
789 self
790 }
791
792 fn into_any_element(self) -> AnyElement {
793 self
794 }
795}
796
797/// The empty element, which renders nothing.
798pub struct Empty;
799
800impl IntoElement for Empty {
801 type Element = Self;
802
803 fn into_element(self) -> Self::Element {
804 self
805 }
806}
807
808impl Element for Empty {
809 type RequestLayoutState = ();
810 type PrepaintState = ();
811
812 fn id(&self) -> Option<ElementId> {
813 None
814 }
815
816 fn source_location(&self) -> Option<&'static panic::Location<'static>> {
817 None
818 }
819
820 fn request_layout(
821 &mut self,
822 _id: Option<&GlobalElementId>,
823 _inspector_id: Option<&InspectorElementId>,
824 window: &mut Window,
825 cx: &mut App,
826 ) -> (LayoutId, Self::RequestLayoutState) {
827 (
828 window.request_layout(
829 Style {
830 display: crate::Display::None,
831 ..Default::default()
832 },
833 None,
834 cx,
835 ),
836 (),
837 )
838 }
839
840 fn prepaint(
841 &mut self,
842 _id: Option<&GlobalElementId>,
843 _inspector_id: Option<&InspectorElementId>,
844 _bounds: Bounds<Pixels>,
845 _state: &mut Self::RequestLayoutState,
846 _window: &mut Window,
847 _cx: &mut App,
848 ) {
849 }
850
851 fn paint(
852 &mut self,
853 _id: Option<&GlobalElementId>,
854 _inspector_id: Option<&InspectorElementId>,
855 _bounds: Bounds<Pixels>,
856 _request_layout: &mut Self::RequestLayoutState,
857 _prepaint: &mut Self::PrepaintState,
858 _window: &mut Window,
859 _cx: &mut App,
860 ) {
861 }
862}