element.rs

  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::State`], 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 [`WindowContext::break_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, ELEMENT_ARENA, ElementId,
 36    FocusHandle, InspectorElementId, LayoutId, Pixels, Point, Size, Style, Window,
 37    taffy::LOG_TAFFY, util::FluentBuilder,
 38};
 39use derive_more::{Deref, DerefMut};
 40pub(crate) use smallvec::SmallVec;
 41use std::{
 42    any::{Any, type_name},
 43    fmt::{self, Debug, Display},
 44    mem, panic,
 45};
 46
 47/// Implemented by types that participate in laying out and painting the contents of a window.
 48/// Elements form a tree and are laid out according to web-based layout rules, as implemented by Taffy.
 49/// You can create custom elements by implementing this trait, see the module-level documentation
 50/// for more details.
 51pub trait Element: 'static + IntoElement {
 52    /// The type of state returned from [`Element::request_layout`]. A mutable reference to this state is subsequently
 53    /// provided to [`Element::prepaint`] and [`Element::paint`].
 54    type RequestLayoutState: 'static;
 55
 56    /// The type of state returned from [`Element::prepaint`]. A mutable reference to this state is subsequently
 57    /// provided to [`Element::paint`].
 58    type PrepaintState: 'static;
 59
 60    /// If this element has a unique identifier, return it here. This is used to track elements across frames, and
 61    /// will cause a GlobalElementId to be passed to the request_layout, prepaint, and paint methods.
 62    ///
 63    /// The global id can in turn be used to access state that's connected to an element with the same id across
 64    /// frames. This id must be unique among children of the first containing element with an id.
 65    fn id(&self) -> Option<ElementId>;
 66
 67    /// Source location where this element was constructed, used to disambiguate elements in the
 68    /// inspector and navigate to their source code.
 69    fn source_location(&self) -> Option<&'static panic::Location<'static>>;
 70
 71    /// Before an element can be painted, we need to know where it's going to be and how big it is.
 72    /// Use this method to request a layout from Taffy and initialize the element's state.
 73    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    /// After laying out an element, we need to commit its bounds to the current frame for hitbox
 82    /// purposes. The state argument is the same state that was returned from [`Element::request_layout()`].
 83    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    /// Once layout has been completed, this method will be called to paint the element to the screen.
 94    /// The state argument is the same state that was returned from [`Element::request_layout()`].
 95    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    /// Convert this element into a dynamically-typed [`AnyElement`].
107    fn into_any(self) -> AnyElement {
108        AnyElement::new(self)
109    }
110}
111
112/// Implemented by any type that can be converted into an element.
113pub trait IntoElement: Sized {
114    /// The specific type of element into which the implementing type is converted.
115    /// Useful for converting other types into elements automatically, like Strings
116    type Element: Element;
117
118    /// Convert self into a type that implements [`Element`].
119    fn into_element(self) -> Self::Element;
120
121    /// Convert self into a dynamically-typed [`AnyElement`].
122    fn into_any_element(self) -> AnyElement {
123        self.into_element().into_any()
124    }
125}
126
127impl<T: IntoElement> FluentBuilder for T {}
128
129/// An object that can be drawn to the screen. This is the trait that distinguishes "views" from
130/// other entities. Views are `Entity`'s which `impl Render` and drawn to the screen.
131pub trait Render: 'static + Sized {
132    /// Render this view into an element tree.
133    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
142/// You can derive [`IntoElement`] on any type that implements this trait.
143/// It is used to construct reusable `components` out of plain data. Think of
144/// components as a recipe for a certain pattern of elements. RenderOnce allows
145/// you to invoke this pattern, without breaking the fluent builder pattern of
146/// the element APIs.
147pub trait RenderOnce: 'static {
148    /// Render this component into an element tree. Note that this method
149    /// takes ownership of self, as compared to [`Render::render()`] method
150    /// which takes a mutable reference.
151    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement;
152}
153
154/// This is a helper trait to provide a uniform interface for constructing elements that
155/// can accept any number of any kind of child elements
156pub trait ParentElement {
157    /// Extend this element's children with the given child elements.
158    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>);
159
160    /// Add a single child element to this element.
161    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    /// Add multiple child elements to this element.
170    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/// An element for rendering components. An implementation detail of the [`IntoElement`] derive macro
180/// for [`RenderOnce`]
181#[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    /// Create a new component from the given RenderOnce type.
190    #[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
200impl<C: RenderOnce> Element for Component<C> {
201    type RequestLayoutState = AnyElement;
202    type PrepaintState = ();
203
204    fn id(&self) -> Option<ElementId> {
205        None
206    }
207
208    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
209        #[cfg(debug_assertions)]
210        return Some(self.source);
211
212        #[cfg(not(debug_assertions))]
213        return None;
214    }
215
216    fn request_layout(
217        &mut self,
218        _id: Option<&GlobalElementId>,
219        _inspector_id: Option<&InspectorElementId>,
220        window: &mut Window,
221        cx: &mut App,
222    ) -> (LayoutId, Self::RequestLayoutState) {
223        window.with_global_id(ElementId::Name(type_name::<C>().into()), |_, window| {
224            let mut element = self
225                .component
226                .take()
227                .unwrap()
228                .render(window, cx)
229                .into_any_element();
230
231            let layout_id = element.request_layout(window, cx);
232            (layout_id, element)
233        })
234    }
235
236    fn prepaint(
237        &mut self,
238        _id: Option<&GlobalElementId>,
239        _inspector_id: Option<&InspectorElementId>,
240        _: Bounds<Pixels>,
241        element: &mut AnyElement,
242        window: &mut Window,
243        cx: &mut App,
244    ) {
245        window.with_global_id(ElementId::Name(type_name::<C>().into()), |_, window| {
246            element.prepaint(window, cx);
247        })
248    }
249
250    fn paint(
251        &mut self,
252        _id: Option<&GlobalElementId>,
253        _inspector_id: Option<&InspectorElementId>,
254        _: Bounds<Pixels>,
255        element: &mut Self::RequestLayoutState,
256        _: &mut Self::PrepaintState,
257        window: &mut Window,
258        cx: &mut App,
259    ) {
260        window.with_global_id(ElementId::Name(type_name::<C>().into()), |_, window| {
261            element.paint(window, cx);
262        })
263    }
264}
265
266impl<C: RenderOnce> IntoElement for Component<C> {
267    type Element = Self;
268
269    fn into_element(self) -> Self::Element {
270        self
271    }
272}
273
274/// A globally unique identifier for an element, used to track state across frames.
275#[derive(Deref, DerefMut, Default, Debug, Eq, PartialEq, Hash)]
276pub struct GlobalElementId(pub(crate) SmallVec<[ElementId; 32]>);
277
278impl Display for GlobalElementId {
279    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
280        for (i, element_id) in self.0.iter().enumerate() {
281            if i > 0 {
282                write!(f, ".")?;
283            }
284            write!(f, "{}", element_id)?;
285        }
286        Ok(())
287    }
288}
289
290trait ElementObject {
291    fn inner_element(&mut self) -> &mut dyn Any;
292
293    fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId;
294
295    fn prepaint(&mut self, window: &mut Window, cx: &mut App);
296
297    fn paint(&mut self, window: &mut Window, cx: &mut App);
298
299    fn layout_as_root(
300        &mut self,
301        available_space: Size<AvailableSpace>,
302        window: &mut Window,
303        cx: &mut App,
304    ) -> Size<Pixels>;
305}
306
307/// A wrapper around an implementer of [`Element`] that allows it to be drawn in a window.
308pub struct Drawable<E: Element> {
309    /// The drawn element.
310    pub element: E,
311    phase: ElementDrawPhase<E::RequestLayoutState, E::PrepaintState>,
312}
313
314#[derive(Default)]
315enum ElementDrawPhase<RequestLayoutState, PrepaintState> {
316    #[default]
317    Start,
318    RequestLayout {
319        layout_id: LayoutId,
320        global_id: Option<GlobalElementId>,
321        inspector_id: Option<InspectorElementId>,
322        request_layout: RequestLayoutState,
323    },
324    LayoutComputed {
325        layout_id: LayoutId,
326        global_id: Option<GlobalElementId>,
327        inspector_id: Option<InspectorElementId>,
328        available_space: Size<AvailableSpace>,
329        request_layout: RequestLayoutState,
330    },
331    Prepaint {
332        node_id: DispatchNodeId,
333        global_id: Option<GlobalElementId>,
334        inspector_id: Option<InspectorElementId>,
335        bounds: Bounds<Pixels>,
336        request_layout: RequestLayoutState,
337        prepaint: PrepaintState,
338    },
339    Painted,
340}
341
342/// A wrapper around an implementer of [`Element`] that allows it to be drawn in a window.
343impl<E: Element> Drawable<E> {
344    pub(crate) fn new(element: E) -> Self {
345        Drawable {
346            element,
347            phase: ElementDrawPhase::Start,
348        }
349    }
350
351    fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
352        match mem::take(&mut self.phase) {
353            ElementDrawPhase::Start => {
354                let global_id = self.element.id().map(|element_id| {
355                    window.element_id_stack.push(element_id);
356                    GlobalElementId(window.element_id_stack.clone())
357                });
358
359                let inspector_id;
360                #[cfg(any(feature = "inspector", debug_assertions))]
361                {
362                    inspector_id = self.element.source_location().map(|source| {
363                        let path = crate::InspectorElementPath {
364                            global_id: GlobalElementId(window.element_id_stack.clone()),
365                            source_location: source,
366                        };
367                        window.build_inspector_element_id(path)
368                    });
369                }
370                #[cfg(not(any(feature = "inspector", debug_assertions)))]
371                {
372                    inspector_id = None;
373                }
374
375                let should_start_logging = global_id
376                    .as_ref()
377                    .map(|id| {
378                        id.0.last()
379                            .map(|last| last == &"open-router-container".into())
380                            .unwrap_or(false)
381                    })
382                    .unwrap_or(false);
383
384                if should_start_logging {
385                    println!("Starting taffy logging for element");
386                    LOG_TAFFY.with_borrow_mut(|log_taffy| {
387                        *log_taffy = true;
388                    });
389                }
390
391                let (layout_id, request_layout) = self.element.request_layout(
392                    global_id.as_ref(),
393                    inspector_id.as_ref(),
394                    window,
395                    cx,
396                );
397
398                // Only turn off logging if this element started it
399                if should_start_logging {
400                    println!("Stopping taffy logging for element");
401                    LOG_TAFFY.with_borrow_mut(|log_taffy| {
402                        *log_taffy = false;
403                    });
404                }
405
406                if global_id.is_some() {
407                    window.element_id_stack.pop();
408                }
409
410                self.phase = ElementDrawPhase::RequestLayout {
411                    layout_id,
412                    global_id,
413                    inspector_id,
414                    request_layout,
415                };
416                layout_id
417            }
418            _ => panic!("must call request_layout only once"),
419        }
420    }
421
422    pub(crate) fn prepaint(&mut self, window: &mut Window, cx: &mut App) {
423        match mem::take(&mut self.phase) {
424            ElementDrawPhase::RequestLayout {
425                layout_id,
426                global_id,
427                inspector_id,
428                mut request_layout,
429            }
430            | ElementDrawPhase::LayoutComputed {
431                layout_id,
432                global_id,
433                inspector_id,
434                mut request_layout,
435                ..
436            } => {
437                if let Some(element_id) = self.element.id() {
438                    window.element_id_stack.push(element_id);
439                    debug_assert_eq!(global_id.as_ref().unwrap().0, window.element_id_stack);
440                }
441
442                let bounds = window.layout_bounds(layout_id);
443                let node_id = window.next_frame.dispatch_tree.push_node();
444                let prepaint = self.element.prepaint(
445                    global_id.as_ref(),
446                    inspector_id.as_ref(),
447                    bounds,
448                    &mut request_layout,
449                    window,
450                    cx,
451                );
452                window.next_frame.dispatch_tree.pop_node();
453
454                if global_id.is_some() {
455                    window.element_id_stack.pop();
456                }
457
458                self.phase = ElementDrawPhase::Prepaint {
459                    node_id,
460                    global_id,
461                    inspector_id,
462                    bounds,
463                    request_layout,
464                    prepaint,
465                };
466            }
467            _ => panic!("must call request_layout before prepaint"),
468        }
469    }
470
471    pub(crate) fn paint(
472        &mut self,
473        window: &mut Window,
474        cx: &mut App,
475    ) -> (E::RequestLayoutState, E::PrepaintState) {
476        match mem::take(&mut self.phase) {
477            ElementDrawPhase::Prepaint {
478                node_id,
479                global_id,
480                inspector_id,
481                bounds,
482                mut request_layout,
483                mut prepaint,
484                ..
485            } => {
486                if let Some(element_id) = self.element.id() {
487                    window.element_id_stack.push(element_id);
488                    debug_assert_eq!(global_id.as_ref().unwrap().0, window.element_id_stack);
489                }
490
491                window.next_frame.dispatch_tree.set_active_node(node_id);
492                self.element.paint(
493                    global_id.as_ref(),
494                    inspector_id.as_ref(),
495                    bounds,
496                    &mut request_layout,
497                    &mut prepaint,
498                    window,
499                    cx,
500                );
501
502                if global_id.is_some() {
503                    window.element_id_stack.pop();
504                }
505
506                self.phase = ElementDrawPhase::Painted;
507                (request_layout, prepaint)
508            }
509            _ => panic!("must call prepaint before paint"),
510        }
511    }
512
513    pub(crate) fn layout_as_root(
514        &mut self,
515        available_space: Size<AvailableSpace>,
516        window: &mut Window,
517        cx: &mut App,
518    ) -> Size<Pixels> {
519        if matches!(&self.phase, ElementDrawPhase::Start) {
520            self.request_layout(window, cx);
521        }
522
523        let layout_id = match mem::take(&mut self.phase) {
524            ElementDrawPhase::RequestLayout {
525                layout_id,
526                global_id,
527                inspector_id,
528                request_layout,
529            } => {
530                window.compute_layout(layout_id, available_space, cx);
531                self.phase = ElementDrawPhase::LayoutComputed {
532                    layout_id,
533                    global_id,
534                    inspector_id,
535                    available_space,
536                    request_layout,
537                };
538                layout_id
539            }
540            ElementDrawPhase::LayoutComputed {
541                layout_id,
542                global_id,
543                inspector_id,
544                available_space: prev_available_space,
545                request_layout,
546            } => {
547                if available_space != prev_available_space {
548                    window.compute_layout(layout_id, available_space, cx);
549                }
550                self.phase = ElementDrawPhase::LayoutComputed {
551                    layout_id,
552                    global_id,
553                    inspector_id,
554                    available_space,
555                    request_layout,
556                };
557                layout_id
558            }
559            _ => panic!("cannot measure after painting"),
560        };
561
562        window.layout_bounds(layout_id).size
563    }
564}
565
566impl<E> ElementObject for Drawable<E>
567where
568    E: Element,
569    E::RequestLayoutState: 'static,
570{
571    fn inner_element(&mut self) -> &mut dyn Any {
572        &mut self.element
573    }
574
575    fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
576        Drawable::request_layout(self, window, cx)
577    }
578
579    fn prepaint(&mut self, window: &mut Window, cx: &mut App) {
580        Drawable::prepaint(self, window, cx);
581    }
582
583    fn paint(&mut self, window: &mut Window, cx: &mut App) {
584        Drawable::paint(self, window, cx);
585    }
586
587    fn layout_as_root(
588        &mut self,
589        available_space: Size<AvailableSpace>,
590        window: &mut Window,
591        cx: &mut App,
592    ) -> Size<Pixels> {
593        Drawable::layout_as_root(self, available_space, window, cx)
594    }
595}
596
597/// A dynamically typed element that can be used to store any element type.
598pub struct AnyElement(ArenaBox<dyn ElementObject>);
599
600impl AnyElement {
601    pub(crate) fn new<E>(element: E) -> Self
602    where
603        E: 'static + Element,
604        E::RequestLayoutState: Any,
605    {
606        let element = ELEMENT_ARENA
607            .with_borrow_mut(|arena| arena.alloc(|| Drawable::new(element)))
608            .map(|element| element as &mut dyn ElementObject);
609        AnyElement(element)
610    }
611
612    /// Attempt to downcast a reference to the boxed element to a specific type.
613    pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
614        self.0.inner_element().downcast_mut::<T>()
615    }
616
617    /// Request the layout ID of the element stored in this `AnyElement`.
618    /// Used for laying out child elements in a parent element.
619    pub fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
620        self.0.request_layout(window, cx)
621    }
622
623    /// Prepares the element to be painted by storing its bounds, giving it a chance to draw hitboxes and
624    /// request autoscroll before the final paint pass is confirmed.
625    pub fn prepaint(&mut self, window: &mut Window, cx: &mut App) -> Option<FocusHandle> {
626        let focus_assigned = window.next_frame.focus.is_some();
627
628        self.0.prepaint(window, cx);
629
630        if !focus_assigned {
631            if let Some(focus_id) = window.next_frame.focus {
632                return FocusHandle::for_id(focus_id, &cx.focus_handles);
633            }
634        }
635
636        None
637    }
638
639    /// Paints the element stored in this `AnyElement`.
640    pub fn paint(&mut self, window: &mut Window, cx: &mut App) {
641        self.0.paint(window, cx);
642    }
643
644    /// Performs layout for this element within the given available space and returns its size.
645    pub fn layout_as_root(
646        &mut self,
647        available_space: Size<AvailableSpace>,
648        window: &mut Window,
649        cx: &mut App,
650    ) -> Size<Pixels> {
651        self.0.layout_as_root(available_space, window, cx)
652    }
653
654    /// Prepaints this element at the given absolute origin.
655    /// If any element in the subtree beneath this element is focused, its FocusHandle is returned.
656    pub fn prepaint_at(
657        &mut self,
658        origin: Point<Pixels>,
659        window: &mut Window,
660        cx: &mut App,
661    ) -> Option<FocusHandle> {
662        window.with_absolute_element_offset(origin, |window| self.prepaint(window, cx))
663    }
664
665    /// Performs layout on this element in the available space, then prepaints it at the given absolute origin.
666    /// If any element in the subtree beneath this element is focused, its FocusHandle is returned.
667    pub fn prepaint_as_root(
668        &mut self,
669        origin: Point<Pixels>,
670        available_space: Size<AvailableSpace>,
671        window: &mut Window,
672        cx: &mut App,
673    ) -> Option<FocusHandle> {
674        self.layout_as_root(available_space, window, cx);
675        window.with_absolute_element_offset(origin, |window| self.prepaint(window, cx))
676    }
677}
678
679impl Element for AnyElement {
680    type RequestLayoutState = ();
681    type PrepaintState = ();
682
683    fn id(&self) -> Option<ElementId> {
684        None
685    }
686
687    fn source_location(&self) -> Option<&'static panic::Location<'static>> {
688        None
689    }
690
691    fn request_layout(
692        &mut self,
693        _: Option<&GlobalElementId>,
694        _inspector_id: Option<&InspectorElementId>,
695        window: &mut Window,
696        cx: &mut App,
697    ) -> (LayoutId, Self::RequestLayoutState) {
698        let layout_id = self.request_layout(window, cx);
699        (layout_id, ())
700    }
701
702    fn prepaint(
703        &mut self,
704        _: Option<&GlobalElementId>,
705        _inspector_id: Option<&InspectorElementId>,
706        _: Bounds<Pixels>,
707        _: &mut Self::RequestLayoutState,
708        window: &mut Window,
709        cx: &mut App,
710    ) {
711        self.prepaint(window, cx);
712    }
713
714    fn paint(
715        &mut self,
716        _: Option<&GlobalElementId>,
717        _inspector_id: Option<&InspectorElementId>,
718        _: Bounds<Pixels>,
719        _: &mut Self::RequestLayoutState,
720        _: &mut Self::PrepaintState,
721        window: &mut Window,
722        cx: &mut App,
723    ) {
724        self.paint(window, cx);
725    }
726}
727
728impl IntoElement for AnyElement {
729    type Element = Self;
730
731    fn into_element(self) -> Self::Element {
732        self
733    }
734
735    fn into_any_element(self) -> AnyElement {
736        self
737    }
738}
739
740/// The empty element, which renders nothing.
741pub struct Empty;
742
743impl IntoElement for Empty {
744    type Element = Self;
745
746    fn into_element(self) -> Self::Element {
747        self
748    }
749}
750
751impl Element for Empty {
752    type RequestLayoutState = ();
753    type PrepaintState = ();
754
755    fn id(&self) -> Option<ElementId> {
756        None
757    }
758
759    fn source_location(&self) -> Option<&'static panic::Location<'static>> {
760        None
761    }
762
763    fn request_layout(
764        &mut self,
765        _id: Option<&GlobalElementId>,
766        _inspector_id: Option<&InspectorElementId>,
767        window: &mut Window,
768        cx: &mut App,
769    ) -> (LayoutId, Self::RequestLayoutState) {
770        (window.request_layout(Style::default(), None, cx), ())
771    }
772
773    fn prepaint(
774        &mut self,
775        _id: Option<&GlobalElementId>,
776        _inspector_id: Option<&InspectorElementId>,
777        _bounds: Bounds<Pixels>,
778        _state: &mut Self::RequestLayoutState,
779        _window: &mut Window,
780        _cx: &mut App,
781    ) {
782    }
783
784    fn paint(
785        &mut self,
786        _id: Option<&GlobalElementId>,
787        _inspector_id: Option<&InspectorElementId>,
788        _bounds: Bounds<Pixels>,
789        _request_layout: &mut Self::RequestLayoutState,
790        _prepaint: &mut Self::PrepaintState,
791        _window: &mut Window,
792        _cx: &mut App,
793    ) {
794    }
795}