This commit is contained in:
Nathan Sobo
2023-10-10 22:49:47 -06:00
parent f53b63eaf6
commit f1cc62c21f
8 changed files with 96 additions and 42 deletions
+5 -2
View File
@@ -25,11 +25,14 @@ pub trait Element: 'static + Send + Sync {
cx: &mut ViewContext<Self::ViewState>,
);
fn id(self, id: ElementId) -> Identified<Self>
fn id(self, id: impl Into<ElementId>) -> Identified<Self>
where
Self: Sized,
{
Identified { element: self, id }
Identified {
element: self,
id: id.into(),
}
}
}
+26 -1
View File
@@ -1,4 +1,10 @@
use crate::{BorrowWindow, Bounds, Element, ElementId, LayoutId, StatefulElement, ViewContext};
use refineable::{Refineable, RefinementCascade};
use smallvec::SmallVec;
use crate::{
AnyElement, BorrowWindow, Bounds, Element, ElementId, LayoutId, ParentElement, StatefulElement,
Styled, ViewContext,
};
pub struct Identified<E> {
pub(crate) element: E,
@@ -36,3 +42,22 @@ impl<E: Element> Element for Identified<E> {
}
impl<E: Element> StatefulElement for Identified<E> {}
impl<E: Styled> Styled for Identified<E> {
type Style = E::Style;
fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> {
self.element.style_cascade()
}
fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
self.element.declared_style()
}
}
impl<E: ParentElement> ParentElement for Identified<E> {
type State = E::State;
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
self.element.children_mut()
}
}
+9 -5
View File
@@ -92,25 +92,29 @@ where
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) {
let slot = self.cascade_slot;
let style = element_state
.pressed
.load(SeqCst)
.then_some(self.pressed_style.clone());
let slot = self.cascade_slot;
self.style_cascade().set(slot, style);
let pressed = element_state.pressed.clone();
cx.on_mouse_event(move |_, event: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Capture {
if bounds.contains_point(event.position) != pressed.load(SeqCst) {
if bounds.contains_point(event.position) {
dbg!("pressed");
pressed.store(true, SeqCst);
cx.notify();
}
}
});
let hovered = element_state.pressed.clone();
cx.on_mouse_event(move |_, event: &MouseUpEvent, phase, cx| {
let pressed = element_state.pressed.clone();
cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
if phase == DispatchPhase::Capture {
if bounds.contains_point(event.position) != hovered.load(SeqCst) {
if pressed.load(SeqCst) {
dbg!("released");
pressed.store(false, SeqCst);
cx.notify();
}
}
+9 -7
View File
@@ -1,4 +1,4 @@
use crate::{Hoverable, Refineable, RefinementCascade};
use crate::{Hoverable, Pressable, Refineable, RefinementCascade};
pub trait Styled {
type Style: 'static + Refineable + Send + Sync + Default;
@@ -19,10 +19,12 @@ pub trait Styled {
Hoverable::new(self)
}
// fn active(self) -> Pressable<Self>
// where
// Self: Sized,
// {
// pressable(self)
// }
fn active(self) -> Pressable<Self>
where
Self: 'static + Sized + Send + Sync,
Self::Style: 'static + Refineable + Default + Send + Sync,
<Self::Style as Refineable>::Refinement: 'static + Default + Send + Sync,
{
Pressable::new(self)
}
}
+18
View File
@@ -1061,3 +1061,21 @@ impl From<SmallVec<[u32; 16]>> for StackingOrder {
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ElementId(ArcCow<'static, [u8]>);
impl From<usize> for ElementId {
fn from(id: usize) -> Self {
Self(id.to_ne_bytes().to_vec().into())
}
}
impl From<i32> for ElementId {
fn from(id: i32) -> Self {
Self(id.to_ne_bytes().to_vec().into())
}
}
impl From<&'static str> for ElementId {
fn from(id: &'static str) -> Self {
Self(id.into())
}
}