div.rs

   1use crate::{
   2    point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext,
   3    BorrowWindow, Bounds, ClickEvent, Component, DispatchPhase, Element, ElementId, FocusEvent,
   4    FocusHandle, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent,
   5    MouseMoveEvent, MouseUpEvent, ParentComponent, Pixels, Point, Render, ScrollWheelEvent,
   6    SharedString, Size, Style, StyleRefinement, Styled, Task, View, ViewContext, Visibility,
   7};
   8use collections::HashMap;
   9use refineable::Refineable;
  10use smallvec::SmallVec;
  11use std::{
  12    any::{Any, TypeId},
  13    cell::RefCell,
  14    fmt::Debug,
  15    marker::PhantomData,
  16    mem,
  17    rc::Rc,
  18    time::Duration,
  19};
  20use taffy::style::Overflow;
  21use util::ResultExt;
  22
  23const DRAG_THRESHOLD: f64 = 2.;
  24const TOOLTIP_DELAY: Duration = Duration::from_millis(500);
  25
  26pub struct GroupStyle {
  27    pub group: SharedString,
  28    pub style: StyleRefinement,
  29}
  30
  31pub trait InteractiveComponent<V: 'static>: Sized + Element<V> {
  32    fn interactivity(&mut self) -> &mut Interactivity<V>;
  33
  34    fn group(mut self, group: impl Into<SharedString>) -> Self {
  35        self.interactivity().group = Some(group.into());
  36        self
  37    }
  38
  39    fn id(mut self, id: impl Into<ElementId>) -> Stateful<V, Self> {
  40        self.interactivity().element_id = Some(id.into());
  41
  42        Stateful {
  43            element: self,
  44            view_type: PhantomData,
  45        }
  46    }
  47
  48    fn track_focus(mut self, focus_handle: &FocusHandle) -> Focusable<V, Self> {
  49        self.interactivity().focusable = true;
  50        self.interactivity().tracked_focus_handle = Some(focus_handle.clone());
  51        Focusable {
  52            element: self,
  53            view_type: PhantomData,
  54        }
  55    }
  56
  57    fn key_context<C, E>(mut self, key_context: C) -> Self
  58    where
  59        C: TryInto<KeyContext, Error = E>,
  60        E: Debug,
  61    {
  62        if let Some(key_context) = key_context.try_into().log_err() {
  63            self.interactivity().key_context = key_context;
  64        }
  65        self
  66    }
  67
  68    fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
  69        self.interactivity().hover_style = f(StyleRefinement::default());
  70        self
  71    }
  72
  73    fn group_hover(
  74        mut self,
  75        group_name: impl Into<SharedString>,
  76        f: impl FnOnce(StyleRefinement) -> StyleRefinement,
  77    ) -> Self {
  78        self.interactivity().group_hover_style = Some(GroupStyle {
  79            group: group_name.into(),
  80            style: f(StyleRefinement::default()),
  81        });
  82        self
  83    }
  84
  85    fn on_mouse_down(
  86        mut self,
  87        button: MouseButton,
  88        handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
  89    ) -> Self {
  90        self.interactivity().mouse_down_listeners.push(Box::new(
  91            move |view, event, bounds, phase, cx| {
  92                if phase == DispatchPhase::Bubble
  93                    && event.button == button
  94                    && bounds.contains_point(&event.position)
  95                {
  96                    handler(view, event, cx)
  97                }
  98            },
  99        ));
 100        self
 101    }
 102
 103    fn on_any_mouse_down(
 104        mut self,
 105        handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
 106    ) -> Self {
 107        self.interactivity().mouse_down_listeners.push(Box::new(
 108            move |view, event, bounds, phase, cx| {
 109                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 110                    handler(view, event, cx)
 111                }
 112            },
 113        ));
 114        self
 115    }
 116
 117    fn on_mouse_up(
 118        mut self,
 119        button: MouseButton,
 120        handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
 121    ) -> Self {
 122        self.interactivity().mouse_up_listeners.push(Box::new(
 123            move |view, event, bounds, phase, cx| {
 124                if phase == DispatchPhase::Bubble
 125                    && event.button == button
 126                    && bounds.contains_point(&event.position)
 127                {
 128                    handler(view, event, cx)
 129                }
 130            },
 131        ));
 132        self
 133    }
 134
 135    fn on_any_mouse_up(
 136        mut self,
 137        handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
 138    ) -> Self {
 139        self.interactivity().mouse_up_listeners.push(Box::new(
 140            move |view, event, bounds, phase, cx| {
 141                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 142                    handler(view, event, cx)
 143                }
 144            },
 145        ));
 146        self
 147    }
 148
 149    fn on_mouse_down_out(
 150        mut self,
 151        handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
 152    ) -> Self {
 153        self.interactivity().mouse_down_listeners.push(Box::new(
 154            move |view, event, bounds, phase, cx| {
 155                if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) {
 156                    handler(view, event, cx)
 157                }
 158            },
 159        ));
 160        self
 161    }
 162
 163    fn on_mouse_up_out(
 164        mut self,
 165        button: MouseButton,
 166        handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + 'static,
 167    ) -> Self {
 168        self.interactivity().mouse_up_listeners.push(Box::new(
 169            move |view, event, bounds, phase, cx| {
 170                if phase == DispatchPhase::Capture
 171                    && event.button == button
 172                    && !bounds.contains_point(&event.position)
 173                {
 174                    handler(view, event, cx);
 175                }
 176            },
 177        ));
 178        self
 179    }
 180
 181    fn on_mouse_move(
 182        mut self,
 183        handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext<V>) + 'static,
 184    ) -> Self {
 185        self.interactivity().mouse_move_listeners.push(Box::new(
 186            move |view, event, bounds, phase, cx| {
 187                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 188                    handler(view, event, cx);
 189                }
 190            },
 191        ));
 192        self
 193    }
 194
 195    fn on_scroll_wheel(
 196        mut self,
 197        handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext<V>) + 'static,
 198    ) -> Self {
 199        self.interactivity().scroll_wheel_listeners.push(Box::new(
 200            move |view, event, bounds, phase, cx| {
 201                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 202                    handler(view, event, cx);
 203                }
 204            },
 205        ));
 206        self
 207    }
 208
 209    /// Capture the given action, fires during the capture phase
 210    fn capture_action<A: Action>(
 211        mut self,
 212        listener: impl Fn(&mut V, &A, &mut ViewContext<V>) + 'static,
 213    ) -> Self {
 214        self.interactivity().action_listeners.push((
 215            TypeId::of::<A>(),
 216            Box::new(move |view, action, phase, cx| {
 217                let action = action.downcast_ref().unwrap();
 218                if phase == DispatchPhase::Capture {
 219                    listener(view, action, cx)
 220                }
 221            }),
 222        ));
 223        self
 224    }
 225
 226    /// Add a listener for the given action, fires during the bubble event phase
 227    fn on_action<A: Action>(
 228        mut self,
 229        listener: impl Fn(&mut V, &A, &mut ViewContext<V>) + 'static,
 230    ) -> Self {
 231        // NOTE: this debug assert has the side-effect of working around
 232        // a bug where a crate consisting only of action definitions does
 233        // not register the actions in debug builds:
 234        //
 235        // https://github.com/rust-lang/rust/issues/47384
 236        // https://github.com/mmastrac/rust-ctor/issues/280
 237        //
 238        // if we are relying on this side-effect still, removing the debug_assert!
 239        // likely breaks the command_palette tests.
 240        debug_assert!(
 241            A::is_registered(),
 242            "{:?} is not registered as an action",
 243            A::qualified_name()
 244        );
 245        self.interactivity().action_listeners.push((
 246            TypeId::of::<A>(),
 247            Box::new(move |view, action, phase, cx| {
 248                let action = action.downcast_ref().unwrap();
 249                if phase == DispatchPhase::Bubble {
 250                    listener(view, action, cx)
 251                }
 252            }),
 253        ));
 254        self
 255    }
 256
 257    fn on_key_down(
 258        mut self,
 259        listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + 'static,
 260    ) -> Self {
 261        self.interactivity()
 262            .key_down_listeners
 263            .push(Box::new(move |view, event, phase, cx| {
 264                listener(view, event, phase, cx)
 265            }));
 266        self
 267    }
 268
 269    fn on_key_up(
 270        mut self,
 271        listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>) + 'static,
 272    ) -> Self {
 273        self.interactivity()
 274            .key_up_listeners
 275            .push(Box::new(move |view, event, phase, cx| {
 276                listener(view, event, phase, cx)
 277            }));
 278        self
 279    }
 280
 281    fn drag_over<S: 'static>(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
 282        self.interactivity()
 283            .drag_over_styles
 284            .push((TypeId::of::<S>(), f(StyleRefinement::default())));
 285        self
 286    }
 287
 288    fn group_drag_over<S: 'static>(
 289        mut self,
 290        group_name: impl Into<SharedString>,
 291        f: impl FnOnce(StyleRefinement) -> StyleRefinement,
 292    ) -> Self {
 293        self.interactivity().group_drag_over_styles.push((
 294            TypeId::of::<S>(),
 295            GroupStyle {
 296                group: group_name.into(),
 297                style: f(StyleRefinement::default()),
 298            },
 299        ));
 300        self
 301    }
 302
 303    fn on_drop<W: 'static>(
 304        mut self,
 305        listener: impl Fn(&mut V, View<W>, &mut ViewContext<V>) + 'static,
 306    ) -> Self {
 307        self.interactivity().drop_listeners.push((
 308            TypeId::of::<W>(),
 309            Box::new(move |view, dragged_view, cx| {
 310                listener(view, dragged_view.downcast().unwrap(), cx);
 311            }),
 312        ));
 313        self
 314    }
 315}
 316
 317pub trait StatefulInteractiveComponent<V: 'static, E: Element<V>>: InteractiveComponent<V> {
 318    fn focusable(mut self) -> Focusable<V, Self> {
 319        self.interactivity().focusable = true;
 320        Focusable {
 321            element: self,
 322            view_type: PhantomData,
 323        }
 324    }
 325
 326    fn overflow_scroll(mut self) -> Self {
 327        self.interactivity().base_style.overflow.x = Some(Overflow::Scroll);
 328        self.interactivity().base_style.overflow.y = Some(Overflow::Scroll);
 329        self
 330    }
 331
 332    fn overflow_x_scroll(mut self) -> Self {
 333        self.interactivity().base_style.overflow.x = Some(Overflow::Scroll);
 334        self
 335    }
 336
 337    fn overflow_y_scroll(mut self) -> Self {
 338        self.interactivity().base_style.overflow.y = Some(Overflow::Scroll);
 339        self
 340    }
 341
 342    fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
 343    where
 344        Self: Sized,
 345    {
 346        self.interactivity().active_style = f(StyleRefinement::default());
 347        self
 348    }
 349
 350    fn group_active(
 351        mut self,
 352        group_name: impl Into<SharedString>,
 353        f: impl FnOnce(StyleRefinement) -> StyleRefinement,
 354    ) -> Self
 355    where
 356        Self: Sized,
 357    {
 358        self.interactivity().group_active_style = Some(GroupStyle {
 359            group: group_name.into(),
 360            style: f(StyleRefinement::default()),
 361        });
 362        self
 363    }
 364
 365    fn on_click(
 366        mut self,
 367        listener: impl Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + 'static,
 368    ) -> Self
 369    where
 370        Self: Sized,
 371    {
 372        self.interactivity()
 373            .click_listeners
 374            .push(Box::new(move |view, event, cx| listener(view, event, cx)));
 375        self
 376    }
 377
 378    fn on_drag<W>(
 379        mut self,
 380        listener: impl Fn(&mut V, &mut ViewContext<V>) -> View<W> + 'static,
 381    ) -> Self
 382    where
 383        Self: Sized,
 384        W: 'static + Render,
 385    {
 386        debug_assert!(
 387            self.interactivity().drag_listener.is_none(),
 388            "calling on_drag more than once on the same element is not supported"
 389        );
 390        self.interactivity().drag_listener =
 391            Some(Box::new(move |view_state, cursor_offset, cx| AnyDrag {
 392                view: listener(view_state, cx).into(),
 393                cursor_offset,
 394            }));
 395        self
 396    }
 397
 398    fn on_hover(mut self, listener: impl 'static + Fn(&mut V, bool, &mut ViewContext<V>)) -> Self
 399    where
 400        Self: Sized,
 401    {
 402        debug_assert!(
 403            self.interactivity().hover_listener.is_none(),
 404            "calling on_hover more than once on the same element is not supported"
 405        );
 406        self.interactivity().hover_listener = Some(Box::new(listener));
 407        self
 408    }
 409
 410    fn tooltip(
 411        mut self,
 412        build_tooltip: impl Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static,
 413    ) -> Self
 414    where
 415        Self: Sized,
 416    {
 417        debug_assert!(
 418            self.interactivity().tooltip_builder.is_none(),
 419            "calling tooltip more than once on the same element is not supported"
 420        );
 421        self.interactivity().tooltip_builder =
 422            Some(Rc::new(move |view_state, cx| build_tooltip(view_state, cx)));
 423
 424        self
 425    }
 426}
 427
 428pub trait FocusableComponent<V: 'static>: InteractiveComponent<V> {
 429    fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
 430    where
 431        Self: Sized,
 432    {
 433        self.interactivity().focus_style = f(StyleRefinement::default());
 434        self
 435    }
 436
 437    fn in_focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
 438    where
 439        Self: Sized,
 440    {
 441        self.interactivity().in_focus_style = f(StyleRefinement::default());
 442        self
 443    }
 444
 445    fn on_focus(
 446        mut self,
 447        listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
 448    ) -> Self
 449    where
 450        Self: Sized,
 451    {
 452        self.interactivity().focus_listeners.push(Box::new(
 453            move |view, focus_handle, event, cx| {
 454                if event.focused.as_ref() == Some(focus_handle) {
 455                    listener(view, event, cx)
 456                }
 457            },
 458        ));
 459        self
 460    }
 461
 462    fn on_blur(
 463        mut self,
 464        listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
 465    ) -> Self
 466    where
 467        Self: Sized,
 468    {
 469        self.interactivity().focus_listeners.push(Box::new(
 470            move |view, focus_handle, event, cx| {
 471                if event.blurred.as_ref() == Some(focus_handle) {
 472                    listener(view, event, cx)
 473                }
 474            },
 475        ));
 476        self
 477    }
 478
 479    fn on_focus_in(
 480        mut self,
 481        listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
 482    ) -> Self
 483    where
 484        Self: Sized,
 485    {
 486        self.interactivity().focus_listeners.push(Box::new(
 487            move |view, focus_handle, event, cx| {
 488                let descendant_blurred = event
 489                    .blurred
 490                    .as_ref()
 491                    .map_or(false, |blurred| focus_handle.contains(blurred, cx));
 492                let descendant_focused = event
 493                    .focused
 494                    .as_ref()
 495                    .map_or(false, |focused| focus_handle.contains(focused, cx));
 496
 497                if !descendant_blurred && descendant_focused {
 498                    listener(view, event, cx)
 499                }
 500            },
 501        ));
 502        self
 503    }
 504
 505    fn on_focus_out(
 506        mut self,
 507        listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
 508    ) -> Self
 509    where
 510        Self: Sized,
 511    {
 512        self.interactivity().focus_listeners.push(Box::new(
 513            move |view, focus_handle, event, cx| {
 514                let descendant_blurred = event
 515                    .blurred
 516                    .as_ref()
 517                    .map_or(false, |blurred| focus_handle.contains(blurred, cx));
 518                let descendant_focused = event
 519                    .focused
 520                    .as_ref()
 521                    .map_or(false, |focused| focus_handle.contains(focused, cx));
 522                if descendant_blurred && !descendant_focused {
 523                    listener(view, event, cx)
 524                }
 525            },
 526        ));
 527        self
 528    }
 529}
 530
 531pub type FocusListeners<V> = SmallVec<[FocusListener<V>; 2]>;
 532
 533pub type FocusListener<V> =
 534    Box<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + 'static>;
 535
 536pub type MouseDownListener<V> = Box<
 537    dyn Fn(&mut V, &MouseDownEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>) + 'static,
 538>;
 539pub type MouseUpListener<V> = Box<
 540    dyn Fn(&mut V, &MouseUpEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>) + 'static,
 541>;
 542
 543pub type MouseMoveListener<V> = Box<
 544    dyn Fn(&mut V, &MouseMoveEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>) + 'static,
 545>;
 546
 547pub type ScrollWheelListener<V> = Box<
 548    dyn Fn(&mut V, &ScrollWheelEvent, &Bounds<Pixels>, DispatchPhase, &mut ViewContext<V>)
 549        + 'static,
 550>;
 551
 552pub type ClickListener<V> = Box<dyn Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + 'static>;
 553
 554pub type DragListener<V> =
 555    Box<dyn Fn(&mut V, Point<Pixels>, &mut ViewContext<V>) -> AnyDrag + 'static>;
 556
 557type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static;
 558
 559pub type HoverListener<V> = Box<dyn Fn(&mut V, bool, &mut ViewContext<V>) + 'static>;
 560
 561pub type TooltipBuilder<V> = Rc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static>;
 562
 563pub type KeyDownListener<V> =
 564    Box<dyn Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + 'static>;
 565
 566pub type KeyUpListener<V> =
 567    Box<dyn Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>) + 'static>;
 568
 569pub type ActionListener<V> =
 570    Box<dyn Fn(&mut V, &dyn Any, DispatchPhase, &mut ViewContext<V>) + 'static>;
 571
 572pub fn div<V: 'static>() -> Div<V> {
 573    Div {
 574        interactivity: Interactivity::default(),
 575        children: SmallVec::default(),
 576    }
 577}
 578
 579pub struct Div<V> {
 580    interactivity: Interactivity<V>,
 581    children: SmallVec<[AnyElement<V>; 2]>,
 582}
 583
 584impl<V> Styled for Div<V> {
 585    fn style(&mut self) -> &mut StyleRefinement {
 586        &mut self.interactivity.base_style
 587    }
 588}
 589
 590impl<V: 'static> InteractiveComponent<V> for Div<V> {
 591    fn interactivity(&mut self) -> &mut Interactivity<V> {
 592        &mut self.interactivity
 593    }
 594}
 595
 596impl<V: 'static> ParentComponent<V> for Div<V> {
 597    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
 598        &mut self.children
 599    }
 600}
 601
 602impl<V: 'static> Element<V> for Div<V> {
 603    type ElementState = DivState;
 604
 605    fn element_id(&self) -> Option<ElementId> {
 606        self.interactivity.element_id.clone()
 607    }
 608
 609    fn layout(
 610        &mut self,
 611        view_state: &mut V,
 612        element_state: Option<Self::ElementState>,
 613        cx: &mut ViewContext<V>,
 614    ) -> (LayoutId, Self::ElementState) {
 615        let mut child_layout_ids = SmallVec::new();
 616        let mut interactivity = mem::take(&mut self.interactivity);
 617        let (layout_id, interactive_state) = interactivity.layout(
 618            element_state.map(|s| s.interactive_state),
 619            cx,
 620            |style, cx| {
 621                cx.with_text_style(style.text_style().cloned(), |cx| {
 622                    child_layout_ids = self
 623                        .children
 624                        .iter_mut()
 625                        .map(|child| child.layout(view_state, cx))
 626                        .collect::<SmallVec<_>>();
 627                    cx.request_layout(&style, child_layout_ids.iter().copied())
 628                })
 629            },
 630        );
 631        self.interactivity = interactivity;
 632        (
 633            layout_id,
 634            DivState {
 635                interactive_state,
 636                child_layout_ids,
 637            },
 638        )
 639    }
 640
 641    fn paint(
 642        &mut self,
 643        bounds: Bounds<Pixels>,
 644        view_state: &mut V,
 645        element_state: &mut Self::ElementState,
 646        cx: &mut ViewContext<V>,
 647    ) {
 648        let mut child_min = point(Pixels::MAX, Pixels::MAX);
 649        let mut child_max = Point::default();
 650        let content_size = if element_state.child_layout_ids.is_empty() {
 651            bounds.size
 652        } else {
 653            for child_layout_id in &element_state.child_layout_ids {
 654                let child_bounds = cx.layout_bounds(*child_layout_id);
 655                child_min = child_min.min(&child_bounds.origin);
 656                child_max = child_max.max(&child_bounds.lower_right());
 657            }
 658            (child_max - child_min).into()
 659        };
 660
 661        let mut interactivity = mem::take(&mut self.interactivity);
 662        interactivity.paint(
 663            bounds,
 664            content_size,
 665            &mut element_state.interactive_state,
 666            cx,
 667            |style, scroll_offset, cx| {
 668                if style.visibility == Visibility::Hidden {
 669                    return;
 670                }
 671
 672                let z_index = style.z_index.unwrap_or(0);
 673
 674                cx.with_z_index(z_index, |cx| {
 675                    cx.with_z_index(0, |cx| {
 676                        style.paint(bounds, cx);
 677                    });
 678                    cx.with_z_index(1, |cx| {
 679                        cx.with_text_style(style.text_style().cloned(), |cx| {
 680                            cx.with_content_mask(style.overflow_mask(bounds), |cx| {
 681                                cx.with_element_offset(scroll_offset, |cx| {
 682                                    for child in &mut self.children {
 683                                        child.paint(view_state, cx);
 684                                    }
 685                                })
 686                            })
 687                        })
 688                    })
 689                })
 690            },
 691        );
 692        self.interactivity = interactivity;
 693    }
 694}
 695
 696impl<V: 'static> Component<V> for Div<V> {
 697    fn render(self) -> AnyElement<V> {
 698        AnyElement::new(self)
 699    }
 700}
 701
 702pub struct DivState {
 703    child_layout_ids: SmallVec<[LayoutId; 4]>,
 704    interactive_state: InteractiveElementState,
 705}
 706
 707impl DivState {
 708    pub fn is_active(&self) -> bool {
 709        self.interactive_state.pending_mouse_down.borrow().is_some()
 710    }
 711}
 712
 713pub struct Interactivity<V> {
 714    pub element_id: Option<ElementId>,
 715    pub key_context: KeyContext,
 716    pub focusable: bool,
 717    pub tracked_focus_handle: Option<FocusHandle>,
 718    pub focus_listeners: FocusListeners<V>,
 719    pub group: Option<SharedString>,
 720    pub base_style: StyleRefinement,
 721    pub focus_style: StyleRefinement,
 722    pub in_focus_style: StyleRefinement,
 723    pub hover_style: StyleRefinement,
 724    pub group_hover_style: Option<GroupStyle>,
 725    pub active_style: StyleRefinement,
 726    pub group_active_style: Option<GroupStyle>,
 727    pub drag_over_styles: SmallVec<[(TypeId, StyleRefinement); 2]>,
 728    pub group_drag_over_styles: SmallVec<[(TypeId, GroupStyle); 2]>,
 729    pub mouse_down_listeners: SmallVec<[MouseDownListener<V>; 2]>,
 730    pub mouse_up_listeners: SmallVec<[MouseUpListener<V>; 2]>,
 731    pub mouse_move_listeners: SmallVec<[MouseMoveListener<V>; 2]>,
 732    pub scroll_wheel_listeners: SmallVec<[ScrollWheelListener<V>; 2]>,
 733    pub key_down_listeners: SmallVec<[KeyDownListener<V>; 2]>,
 734    pub key_up_listeners: SmallVec<[KeyUpListener<V>; 2]>,
 735    pub action_listeners: SmallVec<[(TypeId, ActionListener<V>); 8]>,
 736    pub drop_listeners: SmallVec<[(TypeId, Box<DropListener<V>>); 2]>,
 737    pub click_listeners: SmallVec<[ClickListener<V>; 2]>,
 738    pub drag_listener: Option<DragListener<V>>,
 739    pub hover_listener: Option<HoverListener<V>>,
 740    pub tooltip_builder: Option<TooltipBuilder<V>>,
 741}
 742
 743impl<V> Interactivity<V>
 744where
 745    V: 'static,
 746{
 747    pub fn layout(
 748        &mut self,
 749        element_state: Option<InteractiveElementState>,
 750        cx: &mut ViewContext<V>,
 751        f: impl FnOnce(Style, &mut ViewContext<V>) -> LayoutId,
 752    ) -> (LayoutId, InteractiveElementState) {
 753        let mut element_state = element_state.unwrap_or_default();
 754
 755        // Ensure we store a focus handle in our element state if we're focusable.
 756        // If there's an explicit focus handle we're tracking, use that. Otherwise
 757        // create a new handle and store it in the element state, which lives for as
 758        // as frames contain an element with this id.
 759        if self.focusable {
 760            element_state.focus_handle.get_or_insert_with(|| {
 761                self.tracked_focus_handle
 762                    .clone()
 763                    .unwrap_or_else(|| cx.focus_handle())
 764            });
 765        }
 766
 767        let style = self.compute_style(None, &mut element_state, cx);
 768        let layout_id = f(style, cx);
 769        (layout_id, element_state)
 770    }
 771
 772    pub fn paint(
 773        &mut self,
 774        bounds: Bounds<Pixels>,
 775        content_size: Size<Pixels>,
 776        element_state: &mut InteractiveElementState,
 777        cx: &mut ViewContext<V>,
 778        f: impl FnOnce(Style, Point<Pixels>, &mut ViewContext<V>),
 779    ) {
 780        let style = self.compute_style(Some(bounds), element_state, cx);
 781
 782        if let Some(mouse_cursor) = style.mouse_cursor {
 783            let hovered = bounds.contains_point(&cx.mouse_position());
 784            if hovered {
 785                cx.set_cursor_style(mouse_cursor);
 786            }
 787        }
 788
 789        for listener in self.mouse_down_listeners.drain(..) {
 790            cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| {
 791                listener(state, event, &bounds, phase, cx);
 792            })
 793        }
 794
 795        for listener in self.mouse_up_listeners.drain(..) {
 796            cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| {
 797                listener(state, event, &bounds, phase, cx);
 798            })
 799        }
 800
 801        for listener in self.mouse_move_listeners.drain(..) {
 802            cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| {
 803                listener(state, event, &bounds, phase, cx);
 804            })
 805        }
 806
 807        for listener in self.scroll_wheel_listeners.drain(..) {
 808            cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| {
 809                listener(state, event, &bounds, phase, cx);
 810            })
 811        }
 812
 813        let hover_group_bounds = self
 814            .group_hover_style
 815            .as_ref()
 816            .and_then(|group_hover| GroupBounds::get(&group_hover.group, cx));
 817
 818        if let Some(group_bounds) = hover_group_bounds {
 819            let hovered = group_bounds.contains_point(&cx.mouse_position());
 820            cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
 821                if phase == DispatchPhase::Capture {
 822                    if group_bounds.contains_point(&event.position) != hovered {
 823                        cx.notify();
 824                    }
 825                }
 826            });
 827        }
 828
 829        if self.hover_style.is_some()
 830            || (cx.active_drag.is_some() && !self.drag_over_styles.is_empty())
 831        {
 832            let hovered = bounds.contains_point(&cx.mouse_position());
 833            cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
 834                if phase == DispatchPhase::Capture {
 835                    if bounds.contains_point(&event.position) != hovered {
 836                        cx.notify();
 837                    }
 838                }
 839            });
 840        }
 841
 842        if cx.active_drag.is_some() {
 843            let drop_listeners = mem::take(&mut self.drop_listeners);
 844            cx.on_mouse_event(move |view, event: &MouseUpEvent, phase, cx| {
 845                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 846                    if let Some(drag_state_type) =
 847                        cx.active_drag.as_ref().map(|drag| drag.view.entity_type())
 848                    {
 849                        for (drop_state_type, listener) in &drop_listeners {
 850                            if *drop_state_type == drag_state_type {
 851                                let drag = cx
 852                                    .active_drag
 853                                    .take()
 854                                    .expect("checked for type drag state type above");
 855                                listener(view, drag.view.clone(), cx);
 856                                cx.notify();
 857                                cx.stop_propagation();
 858                            }
 859                        }
 860                    }
 861                }
 862            });
 863        }
 864
 865        let click_listeners = mem::take(&mut self.click_listeners);
 866        let drag_listener = mem::take(&mut self.drag_listener);
 867
 868        if !click_listeners.is_empty() || drag_listener.is_some() {
 869            let pending_mouse_down = element_state.pending_mouse_down.clone();
 870            let mouse_down = pending_mouse_down.borrow().clone();
 871            if let Some(mouse_down) = mouse_down {
 872                if let Some(drag_listener) = drag_listener {
 873                    let active_state = element_state.clicked_state.clone();
 874
 875                    cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
 876                        if cx.active_drag.is_some() {
 877                            if phase == DispatchPhase::Capture {
 878                                cx.notify();
 879                            }
 880                        } else if phase == DispatchPhase::Bubble
 881                            && bounds.contains_point(&event.position)
 882                            && (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
 883                        {
 884                            *active_state.borrow_mut() = ElementClickedState::default();
 885                            let cursor_offset = event.position - bounds.origin;
 886                            let drag = drag_listener(view_state, cursor_offset, cx);
 887                            cx.active_drag = Some(drag);
 888                            cx.notify();
 889                            cx.stop_propagation();
 890                        }
 891                    });
 892                }
 893
 894                cx.on_mouse_event(move |view_state, event: &MouseUpEvent, phase, cx| {
 895                    if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 896                        let mouse_click = ClickEvent {
 897                            down: mouse_down.clone(),
 898                            up: event.clone(),
 899                        };
 900                        for listener in &click_listeners {
 901                            listener(view_state, &mouse_click, cx);
 902                        }
 903                    }
 904                    *pending_mouse_down.borrow_mut() = None;
 905                    cx.notify();
 906                });
 907            } else {
 908                cx.on_mouse_event(move |_view_state, event: &MouseDownEvent, phase, cx| {
 909                    if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 910                        *pending_mouse_down.borrow_mut() = Some(event.clone());
 911                        cx.notify();
 912                    }
 913                });
 914            }
 915        }
 916
 917        if let Some(hover_listener) = self.hover_listener.take() {
 918            let was_hovered = element_state.hover_state.clone();
 919            let has_mouse_down = element_state.pending_mouse_down.clone();
 920
 921            cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
 922                if phase != DispatchPhase::Bubble {
 923                    return;
 924                }
 925                let is_hovered =
 926                    bounds.contains_point(&event.position) && has_mouse_down.borrow().is_none();
 927                let mut was_hovered = was_hovered.borrow_mut();
 928
 929                if is_hovered != was_hovered.clone() {
 930                    *was_hovered = is_hovered;
 931                    drop(was_hovered);
 932
 933                    hover_listener(view_state, is_hovered, cx);
 934                }
 935            });
 936        }
 937
 938        if let Some(tooltip_builder) = self.tooltip_builder.take() {
 939            let active_tooltip = element_state.active_tooltip.clone();
 940            let pending_mouse_down = element_state.pending_mouse_down.clone();
 941
 942            cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
 943                if phase != DispatchPhase::Bubble {
 944                    return;
 945                }
 946
 947                let is_hovered =
 948                    bounds.contains_point(&event.position) && pending_mouse_down.borrow().is_none();
 949                if !is_hovered {
 950                    active_tooltip.borrow_mut().take();
 951                    return;
 952                }
 953
 954                if active_tooltip.borrow().is_none() {
 955                    let task = cx.spawn({
 956                        let active_tooltip = active_tooltip.clone();
 957                        let tooltip_builder = tooltip_builder.clone();
 958
 959                        move |view, mut cx| async move {
 960                            cx.background_executor().timer(TOOLTIP_DELAY).await;
 961                            view.update(&mut cx, move |view_state, cx| {
 962                                active_tooltip.borrow_mut().replace(ActiveTooltip {
 963                                    waiting: None,
 964                                    tooltip: Some(AnyTooltip {
 965                                        view: tooltip_builder(view_state, cx),
 966                                        cursor_offset: cx.mouse_position(),
 967                                    }),
 968                                });
 969                                cx.notify();
 970                            })
 971                            .ok();
 972                        }
 973                    });
 974                    active_tooltip.borrow_mut().replace(ActiveTooltip {
 975                        waiting: Some(task),
 976                        tooltip: None,
 977                    });
 978                }
 979            });
 980
 981            if let Some(active_tooltip) = element_state.active_tooltip.borrow().as_ref() {
 982                if active_tooltip.tooltip.is_some() {
 983                    cx.active_tooltip = active_tooltip.tooltip.clone()
 984                }
 985            }
 986        }
 987
 988        let active_state = element_state.clicked_state.clone();
 989        if !active_state.borrow().is_clicked() {
 990            cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
 991                if phase == DispatchPhase::Capture {
 992                    *active_state.borrow_mut() = ElementClickedState::default();
 993                    cx.notify();
 994                }
 995            });
 996        } else {
 997            let active_group_bounds = self
 998                .group_active_style
 999                .as_ref()
1000                .and_then(|group_active| GroupBounds::get(&group_active.group, cx));
1001            cx.on_mouse_event(move |_view, down: &MouseDownEvent, phase, cx| {
1002                if phase == DispatchPhase::Bubble {
1003                    let group = active_group_bounds
1004                        .map_or(false, |bounds| bounds.contains_point(&down.position));
1005                    let element = bounds.contains_point(&down.position);
1006                    if group || element {
1007                        *active_state.borrow_mut() = ElementClickedState { group, element };
1008                        cx.notify();
1009                    }
1010                }
1011            });
1012        }
1013
1014        let overflow = style.overflow;
1015        if overflow.x == Overflow::Scroll || overflow.y == Overflow::Scroll {
1016            let scroll_offset = element_state
1017                .scroll_offset
1018                .get_or_insert_with(Rc::default)
1019                .clone();
1020            let line_height = cx.line_height();
1021            let scroll_max = (content_size - bounds.size).max(&Size::default());
1022
1023            cx.on_mouse_event(move |_, event: &ScrollWheelEvent, phase, cx| {
1024                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
1025                    let mut scroll_offset = scroll_offset.borrow_mut();
1026                    let old_scroll_offset = *scroll_offset;
1027                    let delta = event.delta.pixel_delta(line_height);
1028
1029                    if overflow.x == Overflow::Scroll {
1030                        scroll_offset.x =
1031                            (scroll_offset.x + delta.x).clamp(-scroll_max.width, px(0.));
1032                    }
1033
1034                    if overflow.y == Overflow::Scroll {
1035                        scroll_offset.y =
1036                            (scroll_offset.y + delta.y).clamp(-scroll_max.height, px(0.));
1037                    }
1038
1039                    if *scroll_offset != old_scroll_offset {
1040                        cx.notify();
1041                        cx.stop_propagation();
1042                    }
1043                }
1044            });
1045        }
1046
1047        if let Some(group) = self.group.clone() {
1048            GroupBounds::push(group, bounds, cx);
1049        }
1050
1051        let scroll_offset = element_state
1052            .scroll_offset
1053            .as_ref()
1054            .map(|scroll_offset| *scroll_offset.borrow());
1055
1056        cx.with_key_dispatch(
1057            self.key_context.clone(),
1058            element_state.focus_handle.clone(),
1059            |_, cx| {
1060                for listener in self.key_down_listeners.drain(..) {
1061                    cx.on_key_event(move |state, event: &KeyDownEvent, phase, cx| {
1062                        listener(state, event, phase, cx);
1063                    })
1064                }
1065
1066                for listener in self.key_up_listeners.drain(..) {
1067                    cx.on_key_event(move |state, event: &KeyUpEvent, phase, cx| {
1068                        listener(state, event, phase, cx);
1069                    })
1070                }
1071
1072                for (action_type, listener) in self.action_listeners.drain(..) {
1073                    cx.on_action(action_type, listener)
1074                }
1075
1076                if let Some(focus_handle) = element_state.focus_handle.as_ref() {
1077                    for listener in self.focus_listeners.drain(..) {
1078                        let focus_handle = focus_handle.clone();
1079                        cx.on_focus_changed(move |view, event, cx| {
1080                            listener(view, &focus_handle, event, cx)
1081                        });
1082                    }
1083                }
1084
1085                f(style, scroll_offset.unwrap_or_default(), cx)
1086            },
1087        );
1088
1089        if let Some(group) = self.group.as_ref() {
1090            GroupBounds::pop(group, cx);
1091        }
1092    }
1093
1094    pub fn compute_style(
1095        &self,
1096        bounds: Option<Bounds<Pixels>>,
1097        element_state: &mut InteractiveElementState,
1098        cx: &mut ViewContext<V>,
1099    ) -> Style {
1100        let mut style = Style::default();
1101        style.refine(&self.base_style);
1102
1103        if let Some(focus_handle) = self.tracked_focus_handle.as_ref() {
1104            if focus_handle.within_focused(cx) {
1105                style.refine(&self.in_focus_style);
1106            }
1107
1108            if focus_handle.is_focused(cx) {
1109                style.refine(&self.focus_style);
1110            }
1111        }
1112
1113        if let Some(bounds) = bounds {
1114            let mouse_position = cx.mouse_position();
1115            if let Some(group_hover) = self.group_hover_style.as_ref() {
1116                if let Some(group_bounds) = GroupBounds::get(&group_hover.group, cx) {
1117                    if group_bounds.contains_point(&mouse_position) {
1118                        style.refine(&group_hover.style);
1119                    }
1120                }
1121            }
1122            if bounds.contains_point(&mouse_position) {
1123                style.refine(&self.hover_style);
1124            }
1125
1126            if let Some(drag) = cx.active_drag.take() {
1127                for (state_type, group_drag_style) in &self.group_drag_over_styles {
1128                    if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) {
1129                        if *state_type == drag.view.entity_type()
1130                            && group_bounds.contains_point(&mouse_position)
1131                        {
1132                            style.refine(&group_drag_style.style);
1133                        }
1134                    }
1135                }
1136
1137                for (state_type, drag_over_style) in &self.drag_over_styles {
1138                    if *state_type == drag.view.entity_type()
1139                        && bounds.contains_point(&mouse_position)
1140                    {
1141                        style.refine(drag_over_style);
1142                    }
1143                }
1144
1145                cx.active_drag = Some(drag);
1146            }
1147        }
1148
1149        let clicked_state = element_state.clicked_state.borrow();
1150        if clicked_state.group {
1151            if let Some(group) = self.group_active_style.as_ref() {
1152                style.refine(&group.style)
1153            }
1154        }
1155
1156        if clicked_state.element {
1157            style.refine(&self.active_style)
1158        }
1159
1160        style
1161    }
1162}
1163
1164impl<V: 'static> Default for Interactivity<V> {
1165    fn default() -> Self {
1166        Self {
1167            element_id: None,
1168            key_context: KeyContext::default(),
1169            focusable: false,
1170            tracked_focus_handle: None,
1171            focus_listeners: SmallVec::default(),
1172            // scroll_offset: Point::default(),
1173            group: None,
1174            base_style: StyleRefinement::default(),
1175            focus_style: StyleRefinement::default(),
1176            in_focus_style: StyleRefinement::default(),
1177            hover_style: StyleRefinement::default(),
1178            group_hover_style: None,
1179            active_style: StyleRefinement::default(),
1180            group_active_style: None,
1181            drag_over_styles: SmallVec::new(),
1182            group_drag_over_styles: SmallVec::new(),
1183            mouse_down_listeners: SmallVec::new(),
1184            mouse_up_listeners: SmallVec::new(),
1185            mouse_move_listeners: SmallVec::new(),
1186            scroll_wheel_listeners: SmallVec::new(),
1187            key_down_listeners: SmallVec::new(),
1188            key_up_listeners: SmallVec::new(),
1189            action_listeners: SmallVec::new(),
1190            drop_listeners: SmallVec::new(),
1191            click_listeners: SmallVec::new(),
1192            drag_listener: None,
1193            hover_listener: None,
1194            tooltip_builder: None,
1195        }
1196    }
1197}
1198
1199#[derive(Default)]
1200pub struct InteractiveElementState {
1201    pub focus_handle: Option<FocusHandle>,
1202    pub clicked_state: Rc<RefCell<ElementClickedState>>,
1203    pub hover_state: Rc<RefCell<bool>>,
1204    pub pending_mouse_down: Rc<RefCell<Option<MouseDownEvent>>>,
1205    pub scroll_offset: Option<Rc<RefCell<Point<Pixels>>>>,
1206    pub active_tooltip: Rc<RefCell<Option<ActiveTooltip>>>,
1207}
1208
1209pub struct ActiveTooltip {
1210    #[allow(unused)] // used to drop the task
1211    waiting: Option<Task<()>>,
1212    tooltip: Option<AnyTooltip>,
1213}
1214
1215/// Whether or not the element or a group that contains it is clicked by the mouse.
1216#[derive(Copy, Clone, Default, Eq, PartialEq)]
1217pub struct ElementClickedState {
1218    pub group: bool,
1219    pub element: bool,
1220}
1221
1222impl ElementClickedState {
1223    fn is_clicked(&self) -> bool {
1224        self.group || self.element
1225    }
1226}
1227
1228#[derive(Default)]
1229pub struct GroupBounds(HashMap<SharedString, SmallVec<[Bounds<Pixels>; 1]>>);
1230
1231impl GroupBounds {
1232    pub fn get(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
1233        cx.default_global::<Self>()
1234            .0
1235            .get(name)
1236            .and_then(|bounds_stack| bounds_stack.last())
1237            .cloned()
1238    }
1239
1240    pub fn push(name: SharedString, bounds: Bounds<Pixels>, cx: &mut AppContext) {
1241        cx.default_global::<Self>()
1242            .0
1243            .entry(name)
1244            .or_default()
1245            .push(bounds);
1246    }
1247
1248    pub fn pop(name: &SharedString, cx: &mut AppContext) {
1249        cx.default_global::<Self>().0.get_mut(name).unwrap().pop();
1250    }
1251}
1252
1253pub struct Focusable<V, E> {
1254    element: E,
1255    view_type: PhantomData<V>,
1256}
1257
1258impl<V: 'static, E: InteractiveComponent<V>> FocusableComponent<V> for Focusable<V, E> {}
1259
1260impl<V, E> InteractiveComponent<V> for Focusable<V, E>
1261where
1262    V: 'static,
1263    E: InteractiveComponent<V>,
1264{
1265    fn interactivity(&mut self) -> &mut Interactivity<V> {
1266        self.element.interactivity()
1267    }
1268}
1269
1270impl<V: 'static, E: StatefulInteractiveComponent<V, E>> StatefulInteractiveComponent<V, E>
1271    for Focusable<V, E>
1272{
1273}
1274
1275impl<V, E> Styled for Focusable<V, E>
1276where
1277    V: 'static,
1278    E: Styled,
1279{
1280    fn style(&mut self) -> &mut StyleRefinement {
1281        self.element.style()
1282    }
1283}
1284
1285impl<V, E> Element<V> for Focusable<V, E>
1286where
1287    V: 'static,
1288    E: Element<V>,
1289{
1290    type ElementState = E::ElementState;
1291
1292    fn element_id(&self) -> Option<ElementId> {
1293        self.element.element_id()
1294    }
1295
1296    fn layout(
1297        &mut self,
1298        view_state: &mut V,
1299        element_state: Option<Self::ElementState>,
1300        cx: &mut ViewContext<V>,
1301    ) -> (LayoutId, Self::ElementState) {
1302        self.element.layout(view_state, element_state, cx)
1303    }
1304
1305    fn paint(
1306        &mut self,
1307        bounds: Bounds<Pixels>,
1308        view_state: &mut V,
1309        element_state: &mut Self::ElementState,
1310        cx: &mut ViewContext<V>,
1311    ) {
1312        self.element.paint(bounds, view_state, element_state, cx);
1313    }
1314}
1315
1316impl<V, E> Component<V> for Focusable<V, E>
1317where
1318    V: 'static,
1319    E: 'static + Element<V>,
1320{
1321    fn render(self) -> AnyElement<V> {
1322        AnyElement::new(self)
1323    }
1324}
1325
1326impl<V, E> ParentComponent<V> for Focusable<V, E>
1327where
1328    V: 'static,
1329    E: ParentComponent<V>,
1330{
1331    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
1332        self.element.children_mut()
1333    }
1334}
1335
1336pub struct Stateful<V, E> {
1337    element: E,
1338    view_type: PhantomData<V>,
1339}
1340
1341impl<V, E> Styled for Stateful<V, E>
1342where
1343    V: 'static,
1344    E: Styled,
1345{
1346    fn style(&mut self) -> &mut StyleRefinement {
1347        self.element.style()
1348    }
1349}
1350
1351impl<V, E> StatefulInteractiveComponent<V, E> for Stateful<V, E>
1352where
1353    V: 'static,
1354    E: Element<V>,
1355    Self: InteractiveComponent<V>,
1356{
1357}
1358
1359impl<V, E> InteractiveComponent<V> for Stateful<V, E>
1360where
1361    V: 'static,
1362    E: InteractiveComponent<V>,
1363{
1364    fn interactivity(&mut self) -> &mut Interactivity<V> {
1365        self.element.interactivity()
1366    }
1367}
1368
1369impl<V: 'static, E: FocusableComponent<V>> FocusableComponent<V> for Stateful<V, E> {}
1370
1371impl<V, E> Element<V> for Stateful<V, E>
1372where
1373    V: 'static,
1374    E: Element<V>,
1375{
1376    type ElementState = E::ElementState;
1377
1378    fn element_id(&self) -> Option<ElementId> {
1379        self.element.element_id()
1380    }
1381
1382    fn layout(
1383        &mut self,
1384        view_state: &mut V,
1385        element_state: Option<Self::ElementState>,
1386        cx: &mut ViewContext<V>,
1387    ) -> (LayoutId, Self::ElementState) {
1388        self.element.layout(view_state, element_state, cx)
1389    }
1390
1391    fn paint(
1392        &mut self,
1393        bounds: Bounds<Pixels>,
1394        view_state: &mut V,
1395        element_state: &mut Self::ElementState,
1396        cx: &mut ViewContext<V>,
1397    ) {
1398        self.element.paint(bounds, view_state, element_state, cx)
1399    }
1400}
1401
1402impl<V, E> Component<V> for Stateful<V, E>
1403where
1404    V: 'static,
1405    E: 'static + Element<V>,
1406{
1407    fn render(self) -> AnyElement<V> {
1408        AnyElement::new(self)
1409    }
1410}
1411
1412impl<V, E> ParentComponent<V> for Stateful<V, E>
1413where
1414    V: 'static,
1415    E: ParentComponent<V>,
1416{
1417    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
1418        self.element.children_mut()
1419    }
1420}