Simplify contexts

This commit is contained in:
Antonio Scandurra 2023-11-02 09:15:14 +01:00
parent ff27700913
commit 18fcb41292
8 changed files with 44 additions and 92 deletions

View file

@ -753,8 +753,6 @@ impl AppContext {
}
impl Context for AppContext {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
/// Build an entity that is owned by the application. The given function will be invoked with
@ -762,7 +760,7 @@ impl Context for AppContext {
/// which can be used to access the entity in a context.
fn build_model<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Model<T> {
self.update(|cx| {
let slot = cx.entities.reserve();
@ -776,7 +774,7 @@ impl Context for AppContext {
fn update_model<T: 'static, R>(
&mut self,
model: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> R {
self.update(|cx| {
let mut entity = cx.entities.lease(model);
@ -788,7 +786,7 @@ impl Context for AppContext {
fn update_window<T, F>(&mut self, handle: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
self.update(|cx| {
let mut window = cx

View file

@ -14,13 +14,11 @@ pub struct AsyncAppContext {
}
impl Context for AsyncAppContext {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = Result<T>;
fn build_model<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Self::Result<Model<T>>
where
T: 'static,
@ -36,7 +34,7 @@ impl Context for AsyncAppContext {
fn update_model<T: 'static, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> Self::Result<R> {
let app = self
.app
@ -48,7 +46,7 @@ impl Context for AsyncAppContext {
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
let app = self.app.upgrade().context("app was released")?;
let mut lock = app.borrow_mut();
@ -200,14 +198,11 @@ impl AsyncWindowContext {
}
impl Context for AsyncWindowContext {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = Result<T>;
fn build_model<T>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Result<Model<T>>
where
T: 'static,
@ -219,7 +214,7 @@ impl Context for AsyncWindowContext {
fn update_model<T: 'static, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> Result<R> {
self.window
.update(self, |_, cx| cx.update_model(handle, update))
@ -227,18 +222,16 @@ impl Context for AsyncWindowContext {
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
self.app.update_window(window, update)
}
}
impl VisualContext for AsyncWindowContext {
type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
fn build_view<V>(
&mut self,
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static,
@ -250,7 +243,7 @@ impl VisualContext for AsyncWindowContext {
fn update_view<V: 'static, R>(
&mut self,
view: &View<V>,
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R,
update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
) -> Self::Result<R> {
self.window
.update(self, |_, cx| cx.update_view(view, update))
@ -258,7 +251,7 @@ impl VisualContext for AsyncWindowContext {
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send + Render,

View file

@ -1,4 +1,4 @@
use crate::{private::Sealed, AnyBox, AppContext, Context, Entity};
use crate::{private::Sealed, AnyBox, AppContext, Context, Entity, ModelContext};
use anyhow::{anyhow, Result};
use derive_more::{Deref, DerefMut};
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
@ -334,7 +334,7 @@ impl<T: 'static> Model<T> {
pub fn update<C, R>(
&self,
cx: &mut C,
update: impl FnOnce(&mut T, &mut C::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> C::Result<R>
where
C: Context,
@ -480,7 +480,7 @@ impl<T: 'static> WeakModel<T> {
pub fn update<C, R>(
&self,
cx: &mut C,
update: impl FnOnce(&mut T, &mut C::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> Result<R>
where
C: Context,

View file

@ -213,13 +213,11 @@ where
}
impl<'a, T> Context for ModelContext<'a, T> {
type WindowContext<'b> = WindowContext<'b>;
type ModelContext<'b, U> = ModelContext<'b, U>;
type Result<U> = U;
fn build_model<U: 'static>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, U>) -> U,
build_model: impl FnOnce(&mut ModelContext<'_, U>) -> U,
) -> Model<U> {
self.app.build_model(build_model)
}
@ -227,14 +225,14 @@ impl<'a, T> Context for ModelContext<'a, T> {
fn update_model<U: 'static, R>(
&mut self,
handle: &Model<U>,
update: impl FnOnce(&mut U, &mut Self::ModelContext<'_, U>) -> R,
update: impl FnOnce(&mut U, &mut ModelContext<'_, U>) -> R,
) -> R {
self.app.update_model(handle, update)
}
fn update_window<R, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<R>
where
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> R,
F: FnOnce(AnyView, &mut WindowContext<'_>) -> R,
{
self.app.update_window(window, update)
}

View file

@ -15,13 +15,11 @@ pub struct TestAppContext {
}
impl Context for TestAppContext {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
fn build_model<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Self::Result<Model<T>>
where
T: 'static,
@ -33,7 +31,7 @@ impl Context for TestAppContext {
fn update_model<T: 'static, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> Self::Result<R> {
let mut app = self.app.borrow_mut();
app.update_model(handle, update)
@ -41,7 +39,7 @@ impl Context for TestAppContext {
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
let mut lock = self.app.borrow_mut();
lock.update_window(window, f)

View file

@ -74,34 +74,30 @@ use taffy::TaffyLayoutEngine;
type AnyBox = Box<dyn Any>;
pub trait Context {
type WindowContext<'a>: UpdateView;
type ModelContext<'a, T>;
type Result<T>;
fn build_model<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Self::Result<Model<T>>;
fn update_model<T, R>(
&mut self,
handle: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> Self::Result<R>
where
T: 'static;
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T;
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
}
pub trait VisualContext: Context {
type ViewContext<'a, V: 'static>;
fn build_view<V>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static;
@ -109,27 +105,17 @@ pub trait VisualContext: Context {
fn update_view<V: 'static, R>(
&mut self,
view: &View<V>,
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R,
update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
) -> Self::Result<R>;
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send + Render;
}
pub trait UpdateView {
type ViewContext<'a, V: 'static>;
fn update_view<V: 'static, R>(
&mut self,
view: &View<V>,
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R,
) -> R;
}
pub trait Entity<T>: Sealed {
type Weak: 'static + Send;

View file

@ -53,7 +53,7 @@ impl<V: 'static> View<V> {
pub fn update<C, R>(
&self,
cx: &mut C,
f: impl FnOnce(&mut V, &mut C::ViewContext<'_, V>) -> R,
f: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
) -> C::Result<R>
where
C: VisualContext,
@ -152,7 +152,7 @@ impl<V: 'static> WeakView<V> {
pub fn update<C, R>(
&self,
cx: &mut C,
f: impl FnOnce(&mut V, &mut C::ViewContext<'_, V>) -> R,
f: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
) -> Result<R>
where
C: VisualContext,

View file

@ -7,7 +7,7 @@ use crate::{
MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Quad,
Render, RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder,
Shadow, SharedString, Size, Style, Subscription, TaffyLayoutEngine, Task, Underline,
UnderlineStyle, UpdateView, View, VisualContext, WeakView, WindowOptions, SUBPIXEL_VARIANTS,
UnderlineStyle, View, VisualContext, WeakView, WindowOptions, SUBPIXEL_VARIANTS,
};
use anyhow::{anyhow, Result};
use collections::HashMap;
@ -1233,13 +1233,11 @@ impl<'a> WindowContext<'a> {
}
impl Context for WindowContext<'_> {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'a, T> = ModelContext<'a, T>;
type Result<T> = T;
fn build_model<T>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Model<T>
where
T: 'static,
@ -1252,7 +1250,7 @@ impl Context for WindowContext<'_> {
fn update_model<T: 'static, R>(
&mut self,
model: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> R {
let mut entity = self.entities.lease(model);
let result = update(
@ -1265,7 +1263,7 @@ impl Context for WindowContext<'_> {
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
if window == self.window.handle {
let root_view = self.window.root_view.clone().unwrap();
@ -1277,11 +1275,9 @@ impl Context for WindowContext<'_> {
}
impl VisualContext for WindowContext<'_> {
type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
fn build_view<V>(
&mut self,
build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static,
@ -1300,7 +1296,7 @@ impl VisualContext for WindowContext<'_> {
fn update_view<T: 'static, R>(
&mut self,
view: &View<T>,
update: impl FnOnce(&mut T, &mut Self::ViewContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ViewContext<'_, T>) -> R,
) -> Self::Result<R> {
let mut lease = self.app.entities.lease(&view.model);
let mut cx = ViewContext::new(&mut *self.app, &mut *self.window, &view);
@ -1311,7 +1307,7 @@ impl VisualContext for WindowContext<'_> {
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static + Send + Render,
@ -1328,18 +1324,6 @@ impl VisualContext for WindowContext<'_> {
}
}
impl UpdateView for WindowContext<'_> {
type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
fn update_view<V: 'static, R>(
&mut self,
view: &View<V>,
update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R,
) -> R {
VisualContext::update_view(self, view, update)
}
}
impl<'a> std::ops::Deref for WindowContext<'a> {
type Target = AppContext;
@ -1882,13 +1866,11 @@ where
}
impl<V> Context for ViewContext<'_, V> {
type WindowContext<'a> = WindowContext<'a>;
type ModelContext<'b, U> = ModelContext<'b, U>;
type Result<U> = U;
fn build_model<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T,
build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
) -> Model<T> {
self.window_cx.build_model(build_model)
}
@ -1896,25 +1878,23 @@ impl<V> Context for ViewContext<'_, V> {
fn update_model<T: 'static, R>(
&mut self,
model: &Model<T>,
update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
) -> R {
self.window_cx.update_model(model, update)
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
where
F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
{
self.window_cx.update_window(window, update)
}
}
impl<V: 'static> VisualContext for ViewContext<'_, V> {
type ViewContext<'a, W: 'static> = ViewContext<'a, W>;
fn build_view<W: 'static>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, W>) -> W,
build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>> {
self.window_cx.build_view(build_view)
}
@ -1922,14 +1902,14 @@ impl<V: 'static> VisualContext for ViewContext<'_, V> {
fn update_view<V2: 'static, R>(
&mut self,
view: &View<V2>,
update: impl FnOnce(&mut V2, &mut Self::ViewContext<'_, V2>) -> R,
update: impl FnOnce(&mut V2, &mut ViewContext<'_, V2>) -> R,
) -> Self::Result<R> {
VisualContext::update_view(&mut self.window_cx, view, update)
}
fn replace_root_view<W>(
&mut self,
build_view: impl FnOnce(&mut Self::ViewContext<'_, W>) -> W,
build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>>
where
W: 'static + Send + Render,
@ -1983,7 +1963,7 @@ impl<V: 'static + Render> WindowHandle<V> {
pub fn update<C, R>(
self,
cx: &mut C,
update: impl FnOnce(&mut V, &mut <C::WindowContext<'_> as UpdateView>::ViewContext<'_, V>) -> R,
update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
) -> Result<R>
where
C: Context,
@ -1992,7 +1972,6 @@ impl<V: 'static + Render> WindowHandle<V> {
let view = root_view
.downcast::<V>()
.map_err(|_| anyhow!("the type of the window's root view has changed"))?;
Ok(cx.update_view(&view, update))
})?
}
@ -2054,7 +2033,7 @@ impl AnyWindowHandle {
pub fn update<C, R>(
self,
cx: &mut C,
update: impl FnOnce(AnyView, &mut C::WindowContext<'_>) -> R,
update: impl FnOnce(AnyView, &mut WindowContext<'_>) -> R,
) -> Result<R>
where
C: Context,