Skip to main content

AsyncWindowContext

Struct AsyncWindowContext 

Source
pub struct AsyncWindowContext { /* private fields */ }
Expand description

A handle to the app and one window, for use across .await points.

Get one from Context::spawn_in or Window::spawn. It derefs to AsyncApp, so the methods of AsyncApp are available on it too. The window methods on this type return an error once the window has closed.

Use it inside a future when you need the window and not only the app. See contexts.

§Examples

use gpui::{Context, Render, TestApp, Window, div, prelude::*};

struct Panel {
    updates: usize,
}

impl Render for Panel {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        div()
    }
}

let mut app = TestApp::new();
let mut window = app.open_window(|_window, _cx| Panel { updates: 0 });

window.update(|panel, window, cx| {
    cx.spawn_in(window, async move |panel, cx| {
        panel
            .update(cx, |panel, cx| {
                panel.updates += 1;
                cx.notify();
            })
            .ok();
    })
    .detach();
});

app.run_until_parked();
window.read(|panel, _cx| assert_eq!(panel.updates, 1));

Implementations§

Source§

impl AsyncWindowContext

Source

pub fn window_handle(&self) -> AnyWindowHandle

Returns the handle of the window this context is for.

Source

pub fn update<R>( &mut self, update: impl FnOnce(&mut Window, &mut App) -> R, ) -> Result<R>

Runs update with the window and the app, and returns its result.

Effects queued by update are flushed before this returns.

§Errors

Returns an error if the window has closed, or if the app is quitting or already borrowed.

Source

pub fn update_root<R>( &mut self, update: impl FnOnce(AnyView, &mut Window, &mut App) -> R, ) -> Result<R>

Like AsyncWindowContext::update, but also passes the window’s root view.

§Errors

Returns an error under the same conditions as AsyncWindowContext::update.

Source

pub fn on_next_frame(&mut self, f: impl FnOnce(&mut Window, &mut App) + 'static)

Runs f before the window draws its next frame. Does nothing if the window has closed.

Schedules the callback like Window::on_next_frame, but tolerates a window that has already been closed.

Source

pub fn read_global<G: Global, R>( &mut self, read: impl FnOnce(&G, &Window, &App) -> R, ) -> Result<R>

Calls read with the global of type G, the window and the app, and returns its result.

§Errors

Returns an error if the window has closed.

§Panics

Panics if no global of type G has been set.

Source

pub fn update_global<G, R>( &mut self, update: impl FnOnce(&mut G, &mut Window, &mut App) -> R, ) -> Result<R>
where G: Global,

Updates the global of type G with update, and returns its result.

The global is taken out of the app while update runs, as with AsyncApp::update_global.

§Errors

Returns an error if the window has closed.

§Panics

Panics if no global of type G has been set.

Source

pub fn spawn<AsyncFn, R>(&self, f: AsyncFn) -> Task<R> ⓘ
where AsyncFn: AsyncFnOnce(&mut AsyncWindowContext) -> R + 'static, R: 'static,

Runs the future returned by f on the main thread, with an AsyncWindowContext for this window, and returns a task for its result.

Dropping the returned Task cancels the future. Store it, await it or call Task::detach.

Source

pub fn prompt<T>( &mut self, level: PromptLevel, message: &str, detail: Option<&str>, answers: &[T], ) -> Receiver<usize> ⓘ
where T: Clone + Into<PromptButton>,

Shows a platform alert with one button for each entry in answers.

The receiver resolves to the index of the button the user clicked. If the window has closed, the receiver is canceled, so awaiting it returns an error. See Window::prompt for level and detail.

Methods from Deref<Target = AsyncApp>§

Source

pub fn refresh(&self)

Schedules a redraw of every window.

The windows draw on the next frame. This doesn’t draw anything itself.

Source

pub fn background_executor(&self) -> &BackgroundExecutor

Returns the BackgroundExecutor, for running work on other threads.

Source

pub fn is_test(&self) -> bool

Whether this app runs on the deterministic test scheduler. See BackgroundExecutor::is_test.

Source

pub fn foreground_executor(&self) -> &ForegroundExecutor

Returns the ForegroundExecutor, for running futures on the main thread.

Source

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.

Source

pub fn subscribe<T, Event>( &mut self, entity: &Entity<T>, on_event: impl FnMut(Entity<T>, &Event, &mut App) + 'static, ) -> Subscription
where 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.

Source

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.

Source

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.

Source

pub fn has_global<G: Global>(&self) -> bool

Returns whether a global of type G has been set.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 AsyncWindowContext

Source§

fn new<T>( &mut self, build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Entity<T>
where T: 'static,

Creates an entity from build_entity, and returns a handle to it. Read more
Source§

fn reserve_entity<T: 'static>(&mut self) -> Reservation<T>

Reserves an entity slot, and returns a Reservation for it. Read more
Source§

fn insert_entity<T: 'static>( &mut self, reservation: Reservation<T>, build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Entity<T>

Creates the entity for a Reservation from AppContext::reserve_entity. Read more
Source§

fn update_entity<T: 'static, R>( &mut self, handle: &Entity<T>, update: impl FnOnce(&mut T, &mut Context<'_, T>) -> R, ) -> R

Runs update with the entity and a Context for it, and returns the result. Read more
Source§

fn as_mut<'a, T>(&'a mut self, _: &Entity<T>) -> GpuiBorrow<'a, T>
where T: 'static,

Borrows the entity mutably, without a closure. Read more
Source§

fn read_entity<T, R>( &self, handle: &Entity<T>, read: impl FnOnce(&T, &App) -> R, ) -> R
where T: 'static,

Reads the entity with a shared borrow, and returns the result of read. Read more
Source§

fn notify(&mut self, entity_id: EntityId)

Tells GPUI that the entity with entity_id changed. Its observers run and windows that show it redraw. Read more
Source§

fn emit<EntityType, EventType>( &mut self, entity: &Entity<EntityType>, event: EventType, )
where EntityType: EventEmitter<EventType>, EventType: 'static,

Emits event from entity. Read more
Source§

fn update_window<T, F>( &mut self, window: AnyWindowHandle, update: F, ) -> Result<T>
where F: FnOnce(AnyView, &mut Window, &mut App) -> T,

Runs f with the window’s root view, the window and the app, and returns the result. Read more
Source§

fn with_window<R>( &mut self, entity_id: EntityId, f: impl FnOnce(&mut Window, &mut App) -> R, ) -> Option<R>

Runs f with the window that most recently rendered the entity entity_id, and returns the result. Read more
Source§

fn read_window<T, R>( &self, window: &WindowHandle<T>, read: impl FnOnce(Entity<T>, &App) -> R, ) -> Result<R>
where T: 'static,

Reads the root view of window with the app, and returns the result. Read more
Source§

fn background_spawn<R>( &self, future: impl Future<Output = R> + Send + 'static, ) -> Task<R> ⓘ
where R: Send + 'static,

Runs future on the background executor and returns a task for its result. Read more
Source§

fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> R
where G: Global,

Calls callback with the global of type G and the app, and returns the result. Read more
Source§

fn insert_global_entity<T: 'static>(&mut self, entity: Entity<T>)

Keeps entity alive without a handle to it. Read more
Source§

fn remove_global_entity<T: 'static>(&mut self, entity: &Entity<T>)

Stops keeping entity alive. It’s dropped once no other handle refers to it.
Source§

fn global_entities<T: 'static>(&self) -> impl Iterator<Item = Entity<T>>

Returns the entities stored with AppContext::insert_global_entity whose type is T.
Source§

impl Clone for AsyncWindowContext

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Deref for AsyncWindowContext

Source§

type Target = AsyncApp

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for AsyncWindowContext

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl VisualContext for AsyncWindowContext

Source§

type Result<T> = Result<T, Error>

The return type of window operations. Read more
Source§

fn window_handle(&self) -> AnyWindowHandle

Returns the handle of the window this context works in.
Source§

fn new_window_entity<T: 'static>( &mut self, build_entity: impl FnOnce(&mut Window, &mut Context<'_, T>) -> T, ) -> Result<Entity<T>>

Creates an entity with access to the window. Read more
Source§

fn update_window_entity<T: 'static, R>( &mut self, view: &Entity<T>, update: impl FnOnce(&mut T, &mut Window, &mut Context<'_, T>) -> R, ) -> Result<R>

Runs update with the entity, its window and a Context for the entity, and returns the result, wrapped in VisualContext::Result.
Source§

fn replace_root_view<V>( &mut self, build_view: impl FnOnce(&mut Window, &mut Context<'_, V>) -> V, ) -> Result<Entity<V>>
where V: 'static + Render,

Replaces the root view of the window with the view that build_view creates, and returns a handle to the new view.
Source§

fn focus<V>(&mut self, view: &Entity<V>) -> Result<()>
where V: Focusable,

Moves keyboard focus to entity, using its Focusable::focus_handle.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
where 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) -> D
where M: TransformMatrix<T>,

👎Deprecated since 0.7.7:

replaced by palette::chromatic_adaptation::AdaptIntoUnclamped

Convert the source color to the destination color using the specified method.
Source§

fn adapt_into(self) -> D

👎Deprecated since 0.7.7:

replaced by palette::chromatic_adaptation::AdaptIntoUnclamped

Convert the source color to the destination color using the bradford method by default.
Source§

impl<T, C> AdaptIntoUnclamped<T> for C
where T: AdaptFromUnclamped<C>,

Source§

type Scalar = <T as AdaptFromUnclamped<C>>::Scalar

The number type that’s used as the color’s components.
Source§

fn adapt_into_unclamped_with<M>(self) -> T

Adapt a color of type Self into a color of type T, using the custom matrix M. Read more
Source§

fn adapt_into_unclamped(self) -> T
where Bradford: LmsToXyz<Self::Scalar> + XyzToLms<Self::Scalar>,

Adapt a color of type Self into a color of type T, using the Bradford matrix. Read more
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T, C> ArraysFrom<C> for T
where C: IntoArrays<T>,

Source§

fn arrays_from(colors: C) -> T

Cast a collection of colors into a collection of arrays.
Source§

impl<T, C> ArraysInto<C> for T
where C: FromArrays<T>,

Source§

fn arrays_into(self) -> C

Cast this collection of arrays into a collection of colors.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for U
where T: FromCam16Unclamped<WpParam, U>,

Source§

type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T, C> ComponentsFrom<C> for T
where C: IntoComponents<T>,

Source§

fn components_from(colors: C) -> T

Cast a collection of colors into a collection of color components.
Source§

impl<T, C> ComponentsInto<C> for T

Source§

fn components_into(self) -> C

Cast this collection of color components into a collection of colors. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromAngle<T> for T

Source§

fn from_angle(angle: T) -> T

Performs a conversion from angle.
Source§

impl<T, U> FromStimulus<U> for T
where U: IntoStimulus<T>,

Source§

fn from_stimulus(other: U) -> T

Converts other into Self, while performing the appropriate scaling, rounding and clamping.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> IntoAngle<U> for T
where U: FromAngle<T>,

Source§

fn into_angle(self) -> U

Performs a conversion into T.
Source§

impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for U
where T: Cam16FromUnclamped<WpParam, U>,

Source§

type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar

The number type that’s used in parameters when converting.
Source§

fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T

Converts self into C, using the provided parameters.
Source§

impl<T, U> IntoColor<U> for T
where U: FromColor<T>,

Source§

fn into_color(self) -> U

Convert into T with values clamped to the color defined bounds Read more
Source§

impl<T, U> IntoColorUnclamped<U> for T
where U: FromColorUnclamped<T>,

Source§

fn into_color_unclamped(self) -> U

Convert into T. The resulting color might be invalid in its color space Read more
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoStimulus<T> for T

Source§

fn into_stimulus(self) -> T

Converts self into T, while performing the appropriate scaling, rounding and clamping.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, C> TryComponentsInto<C> for T
where C: TryFromComponents<T>,

Source§

type Error = <C as TryFromComponents<T>>::Error

The error for when try_into_colors fails to cast.
Source§

fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>

Try to cast this collection of color components into a collection of colors. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryIntoColor<U> for T
where U: TryFromColor<T>,

Source§

fn try_into_color(self) -> Result<U, OutOfBounds<U>>

Convert into T, returning ok if the color is inside of its defined range, otherwise an OutOfBounds error is returned which contains the unclamped color. Read more
Source§

impl<C, U> UintsFrom<C> for U
where C: IntoUints<U>,

Source§

fn uints_from(colors: C) -> U

Cast a collection of colors into a collection of unsigned integers.
Source§

impl<C, U> UintsInto<C> for U
where C: FromUints<U>,

Source§

fn uints_into(self) -> C

Cast this collection of unsigned integers into a collection of colors.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more