This commit is contained in:
Antonio Scandurra 2023-10-10 20:02:23 +02:00
parent 84ad2cb827
commit 4855b8f3de
2 changed files with 170 additions and 63 deletions

View File

@ -2,6 +2,7 @@ mod div;
mod hoverable; mod hoverable;
mod identified; mod identified;
mod img; mod img;
mod pressable;
mod stateless; mod stateless;
mod svg; mod svg;
mod text; mod text;
@ -10,6 +11,7 @@ pub use div::*;
pub use hoverable::*; pub use hoverable::*;
pub use identified::*; pub use identified::*;
pub use img::*; pub use img::*;
pub use pressable::*;
pub use stateless::*; pub use stateless::*;
pub use svg::*; pub use svg::*;
pub use text::*; pub use text::*;

View File

@ -1,106 +1,211 @@
use crate::{ use crate::{
element::{AnyElement, Element, IntoElement, Layout, ParentElement}, AnyElement, Bounds, DispatchPhase, Element, Identified, Interactive, MouseDownEvent,
interactive::{InteractionHandlers, Interactive}, MouseEventListeners, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Styled, ViewContext,
style::{Style, StyleHelpers, Styleable},
ViewContext,
}; };
use anyhow::Result; use anyhow::Result;
use gpui::{geometry::vector::Vector2F, platform::MouseButtonEvent, LayoutId};
use refineable::{CascadeSlot, Refineable, RefinementCascade}; use refineable::{CascadeSlot, Refineable, RefinementCascade};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{cell::Cell, rc::Rc}; use std::sync::{
atomic::{AtomicBool, Ordering::SeqCst},
Arc,
};
pub struct Pressable<E: Styleable> { pub struct Pressable<E: Styled> {
pressed: Rc<Cell<bool>>, pressed: Arc<AtomicBool>,
pressed_style: <E::Style as Refineable>::Refinement,
cascade_slot: CascadeSlot, cascade_slot: CascadeSlot,
pressed_style: <E::Style as Refineable>::Refinement,
child: E, child: E,
} }
pub fn pressable<E: Styleable>(mut child: E) -> Pressable<E> { impl<E: Identified + Styled> Pressable<E> {
Pressable { pub fn new(mut child: E) -> Self {
pressed: Rc::new(Cell::new(false)), Self {
pressed_style: Default::default(), pressed: Arc::new(AtomicBool::new(false)),
cascade_slot: child.style_cascade().reserve(), cascade_slot: child.style_cascade().reserve(),
child, pressed_style: Default::default(),
child,
}
} }
} }
impl<E: Styleable> Styleable for Pressable<E> { impl<E> Styled for Pressable<E>
where
E: Styled,
{
type Style = E::Style; type Style = E::Style;
fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
&mut self.pressed_style
}
fn style_cascade(&mut self) -> &mut RefinementCascade<E::Style> { fn style_cascade(&mut self) -> &mut RefinementCascade<E::Style> {
self.child.style_cascade() self.child.style_cascade()
} }
fn declared_style(&mut self) -> &mut <Self::Style as refineable::Refineable>::Refinement {
&mut self.pressed_style
}
} }
impl<V: 'static, E: Element<V> + Styleable> Element<V> for Pressable<E> { impl<S: 'static + Send + Sync, E: Interactive<S> + Styled> Interactive<S> for Pressable<E> {
type PaintState = E::PaintState; fn listeners(&mut self) -> &mut MouseEventListeners<S> {
self.child.listeners()
}
}
impl<E: Element + Identified + Styled> Element for Pressable<E> {
type State = E::State;
type FrameState = E::FrameState;
fn layout( fn layout(
&mut self, &mut self,
view: &mut V, state: &mut Self::State,
cx: &mut ViewContext<V>, cx: &mut ViewContext<Self::State>,
) -> Result<(LayoutId, Self::PaintState)> ) -> Result<(crate::LayoutId, Self::FrameState)> {
where Ok(self.child.layout(state, cx)?)
Self: Sized,
{
self.child.layout(view, cx)
} }
fn paint( fn paint(
&mut self, &mut self,
view: &mut V, bounds: Bounds<Pixels>,
parent_origin: Vector2F, state: &mut Self::State,
layout: &Layout, frame_state: &mut Self::FrameState,
paint_state: &mut Self::PaintState, cx: &mut ViewContext<Self::State>,
cx: &mut ViewContext<V>, ) -> Result<()> {
) where let pressed = bounds.contains_point(cx.mouse_position());
Self: Sized,
{
let slot = self.cascade_slot; let slot = self.cascade_slot;
let style = self.pressed.get().then_some(self.pressed_style.clone()); let style = pressed.then_some(self.pressed_style.clone());
self.style_cascade().set(slot, style); self.style_cascade().set(slot, style);
self.pressed.store(pressed, SeqCst);
let pressed = self.pressed.clone(); let hovered = self.pressed.clone();
let bounds = layout.bounds + parent_origin; cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
cx.on_event(layout.order, move |_view, event: &MouseButtonEvent, cx| { if phase == DispatchPhase::Capture {
if event.is_down { if bounds.contains_point(event.position) != hovered.load(SeqCst) {
if bounds.contains_point(event.position) { cx.notify();
pressed.set(true); }
cx.repaint(); }
});
cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| {
if phase == DispatchPhase::Capture {
if bounds.contains_point(event.position) != hovered.load(SeqCst) {
cx.notify();
} }
} else if pressed.get() {
pressed.set(false);
cx.repaint();
} }
}); });
self.child self.child.paint(bounds, state, frame_state, cx)?;
.paint(view, parent_origin, layout, paint_state, cx); Ok(())
} }
} }
impl<V: 'static, E: Interactive<V> + Styleable> Interactive<V> for Pressable<E> { impl<E: ParentElement + Styled> ParentElement for Pressable<E> {
fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V> { type State = E::State;
self.child.interaction_handlers()
}
}
impl<V: 'static, E: ParentElement<V> + Styleable> ParentElement<V> for Pressable<E> { fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
self.child.children_mut() self.child.children_mut()
} }
} }
impl<V: 'static, E: Element<V> + Styleable> IntoElement<V> for Pressable<E> { // use crate::{
type Element = Self; // element::{AnyElement, Element, IntoElement, Layout, ParentElement},
// interactive::{InteractionHandlers, Interactive},
// style::{Style, StyleHelpers, Styleable},
// ViewContext,
// };
// use anyhow::Result;
// use gpui::{geometry::vector::Vector2F, platform::MouseButtonEvent, LayoutId};
// use refineable::{CascadeSlot, Refineable, RefinementCascade};
// use smallvec::SmallVec;
// use std::{cell::Cell, rc::Rc};
fn into_element(self) -> Self::Element { // pub struct Pressable<E: Styleable> {
self // pressed: Rc<Cell<bool>>,
} // pressed_style: <E::Style as Refineable>::Refinement,
} // cascade_slot: CascadeSlot,
// child: E,
// }
// pub fn pressable<E: Styleable>(mut child: E) -> Pressable<E> {
// Pressable {
// pressed: Rc::new(Cell::new(false)),
// pressed_style: Default::default(),
// cascade_slot: child.style_cascade().reserve(),
// child,
// }
// }
// impl<E: Styleable> Styleable for Pressable<E> {
// type Style = E::Style;
// fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
// &mut self.pressed_style
// }
// fn style_cascade(&mut self) -> &mut RefinementCascade<E::Style> {
// self.child.style_cascade()
// }
// }
// impl<V: 'static, E: Element<V> + Styleable> Element<V> for Pressable<E> {
// type PaintState = E::PaintState;
// fn layout(
// &mut self,
// view: &mut V,
// cx: &mut ViewContext<V>,
// ) -> Result<(LayoutId, Self::PaintState)>
// where
// Self: Sized,
// {
// self.child.layout(view, cx)
// }
// fn paint(
// &mut self,
// view: &mut V,
// parent_origin: Vector2F,
// layout: &Layout,
// paint_state: &mut Self::PaintState,
// cx: &mut ViewContext<V>,
// ) where
// Self: Sized,
// {
// let slot = self.cascade_slot;
// let style = self.pressed.get().then_some(self.pressed_style.clone());
// self.style_cascade().set(slot, style);
// let pressed = self.pressed.clone();
// let bounds = layout.bounds + parent_origin;
// cx.on_event(layout.order, move |_view, event: &MouseButtonEvent, cx| {
// if event.is_down {
// if bounds.contains_point(event.position) {
// pressed.set(true);
// cx.repaint();
// }
// } else if pressed.get() {
// pressed.set(false);
// cx.repaint();
// }
// });
// self.child
// .paint(view, parent_origin, layout, paint_state, cx);
// }
// }
// impl<V: 'static, E: Interactive<V> + Styleable> Interactive<V> for Pressable<E> {
// fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V> {
// self.child.interaction_handlers()
// }
// }
// impl<V: 'static, E: ParentElement<V> + Styleable> ParentElement<V> for Pressable<E> {
// fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
// self.child.children_mut()
// }
// }
// impl<V: 'static, E: Element<V> + Styleable> IntoElement<V> for Pressable<E> {
// type Element = Self;
// fn into_element(self) -> Self::Element {
// self
// }
// }