From 18fcb41292fe42532d15e0cfcea9c59db65b5685 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 2 Nov 2023 09:15:14 +0100 Subject: [PATCH] Simplify contexts --- crates/gpui2/src/app.rs | 8 ++--- crates/gpui2/src/app/async_context.rs | 25 +++++-------- crates/gpui2/src/app/entity_map.rs | 6 ++-- crates/gpui2/src/app/model_context.rs | 8 ++--- crates/gpui2/src/app/test_context.rs | 8 ++--- crates/gpui2/src/gpui2.rs | 26 ++++---------- crates/gpui2/src/view.rs | 4 +-- crates/gpui2/src/window.rs | 51 ++++++++------------------- 8 files changed, 44 insertions(+), 92 deletions(-) diff --git a/crates/gpui2/src/app.rs b/crates/gpui2/src/app.rs index f71619bc015de2db2eb5a43951f0d33a782246f9..f08c7fb86b3706a50ac71ba6203de578230343b3 100644 --- a/crates/gpui2/src/app.rs +++ b/crates/gpui2/src/app.rs @@ -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; /// 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( &mut self, - build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T, + build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T, ) -> Model { self.update(|cx| { let slot = cx.entities.reserve(); @@ -776,7 +774,7 @@ impl Context for AppContext { fn update_model( &mut self, model: &Model, - 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(&mut self, handle: AnyWindowHandle, update: F) -> Result where - F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T, + F: FnOnce(AnyView, &mut WindowContext<'_>) -> T, { self.update(|cx| { let mut window = cx diff --git a/crates/gpui2/src/app/async_context.rs b/crates/gpui2/src/app/async_context.rs index 08ae307d1b1ad8dc969dd5425b936b50686c13e1..823031b11a2100e3d590b365f1ebd608de5f5ff2 100644 --- a/crates/gpui2/src/app/async_context.rs +++ b/crates/gpui2/src/app/async_context.rs @@ -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 = Result; fn build_model( &mut self, - build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T, + build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T, ) -> Self::Result> where T: 'static, @@ -36,7 +34,7 @@ impl Context for AsyncAppContext { fn update_model( &mut self, handle: &Model, - update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R, + update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R, ) -> Self::Result { let app = self .app @@ -48,7 +46,7 @@ impl Context for AsyncAppContext { fn update_window(&mut self, window: AnyWindowHandle, f: F) -> Result 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 = Result; fn build_model( &mut self, - build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T, + build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T, ) -> Result> where T: 'static, @@ -219,7 +214,7 @@ impl Context for AsyncWindowContext { fn update_model( &mut self, handle: &Model, - update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R, + update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R, ) -> Result { self.window .update(self, |_, cx| cx.update_model(handle, update)) @@ -227,18 +222,16 @@ impl Context for AsyncWindowContext { fn update_window(&mut self, window: AnyWindowHandle, update: F) -> Result 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( &mut self, - build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V, + build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V, ) -> Self::Result> where V: 'static, @@ -250,7 +243,7 @@ impl VisualContext for AsyncWindowContext { fn update_view( &mut self, view: &View, - update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R, + update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R, ) -> Self::Result { self.window .update(self, |_, cx| cx.update_view(view, update)) @@ -258,7 +251,7 @@ impl VisualContext for AsyncWindowContext { fn replace_root_view( &mut self, - build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V, + build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V, ) -> Self::Result> where V: 'static + Send + Render, diff --git a/crates/gpui2/src/app/entity_map.rs b/crates/gpui2/src/app/entity_map.rs index 6faa85bac94ff6932c341a3577bf6cb4e0833b84..5b3462b91e8cd2f2b0df74c399e54e65ad8464d6 100644 --- a/crates/gpui2/src/app/entity_map.rs +++ b/crates/gpui2/src/app/entity_map.rs @@ -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 Model { pub fn update( &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 where C: Context, @@ -480,7 +480,7 @@ impl WeakModel { pub fn update( &self, cx: &mut C, - update: impl FnOnce(&mut T, &mut C::ModelContext<'_, T>) -> R, + update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R, ) -> Result where C: Context, diff --git a/crates/gpui2/src/app/model_context.rs b/crates/gpui2/src/app/model_context.rs index d72b03913925300cb1d915e61174d40ed1326532..cb25adfb63414a5111b0cd2a8d93b66742dc46a0 100644 --- a/crates/gpui2/src/app/model_context.rs +++ b/crates/gpui2/src/app/model_context.rs @@ -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; fn build_model( &mut self, - build_model: impl FnOnce(&mut Self::ModelContext<'_, U>) -> U, + build_model: impl FnOnce(&mut ModelContext<'_, U>) -> U, ) -> Model { self.app.build_model(build_model) } @@ -227,14 +225,14 @@ impl<'a, T> Context for ModelContext<'a, T> { fn update_model( &mut self, handle: &Model, - 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(&mut self, window: AnyWindowHandle, update: F) -> Result where - F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> R, + F: FnOnce(AnyView, &mut WindowContext<'_>) -> R, { self.app.update_window(window, update) } diff --git a/crates/gpui2/src/app/test_context.rs b/crates/gpui2/src/app/test_context.rs index ee1da50136e26fc121b2d0e805a0d047836497fe..27275d3a044c456bb2e4c00bf0b6995ff0330426 100644 --- a/crates/gpui2/src/app/test_context.rs +++ b/crates/gpui2/src/app/test_context.rs @@ -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; fn build_model( &mut self, - build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T, + build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T, ) -> Self::Result> where T: 'static, @@ -33,7 +31,7 @@ impl Context for TestAppContext { fn update_model( &mut self, handle: &Model, - update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R, + update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R, ) -> Self::Result { let mut app = self.app.borrow_mut(); app.update_model(handle, update) @@ -41,7 +39,7 @@ impl Context for TestAppContext { fn update_window(&mut self, window: AnyWindowHandle, f: F) -> Result 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) diff --git a/crates/gpui2/src/gpui2.rs b/crates/gpui2/src/gpui2.rs index 9801167583cb48d71845c656f0f9b9a28e71f816..bd65ade0d3f3d7fd2c842fb6657a583fee01e55b 100644 --- a/crates/gpui2/src/gpui2.rs +++ b/crates/gpui2/src/gpui2.rs @@ -74,34 +74,30 @@ use taffy::TaffyLayoutEngine; type AnyBox = Box; pub trait Context { - type WindowContext<'a>: UpdateView; - type ModelContext<'a, T>; type Result; fn build_model( &mut self, - build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T, + build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T, ) -> Self::Result>; fn update_model( &mut self, handle: &Model, - update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R, + update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R, ) -> Self::Result where T: 'static; fn update_window(&mut self, window: AnyWindowHandle, f: F) -> Result 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( &mut self, - build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V, + build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V, ) -> Self::Result> where V: 'static; @@ -109,27 +105,17 @@ pub trait VisualContext: Context { fn update_view( &mut self, view: &View, - update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R, + update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R, ) -> Self::Result; fn replace_root_view( &mut self, - build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V, + build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V, ) -> Self::Result> where V: 'static + Send + Render; } -pub trait UpdateView { - type ViewContext<'a, V: 'static>; - - fn update_view( - &mut self, - view: &View, - update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R, - ) -> R; -} - pub trait Entity: Sealed { type Weak: 'static + Send; diff --git a/crates/gpui2/src/view.rs b/crates/gpui2/src/view.rs index 82214b381d88c7d57f496cad191a4df8ef6cc307..165eedef9c1473959a0b93d6627da27327bc59c3 100644 --- a/crates/gpui2/src/view.rs +++ b/crates/gpui2/src/view.rs @@ -53,7 +53,7 @@ impl View { pub fn update( &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 where C: VisualContext, @@ -152,7 +152,7 @@ impl WeakView { pub fn update( &self, cx: &mut C, - f: impl FnOnce(&mut V, &mut C::ViewContext<'_, V>) -> R, + f: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R, ) -> Result where C: VisualContext, diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index f059e02a335a6a199fdbbebc6607c4e184d1c419..4435b0e9275f43d8dd20da5afc884b33f28e9cc8 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -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; fn build_model( &mut self, - build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T, + build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T, ) -> Model where T: 'static, @@ -1252,7 +1250,7 @@ impl Context for WindowContext<'_> { fn update_model( &mut self, model: &Model, - 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(&mut self, window: AnyWindowHandle, update: F) -> Result 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( &mut self, - build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V, + build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V, ) -> Self::Result> where V: 'static, @@ -1300,7 +1296,7 @@ impl VisualContext for WindowContext<'_> { fn update_view( &mut self, view: &View, - update: impl FnOnce(&mut T, &mut Self::ViewContext<'_, T>) -> R, + update: impl FnOnce(&mut T, &mut ViewContext<'_, T>) -> R, ) -> Self::Result { 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( &mut self, - build_view: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V, + build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V, ) -> Self::Result> 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( - &mut self, - view: &View, - 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 Context for ViewContext<'_, V> { - type WindowContext<'a> = WindowContext<'a>; - type ModelContext<'b, U> = ModelContext<'b, U>; type Result = U; fn build_model( &mut self, - build_model: impl FnOnce(&mut Self::ModelContext<'_, T>) -> T, + build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T, ) -> Model { self.window_cx.build_model(build_model) } @@ -1896,25 +1878,23 @@ impl Context for ViewContext<'_, V> { fn update_model( &mut self, model: &Model, - 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(&mut self, window: AnyWindowHandle, update: F) -> Result where - F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T, + F: FnOnce(AnyView, &mut WindowContext<'_>) -> T, { self.window_cx.update_window(window, update) } } impl VisualContext for ViewContext<'_, V> { - type ViewContext<'a, W: 'static> = ViewContext<'a, W>; - fn build_view( &mut self, - build_view: impl FnOnce(&mut Self::ViewContext<'_, W>) -> W, + build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W, ) -> Self::Result> { self.window_cx.build_view(build_view) } @@ -1922,14 +1902,14 @@ impl VisualContext for ViewContext<'_, V> { fn update_view( &mut self, view: &View, - update: impl FnOnce(&mut V2, &mut Self::ViewContext<'_, V2>) -> R, + update: impl FnOnce(&mut V2, &mut ViewContext<'_, V2>) -> R, ) -> Self::Result { VisualContext::update_view(&mut self.window_cx, view, update) } fn replace_root_view( &mut self, - build_view: impl FnOnce(&mut Self::ViewContext<'_, W>) -> W, + build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W, ) -> Self::Result> where W: 'static + Send + Render, @@ -1983,7 +1963,7 @@ impl WindowHandle { pub fn update( self, cx: &mut C, - update: impl FnOnce(&mut V, &mut as UpdateView>::ViewContext<'_, V>) -> R, + update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R, ) -> Result where C: Context, @@ -1992,7 +1972,6 @@ impl WindowHandle { let view = root_view .downcast::() .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( self, cx: &mut C, - update: impl FnOnce(AnyView, &mut C::WindowContext<'_>) -> R, + update: impl FnOnce(AnyView, &mut WindowContext<'_>) -> R, ) -> Result where C: Context,