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::MouseButtonEvent, LayoutId};
9use refineable::{CascadeSlot, Refineable, RefinementCascade};
10use smallvec::SmallVec;
11use std::{cell::Cell, rc::Rc};
12
13pub struct Pressable<E: Styleable> {
14 pressed: Rc<Cell<bool>>,
15 pressed_style: <E::Style as Refineable>::Refinement,
16 cascade_slot: CascadeSlot,
17 child: E,
18}
19
20pub fn pressable<E: Styleable>(mut child: E) -> Pressable<E> {
21 Pressable {
22 pressed: Rc::new(Cell::new(false)),
23 pressed_style: Default::default(),
24 cascade_slot: child.style_cascade().reserve(),
25 child,
26 }
27}
28
29impl<E: Styleable> Styleable for Pressable<E> {
30 type Style = E::Style;
31
32 fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
33 &mut self.pressed_style
34 }
35
36 fn style_cascade(&mut self) -> &mut RefinementCascade<E::Style> {
37 self.child.style_cascade()
38 }
39}
40
41impl<V: 'static, E: Element<V> + Styleable> Element<V> for Pressable<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 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 slot = self.cascade_slot;
66 let style = self.pressed.get().then_some(self.pressed_style.clone());
67 self.style_cascade().set(slot, style);
68
69 let pressed = self.pressed.clone();
70 let bounds = layout.bounds + parent_origin;
71 cx.on_event(layout.order, move |_view, event: &MouseButtonEvent, cx| {
72 if event.is_down {
73 if bounds.contains_point(event.position) {
74 pressed.set(true);
75 cx.repaint();
76 } else {
77 cx.bubble_event();
78 }
79 } else {
80 if pressed.get() {
81 pressed.set(false);
82 cx.repaint();
83 }
84 cx.bubble_event();
85 }
86 });
87
88 self.child
89 .paint(view, parent_origin, layout, paint_state, cx);
90 }
91}
92
93impl<E: Styleable<Style = Style>> StyleHelpers for Pressable<E> {}
94
95impl<V: 'static, E: Interactive<V> + Styleable> Interactive<V> for Pressable<E> {
96 fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V> {
97 self.child.interaction_handlers()
98 }
99}
100
101impl<V: 'static, E: ParentElement<V> + Styleable> ParentElement<V> for Pressable<E> {
102 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
103 self.child.children_mut()
104 }
105}
106
107impl<V: 'static, E: Element<V> + Styleable> IntoElement<V> for Pressable<E> {
108 type Element = Self;
109
110 fn into_element(self) -> Self::Element {
111 self
112 }
113}