WIP

Antonio Scandurra created

Change summary

crates/gpui2/src/app.rs               | 70 ++-------------------
crates/gpui2/src/app/async_context.rs | 58 +++++-------------
crates/gpui2/src/app/model_context.rs |  6 
crates/gpui2/src/app/test_context.rs  | 13 ---
crates/gpui2/src/gpui2.rs             | 12 +--
crates/gpui2/src/window.rs            | 90 +++++++++++++---------------
crates/workspace2/src/workspace2.rs   |  6 
7 files changed, 79 insertions(+), 176 deletions(-)

Detailed changes

crates/gpui2/src/app.rs 🔗

@@ -18,8 +18,8 @@ use crate::{
     AppMetadata, AssetSource, ClipboardItem, Context, DispatchPhase, DisplayId, Entity, Executor,
     FocusEvent, FocusHandle, FocusId, KeyBinding, Keymap, LayoutId, MainThread, MainThreadOnly,
     Pixels, Platform, PlatformDisplay, Point, Render, SharedString, SubscriberSet, Subscription,
-    SvgRenderer, Task, TextStyle, TextStyleRefinement, TextSystem, View, ViewContext, Window,
-    WindowContext, WindowHandle, WindowId,
+    SvgRenderer, Task, TextStyle, TextStyleRefinement, TextSystem, View, Window, WindowContext,
+    WindowHandle, WindowId,
 };
 use anyhow::{anyhow, Result};
 use collections::{HashMap, HashSet, VecDeque};
@@ -267,27 +267,6 @@ impl AppContext {
             .collect()
     }
 
-    pub fn update_window_root<V, R>(
-        &mut self,
-        handle: &WindowHandle<V>,
-        update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
-    ) -> Result<R>
-    where
-        V: 'static + Send,
-    {
-        self.update_window(handle.any_handle, |cx| {
-            let root_view = cx
-                .window
-                .root_view
-                .as_ref()
-                .unwrap()
-                .clone()
-                .downcast()
-                .unwrap();
-            root_view.update(cx, update)
-        })
-    }
-
     pub(crate) fn push_effect(&mut self, effect: Effect) {
         match &effect {
             Effect::Notify { emitter } => {
@@ -354,7 +333,7 @@ impl AppContext {
             .collect::<SmallVec<[_; 8]>>();
 
         for dirty_window_handle in dirty_window_ids {
-            self.update_window(dirty_window_handle, |cx| cx.draw())
+            self.update_window(dirty_window_handle, |_, cx| cx.draw())
                 .unwrap();
         }
     }
@@ -384,7 +363,7 @@ impl AppContext {
     /// a window blur handler to restore focus to some logical element.
     fn release_dropped_focus_handles(&mut self) {
         for window_handle in self.windows() {
-            self.update_window(window_handle, |cx| {
+            self.update_window(window_handle, |_, cx| {
                 let mut blur_window = false;
                 let focus = cx.window.focus;
                 cx.window.focus_handles.write().retain(|handle_id, count| {
@@ -424,7 +403,7 @@ impl AppContext {
         window_handle: AnyWindowHandle,
         focused: Option<FocusId>,
     ) {
-        self.update_window(window_handle, |cx| {
+        self.update_window(window_handle, |_, cx| {
             if cx.window.focus == focused {
                 let mut listeners = mem::take(&mut cx.window.focus_listeners);
                 let focused =
@@ -764,7 +743,7 @@ impl Context for AppContext {
 
     fn update_window<T, F>(&mut self, handle: AnyWindowHandle, update: F) -> Result<T>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> T,
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
     {
         self.update(|cx| {
             let mut window = cx
@@ -774,8 +753,8 @@ impl Context for AppContext {
                 .take()
                 .unwrap();
 
-            let result = update(&mut WindowContext::new(cx, &mut window));
-
+            let root_view = window.root_view.clone().unwrap();
+            let result = update(root_view, &mut WindowContext::new(cx, &mut window));
             cx.windows
                 .get_mut(handle.id)
                 .ok_or_else(|| anyhow!("window not found"))?
@@ -854,39 +833,6 @@ impl MainThread<AppContext> {
         })
     }
 
-    pub fn update_window<R>(
-        &mut self,
-        handle: AnyWindowHandle,
-        update: impl FnOnce(&mut MainThread<WindowContext>) -> R,
-    ) -> Result<R> {
-        self.0.update_window(handle, |cx| {
-            update(unsafe {
-                std::mem::transmute::<&mut WindowContext, &mut MainThread<WindowContext>>(cx)
-            })
-        })
-    }
-
-    pub fn update_window_root<V, R>(
-        &mut self,
-        handle: &WindowHandle<V>,
-        update: impl FnOnce(&mut V, &mut MainThread<ViewContext<'_, V>>) -> R,
-    ) -> Result<R>
-    where
-        V: 'static + Send,
-    {
-        self.update_window(handle.any_handle, |cx| {
-            let root_view = cx
-                .window
-                .root_view
-                .as_ref()
-                .unwrap()
-                .clone()
-                .downcast()
-                .unwrap();
-            root_view.update(cx, update)
-        })
-    }
-
     /// Opens a new window with the given option and the root view returned by the given function.
     /// The function is invoked with a `WindowContext`, which can be used to interact with window-specific
     /// functionality.

crates/gpui2/src/app/async_context.rs 🔗

@@ -42,7 +42,7 @@ impl Context for AsyncAppContext {
 
     fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> T,
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
     {
         let app = self.app.upgrade().context("app was released")?;
         let mut lock = app.lock(); // Need this to compile
@@ -68,29 +68,6 @@ impl AsyncAppContext {
         Ok(f(&mut *lock))
     }
 
-    pub fn update_window<R>(
-        &self,
-        window: AnyWindowHandle,
-        update: impl FnOnce(&mut WindowContext) -> R,
-    ) -> Result<R> {
-        let app = self.app.upgrade().context("app was released")?;
-        let mut app_context = app.lock();
-        app_context.update_window(window, update)
-    }
-
-    pub fn update_window_root<V, R>(
-        &mut self,
-        handle: &WindowHandle<V>,
-        update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
-    ) -> Result<R>
-    where
-        V: 'static + Send,
-    {
-        let app = self.app.upgrade().context("app was released")?;
-        let mut app_context = app.lock();
-        app_context.update_window_root(handle, update)
-    }
-
     pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
     where
         Fut: Future<Output = R> + Send + 'static,
@@ -191,22 +168,25 @@ impl AsyncWindowContext {
         Self { app, window }
     }
 
-    pub fn update<R>(&self, update: impl FnOnce(&mut WindowContext) -> R) -> Result<R> {
+    pub fn update<R>(
+        &mut self,
+        update: impl FnOnce(AnyView, &mut WindowContext) -> R,
+    ) -> Result<R> {
         self.app.update_window(self.window, update)
     }
 
     pub fn on_next_frame(&mut self, f: impl FnOnce(&mut WindowContext) + Send + 'static) {
         self.app
-            .update_window(self.window, |cx| cx.on_next_frame(f))
+            .update_window(self.window, |_root, cx| cx.on_next_frame(f))
             .ok();
     }
 
     pub fn read_global<G: 'static, R>(
-        &self,
+        &mut self,
         read: impl FnOnce(&G, &WindowContext) -> R,
     ) -> Result<R> {
         self.app
-            .update_window(self.window, |cx| read(cx.global(), cx))
+            .update_window(self.window, |_, cx| read(cx.global(), cx))
     }
 
     pub fn update_global<G, R>(
@@ -217,7 +197,7 @@ impl AsyncWindowContext {
         G: 'static,
     {
         self.app
-            .update_window(self.window, |cx| cx.update_global(update))
+            .update_window(self.window, |_, cx| cx.update_global(update))
     }
 
     pub fn spawn<Fut, R>(
@@ -245,13 +225,13 @@ impl AsyncWindowContext {
     }
 
     pub fn run_on_main<R>(
-        &self,
+        &mut self,
         f: impl FnOnce(&mut MainThread<WindowContext>) -> R + Send + 'static,
     ) -> Task<Result<R>>
     where
         R: Send + 'static,
     {
-        self.update(|cx| cx.run_on_main(f))
+        self.update(|_, cx| cx.run_on_main(f))
             .unwrap_or_else(|error| Task::ready(Err(error)))
     }
 }
@@ -270,7 +250,7 @@ impl Context for AsyncWindowContext {
         T: 'static + Send,
     {
         self.app
-            .update_window(self.window, |cx| cx.build_model(build_model))
+            .update_window(self.window, |_, cx| cx.build_model(build_model))
     }
 
     fn update_model<T: 'static, R>(
@@ -279,12 +259,12 @@ impl Context for AsyncWindowContext {
         update: impl FnOnce(&mut T, &mut Self::ModelContext<'_, T>) -> R,
     ) -> Result<R> {
         self.app
-            .update_window(self.window, |cx| cx.update_model(handle, update))
+            .update_window(self.window, |_, cx| cx.update_model(handle, update))
     }
 
     fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> T,
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
     {
         self.app.update_window(window, update)
     }
@@ -293,10 +273,6 @@ impl Context for AsyncWindowContext {
 impl VisualContext for AsyncWindowContext {
     type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
 
-    fn root_view(&self) -> Result<AnyView> {
-        self.app.update_window(self.window, |cx| cx.root_view())
-    }
-
     fn build_view<V>(
         &mut self,
         build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
@@ -305,7 +281,7 @@ impl VisualContext for AsyncWindowContext {
         V: 'static + Send,
     {
         self.app
-            .update_window(self.window, |cx| cx.build_view(build_view_state))
+            .update_window(self.window, |_, cx| cx.build_view(build_view_state))
     }
 
     fn update_view<V: 'static, R>(
@@ -314,7 +290,7 @@ impl VisualContext for AsyncWindowContext {
         update: impl FnOnce(&mut V, &mut Self::ViewContext<'_, V>) -> R,
     ) -> Self::Result<R> {
         self.app
-            .update_window(self.window, |cx| cx.update_view(view, update))
+            .update_window(self.window, |_, cx| cx.update_view(view, update))
     }
 
     fn replace_root_view<V>(
@@ -325,7 +301,7 @@ impl VisualContext for AsyncWindowContext {
         V: 'static + Send + Render,
     {
         self.app
-            .update_window(self.window, |cx| cx.replace_root_view(build_view))
+            .update_window(self.window, |_, cx| cx.replace_root_view(build_view))
     }
 }
 

crates/gpui2/src/app/model_context.rs 🔗

@@ -1,6 +1,6 @@
 use crate::{
-    AnyWindowHandle, AppContext, AsyncAppContext, Context, Effect, Entity, EntityId, EventEmitter,
-    MainThread, Model, Subscription, Task, WeakModel, WindowContext,
+    AnyView, AnyWindowHandle, AppContext, AsyncAppContext, Context, Effect, Entity, EntityId,
+    EventEmitter, MainThread, Model, Subscription, Task, WeakModel, WindowContext,
 };
 use anyhow::Result;
 use derive_more::{Deref, DerefMut};
@@ -253,7 +253,7 @@ impl<'a, T> Context for ModelContext<'a, T> {
 
     fn update_window<R, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<R>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> R,
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> R,
     {
         self.app.update_window(window, update)
     }

crates/gpui2/src/app/test_context.rs 🔗

@@ -1,5 +1,5 @@
 use crate::{
-    AnyWindowHandle, AppContext, AsyncAppContext, Context, Executor, MainThread, Model,
+    AnyView, AnyWindowHandle, AppContext, AsyncAppContext, Context, Executor, MainThread, Model,
     ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
 };
 use parking_lot::Mutex;
@@ -38,7 +38,7 @@ impl Context for TestAppContext {
 
     fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> T,
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
     {
         let mut lock = self.app.lock();
         lock.update_window(window, f)
@@ -76,15 +76,6 @@ impl TestAppContext {
         f(&mut *lock)
     }
 
-    pub fn update_window<R>(
-        &self,
-        handle: AnyWindowHandle,
-        update: impl FnOnce(&mut WindowContext) -> R,
-    ) -> R {
-        let mut app = self.app.lock();
-        app.update_window(handle, update).unwrap()
-    }
-
     pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
     where
         Fut: Future<Output = R> + Send + 'static,

crates/gpui2/src/gpui2.rs 🔗

@@ -98,7 +98,7 @@ pub trait Context {
 
     fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> T;
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T;
 }
 
 pub trait VisualContext: Context {
@@ -199,15 +199,15 @@ impl<C: Context> Context for MainThread<C> {
 
     fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> T,
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
     {
-        self.0.update_window(window, |cx| {
+        self.0.update_window(window, |root, cx| {
             let cx = unsafe {
                 mem::transmute::<&mut C::WindowContext<'_>, &mut MainThread<C::WindowContext<'_>>>(
                     cx,
                 )
             };
-            update(cx)
+            update(root, cx)
         })
     }
 }
@@ -215,10 +215,6 @@ impl<C: Context> Context for MainThread<C> {
 impl<C: VisualContext> VisualContext for MainThread<C> {
     type ViewContext<'a, V: 'static> = MainThread<C::ViewContext<'a, V>>;
 
-    fn root_view(&self) -> AnyView {
-        self.0.root_view()
-    }
-
     fn build_view<V>(
         &mut self,
         build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,

crates/gpui2/src/window.rs 🔗

@@ -1,16 +1,16 @@
 use crate::{
     px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
     Bounds, BoxShadow, Context, Corners, DevicePixels, DispatchContext, DisplayId, Edges, Effect,
-    Entity, EntityId, EventEmitter, FileDropEvent, Flatten, FocusEvent, FontId, GlobalElementId,
-    GlyphId, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke,
-    LayoutId, MainThread, MainThreadOnly, Model, ModelContext, Modifiers, MonochromeSprite,
-    MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas,
-    PlatformWindow, Point, PolychromeSprite, Quad, Render, RenderGlyphParams, RenderImageParams,
-    RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription,
-    TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakView,
-    WindowOptions, SUBPIXEL_VARIANTS,
+    Entity, EntityId, EventEmitter, FileDropEvent, FocusEvent, FontId, GlobalElementId, GlyphId,
+    Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch, KeyMatcher, Keystroke, LayoutId,
+    MainThread, MainThreadOnly, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton,
+    MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformWindow,
+    Point, PolychromeSprite, Quad, Render, RenderGlyphParams, RenderImageParams, RenderSvgParams,
+    ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, Subscription, TaffyLayoutEngine,
+    Task, Underline, UnderlineStyle, View, VisualContext, WeakView, WindowOptions,
+    SUBPIXEL_VARIANTS,
 };
-use anyhow::Result;
+use anyhow::{anyhow, Result};
 use collections::HashMap;
 use derive_more::{Deref, DerefMut};
 use parking_lot::RwLock;
@@ -204,9 +204,9 @@ impl Window {
         let content_size = platform_window.content_size();
         let scale_factor = platform_window.scale_factor();
         platform_window.on_resize(Box::new({
-            let cx = cx.to_async();
+            let mut cx = cx.to_async();
             move |content_size, scale_factor| {
-                cx.update_window(handle, |cx| {
+                cx.update_window(handle, |_, cx| {
                     cx.window.scale_factor = scale_factor;
                     cx.window.scene_builder = SceneBuilder::new();
                     cx.window.content_size = content_size;
@@ -223,9 +223,9 @@ impl Window {
         }));
 
         platform_window.on_input({
-            let cx = cx.to_async();
+            let mut cx = cx.to_async();
             Box::new(move |event| {
-                cx.update_window(handle, |cx| cx.dispatch_event(event))
+                cx.update_window(handle, |_, cx| cx.dispatch_event(event))
                     .log_err()
                     .unwrap_or(true)
             })
@@ -372,7 +372,7 @@ impl<'a> WindowContext<'a> {
     pub fn defer(&mut self, f: impl FnOnce(&mut WindowContext) + 'static + Send) {
         let window = self.window.handle;
         self.app.defer(move |cx| {
-            cx.update_window(window, f).ok();
+            cx.update_window(window, |_, cx| f(cx)).ok();
         });
     }
 
@@ -391,7 +391,7 @@ impl<'a> WindowContext<'a> {
         self.app.event_listeners.insert(
             entity_id,
             Box::new(move |event, cx| {
-                cx.update_window(window_handle, |cx| {
+                cx.update_window(window_handle, |_, cx| {
                     if let Some(handle) = E::upgrade_from(&entity) {
                         let event = event.downcast_ref().expect("invalid event type");
                         on_event(handle, event, cx);
@@ -421,7 +421,8 @@ impl<'a> WindowContext<'a> {
             })))
         } else {
             let handle = self.window.handle;
-            self.app.run_on_main(move |cx| cx.update_window(handle, f))
+            self.app
+                .run_on_main(move |cx| cx.update_window(handle, |_, cx| f(cx)))
         }
     }
 
@@ -443,12 +444,12 @@ impl<'a> WindowContext<'a> {
                     return;
                 }
             } else {
-                let async_cx = cx.to_async();
+                let mut async_cx = cx.to_async();
                 cx.next_frame_callbacks.insert(display_id, vec![f]);
                 cx.platform().set_display_link_output_callback(
                     display_id,
                     Box::new(move |_current_time, _output_time| {
-                        let _ = async_cx.update(|cx| {
+                        let _ = async_cx.update(|_, cx| {
                             let callbacks = cx
                                 .next_frame_callbacks
                                 .get_mut(&display_id)
@@ -1181,7 +1182,7 @@ impl<'a> WindowContext<'a> {
         let window_handle = self.window.handle;
         self.global_observers.insert(
             TypeId::of::<G>(),
-            Box::new(move |cx| cx.update_window(window_handle, |cx| f(cx)).is_ok()),
+            Box::new(move |cx| cx.update_window(window_handle, |_, cx| f(cx)).is_ok()),
         )
     }
 
@@ -1297,10 +1298,11 @@ impl Context for WindowContext<'_> {
 
     fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> T,
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
     {
         if window == self.window.handle {
-            Ok(update(self))
+            let root_view = self.window.root_view.clone().unwrap();
+            Ok(update(root_view, self))
         } else {
             self.app.update_window(window, update)
         }
@@ -1310,13 +1312,6 @@ impl Context for WindowContext<'_> {
 impl VisualContext for WindowContext<'_> {
     type ViewContext<'a, V: 'static> = ViewContext<'a, V>;
 
-    fn root_view(&self) -> Self::Result<AnyView> {
-        self.window
-            .root_view
-            .clone()
-            .expect("we only take the root_view value when we draw")
-    }
-
     fn build_view<V>(
         &mut self,
         build_view_state: impl FnOnce(&mut Self::ViewContext<'_, V>) -> V,
@@ -1654,7 +1649,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
         self.app.observers.insert(
             entity_id,
             Box::new(move |cx| {
-                cx.update_window(window_handle, |cx| {
+                cx.update_window(window_handle, |_, cx| {
                     if let Some(handle) = E::upgrade_from(&entity) {
                         view.update(cx, |this, cx| on_notify(this, handle, cx))
                             .is_ok()
@@ -1683,7 +1678,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
         self.app.event_listeners.insert(
             entity_id,
             Box::new(move |event, cx| {
-                cx.update_window(window_handle, |cx| {
+                cx.update_window(window_handle, |_, cx| {
                     if let Some(handle) = E::upgrade_from(&handle) {
                         let event = event.downcast_ref().expect("invalid event type");
                         view.update(cx, |this, cx| on_event(this, handle, event, cx))
@@ -1707,7 +1702,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
             Box::new(move |this, cx| {
                 let this = this.downcast_mut().expect("invalid entity type");
                 // todo!("are we okay with silently swallowing the error?")
-                let _ = cx.update_window(window_handle, |cx| on_release(this, cx));
+                let _ = cx.update_window(window_handle, |_, cx| on_release(this, cx));
             }),
         )
     }
@@ -1729,7 +1724,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
             entity_id,
             Box::new(move |entity, cx| {
                 let entity = entity.downcast_mut().expect("invalid entity type");
-                let _ = cx.update_window(window_handle, |cx| {
+                let _ = cx.update_window(window_handle, |_, cx| {
                     view.update(cx, |this, cx| on_release(this, entity, cx))
                 });
             }),
@@ -1892,7 +1887,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
         self.global_observers.insert(
             TypeId::of::<G>(),
             Box::new(move |cx| {
-                cx.update_window(window_handle, |cx| {
+                cx.update_window(window_handle, |_, cx| {
                     view.update(cx, |view, cx| f(view, cx)).is_ok()
                 })
                 .unwrap_or(false)
@@ -1962,7 +1957,7 @@ impl<V> Context for ViewContext<'_, V> {
 
     fn update_window<T, F>(&mut self, window: AnyWindowHandle, update: F) -> Result<T>
     where
-        F: FnOnce(&mut Self::WindowContext<'_>) -> T,
+        F: FnOnce(AnyView, &mut Self::WindowContext<'_>) -> T,
     {
         self.window_cx.update_window(window, update)
     }
@@ -1971,10 +1966,6 @@ impl<V> Context for ViewContext<'_, V> {
 impl<V: 'static> VisualContext for ViewContext<'_, V> {
     type ViewContext<'a, W: 'static> = ViewContext<'a, W>;
 
-    fn root_view(&self) -> Self::Result<AnyView> {
-        self.window_cx.root_view()
-    }
-
     fn build_view<W: 'static + Send>(
         &mut self,
         build_view: impl FnOnce(&mut Self::ViewContext<'_, W>) -> W,
@@ -2043,21 +2034,24 @@ impl<V: 'static + Render> WindowHandle<V> {
         }
     }
 
-    pub fn update_root<C, R>(
+    pub fn update_root<C, R, W>(
         &self,
         cx: &mut C,
-        update: impl FnOnce(&mut V, &mut ViewContext<V>) -> R,
+        update: impl FnOnce(
+            &mut V,
+            &mut <C::WindowContext<'_> as VisualContext>::ViewContext<'_, V>,
+        ) -> R,
     ) -> Result<R>
     where
-        C: Context,
+        C: for<'a> Context<WindowContext<'a> = W>,
+        W: VisualContext<Result<R> = R>,
     {
-        cx.update_window(self.any_handle, |cx| {
-            let x = Ok(cx.root_view()).flatten();
-
-            // let root_view = x.unwrap().downcast::<V>().unwrap();
-            // root_view.update(cx, update)
-            todo!()
-        })
+        cx.update_window(self.any_handle, |root_view, cx| {
+            let view = root_view
+                .downcast::<V>()
+                .map_err(|_| anyhow!("the type of the window's root view has changed"))?;
+            Ok(view.update(cx, update))
+        })?
     }
 }
 

crates/workspace2/src/workspace2.rs 🔗

@@ -14,7 +14,7 @@ use crate::persistence::model::{
     DockData, DockStructure, SerializedItem, SerializedPane, SerializedPaneGroup,
     SerializedWorkspace,
 };
-use anyhow::{anyhow, Context, Result};
+use anyhow::{anyhow, Context as _, Result};
 use call2::ActiveCall;
 use client2::{
     proto::{self, PeerId},
@@ -29,7 +29,7 @@ use futures::{
 };
 use gpui2::{
     div, point, size, AnyModel, AnyView, AppContext, AsyncAppContext, AsyncWindowContext, Bounds,
-    Div, EventEmitter, GlobalPixels, MainThread, Model, ModelContext, Point, Render, Size,
+    Context, Div, EventEmitter, GlobalPixels, MainThread, Model, ModelContext, Point, Render, Size,
     Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext,
     WindowHandle, WindowOptions,
 };
@@ -837,7 +837,7 @@ impl Workspace {
             };
 
             let window = if let Some(window) = requesting_window {
-                cx.update_window(window.into(), |cx| {
+                cx.update_window(window.into(), |old_workspace, cx| {
                     cx.replace_root_view(|cx| {
                         Workspace::new(workspace_id, project_handle.clone(), app_state.clone(), cx)
                     });