Eliminate ElementStateContext trait

We now always have a RenderContext when rendering MouseEventHandlers or scrollable Flex columns/rows.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
Nathan Sobo 2022-05-26 18:37:28 -06:00
parent b6b16fc9c3
commit bd62a68234
4 changed files with 32 additions and 54 deletions

View file

@ -127,26 +127,6 @@ pub trait UpdateView {
T: View; T: View;
} }
pub trait ElementStateContext: DerefMut<Target = MutableAppContext> {
fn current_view_id(&self) -> usize;
fn element_state<Tag: 'static, T: 'static + Default>(
&mut self,
element_id: usize,
) -> ElementStateHandle<T> {
let id = ElementStateId {
view_id: self.current_view_id(),
element_id,
tag: TypeId::of::<Tag>(),
};
self.cx
.element_states
.entry(id)
.or_insert_with(|| Box::new(T::default()));
ElementStateHandle::new(id, self.frame_count, &self.cx.ref_counts)
}
}
pub struct Menu<'a> { pub struct Menu<'a> {
pub name: &'a str, pub name: &'a str,
pub items: Vec<MenuItem<'a>>, pub items: Vec<MenuItem<'a>>,
@ -3444,7 +3424,7 @@ pub struct MouseState {
pub clicked: bool, pub clicked: bool,
} }
impl<'a, T: View> RenderContext<'a, T> { impl<'a, V: View> RenderContext<'a, V> {
fn new(params: RenderParams, app: &'a mut MutableAppContext) -> Self { fn new(params: RenderParams, app: &'a mut MutableAppContext) -> Self {
Self { Self {
app, app,
@ -3458,7 +3438,7 @@ impl<'a, T: View> RenderContext<'a, T> {
} }
} }
pub fn handle(&self) -> WeakViewHandle<T> { pub fn handle(&self) -> WeakViewHandle<V> {
WeakViewHandle::new(self.window_id, self.view_id) WeakViewHandle::new(self.window_id, self.view_id)
} }
@ -3477,6 +3457,22 @@ impl<'a, T: View> RenderContext<'a, T> {
clicked: self.clicked_region_id == region_id, clicked: self.clicked_region_id == region_id,
} }
} }
pub fn element_state<Tag: 'static, T: 'static + Default>(
&mut self,
element_id: usize,
) -> ElementStateHandle<T> {
let id = ElementStateId {
view_id: self.view_id(),
element_id,
tag: TypeId::of::<Tag>(),
};
self.cx
.element_states
.entry(id)
.or_insert_with(|| Box::new(T::default()));
ElementStateHandle::new(id, self.frame_count, &self.cx.ref_counts)
}
} }
impl AsRef<AppContext> for &AppContext { impl AsRef<AppContext> for &AppContext {
@ -3521,12 +3517,6 @@ impl<V: View> ReadView for RenderContext<'_, V> {
} }
} }
impl<V: View> ElementStateContext for RenderContext<'_, V> {
fn current_view_id(&self) -> usize {
self.view_id
}
}
impl<M> AsRef<AppContext> for ViewContext<'_, M> { impl<M> AsRef<AppContext> for ViewContext<'_, M> {
fn as_ref(&self) -> &AppContext { fn as_ref(&self) -> &AppContext {
&self.app.cx &self.app.cx
@ -3625,12 +3615,6 @@ impl<V: View> UpdateView for ViewContext<'_, V> {
} }
} }
impl<V: View> ElementStateContext for ViewContext<'_, V> {
fn current_view_id(&self) -> usize {
self.view_id
}
}
pub trait Handle<T> { pub trait Handle<T> {
type Weak: 'static; type Weak: 'static;
fn id(&self) -> usize; fn id(&self) -> usize;

View file

@ -2,8 +2,8 @@ use std::{any::Any, f32::INFINITY};
use crate::{ use crate::{
json::{self, ToJson, Value}, json::{self, ToJson, Value},
Axis, DebugContext, Element, ElementBox, ElementStateContext, ElementStateHandle, Event, Axis, DebugContext, Element, ElementBox, ElementStateHandle, Event, EventContext,
EventContext, LayoutContext, PaintContext, SizeConstraint, Vector2FExt, LayoutContext, PaintContext, RenderContext, SizeConstraint, Vector2FExt, View,
}; };
use pathfinder_geometry::{ use pathfinder_geometry::{
rect::RectF, rect::RectF,
@ -40,15 +40,15 @@ impl Flex {
Self::new(Axis::Vertical) Self::new(Axis::Vertical)
} }
pub fn scrollable<Tag, C>( pub fn scrollable<Tag, V>(
mut self, mut self,
element_id: usize, element_id: usize,
scroll_to: Option<usize>, scroll_to: Option<usize>,
cx: &mut C, cx: &mut RenderContext<V>,
) -> Self ) -> Self
where where
Tag: 'static, Tag: 'static,
C: ElementStateContext, V: View,
{ {
let scroll_state = cx.element_state::<Tag, ScrollState>(element_id); let scroll_state = cx.element_state::<Tag, ScrollState>(element_id);
scroll_state.update(cx, |scroll_state, _| scroll_state.scroll_to = scroll_to); scroll_state.update(cx, |scroll_state, _| scroll_state.scroll_to = scroll_to);

View file

@ -6,8 +6,8 @@ use crate::{
}, },
platform::CursorStyle, platform::CursorStyle,
scene::CursorRegion, scene::CursorRegion,
DebugContext, Element, ElementBox, ElementStateContext, ElementStateHandle, Event, DebugContext, Element, ElementBox, ElementStateHandle, Event, EventContext, LayoutContext,
EventContext, LayoutContext, PaintContext, SizeConstraint, PaintContext, RenderContext, SizeConstraint, View,
}; };
use serde_json::json; use serde_json::json;
@ -29,11 +29,11 @@ pub struct MouseState {
} }
impl MouseEventHandler { impl MouseEventHandler {
pub fn new<Tag, C, F>(id: usize, cx: &mut C, render_child: F) -> Self pub fn new<Tag, V, F>(id: usize, cx: &mut RenderContext<V>, render_child: F) -> Self
where where
Tag: 'static, Tag: 'static,
C: ElementStateContext, V: View,
F: FnOnce(&MouseState, &mut C) -> ElementBox, F: FnOnce(&MouseState, &mut RenderContext<V>) -> ElementBox,
{ {
let state_handle = cx.element_state::<Tag, _>(id); let state_handle = cx.element_state::<Tag, _>(id);
let child = state_handle.update(cx, |state, cx| render_child(state, cx)); let child = state_handle.update(cx, |state, cx| render_child(state, cx));

View file

@ -7,10 +7,10 @@ use crate::{
platform::{CursorStyle, Event}, platform::{CursorStyle, Event},
scene::CursorRegion, scene::CursorRegion,
text_layout::TextLayoutCache, text_layout::TextLayoutCache,
Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AssetCache, ElementBox, Action, AnyModelHandle, AnyViewHandle, AnyWeakModelHandle, AssetCache, ElementBox, Entity,
ElementStateContext, Entity, FontSystem, ModelHandle, MouseRegion, MouseRegionId, ReadModel, FontSystem, ModelHandle, MouseRegion, MouseRegionId, ReadModel, ReadView, RenderContext,
ReadView, RenderContext, RenderParams, Scene, UpgradeModelHandle, UpgradeViewHandle, View, RenderParams, Scene, UpgradeModelHandle, UpgradeViewHandle, View, ViewHandle, WeakModelHandle,
ViewHandle, WeakModelHandle, WeakViewHandle, WeakViewHandle,
}; };
use pathfinder_geometry::vector::{vec2f, Vector2F}; use pathfinder_geometry::vector::{vec2f, Vector2F};
use serde_json::json; use serde_json::json;
@ -444,12 +444,6 @@ impl<'a> UpgradeViewHandle for LayoutContext<'a> {
} }
} }
impl<'a> ElementStateContext for LayoutContext<'a> {
fn current_view_id(&self) -> usize {
*self.view_stack.last().unwrap()
}
}
pub struct PaintContext<'a> { pub struct PaintContext<'a> {
rendered_views: &'a mut HashMap<usize, ElementBox>, rendered_views: &'a mut HashMap<usize, ElementBox>,
pub scene: &'a mut Scene, pub scene: &'a mut Scene,