pressable.rs

  1use crate::{
  2    group_bounds, AnyElement, Bounds, DispatchPhase, Element, IdentifiedElement, Interactive,
  3    MouseDownEvent, MouseEventListeners, MouseUpEvent, ParentElement, Pixels, SharedString, Styled,
  4    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> Element 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    type ViewState = E::ViewState;
 64    type ElementState = PressableState<E::ElementState>;
 65
 66    fn element_id(&self) -> Option<crate::ElementId> {
 67        Some(IdentifiedElement::element_id(&self.child))
 68    }
 69
 70    fn layout(
 71        &mut self,
 72        state: &mut Self::ViewState,
 73        element_state: Option<Self::ElementState>,
 74        cx: &mut ViewContext<Self::ViewState>,
 75    ) -> (crate::LayoutId, Self::ElementState) {
 76        if let Some(element_state) = element_state {
 77            let (id, child_state) = self
 78                .child
 79                .layout(state, Some(element_state.child_state), cx);
 80            let element_state = PressableState {
 81                pressed: element_state.pressed,
 82                child_state,
 83            };
 84            (id, element_state)
 85        } else {
 86            let (id, child_state) = self.child.layout(state, None, cx);
 87            let element_state = PressableState {
 88                pressed: Default::default(),
 89                child_state,
 90            };
 91            (id, element_state)
 92        }
 93    }
 94
 95    fn paint(
 96        &mut self,
 97        bounds: Bounds<Pixels>,
 98        state: &mut Self::ViewState,
 99        element_state: &mut Self::ElementState,
100        cx: &mut ViewContext<Self::ViewState>,
101    ) {
102        let target_bounds = self
103            .group
104            .as_ref()
105            .and_then(|group| group_bounds(group, cx))
106            .unwrap_or(bounds);
107
108        let style = element_state
109            .pressed
110            .load(SeqCst)
111            .then_some(self.pressed_style.clone());
112        let slot = self.cascade_slot;
113        self.style_cascade().set(slot, style);
114
115        let pressed = element_state.pressed.clone();
116        cx.on_mouse_event(move |_, event: &MouseDownEvent, phase, cx| {
117            if phase == DispatchPhase::Bubble {
118                if target_bounds.contains_point(event.position) {
119                    pressed.store(true, SeqCst);
120                    cx.notify();
121                }
122            }
123        });
124        let pressed = element_state.pressed.clone();
125        cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
126            if phase == DispatchPhase::Capture {
127                if pressed.load(SeqCst) {
128                    pressed.store(false, SeqCst);
129                    cx.notify();
130                }
131            }
132        });
133
134        self.child
135            .paint(bounds, state, &mut element_state.child_state, cx);
136    }
137}
138
139impl<E> ParentElement for Pressable<E>
140where
141    E: ParentElement + IdentifiedElement + Styled,
142{
143    type State = E::State;
144
145    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
146        self.child.children_mut()
147    }
148}
149
150impl<E> IdentifiedElement for Pressable<E>
151where
152    E: IdentifiedElement + Styled,
153    <E as Styled>::Style: 'static + Refineable + Send + Sync + Default,
154    <<E as Styled>::Style as Refineable>::Refinement: 'static + Refineable + Send + Sync + Default,
155{
156}