Skip to main content

HeadlessAppContext

Struct HeadlessAppContext 

Source
pub struct HeadlessAppContext {
    pub app: Rc<AppCell>,
    pub background_executor: BackgroundExecutor,
    pub foreground_executor: ForegroundExecutor,
    /* private fields */
}
Expand description

A headless app for tests that need real text layout, without a window on screen.

Like TestAppContext, this runs on a deterministic scheduler and a test platform. Unlike it, you supply the text system, so text is measured the way the platform measures it. Windows are hidden and unfocused. Use HeadlessAppContext::debug_bounds and the *_bounds methods to check where things were painted, and HeadlessAppContext::capture_screenshot to get pixels when a renderer is configured.

The text system comes from a platform or text crate. For example, the ParleyTextSystem in the gpui_ce_parley crate measures text with real fonts. This example uses TestTextSystem, which is for tests that don’t check text metrics:

use std::sync::Arc;

use gpui::{AppContext, HeadlessAppContext, TestTextSystem};

let mut cx = HeadlessAppContext::new(Arc::new(TestTextSystem));
let count = cx.new(|_| 1usize);
assert_eq!(cx.read_entity(&count, |count, _| *count), 1);

Fields§

§app: Rc<AppCell>

The underlying app cell.

§background_executor: BackgroundExecutor

The background executor for running async tasks.

§foreground_executor: ForegroundExecutor

The foreground executor for running tasks on the main thread.

Implementations§

Source§

impl HeadlessAppContext

Source

pub fn new(platform_text_system: Arc<dyn PlatformTextSystem>) -> Self

Creates a headless context that measures text with platform_text_system.

It has no asset source and no renderer, so HeadlessAppContext::capture_screenshot returns an error. Use HeadlessAppContext::with_platform for screenshots.

Source

pub fn with_asset_source( platform_text_system: Arc<dyn PlatformTextSystem>, asset_source: Arc<dyn AssetSource>, ) -> Self

Creates a headless context with platform_text_system and asset_source, and no renderer.

Source

pub fn with_platform( platform_text_system: Arc<dyn PlatformTextSystem>, asset_source: Arc<dyn AssetSource>, renderer_factory: impl Fn() -> Result<Option<Box<dyn PlatformHeadlessRenderer>>> + 'static, ) -> Self

Creates a headless context with a text system, an asset source and a renderer factory.

renderer_factory creates the renderer that windows draw with. Return None from it to draw without a renderer, which disables screenshots. The scheduler seed is read from SEED, and is 0 when it’s unset or isn’t a number.

Source

pub fn open_window<V: Render + 'static>( &mut self, size: Size<Pixels>, build_root: impl FnOnce(&mut Window, &mut App) -> Entity<V>, ) -> Result<WindowHandle<V>>

Opens a hidden, unfocused window of size at the origin, with a root view from build_root.

§Errors

Returns an error if the window can’t be opened.

Source

pub fn run_until_parked(&self)

Runs pending tasks until none can make progress. This doesn’t move the simulated clock.

Source

pub fn advance_clock(&self, duration: Duration)

Moves the simulated clock forward by duration, and runs the tasks that become ready.

Source

pub fn allow_parking(&self)

Lets blocking waits in the scheduler sleep in real time, for tests that wait on real I/O.

Pair it with HeadlessAppContext::forbid_parking. See BackgroundExecutor::allow_parking.

Source

pub fn forbid_parking(&self)

Turns off the parking mode from HeadlessAppContext::allow_parking, returning to deterministic execution.

Source

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

Runs f with mutable access to the App. This doesn’t run pending tasks.

Source

pub fn update_window<R>( &mut self, window: AnyWindowHandle, f: impl FnOnce(AnyView, &mut Window, &mut App) -> R, ) -> Result<R>

Runs f with the window’s root view, the Window and the App.

This doesn’t paint the window. Methods that read painted output, such as HeadlessAppContext::debug_bounds, see the last frame that was painted.

§Errors

Returns an error if window has been closed.

Source

pub fn debug_bounds( &mut self, window: AnyWindowHandle, selector: &str, ) -> Result<Option<Bounds<Pixels>>>

Returns the bounds of the element with debug_selector(selector) in the last painted frame, or None.

§Errors

Returns an error if window has been closed.

Source

pub fn solid_quad_bounds( &mut self, window: AnyWindowHandle, color: Hsla, ) -> Result<Vec<Bounds<ScaledPixels>>>

Returns the device-pixel bounds of each solid quad in color in the last painted frame.

The color must match exactly. Quads with other colors are skipped.

§Errors

Returns an error if window has been closed.

Source

pub fn glyph_bounds( &mut self, window: AnyWindowHandle, color: Hsla, ) -> Result<Vec<Bounds<ScaledPixels>>>

Returns the device-pixel bounds of each monochrome glyph painted in color in the last painted frame.

The color must match exactly.

§Errors

Returns an error if window has been closed.

Source

pub fn underline_bounds( &mut self, window: AnyWindowHandle, color: Hsla, ) -> Result<Vec<Bounds<ScaledPixels>>>

Returns the device-pixel bounds of each underline painted in color in the last painted frame.

The color must match exactly.

§Errors

Returns an error if window has been closed.

Source

pub fn capture_screenshot( &mut self, window: AnyWindowHandle, ) -> Result<RgbaImage>

Renders window’s last painted frame to an image.

§Errors

Returns an error if the context was created without a renderer, which is the case for HeadlessAppContext::new and HeadlessAppContext::with_asset_source. Also returns an error if the window has been closed.

Source

pub fn text_system(&self) -> &Arc<TextSystem> ⓘ

Returns the text system.

Source

pub fn background_executor(&self) -> &BackgroundExecutor

Returns the background executor.

Source

pub fn foreground_executor(&self) -> &ForegroundExecutor

Returns the foreground executor.

Trait Implementations§

Source§

impl AppContext for HeadlessAppContext

Source§

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

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, f: 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 Drop for HeadlessAppContext

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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, 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> 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<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