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