Re-re-fork (#23)

This commit is contained in:
Miles Wirht
2026-03-22 12:21:07 -04:00
committed by GitHub
parent 18aeb44a54
commit de080b0773
246 changed files with 34208 additions and 11176 deletions
+96 -104
View File
@@ -1,18 +1,23 @@
use crate::{
AnyView, AnyWindowHandle, App, AppCell, AppContext, BackgroundExecutor, BorrowAppContext,
Entity, EventEmitter, Focusable, ForegroundExecutor, Global, PromptButton, PromptLevel, Render,
Reservation, Result, Subscription, Task, VisualContext, Window, WindowHandle,
Entity, EventEmitter, Focusable, ForegroundExecutor, Global, GpuiBorrow, PromptButton,
PromptLevel, Render, Reservation, Result, Subscription, Task, VisualContext, Window,
WindowHandle,
};
use anyhow::{Context as _, anyhow};
use anyhow::{Context as _, bail};
use derive_more::{Deref, DerefMut};
use futures::channel::oneshot;
use futures::future::FutureExt;
use std::{future::Future, rc::Weak};
use super::{Context, WeakEntity};
/// An async-friendly version of [App] with a static lifetime so it can be held across `await` points in async code.
/// You're provided with an instance when calling [App::spawn], and you can also create one with [App::to_async].
/// Internally, this holds a weak reference to an `App`, so its methods are fallible to protect against cases where the [App] is dropped.
///
/// Internally, this holds a weak reference to an `App`. Methods will panic if the app has been dropped,
/// but this should not happen in practice when using foreground tasks spawned via `cx.spawn()`,
/// as the executor checks if the app is alive before running each task.
#[derive(Clone)]
pub struct AsyncApp {
pub(crate) app: Weak<AppCell>,
@@ -20,64 +25,61 @@ pub struct AsyncApp {
pub(crate) foreground_executor: ForegroundExecutor,
}
impl AppContext for AsyncApp {
type Result<T> = Result<T>;
impl AsyncApp {
fn app(&self) -> std::rc::Rc<AppCell> {
self.app
.upgrade()
.expect("app was released before async operation completed")
}
}
fn new<T: 'static>(
&mut self,
build_entity: impl FnOnce(&mut Context<T>) -> T,
) -> Self::Result<Entity<T>> {
let app = self.app.upgrade().context("app was released")?;
impl AppContext for AsyncApp {
fn new<T: 'static>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T> {
let app = self.app();
let mut app = app.borrow_mut();
Ok(app.new(build_entity))
app.new(build_entity)
}
fn reserve_entity<T: 'static>(&mut self) -> Result<Reservation<T>> {
let app = self.app.upgrade().context("app was released")?;
fn reserve_entity<T: 'static>(&mut self) -> Reservation<T> {
let app = self.app();
let mut app = app.borrow_mut();
Ok(app.reserve_entity())
app.reserve_entity()
}
fn insert_entity<T: 'static>(
&mut self,
reservation: Reservation<T>,
build_entity: impl FnOnce(&mut Context<T>) -> T,
) -> Result<Entity<T>> {
let app = self.app.upgrade().context("app was released")?;
) -> Entity<T> {
let app = self.app();
let mut app = app.borrow_mut();
Ok(app.insert_entity(reservation, build_entity))
app.insert_entity(reservation, build_entity)
}
fn update_entity<T: 'static, R>(
&mut self,
handle: &Entity<T>,
update: impl FnOnce(&mut T, &mut Context<T>) -> R,
) -> Self::Result<R> {
let app = self.app.upgrade().context("app was released")?;
) -> R {
let app = self.app();
let mut app = app.borrow_mut();
Ok(app.update_entity(handle, update))
app.update_entity(handle, update)
}
fn as_mut<'a, T>(&'a mut self, _handle: &Entity<T>) -> Self::Result<super::GpuiBorrow<'a, T>>
fn as_mut<'a, T>(&'a mut self, _handle: &Entity<T>) -> GpuiBorrow<'a, T>
where
T: 'static,
{
Err(anyhow!(
"Cannot as_mut with an async context. Try calling update() first"
))
panic!("Cannot as_mut with an async context. Try calling update() first")
}
fn read_entity<T, R>(
&self,
handle: &Entity<T>,
callback: impl FnOnce(&T, &App) -> R,
) -> Self::Result<R>
fn read_entity<T, R>(&self, handle: &Entity<T>, callback: impl FnOnce(&T, &App) -> R) -> R
where
T: 'static,
{
let app = self.app.upgrade().context("app was released")?;
let app = self.app();
let lock = app.borrow();
Ok(lock.read_entity(handle, callback))
lock.read_entity(handle, callback)
}
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
@@ -86,6 +88,9 @@ impl AppContext for AsyncApp {
{
let app = self.app.upgrade().context("app was released")?;
let mut lock = app.try_borrow_mut()?;
if lock.quitting {
bail!("app is quitting");
}
lock.update_window(window, f)
}
@@ -99,9 +104,13 @@ impl AppContext for AsyncApp {
{
let app = self.app.upgrade().context("app was released")?;
let lock = app.borrow();
if lock.quitting {
bail!("app is quitting");
}
lock.read_window(window, read)
}
#[track_caller]
fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
where
R: Send + 'static,
@@ -109,23 +118,22 @@ impl AppContext for AsyncApp {
self.background_executor.spawn(future)
}
fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result<R>
fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> R
where
G: Global,
{
let app = self.app.upgrade().context("app was released")?;
let app = self.app();
let mut lock = app.borrow_mut();
Ok(lock.update(|this| this.read_global(callback)))
lock.update(|this| this.read_global(callback))
}
}
impl AsyncApp {
/// Schedules all windows in the application to be redrawn.
pub fn refresh(&self) -> Result<()> {
let app = self.app.upgrade().context("app was released")?;
pub fn refresh(&self) {
let app = self.app();
let mut lock = app.borrow_mut();
lock.refresh_windows();
Ok(())
}
/// Get an executor which can be used to spawn futures in the background.
@@ -139,10 +147,10 @@ impl AsyncApp {
}
/// Invoke the given function in the context of the app, then flush any effects produced during its invocation.
pub fn update<R>(&self, f: impl FnOnce(&mut App) -> R) -> Result<R> {
let app = self.app.upgrade().context("app was released")?;
pub fn update<R>(&self, f: impl FnOnce(&mut App) -> R) -> R {
let app = self.app();
let mut lock = app.borrow_mut();
Ok(lock.update(f))
lock.update(f)
}
/// Arrange for the given callback to be invoked whenever the given entity emits an event of a given type.
@@ -150,16 +158,15 @@ impl AsyncApp {
pub fn subscribe<T, Event>(
&mut self,
entity: &Entity<T>,
mut on_event: impl FnMut(Entity<T>, &Event, &mut App) + 'static,
) -> Result<Subscription>
on_event: impl FnMut(Entity<T>, &Event, &mut App) + 'static,
) -> Subscription
where
T: 'static + EventEmitter<Event>,
Event: 'static,
{
let app = self.app.upgrade().context("app was released")?;
let app = self.app();
let mut lock = app.borrow_mut();
let subscription = lock.subscribe(entity, on_event);
Ok(subscription)
lock.subscribe(entity, on_event)
}
/// Open a window with the given options based on the root view returned by the given function.
@@ -171,8 +178,11 @@ impl AsyncApp {
where
V: 'static + Render,
{
let app = self.app.upgrade().context("app was released")?;
let app = self.app();
let mut lock = app.borrow_mut();
if lock.quitting {
bail!("app is quitting");
}
lock.open_window(options, build_root_view)
}
@@ -185,65 +195,57 @@ impl AsyncApp {
{
let mut cx = self.clone();
self.foreground_executor
.spawn(async move { f(&mut cx).await })
.spawn(async move { f(&mut cx).await }.boxed_local())
}
/// Determine whether global state of the specified type has been assigned.
/// Returns an error if the `App` has been dropped.
pub fn has_global<G: Global>(&self) -> Result<bool> {
let app = self.app.upgrade().context("app was released")?;
pub fn has_global<G: Global>(&self) -> bool {
let app = self.app();
let app = app.borrow_mut();
Ok(app.has_global::<G>())
app.has_global::<G>()
}
/// Reads the global state of the specified type, passing it to the given callback.
///
/// Panics if no global state of the specified type has been assigned.
/// Returns an error if the `App` has been dropped.
pub fn read_global<G: Global, R>(&self, read: impl FnOnce(&G, &App) -> R) -> Result<R> {
let app = self.app.upgrade().context("app was released")?;
pub fn read_global<G: Global, R>(&self, read: impl FnOnce(&G, &App) -> R) -> R {
let app = self.app();
let app = app.borrow_mut();
Ok(read(app.global(), &app))
read(app.global(), &app)
}
/// Reads the global state of the specified type, passing it to the given callback.
///
/// Similar to [`AsyncApp::read_global`], but returns an error instead of panicking
/// if no state of the specified type has been assigned.
///
/// Returns an error if no state of the specified type has been assigned the `App` has been dropped.
pub fn try_read_global<G: Global, R>(&self, read: impl FnOnce(&G, &App) -> R) -> Option<R> {
let app = self.app.upgrade()?;
let app = self.app();
let app = app.borrow_mut();
if app.quitting {
return None;
}
Some(read(app.try_global()?, &app))
}
/// Reads the global state of the specified type, passing it to the given callback.
/// A default value is assigned if a global of this type has not yet been assigned.
///
/// # Errors
/// If the app has ben dropped this returns an error.
pub fn try_read_default_global<G: Global + Default, R>(
pub fn read_default_global<G: Global + Default, R>(
&self,
read: impl FnOnce(&G, &App) -> R,
) -> Result<R> {
let app = self.app.upgrade().context("app was released")?;
) -> R {
let app = self.app();
let mut app = app.borrow_mut();
app.update(|cx| {
cx.default_global::<G>();
});
Ok(read(app.try_global().context("app was released")?, &app))
read(app.global(), &app)
}
/// A convenience method for [`App::update_global`](BorrowAppContext::update_global)
/// for updating the global state of the specified type.
pub fn update_global<G: Global, R>(
&self,
update: impl FnOnce(&mut G, &mut App) -> R,
) -> Result<R> {
let app = self.app.upgrade().context("app was released")?;
pub fn update_global<G: Global, R>(&self, update: impl FnOnce(&mut G, &mut App) -> R) -> R {
let app = self.app();
let mut app = app.borrow_mut();
Ok(app.update(|cx| cx.update_global(update)))
app.update(|cx| cx.update_global(update))
}
/// Run something using this entity and cx, when the returned struct is dropped
@@ -334,7 +336,7 @@ impl AsyncWindowContext {
{
let mut cx = self.clone();
self.foreground_executor
.spawn(async move { f(&mut cx).await })
.spawn(async move { f(&mut cx).await }.boxed_local())
}
/// Present a platform dialog.
@@ -359,54 +361,41 @@ impl AsyncWindowContext {
}
impl AppContext for AsyncWindowContext {
type Result<T> = Result<T>;
fn new<T>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Result<Entity<T>>
fn new<T>(&mut self, build_entity: impl FnOnce(&mut Context<T>) -> T) -> Entity<T>
where
T: 'static,
{
self.app
.update_window(self.window, |_, _, cx| cx.new(build_entity))
self.app.new(build_entity)
}
fn reserve_entity<T: 'static>(&mut self) -> Result<Reservation<T>> {
self.app
.update_window(self.window, |_, _, cx| cx.reserve_entity())
fn reserve_entity<T: 'static>(&mut self) -> Reservation<T> {
self.app.reserve_entity()
}
fn insert_entity<T: 'static>(
&mut self,
reservation: Reservation<T>,
build_entity: impl FnOnce(&mut Context<T>) -> T,
) -> Self::Result<Entity<T>> {
self.app.update_window(self.window, |_, _, cx| {
cx.insert_entity(reservation, build_entity)
})
) -> Entity<T> {
self.app.insert_entity(reservation, build_entity)
}
fn update_entity<T: 'static, R>(
&mut self,
handle: &Entity<T>,
update: impl FnOnce(&mut T, &mut Context<T>) -> R,
) -> Result<R> {
self.app
.update_window(self.window, |_, _, cx| cx.update_entity(handle, update))
) -> R {
self.app.update_entity(handle, update)
}
fn as_mut<'a, T>(&'a mut self, _: &Entity<T>) -> Self::Result<super::GpuiBorrow<'a, T>>
fn as_mut<'a, T>(&'a mut self, _: &Entity<T>) -> GpuiBorrow<'a, T>
where
T: 'static,
{
Err(anyhow!(
"Cannot use as_mut() from an async context, call `update`"
))
panic!("Cannot use as_mut() from an async context, call `update`")
}
fn read_entity<T, R>(
&self,
handle: &Entity<T>,
read: impl FnOnce(&T, &App) -> R,
) -> Self::Result<R>
fn read_entity<T, R>(&self, handle: &Entity<T>, read: impl FnOnce(&T, &App) -> R) -> R
where
T: 'static,
{
@@ -431,6 +420,7 @@ impl AppContext for AsyncWindowContext {
self.app.read_window(window, read)
}
#[track_caller]
fn background_spawn<R>(&self, future: impl Future<Output = R> + Send + 'static) -> Task<R>
where
R: Send + 'static,
@@ -438,7 +428,7 @@ impl AppContext for AsyncWindowContext {
self.app.background_executor.spawn(future)
}
fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Result<R>
fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> R
where
G: Global,
{
@@ -447,6 +437,8 @@ impl AppContext for AsyncWindowContext {
}
impl VisualContext for AsyncWindowContext {
type Result<T> = Result<T>;
fn window_handle(&self) -> AnyWindowHandle {
self.window
}
@@ -454,7 +446,7 @@ impl VisualContext for AsyncWindowContext {
fn new_window_entity<T: 'static>(
&mut self,
build_entity: impl FnOnce(&mut Window, &mut Context<T>) -> T,
) -> Self::Result<Entity<T>> {
) -> Result<Entity<T>> {
self.app.update_window(self.window, |_, window, cx| {
cx.new(|cx| build_entity(window, cx))
})
@@ -464,7 +456,7 @@ impl VisualContext for AsyncWindowContext {
&mut self,
view: &Entity<T>,
update: impl FnOnce(&mut T, &mut Window, &mut Context<T>) -> R,
) -> Self::Result<R> {
) -> Result<R> {
self.app.update_window(self.window, |_, window, cx| {
view.update(cx, |entity, cx| update(entity, window, cx))
})
@@ -473,7 +465,7 @@ impl VisualContext for AsyncWindowContext {
fn replace_root_view<V>(
&mut self,
build_view: impl FnOnce(&mut Window, &mut Context<V>) -> V,
) -> Self::Result<Entity<V>>
) -> Result<Entity<V>>
where
V: 'static + Render,
{
@@ -482,12 +474,12 @@ impl VisualContext for AsyncWindowContext {
})
}
fn focus<V>(&mut self, view: &Entity<V>) -> Self::Result<()>
fn focus<V>(&mut self, view: &Entity<V>) -> Result<()>
where
V: Focusable,
{
self.app.update_window(self.window, |_, window, cx| {
view.read(cx).focus_handle(cx).focus(window);
view.read(cx).focus_handle(cx).focus(window, cx);
})
}
}