From d28c81571c49782800fa4b2e25f172bf6402e487 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 4 Oct 2023 19:59:24 +0200 Subject: [PATCH] Checkpoint --- crates/gpui3/src/app.rs | 24 ++++++++---------------- crates/gpui3/src/window.rs | 36 ++++++++++++------------------------ 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/crates/gpui3/src/app.rs b/crates/gpui3/src/app.rs index a05f9bb792c1aed7813aff03f28d18c79d6e6b9f..592a6076defb6b43f879e08584d44ddc94ade01c 100644 --- a/crates/gpui3/src/app.rs +++ b/crates/gpui3/src/app.rs @@ -14,7 +14,7 @@ use crate::{ }; use anyhow::{anyhow, Result}; use collections::{HashMap, VecDeque}; -use futures::{channel::oneshot, Future}; +use futures::Future; use parking_lot::Mutex; use slotmap::SlotMap; use smallvec::SmallVec; @@ -191,29 +191,21 @@ impl AppContext { pub fn run_on_main( &mut self, f: impl FnOnce(&mut MainThread) -> R + Send + 'static, - ) -> impl Future + ) -> Task where R: Send + 'static, { - let (tx, rx) = oneshot::channel(); if self.executor.is_main_thread() { - let _ = tx.send(f(unsafe { + Task::ready(f(unsafe { mem::transmute::<&mut AppContext, &mut MainThread>(self) - })); + })) } else { let this = self.this.upgrade().unwrap(); - self.executor - .run_on_main(move || { - let cx = &mut *this.lock(); - cx.update(|cx| { - let _ = tx.send(f(unsafe { - mem::transmute::<&mut Self, &mut MainThread>(cx) - })); - }) - }) - .detach(); + self.executor.run_on_main(move || { + let cx = &mut *this.lock(); + cx.update(|cx| f(unsafe { mem::transmute::<&mut Self, &mut MainThread>(cx) })) + }) } - async move { rx.await.unwrap() } } pub fn spawn_on_main( diff --git a/crates/gpui3/src/window.rs b/crates/gpui3/src/window.rs index 5cf40e0a6698480e0ffe17d2564cc03ec1f3d826..3782dfe97a563b5e6ecc57ee13f834dc31ad66c4 100644 --- a/crates/gpui3/src/window.rs +++ b/crates/gpui3/src/window.rs @@ -3,11 +3,10 @@ use crate::{ Bounds, Context, Corners, DevicePixels, Effect, Element, EntityId, FontId, GlyphId, Handle, Hsla, ImageData, IsZero, LayerId, LayoutId, MainThread, MainThreadOnly, MonochromeSprite, Pixels, PlatformAtlas, PlatformWindow, Point, PolychromeSprite, Reference, RenderGlyphParams, - RenderSvgParams, ScaledPixels, Scene, SharedString, Size, Style, TaffyLayoutEngine, WeakHandle, - WindowOptions, SUBPIXEL_VARIANTS, + RenderSvgParams, ScaledPixels, Scene, SharedString, Size, Style, TaffyLayoutEngine, Task, + WeakHandle, WindowOptions, SUBPIXEL_VARIANTS, }; use anyhow::Result; -use futures::{channel::oneshot, Future}; use smallvec::SmallVec; use std::{any::TypeId, borrow::Cow, marker::PhantomData, mem, sync::Arc}; use util::ResultExt; @@ -116,24 +115,18 @@ impl<'a, 'w> WindowContext<'a, 'w> { fn run_on_main( &mut self, f: impl FnOnce(&mut MainThread>) -> R + Send + 'static, - ) -> impl Future + ) -> Task> where R: Send + 'static, { - let (tx, rx) = oneshot::channel(); if self.executor.is_main_thread() { - let _ = tx.send(f(unsafe { + Task::ready(Ok(f(unsafe { mem::transmute::<&mut Self, &mut MainThread>(self) - })); + }))) } else { let id = self.window.handle.id; - let _ = self.app.run_on_main(move |cx| { - cx.update_window(id, |cx| { - let _ = tx.send(f(cx)); - }) - }); + self.app.run_on_main(move |cx| cx.update_window(id, f)) } - async move { rx.await.unwrap() } } pub fn request_layout( @@ -397,13 +390,14 @@ impl<'a, 'w> WindowContext<'a, 'w> { cx.window.root_view = Some(root_view); let scene = cx.window.scene.take(); - let _ = cx.run_on_main(view, |_, cx| { + cx.run_on_main(view, |_, cx| { cx.window .platform_window .borrow_on_main_thread() .draw(scene); cx.window.dirty = false; - }); + }) + .detach(); Ok(()) }) @@ -594,23 +588,17 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> { &mut self, view: &mut S, f: impl FnOnce(&mut S, &mut MainThread>) -> R + Send + 'static, - ) -> impl Future + ) -> Task> where R: Send + 'static, { - let (tx, rx) = oneshot::channel(); if self.executor.is_main_thread() { let cx = unsafe { mem::transmute::<&mut Self, &mut MainThread>(self) }; - let _ = tx.send(f(view, cx)); + Task::ready(Ok(f(view, cx))) } else { let handle = self.handle().upgrade(self).unwrap(); - let _ = self.window_cx.run_on_main(move |cx| { - handle.update(cx, |view, cx| { - let _ = tx.send(f(view, cx)); - }) - }); + self.window_cx.run_on_main(move |cx| handle.update(cx, f)) } - async move { rx.await.unwrap() } } pub(crate) fn erase_state(&mut self, f: impl FnOnce(&mut ViewContext<()>) -> R) -> R {