pressable.rs

  1// use crate::{
  2//     AnyElement, Bounds, DispatchPhase, Element, Identified, Interactive, MouseDownEvent,
  3//     MouseEventListeners, MouseUpEvent, ParentElement, Pixels, Styled, ViewContext,
  4// };
  5// use anyhow::Result;
  6// use refineable::{CascadeSlot, Refineable, RefinementCascade};
  7// use smallvec::SmallVec;
  8// use std::sync::{
  9//     atomic::{AtomicBool, Ordering::SeqCst},
 10//     Arc,
 11// };
 12
 13// pub struct Pressable<E: Styled> {
 14//     pressed: Arc<AtomicBool>,
 15//     cascade_slot: CascadeSlot,
 16//     pressed_style: <E::Style as Refineable>::Refinement,
 17//     child: E,
 18// }
 19
 20// impl<E: Identified + Styled> Pressable<E> {
 21//     pub fn new(mut child: E) -> Self {
 22//         Self {
 23//             pressed: Arc::new(AtomicBool::new(false)),
 24//             cascade_slot: child.style_cascade().reserve(),
 25//             pressed_style: Default::default(),
 26//             child,
 27//         }
 28//     }
 29// }
 30
 31// impl<E> Styled for Pressable<E>
 32// where
 33//     E: Styled,
 34// {
 35//     type Style = E::Style;
 36
 37//     fn style_cascade(&mut self) -> &mut RefinementCascade<E::Style> {
 38//         self.child.style_cascade()
 39//     }
 40
 41//     fn declared_style(&mut self) -> &mut <Self::Style as refineable::Refineable>::Refinement {
 42//         &mut self.pressed_style
 43//     }
 44// }
 45
 46// impl<S: 'static + Send + Sync, E: Interactive<S> + Styled> Interactive<S> for Pressable<E> {
 47//     fn listeners(&mut self) -> &mut MouseEventListeners<S> {
 48//         self.child.listeners()
 49//     }
 50// }
 51
 52// impl<E: Element + Identified + Styled> Element for Pressable<E> {
 53//     type State = E::State;
 54//     type FrameState = E::FrameState;
 55
 56//     fn layout(
 57//         &mut self,
 58//         state: &mut Self::State,
 59//         cx: &mut ViewContext<Self::State>,
 60//     ) -> Result<(crate::LayoutId, Self::FrameState)> {
 61//         Ok(self.child.layout(state, cx)?)
 62//     }
 63
 64//     fn paint(
 65//         &mut self,
 66//         bounds: Bounds<Pixels>,
 67//         state: &mut Self::State,
 68//         frame_state: &mut Self::FrameState,
 69//         cx: &mut ViewContext<Self::State>,
 70//     ) -> Result<()> {
 71//         let pressed = bounds.contains_point(cx.mouse_position());
 72//         let slot = self.cascade_slot;
 73//         let style = pressed.then_some(self.pressed_style.clone());
 74//         self.style_cascade().set(slot, style);
 75//         self.pressed.store(pressed, SeqCst);
 76
 77//         let hovered = self.pressed.clone();
 78//         cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
 79//             if phase == DispatchPhase::Capture {
 80//                 if bounds.contains_point(event.position) != hovered.load(SeqCst) {
 81//                     cx.notify();
 82//                 }
 83//             }
 84//         });
 85//         cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| {
 86//             if phase == DispatchPhase::Capture {
 87//                 if bounds.contains_point(event.position) != hovered.load(SeqCst) {
 88//                     cx.notify();
 89//                 }
 90//             }
 91//         });
 92
 93//         self.child.paint(bounds, state, frame_state, cx)?;
 94//         Ok(())
 95//     }
 96// }
 97
 98// impl<E: ParentElement + Styled> ParentElement for Pressable<E> {
 99//     type State = E::State;
100
101//     fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
102//         self.child.children_mut()
103//     }
104// }
105
106// // use crate::{
107// //     element::{AnyElement, Element, IntoElement, Layout, ParentElement},
108// //     interactive::{InteractionHandlers, Interactive},
109// //     style::{Style, StyleHelpers, Styleable},
110// //     ViewContext,
111// // };
112// // use anyhow::Result;
113// // use gpui::{geometry::vector::Vector2F, platform::MouseButtonEvent, LayoutId};
114// // use refineable::{CascadeSlot, Refineable, RefinementCascade};
115// // use smallvec::SmallVec;
116// // use std::{cell::Cell, rc::Rc};
117
118// // pub struct Pressable<E: Styleable> {
119// //     pressed: Rc<Cell<bool>>,
120// //     pressed_style: <E::Style as Refineable>::Refinement,
121// //     cascade_slot: CascadeSlot,
122// //     child: E,
123// // }
124
125// // pub fn pressable<E: Styleable>(mut child: E) -> Pressable<E> {
126// //     Pressable {
127// //         pressed: Rc::new(Cell::new(false)),
128// //         pressed_style: Default::default(),
129// //         cascade_slot: child.style_cascade().reserve(),
130// //         child,
131// //     }
132// // }
133
134// // impl<E: Styleable> Styleable for Pressable<E> {
135// //     type Style = E::Style;
136
137// //     fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
138// //         &mut self.pressed_style
139// //     }
140
141// //     fn style_cascade(&mut self) -> &mut RefinementCascade<E::Style> {
142// //         self.child.style_cascade()
143// //     }
144// // }
145
146// // impl<V: 'static, E: Element<V> + Styleable> Element<V> for Pressable<E> {
147// //     type PaintState = E::PaintState;
148
149// //     fn layout(
150// //         &mut self,
151// //         view: &mut V,
152// //         cx: &mut ViewContext<V>,
153// //     ) -> Result<(LayoutId, Self::PaintState)>
154// //     where
155// //         Self: Sized,
156// //     {
157// //         self.child.layout(view, cx)
158// //     }
159
160// //     fn paint(
161// //         &mut self,
162// //         view: &mut V,
163// //         parent_origin: Vector2F,
164// //         layout: &Layout,
165// //         paint_state: &mut Self::PaintState,
166// //         cx: &mut ViewContext<V>,
167// //     ) where
168// //         Self: Sized,
169// //     {
170// //         let slot = self.cascade_slot;
171// //         let style = self.pressed.get().then_some(self.pressed_style.clone());
172// //         self.style_cascade().set(slot, style);
173
174// //         let pressed = self.pressed.clone();
175// //         let bounds = layout.bounds + parent_origin;
176// //         cx.on_event(layout.order, move |_view, event: &MouseButtonEvent, cx| {
177// //             if event.is_down {
178// //                 if bounds.contains_point(event.position) {
179// //                     pressed.set(true);
180// //                     cx.repaint();
181// //                 }
182// //             } else if pressed.get() {
183// //                 pressed.set(false);
184// //                 cx.repaint();
185// //             }
186// //         });
187
188// //         self.child
189// //             .paint(view, parent_origin, layout, paint_state, cx);
190// //     }
191// // }
192
193// // impl<V: 'static, E: Interactive<V> + Styleable> Interactive<V> for Pressable<E> {
194// //     fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V> {
195// //         self.child.interaction_handlers()
196// //     }
197// // }
198
199// // impl<V: 'static, E: ParentElement<V> + Styleable> ParentElement<V> for Pressable<E> {
200// //     fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
201// //         self.child.children_mut()
202// //     }
203// // }
204
205// // impl<V: 'static, E: Element<V> + Styleable> IntoElement<V> for Pressable<E> {
206// //     type Element = Self;
207
208// //     fn into_element(self) -> Self::Element {
209// //         self
210// //     }
211// // }