1use crate::{
2 element::{AnyElement, Element, IntoElement, Layout, ParentElement},
3 interactive::{InteractionHandlers, Interactive},
4 layout_context::LayoutContext,
5 paint_context::PaintContext,
6 style::{Style, StyleHelpers, Styleable},
7};
8use anyhow::Result;
9use gpui::{platform::MouseMovedEvent, LayoutId};
10use refineable::{CascadeSlot, Refineable, RefinementCascade};
11use smallvec::SmallVec;
12use std::{cell::Cell, rc::Rc};
13
14pub struct Hoverable<E: Styleable> {
15 hovered: Rc<Cell<bool>>,
16 cascade_slot: CascadeSlot,
17 hovered_style: <E::Style as Refineable>::Refinement,
18 child: E,
19}
20
21pub fn hoverable<E: Styleable>(mut child: E) -> Hoverable<E> {
22 Hoverable {
23 hovered: Rc::new(Cell::new(false)),
24 cascade_slot: child.style_cascade().reserve(),
25 hovered_style: Default::default(),
26 child,
27 }
28}
29
30impl<E: Styleable> Styleable for Hoverable<E> {
31 type Style = E::Style;
32
33 fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> {
34 self.child.style_cascade()
35 }
36
37 fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
38 &mut self.hovered_style
39 }
40}
41
42impl<V: 'static, E: Element<V> + Styleable> Element<V> for Hoverable<E> {
43 type PaintState = E::PaintState;
44
45 fn layout(
46 &mut self,
47 view: &mut V,
48 cx: &mut LayoutContext<V>,
49 ) -> Result<(LayoutId, Self::PaintState)>
50 where
51 Self: Sized,
52 {
53 Ok(self.child.layout(view, cx)?)
54 }
55
56 fn paint(
57 &mut self,
58 view: &mut V,
59 layout: &Layout,
60 paint_state: &mut Self::PaintState,
61 cx: &mut PaintContext<V>,
62 ) where
63 Self: Sized,
64 {
65 self.hovered
66 .set(layout.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 let bounds = layout.bounds;
74 cx.on_event(layout.order, move |_view, _: &MouseMovedEvent, cx| {
75 cx.bubble_event();
76 if bounds.contains_point(cx.mouse_position()) != hovered.get() {
77 cx.repaint();
78 }
79 });
80
81 self.child.paint(view, 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}