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::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 derive_more::{Deref, DerefMut};
 40use std::{
 41    any::{Any, type_name},
 42    fmt::{self, Debug, Display},
 43    mem, panic,
 44    sync::Arc,
 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
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/// A globally unique identifier for an element, used to track state across frames.
290#[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
322/// A wrapper around an implementer of [`Element`] that allows it to be drawn in a window.
323pub struct Drawable<E: Element> {
324    /// The drawn element.
325    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
357/// A wrapper around an implementer of [`Element`] that allows it to be drawn in a window.
358impl<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(any(feature = "inspector", 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(any(feature = "inspector", 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
592/// A dynamically typed element that can be used to store any element type.
593pub 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    /// Attempt to downcast a reference to the boxed element to a specific type.
607    pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
608        self.0.inner_element().downcast_mut::<T>()
609    }
610
611    /// Request the layout ID of the element stored in this `AnyElement`.
612    /// Used for laying out child elements in a parent element.
613    pub fn request_layout(&mut self, window: &mut Window, cx: &mut App) -> LayoutId {
614        self.0.request_layout(window, cx)
615    }
616
617    /// Prepares the element to be painted by storing its bounds, giving it a chance to draw hitboxes and
618    /// request autoscroll before the final paint pass is confirmed.
619    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    /// Paints the element stored in this `AnyElement`.
632    pub fn paint(&mut self, window: &mut Window, cx: &mut App) {
633        self.0.paint(window, cx);
634    }
635
636    /// Performs layout for this element within the given available space and returns its size.
637    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    /// Prepaints this element at the given absolute origin.
647    /// If any element in the subtree beneath this element is focused, its FocusHandle is returned.
648    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    /// Performs layout on this element in the available space, then prepaints it at the given absolute origin.
658    /// If any element in the subtree beneath this element is focused, its FocusHandle is returned.
659    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
732/// The empty element, which renders nothing.
733pub 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}