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 scroll_offset: Point<Pixels>,
 720    pub group: Option<SharedString>,
 721    pub base_style: StyleRefinement,
 722    pub focus_style: StyleRefinement,
 723    pub focus_in_style: StyleRefinement,
 724    pub in_focus_style: StyleRefinement,
 725    pub hover_style: StyleRefinement,
 726    pub group_hover_style: Option<GroupStyle>,
 727    pub active_style: StyleRefinement,
 728    pub group_active_style: Option<GroupStyle>,
 729    pub drag_over_styles: SmallVec<[(TypeId, StyleRefinement); 2]>,
 730    pub group_drag_over_styles: SmallVec<[(TypeId, GroupStyle); 2]>,
 731    pub mouse_down_listeners: SmallVec<[MouseDownListener<V>; 2]>,
 732    pub mouse_up_listeners: SmallVec<[MouseUpListener<V>; 2]>,
 733    pub mouse_move_listeners: SmallVec<[MouseMoveListener<V>; 2]>,
 734    pub scroll_wheel_listeners: SmallVec<[ScrollWheelListener<V>; 2]>,
 735    pub key_down_listeners: SmallVec<[KeyDownListener<V>; 2]>,
 736    pub key_up_listeners: SmallVec<[KeyUpListener<V>; 2]>,
 737    pub action_listeners: SmallVec<[(TypeId, ActionListener<V>); 8]>,
 738    pub drop_listeners: SmallVec<[(TypeId, Box<DropListener<V>>); 2]>,
 739    pub click_listeners: SmallVec<[ClickListener<V>; 2]>,
 740    pub drag_listener: Option<DragListener<V>>,
 741    pub hover_listener: Option<HoverListener<V>>,
 742    pub tooltip_builder: Option<TooltipBuilder<V>>,
 743}
 744
 745impl<V> Interactivity<V>
 746where
 747    V: 'static,
 748{
 749    pub fn initialize(
 750        &mut self,
 751        element_state: Option<InteractiveElementState>,
 752        cx: &mut ViewContext<V>,
 753    ) -> InteractiveElementState {
 754        let mut element_state = element_state.unwrap_or_default();
 755
 756        // Ensure we store a focus handle in our element state if we're focusable.
 757        // If there's an explicit focus handle we're tracking, use that. Otherwise
 758        // create a new handle and store it in the element state, which lives for as
 759        // as frames contain an element with this id.
 760        if self.focusable {
 761            element_state.focus_handle.get_or_insert_with(|| {
 762                self.tracked_focus_handle
 763                    .clone()
 764                    .unwrap_or_else(|| cx.focus_handle())
 765            });
 766        }
 767        element_state
 768    }
 769
 770    pub fn layout(
 771        &mut self,
 772        element_state: &mut InteractiveElementState,
 773        cx: &mut ViewContext<V>,
 774        f: impl FnOnce(Style, &mut ViewContext<V>) -> LayoutId,
 775    ) -> LayoutId {
 776        let style = self.compute_style(None, element_state, cx);
 777        cx.with_key_dispatch(
 778            self.key_context.clone(),
 779            self.tracked_focus_handle.clone(),
 780            |_, cx| f(style, cx),
 781        )
 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.lock().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.lock() = 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.lock() = None;
 917                });
 918            } else {
 919                cx.on_mouse_event(move |_state, event: &MouseDownEvent, phase, _cx| {
 920                    if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 921                        *pending_mouse_down.lock() = Some(event.clone());
 922                    }
 923                });
 924            }
 925        }
 926
 927        if let Some(hover_listener) = self.hover_listener.take() {
 928            let was_hovered = element_state.hover_state.clone();
 929            let has_mouse_down = element_state.pending_mouse_down.clone();
 930
 931            cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
 932                if phase != DispatchPhase::Bubble {
 933                    return;
 934                }
 935                let is_hovered =
 936                    bounds.contains_point(&event.position) && has_mouse_down.lock().is_none();
 937                let mut was_hovered = was_hovered.lock();
 938
 939                if is_hovered != was_hovered.clone() {
 940                    *was_hovered = is_hovered;
 941                    drop(was_hovered);
 942
 943                    hover_listener(view_state, is_hovered, cx);
 944                }
 945            });
 946        }
 947
 948        if let Some(tooltip_builder) = self.tooltip_builder.take() {
 949            let active_tooltip = element_state.active_tooltip.clone();
 950            let pending_mouse_down = element_state.pending_mouse_down.clone();
 951
 952            cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
 953                if phase != DispatchPhase::Bubble {
 954                    return;
 955                }
 956
 957                let is_hovered =
 958                    bounds.contains_point(&event.position) && pending_mouse_down.lock().is_none();
 959                if !is_hovered {
 960                    active_tooltip.lock().take();
 961                    return;
 962                }
 963
 964                if active_tooltip.lock().is_none() {
 965                    let task = cx.spawn({
 966                        let active_tooltip = active_tooltip.clone();
 967                        let tooltip_builder = tooltip_builder.clone();
 968
 969                        move |view, mut cx| async move {
 970                            cx.background_executor().timer(TOOLTIP_DELAY).await;
 971                            view.update(&mut cx, move |view_state, cx| {
 972                                active_tooltip.lock().replace(ActiveTooltip {
 973                                    waiting: None,
 974                                    tooltip: Some(AnyTooltip {
 975                                        view: tooltip_builder(view_state, cx),
 976                                        cursor_offset: cx.mouse_position() + TOOLTIP_OFFSET,
 977                                    }),
 978                                });
 979                                cx.notify();
 980                            })
 981                            .ok();
 982                        }
 983                    });
 984                    active_tooltip.lock().replace(ActiveTooltip {
 985                        waiting: Some(task),
 986                        tooltip: None,
 987                    });
 988                }
 989            });
 990
 991            if let Some(active_tooltip) = element_state.active_tooltip.lock().as_ref() {
 992                if active_tooltip.tooltip.is_some() {
 993                    cx.active_tooltip = active_tooltip.tooltip.clone()
 994                }
 995            }
 996        }
 997
 998        let active_state = element_state.clicked_state.clone();
 999        if !active_state.lock().is_clicked() {
1000            cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
1001                if phase == DispatchPhase::Capture {
1002                    *active_state.lock() = ElementClickedState::default();
1003                    cx.notify();
1004                }
1005            });
1006        } else {
1007            let active_group_bounds = self
1008                .group_active_style
1009                .as_ref()
1010                .and_then(|group_active| GroupBounds::get(&group_active.group, cx));
1011            cx.on_mouse_event(move |_view, down: &MouseDownEvent, phase, cx| {
1012                if phase == DispatchPhase::Bubble {
1013                    let group = active_group_bounds
1014                        .map_or(false, |bounds| bounds.contains_point(&down.position));
1015                    let element = bounds.contains_point(&down.position);
1016                    if group || element {
1017                        *active_state.lock() = ElementClickedState { group, element };
1018                        cx.notify();
1019                    }
1020                }
1021            });
1022        }
1023
1024        let overflow = style.overflow;
1025        if overflow.x == Overflow::Scroll || overflow.y == Overflow::Scroll {
1026            let scroll_offset = element_state
1027                .scroll_offset
1028                .get_or_insert_with(Arc::default)
1029                .clone();
1030            let line_height = cx.line_height();
1031            let scroll_max = (content_size - bounds.size).max(&Size::default());
1032
1033            cx.on_mouse_event(move |_, event: &ScrollWheelEvent, phase, cx| {
1034                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
1035                    let mut scroll_offset = scroll_offset.lock();
1036                    let old_scroll_offset = *scroll_offset;
1037                    let delta = event.delta.pixel_delta(line_height);
1038
1039                    if overflow.x == Overflow::Scroll {
1040                        scroll_offset.x =
1041                            (scroll_offset.x + delta.x).clamp(-scroll_max.width, px(0.));
1042                    }
1043
1044                    if overflow.y == Overflow::Scroll {
1045                        scroll_offset.y =
1046                            (scroll_offset.y + delta.y).clamp(-scroll_max.height, px(0.));
1047                    }
1048
1049                    if *scroll_offset != old_scroll_offset {
1050                        cx.notify();
1051                        cx.stop_propagation();
1052                    }
1053                }
1054            });
1055        }
1056
1057        if let Some(group) = self.group.clone() {
1058            GroupBounds::push(group, bounds, cx);
1059        }
1060
1061        let scroll_offset = element_state
1062            .scroll_offset
1063            .as_ref()
1064            .map(|scroll_offset| *scroll_offset.lock());
1065
1066        cx.with_key_dispatch(
1067            self.key_context.clone(),
1068            element_state.focus_handle.clone(),
1069            |_, cx| {
1070                for listener in self.key_down_listeners.drain(..) {
1071                    cx.on_key_event(move |state, event: &KeyDownEvent, phase, cx| {
1072                        listener(state, event, phase, cx);
1073                    })
1074                }
1075
1076                for listener in self.key_up_listeners.drain(..) {
1077                    cx.on_key_event(move |state, event: &KeyUpEvent, phase, cx| {
1078                        listener(state, event, phase, cx);
1079                    })
1080                }
1081
1082                for (action_type, listener) in self.action_listeners.drain(..) {
1083                    cx.on_action(action_type, listener)
1084                }
1085
1086                if let Some(focus_handle) = element_state.focus_handle.as_ref() {
1087                    for listener in self.focus_listeners.drain(..) {
1088                        let focus_handle = focus_handle.clone();
1089                        cx.on_focus_changed(move |view, event, cx| {
1090                            listener(view, &focus_handle, event, cx)
1091                        });
1092                    }
1093                }
1094
1095                f(style, scroll_offset.unwrap_or_default(), cx)
1096            },
1097        );
1098
1099        if let Some(group) = self.group.as_ref() {
1100            GroupBounds::pop(group, cx);
1101        }
1102    }
1103
1104    pub fn compute_style(
1105        &self,
1106        bounds: Option<Bounds<Pixels>>,
1107        element_state: &mut InteractiveElementState,
1108        cx: &mut ViewContext<V>,
1109    ) -> Style {
1110        let mut style = Style::default();
1111        style.refine(&self.base_style);
1112
1113        if let Some(focus_handle) = self.tracked_focus_handle.as_ref() {
1114            if focus_handle.contains_focused(cx) {
1115                style.refine(&self.focus_in_style);
1116            }
1117
1118            if focus_handle.within_focused(cx) {
1119                style.refine(&self.in_focus_style);
1120            }
1121
1122            if focus_handle.is_focused(cx) {
1123                style.refine(&self.focus_style);
1124            }
1125        }
1126
1127        if let Some(bounds) = bounds {
1128            let mouse_position = cx.mouse_position();
1129            if let Some(group_hover) = self.group_hover_style.as_ref() {
1130                if let Some(group_bounds) = GroupBounds::get(&group_hover.group, cx) {
1131                    if group_bounds.contains_point(&mouse_position) {
1132                        style.refine(&group_hover.style);
1133                    }
1134                }
1135            }
1136            if bounds.contains_point(&mouse_position) {
1137                style.refine(&self.hover_style);
1138            }
1139
1140            if let Some(drag) = cx.active_drag.take() {
1141                for (state_type, group_drag_style) in &self.group_drag_over_styles {
1142                    if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) {
1143                        if *state_type == drag.view.entity_type()
1144                            && group_bounds.contains_point(&mouse_position)
1145                        {
1146                            style.refine(&group_drag_style.style);
1147                        }
1148                    }
1149                }
1150
1151                for (state_type, drag_over_style) in &self.drag_over_styles {
1152                    if *state_type == drag.view.entity_type()
1153                        && bounds.contains_point(&mouse_position)
1154                    {
1155                        style.refine(drag_over_style);
1156                    }
1157                }
1158
1159                cx.active_drag = Some(drag);
1160            }
1161        }
1162
1163        let clicked_state = element_state.clicked_state.lock();
1164        if clicked_state.group {
1165            if let Some(group) = self.group_active_style.as_ref() {
1166                style.refine(&group.style)
1167            }
1168        }
1169
1170        if clicked_state.element {
1171            style.refine(&self.active_style)
1172        }
1173
1174        style
1175    }
1176}
1177
1178impl<V: 'static> Default for Interactivity<V> {
1179    fn default() -> Self {
1180        Self {
1181            element_id: None,
1182            key_context: KeyContext::default(),
1183            focusable: false,
1184            tracked_focus_handle: None,
1185            focus_listeners: SmallVec::default(),
1186            // scroll_offset: Point::default(),
1187            group: None,
1188            base_style: StyleRefinement::default(),
1189            focus_style: StyleRefinement::default(),
1190            focus_in_style: StyleRefinement::default(),
1191            in_focus_style: StyleRefinement::default(),
1192            hover_style: StyleRefinement::default(),
1193            group_hover_style: None,
1194            active_style: StyleRefinement::default(),
1195            group_active_style: None,
1196            drag_over_styles: SmallVec::new(),
1197            group_drag_over_styles: SmallVec::new(),
1198            mouse_down_listeners: SmallVec::new(),
1199            mouse_up_listeners: SmallVec::new(),
1200            mouse_move_listeners: SmallVec::new(),
1201            scroll_wheel_listeners: SmallVec::new(),
1202            key_down_listeners: SmallVec::new(),
1203            key_up_listeners: SmallVec::new(),
1204            action_listeners: SmallVec::new(),
1205            drop_listeners: SmallVec::new(),
1206            click_listeners: SmallVec::new(),
1207            drag_listener: None,
1208            hover_listener: None,
1209            tooltip_builder: None,
1210        }
1211    }
1212}
1213
1214#[derive(Default)]
1215pub struct InteractiveElementState {
1216    pub focus_handle: Option<FocusHandle>,
1217    pub clicked_state: Arc<Mutex<ElementClickedState>>,
1218    pub hover_state: Arc<Mutex<bool>>,
1219    pub pending_mouse_down: Arc<Mutex<Option<MouseDownEvent>>>,
1220    pub scroll_offset: Option<Arc<Mutex<Point<Pixels>>>>,
1221    pub active_tooltip: Arc<Mutex<Option<ActiveTooltip>>>,
1222}
1223
1224pub struct ActiveTooltip {
1225    #[allow(unused)] // used to drop the task
1226    waiting: Option<Task<()>>,
1227    tooltip: Option<AnyTooltip>,
1228}
1229
1230/// Whether or not the element or a group that contains it is clicked by the mouse.
1231#[derive(Copy, Clone, Default, Eq, PartialEq)]
1232pub struct ElementClickedState {
1233    pub group: bool,
1234    pub element: bool,
1235}
1236
1237impl ElementClickedState {
1238    fn is_clicked(&self) -> bool {
1239        self.group || self.element
1240    }
1241}
1242
1243#[derive(Default)]
1244pub struct GroupBounds(HashMap<SharedString, SmallVec<[Bounds<Pixels>; 1]>>);
1245
1246impl GroupBounds {
1247    pub fn get(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
1248        cx.default_global::<Self>()
1249            .0
1250            .get(name)
1251            .and_then(|bounds_stack| bounds_stack.last())
1252            .cloned()
1253    }
1254
1255    pub fn push(name: SharedString, bounds: Bounds<Pixels>, cx: &mut AppContext) {
1256        cx.default_global::<Self>()
1257            .0
1258            .entry(name)
1259            .or_default()
1260            .push(bounds);
1261    }
1262
1263    pub fn pop(name: &SharedString, cx: &mut AppContext) {
1264        cx.default_global::<Self>().0.get_mut(name).unwrap().pop();
1265    }
1266}
1267
1268pub struct Focusable<V, E> {
1269    element: E,
1270    view_type: PhantomData<V>,
1271}
1272
1273impl<V: 'static, E: InteractiveComponent<V>> FocusableComponent<V> for Focusable<V, E> {}
1274
1275impl<V, E> InteractiveComponent<V> for Focusable<V, E>
1276where
1277    V: 'static,
1278    E: InteractiveComponent<V>,
1279{
1280    fn interactivity(&mut self) -> &mut Interactivity<V> {
1281        self.element.interactivity()
1282    }
1283}
1284
1285impl<V: 'static, E: StatefulInteractiveComponent<V, E>> StatefulInteractiveComponent<V, E>
1286    for Focusable<V, E>
1287{
1288}
1289
1290impl<V, E> Styled for Focusable<V, E>
1291where
1292    V: 'static,
1293    E: Styled,
1294{
1295    fn style(&mut self) -> &mut StyleRefinement {
1296        self.element.style()
1297    }
1298}
1299
1300impl<V, E> Element<V> for Focusable<V, E>
1301where
1302    V: 'static,
1303    E: Element<V>,
1304{
1305    type ElementState = E::ElementState;
1306
1307    fn element_id(&self) -> Option<ElementId> {
1308        self.element.element_id()
1309    }
1310
1311    fn initialize(
1312        &mut self,
1313        view_state: &mut V,
1314        element_state: Option<Self::ElementState>,
1315        cx: &mut ViewContext<V>,
1316    ) -> Self::ElementState {
1317        self.element.initialize(view_state, element_state, cx)
1318    }
1319
1320    fn layout(
1321        &mut self,
1322        view_state: &mut V,
1323        element_state: &mut Self::ElementState,
1324        cx: &mut ViewContext<V>,
1325    ) -> LayoutId {
1326        self.element.layout(view_state, element_state, cx)
1327    }
1328
1329    fn paint(
1330        &mut self,
1331        bounds: Bounds<Pixels>,
1332        view_state: &mut V,
1333        element_state: &mut Self::ElementState,
1334        cx: &mut ViewContext<V>,
1335    ) {
1336        self.element.paint(bounds, view_state, element_state, cx);
1337    }
1338}
1339
1340impl<V, E> Component<V> for Focusable<V, E>
1341where
1342    V: 'static,
1343    E: 'static + Element<V>,
1344{
1345    fn render(self) -> AnyElement<V> {
1346        AnyElement::new(self)
1347    }
1348}
1349
1350impl<V, E> ParentComponent<V> for Focusable<V, E>
1351where
1352    V: 'static,
1353    E: ParentComponent<V>,
1354{
1355    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
1356        self.element.children_mut()
1357    }
1358}
1359
1360pub struct Stateful<V, E> {
1361    element: E,
1362    view_type: PhantomData<V>,
1363}
1364
1365impl<V, E> Styled for Stateful<V, E>
1366where
1367    V: 'static,
1368    E: Styled,
1369{
1370    fn style(&mut self) -> &mut StyleRefinement {
1371        self.element.style()
1372    }
1373}
1374
1375impl<V, E> StatefulInteractiveComponent<V, E> for Stateful<V, E>
1376where
1377    V: 'static,
1378    E: Element<V>,
1379    Self: InteractiveComponent<V>,
1380{
1381}
1382
1383impl<V, E> InteractiveComponent<V> for Stateful<V, E>
1384where
1385    V: 'static,
1386    E: InteractiveComponent<V>,
1387{
1388    fn interactivity(&mut self) -> &mut Interactivity<V> {
1389        self.element.interactivity()
1390    }
1391}
1392
1393impl<V: 'static, E: FocusableComponent<V>> FocusableComponent<V> for Stateful<V, E> {}
1394
1395impl<V, E> Element<V> for Stateful<V, E>
1396where
1397    V: 'static,
1398    E: Element<V>,
1399{
1400    type ElementState = E::ElementState;
1401
1402    fn element_id(&self) -> Option<ElementId> {
1403        self.element.element_id()
1404    }
1405
1406    fn initialize(
1407        &mut self,
1408        view_state: &mut V,
1409        element_state: Option<Self::ElementState>,
1410        cx: &mut ViewContext<V>,
1411    ) -> Self::ElementState {
1412        self.element.initialize(view_state, element_state, cx)
1413    }
1414
1415    fn layout(
1416        &mut self,
1417        view_state: &mut V,
1418        element_state: &mut Self::ElementState,
1419        cx: &mut ViewContext<V>,
1420    ) -> LayoutId {
1421        self.element.layout(view_state, element_state, cx)
1422    }
1423
1424    fn paint(
1425        &mut self,
1426        bounds: Bounds<Pixels>,
1427        view_state: &mut V,
1428        element_state: &mut Self::ElementState,
1429        cx: &mut ViewContext<V>,
1430    ) {
1431        self.element.paint(bounds, view_state, element_state, cx)
1432    }
1433}
1434
1435impl<V, E> Component<V> for Stateful<V, E>
1436where
1437    V: 'static,
1438    E: 'static + Element<V>,
1439{
1440    fn render(self) -> AnyElement<V> {
1441        AnyElement::new(self)
1442    }
1443}
1444
1445impl<V, E> ParentComponent<V> for Stateful<V, E>
1446where
1447    V: 'static,
1448    E: ParentComponent<V>,
1449{
1450    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
1451        self.element.children_mut()
1452    }
1453}