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