pub struct AsyncApp { /* private fields */ }Expand description
A handle to the app that you can hold across .await points.
App can’t be borrowed across an .await, because the rest of the application
needs it while your future waits. AsyncApp is the alternative: you get one as
the argument of a future started with App::spawn, or from App::to_async.
Each method borrows the app for the length of one call, runs a complete update,
and releases the borrow. Entity methods are on the AppContext trait, which
AsyncApp implements, so helpers written against that trait work here too.
AsyncApp holds a weak reference to the app, so it doesn’t keep the app alive.
Most methods panic if the app has been released. The AppContext methods that return a
Result or Option, such as update_window and with_window, report it as an error or
None instead. Keep an AsyncApp only inside the futures that use it. One stored
somewhere else can outlive the app.
See contexts for how async contexts fit with the other contexts.
§Examples
use gpui::{AppContext, TestApp};
let mut app = TestApp::new();
let count = app.update(|cx| cx.new(|_| 0_usize));
let task = app.to_async().spawn({
let count = count.clone();
async move |cx| {
cx.update(|cx| {
count.update(cx, |count, cx| {
*count += 1;
cx.notify();
})
});
}
});
task.detach();
app.run_until_parked();
app.read(|cx| assert_eq!(*count.read(cx), 1));Implementations§
Source§impl AsyncApp
impl AsyncApp
Sourcepub fn refresh(&self)
pub fn refresh(&self)
Schedules a redraw of every window.
The windows draw on the next frame. This doesn’t draw anything itself.
Sourcepub fn background_executor(&self) -> &BackgroundExecutor
pub fn background_executor(&self) -> &BackgroundExecutor
Returns the BackgroundExecutor, for running work on other threads.
Sourcepub fn is_test(&self) -> bool
pub fn is_test(&self) -> bool
Whether this app runs on the deterministic test scheduler. See
BackgroundExecutor::is_test.
Sourcepub fn foreground_executor(&self) -> &ForegroundExecutor
pub fn foreground_executor(&self) -> &ForegroundExecutor
Returns the ForegroundExecutor, for running futures on the main thread.
Sourcepub fn update<R>(&self, f: impl FnOnce(&mut App) -> R) -> R
pub fn update<R>(&self, f: impl FnOnce(&mut App) -> R) -> R
Runs f with the app and returns its result.
Effects queued by f, such as notifications, are flushed before this returns.
§Panics
Panics if the app has been released, or if it is already borrowed, such as when called from inside another update.
Sourcepub fn subscribe<T, Event>(
&mut self,
entity: &Entity<T>,
on_event: impl FnMut(Entity<T>, &Event, &mut App) + 'static,
) -> Subscriptionwhere
T: 'static + EventEmitter<Event>,
Event: 'static,
pub fn subscribe<T, Event>(
&mut self,
entity: &Entity<T>,
on_event: impl FnMut(Entity<T>, &Event, &mut App) + 'static,
) -> Subscriptionwhere
T: 'static + EventEmitter<Event>,
Event: 'static,
Calls on_event each time entity emits an Event.
The callback receives the emitting entity, the event and the app. The
subscription stays active while you hold it. Dropping it unsubscribes, and
Subscription::detach keeps the subscription for as long as the entity lives.
Inside an entity’s own context, Context::subscribe is the usual choice.
Sourcepub fn open_window<V>(
&self,
options: WindowOptions,
build_root_view: impl FnOnce(&mut Window, &mut App) -> Entity<V>,
) -> Result<WindowHandle<V>>where
V: 'static + Render,
pub fn open_window<V>(
&self,
options: WindowOptions,
build_root_view: impl FnOnce(&mut Window, &mut App) -> Entity<V>,
) -> Result<WindowHandle<V>>where
V: 'static + Render,
Opens a window with options and returns a handle to it.
build_root_view creates the window’s root view. The method returns an error if the app is
quitting.
§Panics
Panics if the app has been released, or if it is already borrowed.
Sourcepub fn spawn<AsyncFn, R>(&self, f: AsyncFn) -> Task<R> ⓘwhere
AsyncFn: AsyncFnOnce(&mut AsyncApp) -> R + 'static,
R: 'static,
pub fn spawn<AsyncFn, R>(&self, f: AsyncFn) -> Task<R> ⓘwhere
AsyncFn: AsyncFnOnce(&mut AsyncApp) -> R + 'static,
R: 'static,
Runs the future returned by f on the main thread, and returns a task for its
result.
f receives an &mut AsyncApp. Dropping the returned Task cancels the
future, so store it, await it or call Task::detach.
Sourcepub fn has_global<G: Global>(&self) -> bool
pub fn has_global<G: Global>(&self) -> bool
Returns whether a global of type G has been set.
Sourcepub fn read_global<G: Global, R>(&self, read: impl FnOnce(&G, &App) -> R) -> R
pub fn read_global<G: Global, R>(&self, read: impl FnOnce(&G, &App) -> R) -> R
Calls read with the global of type G and the app, and returns its result.
Use AsyncApp::try_read_global if the global may not be set.
§Panics
Panics if no global of type G has been set, or if the app has been released.
Sourcepub fn try_read_global<G: Global, R>(
&self,
read: impl FnOnce(&G, &App) -> R,
) -> Option<R>
pub fn try_read_global<G: Global, R>( &self, read: impl FnOnce(&G, &App) -> R, ) -> Option<R>
Like AsyncApp::read_global, but returns None instead of panicking when no
global of type G is set, or when the app is quitting.
Sourcepub fn read_default_global<G: Global + Default, R>(
&self,
read: impl FnOnce(&G, &App) -> R,
) -> R
pub fn read_default_global<G: Global + Default, R>( &self, read: impl FnOnce(&G, &App) -> R, ) -> R
Like AsyncApp::read_global, but first sets a G::default() if no global of
type G has been set.
Sourcepub fn update_global<G: Global, R>(
&self,
update: impl FnOnce(&mut G, &mut App) -> R,
) -> R
pub fn update_global<G: Global, R>( &self, update: impl FnOnce(&mut G, &mut App) -> R, ) -> R
Updates the global of type G with update, and returns its result.
While update runs, the global is taken out of the app. Reading it through the
app from inside update panics, so change the global through the &mut G that
update receives.
§Panics
Panics if no global of type G has been set, or if the app has been released.
Sourcepub fn on_drop<T: 'static, Callback: FnOnce(&mut T, &mut Context<'_, T>) + 'static>(
&self,
entity: &WeakEntity<T>,
f: Callback,
) -> Deferred<impl FnOnce() + use<T, Callback>>
pub fn on_drop<T: 'static, Callback: FnOnce(&mut T, &mut Context<'_, T>) + 'static>( &self, entity: &WeakEntity<T>, f: Callback, ) -> Deferred<impl FnOnce() + use<T, Callback>>
Returns a guard that calls f with entity when the guard is dropped.
f doesn’t run if the entity has been released by then. Hold the guard in a
variable. A guard that is dropped at once runs f immediately.
§Examples
use gpui::{AppContext, TestApp};
let mut app = TestApp::new();
let log = app.update(|cx| cx.new(|_| Vec::<&'static str>::new()));
let guard = app.update(|cx| {
cx.to_async().on_drop(&log.downgrade(), |log, cx| {
log.push("finished");
cx.notify();
})
});
drop(guard);
app.read(|cx| assert_eq!(*log.read(cx), vec!["finished"]));Trait Implementations§
Source§impl AppContext for AsyncApp
impl AppContext for AsyncApp
Source§fn new<T: 'static>(
&mut self,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Entity<T>
fn new<T: 'static>( &mut self, build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Entity<T>
build_entity, and returns a handle to it. Read moreSource§fn reserve_entity<T: 'static>(&mut self) -> Reservation<T>
fn reserve_entity<T: 'static>(&mut self) -> Reservation<T>
Reservation for it. Read moreSource§fn insert_entity<T: 'static>(
&mut self,
reservation: Reservation<T>,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Entity<T>
fn insert_entity<T: 'static>( &mut self, reservation: Reservation<T>, build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Entity<T>
Source§fn update_entity<T: 'static, R>(
&mut self,
handle: &Entity<T>,
update: impl FnOnce(&mut T, &mut Context<'_, T>) -> R,
) -> R
fn update_entity<T: 'static, R>( &mut self, handle: &Entity<T>, update: impl FnOnce(&mut T, &mut Context<'_, T>) -> R, ) -> R
Source§fn as_mut<'a, T>(&'a mut self, _handle: &Entity<T>) -> GpuiBorrow<'a, T>where
T: 'static,
fn as_mut<'a, T>(&'a mut self, _handle: &Entity<T>) -> GpuiBorrow<'a, T>where
T: 'static,
Source§fn read_entity<T, R>(
&self,
handle: &Entity<T>,
callback: impl FnOnce(&T, &App) -> R,
) -> Rwhere
T: 'static,
fn read_entity<T, R>(
&self,
handle: &Entity<T>,
callback: impl FnOnce(&T, &App) -> R,
) -> Rwhere
T: 'static,
read. Read moreSource§fn notify(&mut self, entity_id: EntityId)
fn notify(&mut self, entity_id: EntityId)
entity_id changed. Its observers run and windows
that show it redraw. Read moreSource§fn emit<EntityType, EventType>(
&mut self,
entity: &Entity<EntityType>,
event: EventType,
)where
EntityType: EventEmitter<EventType>,
EventType: 'static,
fn emit<EntityType, EventType>(
&mut self,
entity: &Entity<EntityType>,
event: EventType,
)where
EntityType: EventEmitter<EventType>,
EventType: 'static,
Source§fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
f with the window’s root view, the window and the app, and returns the
result. Read moreSource§fn with_window<R>(
&mut self,
entity_id: EntityId,
f: impl FnOnce(&mut Window, &mut App) -> R,
) -> Option<R>
fn with_window<R>( &mut self, entity_id: EntityId, f: impl FnOnce(&mut Window, &mut App) -> R, ) -> Option<R>
f with the window that most recently rendered the entity entity_id, and
returns the result. Read moreSource§fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(Entity<T>, &App) -> R,
) -> Result<R>where
T: 'static,
fn read_window<T, R>(
&self,
window: &WindowHandle<T>,
read: impl FnOnce(Entity<T>, &App) -> R,
) -> Result<R>where
T: 'static,
window with the app, and returns the result. Read moreSource§fn background_spawn<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> Task<R> ⓘwhere
R: Send + 'static,
fn background_spawn<R>(
&self,
future: impl Future<Output = R> + Send + 'static,
) -> Task<R> ⓘwhere
R: Send + 'static,
future on the background executor and returns a task for its result. Read moreSource§fn insert_global_entity<T: 'static>(&mut self, entity: Entity<T>)
fn insert_global_entity<T: 'static>(&mut self, entity: Entity<T>)
entity alive without a handle to it. Read moreSource§fn remove_global_entity<T: 'static>(&mut self, entity: &Entity<T>)
fn remove_global_entity<T: 'static>(&mut self, entity: &Entity<T>)
entity alive. It’s dropped once no other handle refers to it.Source§fn global_entities<T: 'static>(&self) -> impl Iterator<Item = Entity<T>>
fn global_entities<T: 'static>(&self) -> impl Iterator<Item = Entity<T>>
AppContext::insert_global_entity whose type is
T.Auto Trait Implementations§
impl !RefUnwindSafe for AsyncApp
impl !Send for AsyncApp
impl !Sync for AsyncApp
impl !UnwindSafe for AsyncApp
impl Freeze for AsyncApp
impl Unpin for AsyncApp
impl UnsafeUnpin for AsyncApp
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
replaced by palette::chromatic_adaptation::AdaptIntoUnclamped
Source§impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
impl<T, C> AdaptIntoUnclamped<T> for Cwhere
T: AdaptFromUnclamped<C>,
Source§type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
type Scalar = <T as AdaptFromUnclamped<C>>::Scalar
Source§fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
fn adapt_into_unclamped_with<M>(self) -> Twhere
M: LmsToXyz<<C as AdaptIntoUnclamped<T>>::Scalar> + XyzToLms<<C as AdaptIntoUnclamped<T>>::Scalar>,
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T, C> ComponentsInto<C> for T
impl<T, C> ComponentsInto<C> for T
Source§fn components_into(self) -> C
fn components_into(self) -> C
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more