Checkpoint

This commit is contained in:
Antonio Scandurra 2023-10-19 22:28:42 +02:00
parent d446b91117
commit 0fbf84e6bc
4 changed files with 115 additions and 68 deletions

View File

@ -5,7 +5,7 @@ use crate::{
use derive_more::{Deref, DerefMut};
use refineable::Refineable;
pub(crate) use smallvec::SmallVec;
use std::mem;
use std::{marker::PhantomData, mem};
pub trait Element: 'static + Send + Sync + IntoAnyElement<Self::ViewState> {
type ViewState: 'static + Send + Sync;
@ -39,22 +39,66 @@ pub trait Element: 'static + Send + Sync + IntoAnyElement<Self::ViewState> {
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
pub trait ElementIdentity: 'static + Send + Sync {
fn id(&self) -> Option<ElementId>;
}
pub trait ElementInteractivity<V: 'static + Send + Sync>: 'static + Send + Sync {
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>>;
pub struct Identified(pub(crate) ElementId);
impl ElementIdentity for Identified {
fn id(&self) -> Option<ElementId> {
Some(self.0.clone())
fn initialize<R>(
&self,
cx: &mut ViewContext<V>,
f: impl FnOnce(Option<GlobalElementId>, &mut ViewContext<V>) -> R,
) -> R {
if let Some(identified) = self.as_stateful() {
cx.with_element_id(identified.id.clone(), |global_id, cx| {
f(Some(global_id), cx)
})
} else {
f(None, cx)
}
}
}
pub struct Anonymous;
#[derive(Deref, DerefMut)]
pub struct StatefulInteractivity<V: 'static + Send + Sync> {
pub id: ElementId,
#[deref]
#[deref_mut]
common: StatelessInteractivity<V>,
}
impl ElementIdentity for Anonymous {
fn id(&self) -> Option<ElementId> {
impl<V> ElementInteractivity<V> for StatefulInteractivity<V>
where
V: 'static + Send + Sync,
{
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>> {
Some(self)
}
}
impl<V> From<ElementId> for StatefulInteractivity<V>
where
V: 'static + Send + Sync,
{
fn from(id: ElementId) -> Self {
Self {
id,
common: StatelessInteractivity::default(),
}
}
}
pub struct StatelessInteractivity<V>(PhantomData<V>);
impl<V> Default for StatelessInteractivity<V> {
fn default() -> Self {
Self(PhantomData)
}
}
impl<V> ElementInteractivity<V> for StatelessInteractivity<V>
where
V: 'static + Send + Sync,
{
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>> {
None
}
}

View File

@ -1,9 +1,10 @@
use crate::{
Active, Anonymous, AnyElement, AppContext, BorrowWindow, Bounds, Click, DispatchPhase, Element,
ElementFocusability, ElementId, ElementIdentity, Focus, FocusHandle, FocusListeners, Focusable,
GlobalElementId, Hover, Identified, Interactive, Interactivity, IntoAnyElement, KeyDownEvent,
Active, AnyElement, AppContext, BorrowWindow, Bounds, Click, DispatchPhase, Element,
ElementFocusability, ElementId, ElementInteractivity, Focus, FocusHandle, FocusListeners,
Focusable, GlobalElementId, Hover, Interactive, Interactivity, IntoAnyElement, KeyDownEvent,
KeyMatch, LayoutId, MouseDownEvent, MouseMoveEvent, MouseUpEvent, NonFocusable, Overflow,
ParentElement, Pixels, Point, SharedString, Style, StyleRefinement, Styled, ViewContext,
ParentElement, Pixels, Point, SharedString, StatefulInteractivity, StatelessInteractivity,
Style, StyleRefinement, Styled, ViewContext,
};
use collections::HashMap;
use parking_lot::Mutex;
@ -62,7 +63,7 @@ impl ScrollState {
pub struct Div<
V: 'static + Send + Sync,
I: ElementIdentity = Anonymous,
I: ElementInteractivity<V> = StatelessInteractivity<V>,
F: ElementFocusability<V> = NonFocusable,
> {
identity: I,
@ -77,12 +78,12 @@ pub struct Div<
group_active: Option<GroupStyle>,
}
pub fn div<V>() -> Div<V, Anonymous, NonFocusable>
pub fn div<V>() -> Div<V, StatelessInteractivity<V>, NonFocusable>
where
V: 'static + Send + Sync,
{
Div {
identity: Anonymous,
identity: StatelessInteractivity::default(),
focusability: NonFocusable,
interactivity: Interactivity::default(),
children: SmallVec::new(),
@ -100,14 +101,14 @@ struct GroupStyle {
style: StyleRefinement,
}
impl<V, F> Div<V, Anonymous, F>
impl<V, F> Div<V, StatelessInteractivity<V>, F>
where
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
pub fn id(self, id: impl Into<ElementId>) -> Div<V, Identified, F> {
pub fn id(self, id: impl Into<ElementId>) -> Div<V, StatefulInteractivity<V>, F> {
Div {
identity: Identified(id.into()),
identity: id.into().into(),
focusability: self.focusability,
interactivity: self.interactivity,
children: self.children,
@ -123,7 +124,7 @@ where
impl<V, I, F> Div<V, I, F>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
@ -271,7 +272,7 @@ where
impl<V, I> Div<V, I, NonFocusable>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
V: 'static + Send + Sync,
{
pub fn focusable(self, handle: &FocusHandle) -> Div<V, I, Focusable<V>> {
@ -292,7 +293,7 @@ where
impl<V, I> Focus for Div<V, I, Focusable<V>>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
V: 'static + Send + Sync,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<V> {
@ -318,7 +319,7 @@ where
impl<V, I, F> Element for Div<V, I, F>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
@ -326,7 +327,9 @@ where
type ElementState = DivState;
fn id(&self) -> Option<ElementId> {
self.identity.id()
self.identity
.as_stateful()
.map(|identified| identified.id.clone())
}
fn initialize(
@ -456,7 +459,7 @@ where
impl<V, I, F> IntoAnyElement<V> for Div<V, I, F>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
@ -467,7 +470,7 @@ where
impl<V, I, F> ParentElement for Div<V, I, F>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
@ -478,7 +481,7 @@ where
impl<V, I, F> Styled for Div<V, I, F>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
@ -489,7 +492,7 @@ where
impl<V, I, F> Interactive for Div<V, I, F>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
@ -500,7 +503,7 @@ where
impl<V, I, F> Hover for Div<V, I, F>
where
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
@ -513,14 +516,14 @@ where
}
}
impl<V, F> Click for Div<V, Identified, F>
impl<V, F> Click for Div<V, StatefulInteractivity<V>, F>
where
F: ElementFocusability<V>,
V: 'static + Send + Sync,
{
}
impl<V, F> Active for Div<V, Identified, F>
impl<V, F> Active for Div<V, StatefulInteractivity<V>, F>
where
F: ElementFocusability<V>,
V: 'static + Send + Sync,

View File

@ -1,15 +1,15 @@
use crate::{
div, Active, Anonymous, AnyElement, BorrowWindow, Bounds, Click, Div, DivState, Element,
ElementFocusability, ElementId, ElementIdentity, Focus, FocusListeners, Focusable, Hover,
Identified, Interactive, Interactivity, IntoAnyElement, LayoutId, NonFocusable, Pixels,
SharedString, StyleRefinement, Styled, ViewContext,
div, Active, AnyElement, BorrowWindow, Bounds, Click, Div, DivState, Element,
ElementFocusability, ElementId, ElementInteractivity, Focus, FocusListeners, Focusable, Hover,
Interactive, Interactivity, IntoAnyElement, LayoutId, NonFocusable, Pixels, SharedString,
StatefulInteractivity, StatelessInteractivity, StyleRefinement, Styled, ViewContext,
};
use futures::FutureExt;
use util::ResultExt;
pub struct Img<
V: 'static + Send + Sync,
I: ElementIdentity = Anonymous,
I: ElementInteractivity<V> = StatelessInteractivity<V>,
F: ElementFocusability<V> = NonFocusable,
> {
base: Div<V, I, F>,
@ -17,7 +17,7 @@ pub struct Img<
grayscale: bool,
}
pub fn img<V>() -> Img<V, Anonymous, NonFocusable>
pub fn img<V>() -> Img<V, StatelessInteractivity<V>, NonFocusable>
where
V: 'static + Send + Sync,
{
@ -31,7 +31,7 @@ where
impl<V, I, F> Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
pub fn uri(mut self, uri: impl Into<SharedString>) -> Self {
@ -45,12 +45,12 @@ where
}
}
impl<V, F> Img<V, Anonymous, F>
impl<V, F> Img<V, StatelessInteractivity<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocusability<V>,
{
pub fn id(self, id: impl Into<ElementId>) -> Img<V, Identified, F> {
pub fn id(self, id: impl Into<ElementId>) -> Img<V, StatefulInteractivity<V>, F> {
Img {
base: self.base.id(id),
uri: self.uri,
@ -62,7 +62,7 @@ where
impl<V, I, F> IntoAnyElement<V> for Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
fn into_any(self) -> AnyElement<V> {
@ -73,7 +73,7 @@ where
impl<V, I, F> Element for Img<V, I, F>
where
V: Send + Sync + 'static,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
type ViewState = V;
@ -142,7 +142,7 @@ where
impl<V, I, F> Styled for Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
fn style(&mut self) -> &mut StyleRefinement {
@ -153,7 +153,7 @@ where
impl<V, I, F> Interactive for Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
fn interactivity(&mut self) -> &mut Interactivity<V> {
@ -164,7 +164,7 @@ where
impl<V, I, F> Hover for Img<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
fn set_hover_style(&mut self, group: Option<SharedString>, style: StyleRefinement) {
@ -172,14 +172,14 @@ where
}
}
impl<V, F> Click for Img<V, Identified, F>
impl<V, F> Click for Img<V, StatefulInteractivity<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocusability<V>,
{
}
impl<V, F> Active for Img<V, Identified, F>
impl<V, F> Active for Img<V, StatefulInteractivity<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocusability<V>,
@ -192,7 +192,7 @@ where
impl<V, I> Focus for Img<V, I, Focusable<V>>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
self.base.focus_listeners()

View File

@ -1,21 +1,21 @@
use crate::{
div, Active, Anonymous, AnyElement, Bounds, Click, Div, DivState, Element, ElementFocusability,
ElementId, ElementIdentity, Focus, FocusListeners, Focusable, Hover, Identified, Interactive,
Interactivity, IntoAnyElement, LayoutId, NonFocusable, Pixels, SharedString, StyleRefinement,
Styled, ViewContext,
div, Active, AnyElement, Bounds, Click, Div, DivState, Element, ElementFocusability, ElementId,
ElementInteractivity, Focus, FocusListeners, Focusable, Hover, Interactive, Interactivity,
IntoAnyElement, LayoutId, NonFocusable, Pixels, SharedString, StatefulInteractivity,
StatelessInteractivity, StyleRefinement, Styled, ViewContext,
};
use util::ResultExt;
pub struct Svg<
V: 'static + Send + Sync,
I: ElementIdentity = Anonymous,
I: ElementInteractivity<V> = StatelessInteractivity<V>,
F: ElementFocusability<V> = NonFocusable,
> {
base: Div<V, I, F>,
path: Option<SharedString>,
}
pub fn svg<V>() -> Svg<V, Anonymous, NonFocusable>
pub fn svg<V>() -> Svg<V, StatelessInteractivity<V>, NonFocusable>
where
V: 'static + Send + Sync,
{
@ -28,7 +28,7 @@ where
impl<V, I, F> Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
pub fn path(mut self, path: impl Into<SharedString>) -> Self {
@ -37,12 +37,12 @@ where
}
}
impl<V, F> Svg<V, Anonymous, F>
impl<V, F> Svg<V, StatelessInteractivity<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocusability<V>,
{
pub fn id(self, id: impl Into<ElementId>) -> Svg<V, Identified, F> {
pub fn id(self, id: impl Into<ElementId>) -> Svg<V, StatefulInteractivity<V>, F> {
Svg {
base: self.base.id(id),
path: self.path,
@ -53,7 +53,7 @@ where
impl<V, I, F> IntoAnyElement<V> for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
fn into_any(self) -> AnyElement<V> {
@ -64,7 +64,7 @@ where
impl<V, I, F> Element for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
type ViewState = V;
@ -116,7 +116,7 @@ where
impl<V, I, F> Styled for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
fn style(&mut self) -> &mut StyleRefinement {
@ -127,7 +127,7 @@ where
impl<V, I, F> Interactive for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
fn interactivity(&mut self) -> &mut Interactivity<V> {
@ -138,7 +138,7 @@ where
impl<V, I, F> Hover for Svg<V, I, F>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
F: ElementFocusability<V>,
{
fn set_hover_style(&mut self, group: Option<SharedString>, style: StyleRefinement) {
@ -146,14 +146,14 @@ where
}
}
impl<V, F> Click for Svg<V, Identified, F>
impl<V, F> Click for Svg<V, StatefulInteractivity<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocusability<V>,
{
}
impl<V, F> Active for Svg<V, Identified, F>
impl<V, F> Active for Svg<V, StatefulInteractivity<V>, F>
where
V: 'static + Send + Sync,
F: ElementFocusability<V>,
@ -166,7 +166,7 @@ where
impl<V, I> Focus for Svg<V, I, Focusable<V>>
where
V: 'static + Send + Sync,
I: ElementIdentity,
I: ElementInteractivity<V>,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
self.base.focus_listeners()