1use crate::{
2 element::{AnyElement, Element, IntoElement, Layout, ParentElement},
3 interactive::{InteractionHandlers, Interactive},
4 style::{Style, StyleHelpers, Styleable},
5 ViewContext,
6};
7use anyhow::Result;
8use gpui::{geometry::vector::Vector2F, platform::MouseMovedEvent, LayoutId};
9use refineable::{CascadeSlot, Refineable, RefinementCascade};
10use smallvec::SmallVec;
11use std::{cell::Cell, rc::Rc};
12
13pub struct Hoverable<E: Styleable> {
14 hovered: Rc<Cell<bool>>,
15 cascade_slot: CascadeSlot,
16 hovered_style: <E::Style as Refineable>::Refinement,
17 child: E,
18}
19
20pub fn hoverable<E: Styleable>(mut child: E) -> Hoverable<E> {
21 Hoverable {
22 hovered: Rc::new(Cell::new(false)),
23 cascade_slot: child.style_cascade().reserve(),
24 hovered_style: Default::default(),
25 child,
26 }
27}
28
29impl<E: Styleable> Styleable for Hoverable<E> {
30 type Style = E::Style;
31
32 fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> {
33 self.child.style_cascade()
34 }
35
36 fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
37 &mut self.hovered_style
38 }
39}
40
41impl<V: 'static, E: Element<V> + Styleable> Element<V> for Hoverable<E> {
42 type PaintState = E::PaintState;
43
44 fn layout(
45 &mut self,
46 view: &mut V,
47 cx: &mut ViewContext<V>,
48 ) -> Result<(LayoutId, Self::PaintState)>
49 where
50 Self: Sized,
51 {
52 Ok(self.child.layout(view, cx)?)
53 }
54
55 fn paint(
56 &mut self,
57 view: &mut V,
58 parent_origin: Vector2F,
59 layout: &Layout,
60 paint_state: &mut Self::PaintState,
61 cx: &mut ViewContext<V>,
62 ) where
63 Self: Sized,
64 {
65 let bounds = layout.bounds + parent_origin;
66 self.hovered.set(bounds.contains_point(cx.mouse_position()));
67
68 let slot = self.cascade_slot;
69 let style = self.hovered.get().then_some(self.hovered_style.clone());
70 self.style_cascade().set(slot, style);
71
72 let hovered = self.hovered.clone();
73 cx.on_event(layout.order, move |_view, _: &MouseMovedEvent, cx| {
74 cx.bubble_event();
75 if bounds.contains_point(cx.mouse_position()) != hovered.get() {
76 cx.repaint();
77 }
78 });
79
80 self.child
81 .paint(view, parent_origin, layout, paint_state, cx);
82 }
83}
84
85impl<E: Styleable<Style = Style>> StyleHelpers for Hoverable<E> {}
86
87impl<V: 'static, E: Interactive<V> + Styleable> Interactive<V> for Hoverable<E> {
88 fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V> {
89 self.child.interaction_handlers()
90 }
91}
92
93impl<V: 'static, E: ParentElement<V> + Styleable> ParentElement<V> for Hoverable<E> {
94 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
95 self.child.children_mut()
96 }
97}
98
99impl<V: 'static, E: Element<V> + Styleable> IntoElement<V> for Hoverable<E> {
100 type Element = Self;
101
102 fn into_element(self) -> Self::Element {
103 self
104 }
105}