WIP: Pass in V to Element

This commit is contained in:
Marshall Bowers 2023-10-26 09:45:26 +02:00
parent 9c10152c89
commit 965bfd8439
7 changed files with 87 additions and 143 deletions

View file

@ -3,36 +3,35 @@ use derive_more::{Deref, DerefMut};
pub(crate) use smallvec::SmallVec;
use std::{any::Any, mem};
pub trait Element: IntoAnyElement<Self::ViewState> {
type ViewState: 'static;
pub trait Element<V: 'static>: IntoAnyElement<V> {
type ElementState: 'static;
fn id(&self) -> Option<ElementId>;
fn initialize(
&mut self,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState;
// where
// Self::ViewState: Any + Send + Sync;
// V: Any + Send + Sync;
fn layout(
&mut self,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> LayoutId;
// where
// Self::ViewState: Any + Send + Sync;
// V: Any + Send + Sync;
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
);
// where
@ -42,10 +41,10 @@ pub trait Element: IntoAnyElement<Self::ViewState> {
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
pub trait ParentElement: Element {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]>;
pub trait ParentElement<V>: Element<V> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]>;
fn child(mut self, child: impl IntoAnyElement<Self::ViewState>) -> Self
fn child(mut self, child: impl IntoAnyElement<V>) -> Self
where
Self: Sized,
{
@ -53,10 +52,7 @@ pub trait ParentElement: Element {
self
}
fn children(
mut self,
iter: impl IntoIterator<Item = impl IntoAnyElement<Self::ViewState>>,
) -> Self
fn children(mut self, iter: impl IntoIterator<Item = impl IntoAnyElement<V>>) -> Self
where
Self: Sized,
{
@ -72,7 +68,7 @@ trait ElementObject<V> {
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
}
struct RenderedElement<E: Element> {
struct RenderedElement<V, E: Element<V>> {
element: E,
phase: ElementRenderPhase<E::ElementState>,
}
@ -94,7 +90,7 @@ enum ElementRenderPhase<V> {
/// Internal struct that wraps an element to store Layout and ElementState after the element is rendered.
/// It's allocated as a trait object to erase the element type and wrapped in AnyElement<E::State> for
/// improved usability.
impl<E: Element> RenderedElement<E> {
impl<V, E: Element<V>> RenderedElement<V, E> {
fn new(element: E) -> Self {
RenderedElement {
element,
@ -103,13 +99,13 @@ impl<E: Element> RenderedElement<E> {
}
}
impl<E> ElementObject<E::ViewState> for RenderedElement<E>
impl<V, E> ElementObject<V> for RenderedElement<V, E>
where
E: Element,
E: Element<V>,
// E::ViewState: Any + Send + Sync,
E::ElementState: Any + Send + Sync,
{
fn initialize(&mut self, view_state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) {
fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
let frame_state = if let Some(id) = self.element.id() {
cx.with_element_state(id, |element_state, cx| {
let element_state = self.element.initialize(view_state, element_state, cx);
@ -124,7 +120,7 @@ where
self.phase = ElementRenderPhase::Initialized { frame_state };
}
fn layout(&mut self, state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) -> LayoutId {
fn layout(&mut self, state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
let layout_id;
let mut frame_state;
match mem::take(&mut self.phase) {
@ -154,7 +150,7 @@ where
layout_id
}
fn paint(&mut self, view_state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) {
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
self.phase = match mem::take(&mut self.phase) {
ElementRenderPhase::LayoutRequested {
layout_id,
@ -186,7 +182,7 @@ impl<V> AnyElement<V> {
pub fn new<E>(element: E) -> Self
where
E: 'static + Send + Sync,
E: Element<ViewState = V>,
E: Element<V>,
E::ElementState: Any + Send + Sync,
{
AnyElement(Box::new(RenderedElement::new(element)))

View file

@ -189,12 +189,11 @@ pub struct DivState {
child_layout_ids: SmallVec<[LayoutId; 4]>,
}
impl<V, I, F> Element for Div<V, I, F>
impl<V, I, F> Element<V> for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<ElementId> {
@ -205,9 +204,9 @@ where
fn initialize(
&mut self,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState {
let mut element_state = element_state.unwrap_or_default();
self.focus
@ -224,9 +223,9 @@ where
fn layout(
&mut self,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> LayoutId {
let style = self.compute_style(Bounds::default(), element_state, cx);
style.apply_text_style(cx, |cx| {
@ -245,9 +244,9 @@ where
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view_state: &mut Self::ViewState,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) {
self.with_element_id(cx, |this, _global_id, cx| {
if let Some(group) = this.group.clone() {
@ -315,12 +314,12 @@ where
}
}
impl<V, I, F> ParentElement for Div<V, I, F>
impl<V, I, F> ParentElement<V> for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
&mut self.children
}
}
@ -335,7 +334,7 @@ where
}
}
impl<V, I, F> StatelessInteractive for Div<V, I, F>
impl<V, I, F> StatelessInteractive<V> for Div<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
@ -345,11 +344,11 @@ where
}
}
impl<V, F> StatefulInteractive for Div<V, StatefulInteraction<V>, F>
impl<V, F> StatefulInteractive<V> for Div<V, StatefulInteraction<V>, F>
where
F: ElementFocus<V>,
{
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V> {
&mut self.interaction
}
}

View file

@ -65,12 +65,11 @@ where
}
}
impl<V, I, F> Element for Img<V, I, F>
impl<V, I, F> Element<V> for Img<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<crate::ElementId> {
@ -90,7 +89,7 @@ where
&mut self,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> LayoutId {
self.base.layout(view_state, element_state, cx)
}
@ -143,7 +142,7 @@ where
}
}
impl<V, I, F> StatelessInteractive for Img<V, I, F>
impl<V, I, F> StatelessInteractive<V> for Img<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
@ -153,11 +152,11 @@ where
}
}
impl<V, F> StatefulInteractive for Img<V, StatefulInteraction<V>, F>
impl<V, F> StatefulInteractive<V> for Img<V, StatefulInteraction<V>, F>
where
F: ElementFocus<V>,
{
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V> {
self.base.stateful_interaction()
}
}
@ -167,7 +166,7 @@ where
V: 'static,
I: ElementInteraction<V>,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V> {
self.base.focus_listeners()
}

View file

@ -55,12 +55,11 @@ where
}
}
impl<V, I, F> Element for Svg<V, I, F>
impl<V, I, F> Element<V> for Svg<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
{
type ViewState = V;
type ElementState = DivState;
fn id(&self) -> Option<crate::ElementId> {
@ -80,7 +79,7 @@ where
&mut self,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<V>,
) -> LayoutId {
self.base.layout(view_state, element_state, cx)
}
@ -88,7 +87,7 @@ where
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view: &mut Self::ViewState,
view: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) where
@ -116,7 +115,7 @@ where
}
}
impl<V, I, F> StatelessInteractive for Svg<V, I, F>
impl<V, I, F> StatelessInteractive<V> for Svg<V, I, F>
where
I: ElementInteraction<V>,
F: ElementFocus<V>,
@ -126,12 +125,12 @@ where
}
}
impl<V, F> StatefulInteractive for Svg<V, StatefulInteraction<V>, F>
impl<V, F> StatefulInteractive<V> for Svg<V, StatefulInteraction<V>, F>
where
V: 'static,
F: ElementFocus<V>,
{
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V> {
self.base.stateful_interaction()
}
}
@ -140,7 +139,7 @@ impl<V: 'static, I> Focusable for Svg<V, I, FocusEnabled<V>>
where
I: ElementInteraction<V>,
{
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V> {
self.base.focus_listeners()
}

View file

@ -11,8 +11,8 @@ pub type FocusListeners<V> = SmallVec<[FocusListener<V>; 2]>;
pub type FocusListener<V> =
Arc<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static>;
pub trait Focusable: Element {
fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState>;
pub trait Focusable<V>: Element<V> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V>;
fn set_focus_style(&mut self, style: StyleRefinement);
fn set_focus_in_style(&mut self, style: StyleRefinement);
fn set_in_focus_style(&mut self, style: StyleRefinement);
@ -62,10 +62,7 @@ pub trait Focusable: Element {
fn on_blur(
mut self,
listener: impl Fn(&mut Self::ViewState, &FocusEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -81,10 +78,7 @@ pub trait Focusable: Element {
fn on_focus_in(
mut self,
listener: impl Fn(&mut Self::ViewState, &FocusEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -109,10 +103,7 @@ pub trait Focusable: Element {
fn on_focus_out(
mut self,
listener: impl Fn(&mut Self::ViewState, &FocusEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,

View file

@ -19,8 +19,8 @@ use std::{
const DRAG_THRESHOLD: f64 = 2.;
pub trait StatelessInteractive: Element {
fn stateless_interaction(&mut self) -> &mut StatelessInteraction<Self::ViewState>;
pub trait StatelessInteractive<V>: Element<V> {
fn stateless_interaction(&mut self) -> &mut StatelessInteraction<V>;
fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
@ -48,10 +48,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_down(
mut self,
button: MouseButton,
handler: impl Fn(&mut Self::ViewState, &MouseDownEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -72,10 +69,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_up(
mut self,
button: MouseButton,
handler: impl Fn(&mut Self::ViewState, &MouseUpEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -96,10 +90,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_down_out(
mut self,
button: MouseButton,
handler: impl Fn(&mut Self::ViewState, &MouseDownEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -120,10 +111,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_up_out(
mut self,
button: MouseButton,
handler: impl Fn(&mut Self::ViewState, &MouseUpEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -143,10 +131,7 @@ pub trait StatelessInteractive: Element {
fn on_mouse_move(
mut self,
handler: impl Fn(&mut Self::ViewState, &MouseMoveEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -163,10 +148,7 @@ pub trait StatelessInteractive: Element {
fn on_scroll_wheel(
mut self,
handler: impl Fn(&mut Self::ViewState, &ScrollWheelEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -194,10 +176,7 @@ pub trait StatelessInteractive: Element {
fn on_action<A: 'static>(
mut self,
listener: impl Fn(&mut Self::ViewState, &A, DispatchPhase, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &A, DispatchPhase, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -215,12 +194,8 @@ pub trait StatelessInteractive: Element {
fn on_key_down(
mut self,
listener: impl Fn(
&mut Self::ViewState,
&KeyDownEvent,
DispatchPhase,
&mut ViewContext<Self::ViewState>,
) + Send
listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
) -> Self
@ -240,7 +215,7 @@ pub trait StatelessInteractive: Element {
fn on_key_up(
mut self,
listener: impl Fn(&mut Self::ViewState, &KeyUpEvent, DispatchPhase, &mut ViewContext<Self::ViewState>)
listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext<V>)
+ Send
+ Sync
+ 'static,
@ -289,10 +264,7 @@ pub trait StatelessInteractive: Element {
fn on_drop<S: 'static>(
mut self,
listener: impl Fn(&mut Self::ViewState, S, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, S, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -307,8 +279,8 @@ pub trait StatelessInteractive: Element {
}
}
pub trait StatefulInteractive: StatelessInteractive {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState>;
pub trait StatefulInteractive<V>: StatelessInteractive<V> {
fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V>;
fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
@ -335,10 +307,7 @@ pub trait StatefulInteractive: StatelessInteractive {
fn on_click(
mut self,
listener: impl Fn(&mut Self::ViewState, &ClickEvent, &mut ViewContext<Self::ViewState>)
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &ClickEvent, &mut ViewContext<V>) + Send + Sync + 'static,
) -> Self
where
Self: Sized,
@ -351,20 +320,14 @@ pub trait StatefulInteractive: StatelessInteractive {
fn on_drag<S, R, E>(
mut self,
listener: impl Fn(
&mut Self::ViewState,
&mut ViewContext<Self::ViewState>,
) -> Drag<S, R, Self::ViewState, E>
+ Send
+ Sync
+ 'static,
listener: impl Fn(&mut V, &mut ViewContext<V>) -> Drag<S, R, V, E> + Send + Sync + 'static,
) -> Self
where
Self: Sized,
S: Any + Send + Sync,
R: Fn(&mut Self::ViewState, &mut ViewContext<Self::ViewState>) -> E,
R: Fn(&mut V, &mut ViewContext<V>) -> E,
R: 'static + Send + Sync,
E: Element<ViewState = Self::ViewState>,
E: Element<V>,
{
debug_assert!(
self.stateful_interaction().drag_listener.is_none(),
@ -907,7 +870,7 @@ pub struct ClickEvent {
pub struct Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
E: Element<ViewState = V>,
E: Element<V>,
{
pub state: S,
pub render_drag_handle: R,
@ -917,7 +880,7 @@ where
impl<S, R, V, E> Drag<S, R, V, E>
where
R: Fn(&mut V, &mut ViewContext<V>) -> E,
E: Element<ViewState = V>,
E: Element<V>,
{
pub fn new(state: S, render_drag_handle: R) -> Self {
Drag {

View file

@ -50,8 +50,7 @@ impl<V: 'static, ParentViewState: 'static> IntoAnyElement<ParentViewState> for V
}
}
impl<V: 'static> Element for View<V> {
type ViewState = ();
impl<V: 'static> Element<()> for View<V> {
type ElementState = AnyElement<V>;
fn id(&self) -> Option<crate::ElementId> {
@ -105,8 +104,7 @@ impl<V: 'static, ParentV: 'static> IntoAnyElement<ParentV> for EraseViewState<V,
}
}
impl<V: 'static, ParentV: 'static> Element for EraseViewState<V, ParentV> {
type ViewState = ParentV;
impl<V: 'static, ParentV: 'static> Element<ParentV> for EraseViewState<V, ParentV> {
type ElementState = AnyBox;
fn id(&self) -> Option<crate::ElementId> {
@ -115,18 +113,18 @@ impl<V: 'static, ParentV: 'static> Element for EraseViewState<V, ParentV> {
fn initialize(
&mut self,
_: &mut Self::ViewState,
_: &mut ParentV,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) -> Self::ElementState {
ViewObject::initialize(&mut self.view, cx)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) -> LayoutId {
ViewObject::layout(&mut self.view, element, cx)
}
@ -134,9 +132,9 @@ impl<V: 'static, ParentV: 'static> Element for EraseViewState<V, ParentV> {
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::ViewState,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) {
ViewObject::paint(&mut self.view, bounds, element, cx)
}
@ -247,8 +245,7 @@ impl<ParentV: 'static> IntoAnyElement<ParentV> for EraseAnyViewState<ParentV> {
}
}
impl<ParentV: 'static> Element for EraseAnyViewState<ParentV> {
type ViewState = ParentV;
impl<ParentV: 'static> Element<ParentV> for EraseAnyViewState<ParentV> {
type ElementState = AnyBox;
fn id(&self) -> Option<crate::ElementId> {
@ -257,18 +254,18 @@ impl<ParentV: 'static> Element for EraseAnyViewState<ParentV> {
fn initialize(
&mut self,
_: &mut Self::ViewState,
_: &mut ParentV,
_: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) -> Self::ElementState {
self.view.view.lock().initialize(cx)
}
fn layout(
&mut self,
_: &mut Self::ViewState,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) -> LayoutId {
self.view.view.lock().layout(element, cx)
}
@ -276,9 +273,9 @@ impl<ParentV: 'static> Element for EraseAnyViewState<ParentV> {
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::ViewState,
_: &mut ParentV,
element: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
cx: &mut ViewContext<ParentV>,
) {
self.view.view.lock().paint(bounds, element, cx)
}