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
 616        for child in &mut self.children {
 617            child.initialize(view_state, cx);
 618        }
 619
 620        NodeState {
 621            interactive_state,
 622            child_layout_ids: SmallVec::new(),
 623        }
 624    }
 625
 626    fn layout(
 627        &mut self,
 628        view_state: &mut V,
 629        element_state: &mut Self::ElementState,
 630        cx: &mut ViewContext<V>,
 631    ) -> crate::LayoutId {
 632        let mut interactivity = mem::take(&mut self.interactivity);
 633        let layout_id =
 634            interactivity.layout(&mut element_state.interactive_state, cx, |style, cx| {
 635                cx.with_text_style(style.text_style().cloned(), |cx| {
 636                    element_state.child_layout_ids = self
 637                        .children
 638                        .iter_mut()
 639                        .map(|child| child.layout(view_state, cx))
 640                        .collect::<SmallVec<_>>();
 641                    cx.request_layout(&style, element_state.child_layout_ids.iter().copied())
 642                })
 643            });
 644        self.interactivity = interactivity;
 645        layout_id
 646    }
 647
 648    fn paint(
 649        &mut self,
 650        bounds: Bounds<Pixels>,
 651        view_state: &mut V,
 652        element_state: &mut Self::ElementState,
 653        cx: &mut ViewContext<V>,
 654    ) {
 655        let mut child_min = point(Pixels::MAX, Pixels::MAX);
 656        let mut child_max = Point::default();
 657        let content_size = if element_state.child_layout_ids.is_empty() {
 658            bounds.size
 659        } else {
 660            for child_layout_id in &element_state.child_layout_ids {
 661                let child_bounds = cx.layout_bounds(*child_layout_id);
 662                child_min = child_min.min(&child_bounds.origin);
 663                child_max = child_max.max(&child_bounds.lower_right());
 664            }
 665            (child_max - child_min).into()
 666        };
 667
 668        let mut interactivity = mem::take(&mut self.interactivity);
 669        interactivity.paint(
 670            bounds,
 671            content_size,
 672            &mut element_state.interactive_state,
 673            cx,
 674            |style, scroll_offset, cx| {
 675                if style.visibility == Visibility::Hidden {
 676                    return;
 677                }
 678
 679                let z_index = style.z_index.unwrap_or(0);
 680
 681                cx.with_z_index(z_index, |cx| {
 682                    cx.with_z_index(0, |cx| {
 683                        style.paint(bounds, cx);
 684                    });
 685                    cx.with_z_index(1, |cx| {
 686                        cx.with_text_style(style.text_style().cloned(), |cx| {
 687                            cx.with_content_mask(style.overflow_mask(bounds), |cx| {
 688                                cx.with_element_offset(scroll_offset, |cx| {
 689                                    for child in &mut self.children {
 690                                        child.paint(view_state, cx);
 691                                    }
 692                                })
 693                            })
 694                        })
 695                    })
 696                })
 697            },
 698        );
 699        self.interactivity = interactivity;
 700    }
 701}
 702
 703impl<V: 'static> Component<V> for Div<V> {
 704    fn render(self) -> AnyElement<V> {
 705        AnyElement::new(self)
 706    }
 707}
 708
 709pub struct NodeState {
 710    child_layout_ids: SmallVec<[LayoutId; 4]>,
 711    interactive_state: InteractiveElementState,
 712}
 713
 714pub struct Interactivity<V> {
 715    pub element_id: Option<ElementId>,
 716    pub key_context: KeyContext,
 717    pub focusable: bool,
 718    pub tracked_focus_handle: Option<FocusHandle>,
 719    pub focus_listeners: FocusListeners<V>,
 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
 768        element_state
 769    }
 770
 771    pub fn layout(
 772        &mut self,
 773        element_state: &mut InteractiveElementState,
 774        cx: &mut ViewContext<V>,
 775        f: impl FnOnce(Style, &mut ViewContext<V>) -> LayoutId,
 776    ) -> LayoutId {
 777        let style = self.compute_style(None, element_state, cx);
 778        f(style, cx)
 779    }
 780
 781    pub fn paint(
 782        &mut self,
 783        bounds: Bounds<Pixels>,
 784        content_size: Size<Pixels>,
 785        element_state: &mut InteractiveElementState,
 786        cx: &mut ViewContext<V>,
 787        f: impl FnOnce(Style, Point<Pixels>, &mut ViewContext<V>),
 788    ) {
 789        let style = self.compute_style(Some(bounds), element_state, cx);
 790
 791        if let Some(mouse_cursor) = style.mouse_cursor {
 792            let hovered = bounds.contains_point(&cx.mouse_position());
 793            if hovered {
 794                cx.set_cursor_style(mouse_cursor);
 795            }
 796        }
 797
 798        for listener in self.mouse_down_listeners.drain(..) {
 799            cx.on_mouse_event(move |state, event: &MouseDownEvent, phase, cx| {
 800                listener(state, event, &bounds, phase, cx);
 801            })
 802        }
 803
 804        for listener in self.mouse_up_listeners.drain(..) {
 805            cx.on_mouse_event(move |state, event: &MouseUpEvent, phase, cx| {
 806                listener(state, event, &bounds, phase, cx);
 807            })
 808        }
 809
 810        for listener in self.mouse_move_listeners.drain(..) {
 811            cx.on_mouse_event(move |state, event: &MouseMoveEvent, phase, cx| {
 812                listener(state, event, &bounds, phase, cx);
 813            })
 814        }
 815
 816        for listener in self.scroll_wheel_listeners.drain(..) {
 817            cx.on_mouse_event(move |state, event: &ScrollWheelEvent, phase, cx| {
 818                listener(state, event, &bounds, phase, cx);
 819            })
 820        }
 821
 822        let hover_group_bounds = self
 823            .group_hover_style
 824            .as_ref()
 825            .and_then(|group_hover| GroupBounds::get(&group_hover.group, cx));
 826
 827        if let Some(group_bounds) = hover_group_bounds {
 828            let hovered = group_bounds.contains_point(&cx.mouse_position());
 829            cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
 830                if phase == DispatchPhase::Capture {
 831                    if group_bounds.contains_point(&event.position) != hovered {
 832                        cx.notify();
 833                    }
 834                }
 835            });
 836        }
 837
 838        if self.hover_style.is_some()
 839            || (cx.active_drag.is_some() && !self.drag_over_styles.is_empty())
 840        {
 841            let hovered = bounds.contains_point(&cx.mouse_position());
 842            cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
 843                if phase == DispatchPhase::Capture {
 844                    if bounds.contains_point(&event.position) != hovered {
 845                        cx.notify();
 846                    }
 847                }
 848            });
 849        }
 850
 851        if cx.active_drag.is_some() {
 852            let drop_listeners = mem::take(&mut self.drop_listeners);
 853            cx.on_mouse_event(move |view, event: &MouseUpEvent, phase, cx| {
 854                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 855                    if let Some(drag_state_type) =
 856                        cx.active_drag.as_ref().map(|drag| drag.view.entity_type())
 857                    {
 858                        for (drop_state_type, listener) in &drop_listeners {
 859                            if *drop_state_type == drag_state_type {
 860                                let drag = cx
 861                                    .active_drag
 862                                    .take()
 863                                    .expect("checked for type drag state type above");
 864                                listener(view, drag.view.clone(), cx);
 865                                cx.notify();
 866                                cx.stop_propagation();
 867                            }
 868                        }
 869                    }
 870                }
 871            });
 872        }
 873
 874        let click_listeners = mem::take(&mut self.click_listeners);
 875        let drag_listener = mem::take(&mut self.drag_listener);
 876
 877        if !click_listeners.is_empty() || drag_listener.is_some() {
 878            let pending_mouse_down = element_state.pending_mouse_down.clone();
 879            let mouse_down = pending_mouse_down.lock().clone();
 880            if let Some(mouse_down) = mouse_down {
 881                if let Some(drag_listener) = drag_listener {
 882                    let active_state = element_state.clicked_state.clone();
 883
 884                    cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
 885                        if cx.active_drag.is_some() {
 886                            if phase == DispatchPhase::Capture {
 887                                cx.notify();
 888                            }
 889                        } else if phase == DispatchPhase::Bubble
 890                            && bounds.contains_point(&event.position)
 891                            && (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
 892                        {
 893                            *active_state.lock() = ElementClickedState::default();
 894                            let cursor_offset = event.position - bounds.origin;
 895                            let drag = drag_listener(view_state, cursor_offset, cx);
 896                            cx.active_drag = Some(drag);
 897                            cx.notify();
 898                            cx.stop_propagation();
 899                        }
 900                    });
 901                }
 902
 903                cx.on_mouse_event(move |view_state, event: &MouseUpEvent, phase, cx| {
 904                    if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 905                        let mouse_click = ClickEvent {
 906                            down: mouse_down.clone(),
 907                            up: event.clone(),
 908                        };
 909                        for listener in &click_listeners {
 910                            listener(view_state, &mouse_click, cx);
 911                        }
 912                    }
 913                    *pending_mouse_down.lock() = None;
 914                });
 915            } else {
 916                cx.on_mouse_event(move |_state, event: &MouseDownEvent, phase, _cx| {
 917                    if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
 918                        *pending_mouse_down.lock() = Some(event.clone());
 919                    }
 920                });
 921            }
 922        }
 923
 924        if let Some(hover_listener) = self.hover_listener.take() {
 925            let was_hovered = element_state.hover_state.clone();
 926            let has_mouse_down = element_state.pending_mouse_down.clone();
 927
 928            cx.on_mouse_event(move |view_state, event: &MouseMoveEvent, phase, cx| {
 929                if phase != DispatchPhase::Bubble {
 930                    return;
 931                }
 932                let is_hovered =
 933                    bounds.contains_point(&event.position) && has_mouse_down.lock().is_none();
 934                let mut was_hovered = was_hovered.lock();
 935
 936                if is_hovered != was_hovered.clone() {
 937                    *was_hovered = is_hovered;
 938                    drop(was_hovered);
 939
 940                    hover_listener(view_state, is_hovered, cx);
 941                }
 942            });
 943        }
 944
 945        if let Some(tooltip_builder) = self.tooltip_builder.take() {
 946            let active_tooltip = element_state.active_tooltip.clone();
 947            let pending_mouse_down = element_state.pending_mouse_down.clone();
 948
 949            cx.on_mouse_event(move |_, event: &MouseMoveEvent, phase, cx| {
 950                if phase != DispatchPhase::Bubble {
 951                    return;
 952                }
 953
 954                let is_hovered =
 955                    bounds.contains_point(&event.position) && pending_mouse_down.lock().is_none();
 956                if !is_hovered {
 957                    active_tooltip.lock().take();
 958                    return;
 959                }
 960
 961                if active_tooltip.lock().is_none() {
 962                    let task = cx.spawn({
 963                        let active_tooltip = active_tooltip.clone();
 964                        let tooltip_builder = tooltip_builder.clone();
 965
 966                        move |view, mut cx| async move {
 967                            cx.background_executor().timer(TOOLTIP_DELAY).await;
 968                            view.update(&mut cx, move |view_state, cx| {
 969                                active_tooltip.lock().replace(ActiveTooltip {
 970                                    waiting: None,
 971                                    tooltip: Some(AnyTooltip {
 972                                        view: tooltip_builder(view_state, cx),
 973                                        cursor_offset: cx.mouse_position() + TOOLTIP_OFFSET,
 974                                    }),
 975                                });
 976                                cx.notify();
 977                            })
 978                            .ok();
 979                        }
 980                    });
 981                    active_tooltip.lock().replace(ActiveTooltip {
 982                        waiting: Some(task),
 983                        tooltip: None,
 984                    });
 985                }
 986            });
 987
 988            if let Some(active_tooltip) = element_state.active_tooltip.lock().as_ref() {
 989                if active_tooltip.tooltip.is_some() {
 990                    cx.active_tooltip = active_tooltip.tooltip.clone()
 991                }
 992            }
 993        }
 994
 995        let active_state = element_state.clicked_state.clone();
 996        if !active_state.lock().is_clicked() {
 997            cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
 998                if phase == DispatchPhase::Capture {
 999                    *active_state.lock() = ElementClickedState::default();
1000                    cx.notify();
1001                }
1002            });
1003        } else {
1004            let active_group_bounds = self
1005                .group_active_style
1006                .as_ref()
1007                .and_then(|group_active| GroupBounds::get(&group_active.group, cx));
1008            cx.on_mouse_event(move |_view, down: &MouseDownEvent, phase, cx| {
1009                if phase == DispatchPhase::Bubble {
1010                    let group = active_group_bounds
1011                        .map_or(false, |bounds| bounds.contains_point(&down.position));
1012                    let element = bounds.contains_point(&down.position);
1013                    if group || element {
1014                        *active_state.lock() = ElementClickedState { group, element };
1015                        cx.notify();
1016                    }
1017                }
1018            });
1019        }
1020
1021        let overflow = style.overflow;
1022        if overflow.x == Overflow::Scroll || overflow.y == Overflow::Scroll {
1023            let scroll_offset = element_state
1024                .scroll_offset
1025                .get_or_insert_with(Arc::default)
1026                .clone();
1027            let line_height = cx.line_height();
1028            let scroll_max = (content_size - bounds.size).max(&Size::default());
1029
1030            cx.on_mouse_event(move |_, event: &ScrollWheelEvent, phase, cx| {
1031                if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
1032                    let mut scroll_offset = scroll_offset.lock();
1033                    let old_scroll_offset = *scroll_offset;
1034                    let delta = event.delta.pixel_delta(line_height);
1035
1036                    if overflow.x == Overflow::Scroll {
1037                        scroll_offset.x =
1038                            (scroll_offset.x + delta.x).clamp(-scroll_max.width, px(0.));
1039                    }
1040
1041                    if overflow.y == Overflow::Scroll {
1042                        scroll_offset.y =
1043                            (scroll_offset.y + delta.y).clamp(-scroll_max.height, px(0.));
1044                    }
1045
1046                    if *scroll_offset != old_scroll_offset {
1047                        cx.notify();
1048                        cx.stop_propagation();
1049                    }
1050                }
1051            });
1052        }
1053
1054        if let Some(group) = self.group.clone() {
1055            GroupBounds::push(group, bounds, cx);
1056        }
1057
1058        let scroll_offset = element_state
1059            .scroll_offset
1060            .as_ref()
1061            .map(|scroll_offset| *scroll_offset.lock());
1062
1063        cx.with_key_dispatch(
1064            self.key_context.clone(),
1065            element_state.focus_handle.clone(),
1066            |_, cx| {
1067                for listener in self.key_down_listeners.drain(..) {
1068                    cx.on_key_event(move |state, event: &KeyDownEvent, phase, cx| {
1069                        listener(state, event, phase, cx);
1070                    })
1071                }
1072
1073                for listener in self.key_up_listeners.drain(..) {
1074                    cx.on_key_event(move |state, event: &KeyUpEvent, phase, cx| {
1075                        listener(state, event, phase, cx);
1076                    })
1077                }
1078
1079                for (action_type, listener) in self.action_listeners.drain(..) {
1080                    cx.on_action(action_type, listener)
1081                }
1082
1083                if let Some(focus_handle) = element_state.focus_handle.as_ref() {
1084                    for listener in self.focus_listeners.drain(..) {
1085                        let focus_handle = focus_handle.clone();
1086                        cx.on_focus_changed(move |view, event, cx| {
1087                            listener(view, &focus_handle, event, cx)
1088                        });
1089                    }
1090                }
1091
1092                f(style, scroll_offset.unwrap_or_default(), cx)
1093            },
1094        );
1095
1096        if let Some(group) = self.group.as_ref() {
1097            GroupBounds::pop(group, cx);
1098        }
1099    }
1100
1101    pub fn compute_style(
1102        &self,
1103        bounds: Option<Bounds<Pixels>>,
1104        element_state: &mut InteractiveElementState,
1105        cx: &mut ViewContext<V>,
1106    ) -> Style {
1107        let mut style = Style::default();
1108        style.refine(&self.base_style);
1109
1110        if let Some(focus_handle) = self.tracked_focus_handle.as_ref() {
1111            if focus_handle.contains_focused(cx) {
1112                style.refine(&self.focus_in_style);
1113            }
1114
1115            if focus_handle.within_focused(cx) {
1116                style.refine(&self.in_focus_style);
1117            }
1118
1119            if focus_handle.is_focused(cx) {
1120                style.refine(&self.focus_style);
1121            }
1122        }
1123
1124        if let Some(bounds) = bounds {
1125            let mouse_position = cx.mouse_position();
1126            if let Some(group_hover) = self.group_hover_style.as_ref() {
1127                if let Some(group_bounds) = GroupBounds::get(&group_hover.group, cx) {
1128                    if group_bounds.contains_point(&mouse_position) {
1129                        style.refine(&group_hover.style);
1130                    }
1131                }
1132            }
1133            if bounds.contains_point(&mouse_position) {
1134                style.refine(&self.hover_style);
1135            }
1136
1137            if let Some(drag) = cx.active_drag.take() {
1138                for (state_type, group_drag_style) in &self.group_drag_over_styles {
1139                    if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) {
1140                        if *state_type == drag.view.entity_type()
1141                            && group_bounds.contains_point(&mouse_position)
1142                        {
1143                            style.refine(&group_drag_style.style);
1144                        }
1145                    }
1146                }
1147
1148                for (state_type, drag_over_style) in &self.drag_over_styles {
1149                    if *state_type == drag.view.entity_type()
1150                        && bounds.contains_point(&mouse_position)
1151                    {
1152                        style.refine(drag_over_style);
1153                    }
1154                }
1155
1156                cx.active_drag = Some(drag);
1157            }
1158        }
1159
1160        let clicked_state = element_state.clicked_state.lock();
1161        if clicked_state.group {
1162            if let Some(group) = self.group_active_style.as_ref() {
1163                style.refine(&group.style)
1164            }
1165        }
1166
1167        if clicked_state.element {
1168            style.refine(&self.active_style)
1169        }
1170
1171        style
1172    }
1173}
1174
1175impl<V: 'static> Default for Interactivity<V> {
1176    fn default() -> Self {
1177        Self {
1178            element_id: None,
1179            key_context: KeyContext::default(),
1180            focusable: false,
1181            tracked_focus_handle: None,
1182            focus_listeners: SmallVec::default(),
1183            // scroll_offset: Point::default(),
1184            group: None,
1185            base_style: StyleRefinement::default(),
1186            focus_style: StyleRefinement::default(),
1187            focus_in_style: StyleRefinement::default(),
1188            in_focus_style: StyleRefinement::default(),
1189            hover_style: StyleRefinement::default(),
1190            group_hover_style: None,
1191            active_style: StyleRefinement::default(),
1192            group_active_style: None,
1193            drag_over_styles: SmallVec::new(),
1194            group_drag_over_styles: SmallVec::new(),
1195            mouse_down_listeners: SmallVec::new(),
1196            mouse_up_listeners: SmallVec::new(),
1197            mouse_move_listeners: SmallVec::new(),
1198            scroll_wheel_listeners: SmallVec::new(),
1199            key_down_listeners: SmallVec::new(),
1200            key_up_listeners: SmallVec::new(),
1201            action_listeners: SmallVec::new(),
1202            drop_listeners: SmallVec::new(),
1203            click_listeners: SmallVec::new(),
1204            drag_listener: None,
1205            hover_listener: None,
1206            tooltip_builder: None,
1207        }
1208    }
1209}
1210
1211#[derive(Default)]
1212pub struct InteractiveElementState {
1213    pub focus_handle: Option<FocusHandle>,
1214    pub clicked_state: Arc<Mutex<ElementClickedState>>,
1215    pub hover_state: Arc<Mutex<bool>>,
1216    pub pending_mouse_down: Arc<Mutex<Option<MouseDownEvent>>>,
1217    pub scroll_offset: Option<Arc<Mutex<Point<Pixels>>>>,
1218    pub active_tooltip: Arc<Mutex<Option<ActiveTooltip>>>,
1219}
1220
1221pub struct ActiveTooltip {
1222    #[allow(unused)] // used to drop the task
1223    waiting: Option<Task<()>>,
1224    tooltip: Option<AnyTooltip>,
1225}
1226
1227/// Whether or not the element or a group that contains it is clicked by the mouse.
1228#[derive(Copy, Clone, Default, Eq, PartialEq)]
1229pub struct ElementClickedState {
1230    pub group: bool,
1231    pub element: bool,
1232}
1233
1234impl ElementClickedState {
1235    fn is_clicked(&self) -> bool {
1236        self.group || self.element
1237    }
1238}
1239
1240#[derive(Default)]
1241pub struct GroupBounds(HashMap<SharedString, SmallVec<[Bounds<Pixels>; 1]>>);
1242
1243impl GroupBounds {
1244    pub fn get(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
1245        cx.default_global::<Self>()
1246            .0
1247            .get(name)
1248            .and_then(|bounds_stack| bounds_stack.last())
1249            .cloned()
1250    }
1251
1252    pub fn push(name: SharedString, bounds: Bounds<Pixels>, cx: &mut AppContext) {
1253        cx.default_global::<Self>()
1254            .0
1255            .entry(name)
1256            .or_default()
1257            .push(bounds);
1258    }
1259
1260    pub fn pop(name: &SharedString, cx: &mut AppContext) {
1261        cx.default_global::<Self>().0.get_mut(name).unwrap().pop();
1262    }
1263}
1264
1265pub struct Focusable<V, E> {
1266    element: E,
1267    view_type: PhantomData<V>,
1268}
1269
1270impl<V: 'static, E: InteractiveComponent<V>> FocusableComponent<V> for Focusable<V, E> {}
1271
1272impl<V, E> InteractiveComponent<V> for Focusable<V, E>
1273where
1274    V: 'static,
1275    E: InteractiveComponent<V>,
1276{
1277    fn interactivity(&mut self) -> &mut Interactivity<V> {
1278        self.element.interactivity()
1279    }
1280}
1281
1282impl<V: 'static, E: StatefulInteractiveComponent<V, E>> StatefulInteractiveComponent<V, E>
1283    for Focusable<V, E>
1284{
1285}
1286
1287impl<V, E> Styled for Focusable<V, E>
1288where
1289    V: 'static,
1290    E: Styled,
1291{
1292    fn style(&mut self) -> &mut StyleRefinement {
1293        self.element.style()
1294    }
1295}
1296
1297impl<V, E> Element<V> for Focusable<V, E>
1298where
1299    V: 'static,
1300    E: Element<V>,
1301{
1302    type ElementState = E::ElementState;
1303
1304    fn element_id(&self) -> Option<ElementId> {
1305        self.element.element_id()
1306    }
1307
1308    fn initialize(
1309        &mut self,
1310        view_state: &mut V,
1311        element_state: Option<Self::ElementState>,
1312        cx: &mut ViewContext<V>,
1313    ) -> Self::ElementState {
1314        self.element.initialize(view_state, element_state, cx)
1315    }
1316
1317    fn layout(
1318        &mut self,
1319        view_state: &mut V,
1320        element_state: &mut Self::ElementState,
1321        cx: &mut ViewContext<V>,
1322    ) -> LayoutId {
1323        self.element.layout(view_state, element_state, cx)
1324    }
1325
1326    fn paint(
1327        &mut self,
1328        bounds: Bounds<Pixels>,
1329        view_state: &mut V,
1330        element_state: &mut Self::ElementState,
1331        cx: &mut ViewContext<V>,
1332    ) {
1333        self.element.paint(bounds, view_state, element_state, cx);
1334    }
1335}
1336
1337impl<V, E> Component<V> for Focusable<V, E>
1338where
1339    V: 'static,
1340    E: 'static + Element<V>,
1341{
1342    fn render(self) -> AnyElement<V> {
1343        AnyElement::new(self)
1344    }
1345}
1346
1347impl<V, E> ParentComponent<V> for Focusable<V, E>
1348where
1349    V: 'static,
1350    E: ParentComponent<V>,
1351{
1352    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
1353        self.element.children_mut()
1354    }
1355}
1356
1357pub struct Stateful<V, E> {
1358    element: E,
1359    view_type: PhantomData<V>,
1360}
1361
1362impl<V, E> Styled for Stateful<V, E>
1363where
1364    V: 'static,
1365    E: Styled,
1366{
1367    fn style(&mut self) -> &mut StyleRefinement {
1368        self.element.style()
1369    }
1370}
1371
1372impl<V, E> StatefulInteractiveComponent<V, E> for Stateful<V, E>
1373where
1374    V: 'static,
1375    E: Element<V>,
1376    Self: InteractiveComponent<V>,
1377{
1378}
1379
1380impl<V, E> InteractiveComponent<V> for Stateful<V, E>
1381where
1382    V: 'static,
1383    E: InteractiveComponent<V>,
1384{
1385    fn interactivity(&mut self) -> &mut Interactivity<V> {
1386        self.element.interactivity()
1387    }
1388}
1389
1390impl<V: 'static, E: FocusableComponent<V>> FocusableComponent<V> for Stateful<V, E> {}
1391
1392impl<V, E> Element<V> for Stateful<V, E>
1393where
1394    V: 'static,
1395    E: Element<V>,
1396{
1397    type ElementState = E::ElementState;
1398
1399    fn element_id(&self) -> Option<ElementId> {
1400        self.element.element_id()
1401    }
1402
1403    fn initialize(
1404        &mut self,
1405        view_state: &mut V,
1406        element_state: Option<Self::ElementState>,
1407        cx: &mut ViewContext<V>,
1408    ) -> Self::ElementState {
1409        self.element.initialize(view_state, element_state, cx)
1410    }
1411
1412    fn layout(
1413        &mut self,
1414        view_state: &mut V,
1415        element_state: &mut Self::ElementState,
1416        cx: &mut ViewContext<V>,
1417    ) -> LayoutId {
1418        self.element.layout(view_state, element_state, cx)
1419    }
1420
1421    fn paint(
1422        &mut self,
1423        bounds: Bounds<Pixels>,
1424        view_state: &mut V,
1425        element_state: &mut Self::ElementState,
1426        cx: &mut ViewContext<V>,
1427    ) {
1428        self.element.paint(bounds, view_state, element_state, cx)
1429    }
1430}
1431
1432impl<V, E> Component<V> for Stateful<V, E>
1433where
1434    V: 'static,
1435    E: 'static + Element<V>,
1436{
1437    fn render(self) -> AnyElement<V> {
1438        AnyElement::new(self)
1439    }
1440}
1441
1442impl<V, E> ParentComponent<V> for Stateful<V, E>
1443where
1444    V: 'static,
1445    E: ParentComponent<V>,
1446{
1447    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
1448        self.element.children_mut()
1449    }
1450}