Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Simplify contexts
Antonio Scandurra18fcb41292fe
diff --git a/crates/gpui2/src/app.rs b/crates/gpui2/src/app.rsindex f71619bc015..f08c7fb86b3 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> = 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 = cxdiff --git a/crates/gpui2/src/app/async_context.rs b/crates/gpui2/src/app/async_context.rsindex 08ae307d1b1..823031b11a2 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<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>>whereT: '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>>whereT: '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>>whereV: '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>>whereV: 'static + Send + Render,diff --git a/crates/gpui2/src/app/entity_map.rs b/crates/gpui2/src/app/entity_map.rsindex 6faa85bac94..5b3462b91e8 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<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>whereC: 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>whereC: Context,diff --git a/crates/gpui2/src/app/model_context.rs b/crates/gpui2/src/app/model_context.rsindex d72b0391392..cb25adfb634 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> = 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)}diff --git a/crates/gpui2/src/app/test_context.rs b/crates/gpui2/src/app/test_context.rsindex ee1da50136e..27275d3a044 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> = 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>>whereT: '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)diff --git a/crates/gpui2/src/gpui2.rs b/crates/gpui2/src/gpui2.rsindex 9801167583c..bd65ade0d3f 100644--- a/crates/gpui2/src/gpui2.rs+++ b/crates/gpui2/src/gpui2.rs@@ -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>whereT: '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>>whereV: '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>>whereV: '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;diff --git a/crates/gpui2/src/view.rs b/crates/gpui2/src/view.rsindex 82214b381d8..165eedef9c1 100644--- a/crates/gpui2/src/view.rs+++ b/crates/gpui2/src/view.rs@@ -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>whereC: 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>whereC: VisualContext,diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rsindex f059e02a335..4435b0e9275 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> = 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>whereT: '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>>whereV: '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>>whereV: '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>>whereW: '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>whereC: 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>whereC: Context,