pressable.rs

  1use crate::{
  2    group_bounds, AnyElement, Bounds, DispatchPhase, Element, IdentifiedElement, Interactive,
  3    IntoAnyElement, MouseDownEvent, MouseEventListeners, MouseUpEvent, ParentElement, Pixels,
  4    SharedString, Styled, ViewContext,
  5};
  6use refineable::{Cascade, CascadeSlot, Refineable};
  7use smallvec::SmallVec;
  8use std::sync::{
  9    atomic::{AtomicBool, Ordering::SeqCst},
 10    Arc,
 11};
 12
 13pub struct Pressable<E: Styled> {
 14    group: Option<SharedString>,
 15    cascade_slot: CascadeSlot,
 16    pressed_style: <E::Style as Refineable>::Refinement,
 17    child: E,
 18}
 19
 20pub struct PressableState<S> {
 21    pressed: Arc<AtomicBool>,
 22    child_state: S,
 23}
 24
 25impl<E: Styled> Pressable<E> {
 26    pub fn new(mut child: E, group: Option<SharedString>) -> Self {
 27        Self {
 28            group,
 29            cascade_slot: child.style_cascade().reserve(),
 30            pressed_style: Default::default(),
 31            child,
 32        }
 33    }
 34}
 35
 36impl<E> Styled for Pressable<E>
 37where
 38    E: Styled,
 39{
 40    type Style = E::Style;
 41
 42    fn style_cascade(&mut self) -> &mut Cascade<E::Style> {
 43        self.child.style_cascade()
 44    }
 45
 46    fn declared_style(&mut self) -> &mut <Self::Style as refineable::Refineable>::Refinement {
 47        &mut self.pressed_style
 48    }
 49}
 50
 51impl<S: 'static + Send + Sync, E: Interactive<S> + Styled> Interactive<S> for Pressable<E> {
 52    fn listeners(&mut self) -> &mut MouseEventListeners<S> {
 53        self.child.listeners()
 54    }
 55}
 56
 57impl<E> IntoAnyElement<E::ViewState> for Pressable<E>
 58where
 59    E: Styled + IdentifiedElement,
 60    <E as Styled>::Style: 'static + Refineable + Send + Sync + Default,
 61    <<E as Styled>::Style as Refineable>::Refinement: 'static + Refineable + Send + Sync + Default,
 62{
 63    fn into_any(self) -> AnyElement<E::ViewState> {
 64        AnyElement::new(self)
 65    }
 66}
 67
 68impl<E> Element for Pressable<E>
 69where
 70    E: Styled + IdentifiedElement,
 71    <E as Styled>::Style: 'static + Refineable + Send + Sync + Default,
 72    <<E as Styled>::Style as Refineable>::Refinement: 'static + Refineable + Send + Sync + Default,
 73{
 74    type ViewState = E::ViewState;
 75    type ElementState = PressableState<E::ElementState>;
 76
 77    fn element_id(&self) -> Option<crate::ElementId> {
 78        Some(IdentifiedElement::element_id(&self.child))
 79    }
 80
 81    fn layout(
 82        &mut self,
 83        state: &mut Self::ViewState,
 84        element_state: Option<Self::ElementState>,
 85        cx: &mut ViewContext<Self::ViewState>,
 86    ) -> (crate::LayoutId, Self::ElementState) {
 87        if let Some(element_state) = element_state {
 88            let (id, child_state) = self
 89                .child
 90                .layout(state, Some(element_state.child_state), cx);
 91            let element_state = PressableState {
 92                pressed: element_state.pressed,
 93                child_state,
 94            };
 95            (id, element_state)
 96        } else {
 97            let (id, child_state) = self.child.layout(state, None, cx);
 98            let element_state = PressableState {
 99                pressed: Default::default(),
100                child_state,
101            };
102            (id, element_state)
103        }
104    }
105
106    fn paint(
107        &mut self,
108        bounds: Bounds<Pixels>,
109        state: &mut Self::ViewState,
110        element_state: &mut Self::ElementState,
111        cx: &mut ViewContext<Self::ViewState>,
112    ) {
113        let target_bounds = self
114            .group
115            .as_ref()
116            .and_then(|group| group_bounds(group, cx))
117            .unwrap_or(bounds);
118
119        let style = element_state
120            .pressed
121            .load(SeqCst)
122            .then_some(self.pressed_style.clone());
123        let slot = self.cascade_slot;
124        self.style_cascade().set(slot, style);
125
126        let pressed = element_state.pressed.clone();
127        cx.on_mouse_event(move |_, event: &MouseDownEvent, phase, cx| {
128            if phase == DispatchPhase::Bubble {
129                if target_bounds.contains_point(event.position) {
130                    pressed.store(true, SeqCst);
131                    cx.notify();
132                }
133            }
134        });
135        let pressed = element_state.pressed.clone();
136        cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
137            if phase == DispatchPhase::Capture {
138                if pressed.load(SeqCst) {
139                    pressed.store(false, SeqCst);
140                    cx.notify();
141                }
142            }
143        });
144
145        self.child
146            .paint(bounds, state, &mut element_state.child_state, cx);
147    }
148}
149
150impl<E> ParentElement for Pressable<E>
151where
152    E: ParentElement + IdentifiedElement + Styled,
153{
154    type State = E::State;
155
156    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
157        self.child.children_mut()
158    }
159}
160
161impl<E> IdentifiedElement for Pressable<E>
162where
163    E: IdentifiedElement + Styled,
164    <E as Styled>::Style: 'static + Refineable + Send + Sync + Default,
165    <<E as Styled>::Style as Refineable>::Refinement: 'static + Refineable + Send + Sync + Default,
166{
167}