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: BackgroundExecutorThe background executor for running async tasks.
foreground_executor: ForegroundExecutorThe foreground executor for running tasks on the main thread.
Implementations§
Source§impl HeadlessAppContext
impl HeadlessAppContext
Sourcepub fn new(platform_text_system: Arc<dyn PlatformTextSystem>) -> Self
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.
Sourcepub fn with_asset_source(
platform_text_system: Arc<dyn PlatformTextSystem>,
asset_source: Arc<dyn AssetSource>,
) -> Self
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.
Sourcepub 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
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.
Sourcepub fn open_window<V: Render + 'static>(
&mut self,
size: Size<Pixels>,
build_root: impl FnOnce(&mut Window, &mut App) -> Entity<V>,
) -> Result<WindowHandle<V>>
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.
Sourcepub fn run_until_parked(&self)
pub fn run_until_parked(&self)
Runs pending tasks until none can make progress. This doesn’t move the simulated clock.
Sourcepub fn advance_clock(&self, duration: Duration)
pub fn advance_clock(&self, duration: Duration)
Moves the simulated clock forward by duration, and runs the tasks that become ready.
Sourcepub fn allow_parking(&self)
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.
Sourcepub fn forbid_parking(&self)
pub fn forbid_parking(&self)
Turns off the parking mode from HeadlessAppContext::allow_parking, returning to deterministic execution.
Sourcepub fn update<R>(&mut self, f: impl FnOnce(&mut App) -> R) -> R
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.
Sourcepub fn update_window<R>(
&mut self,
window: AnyWindowHandle,
f: impl FnOnce(AnyView, &mut Window, &mut App) -> R,
) -> Result<R>
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.
Sourcepub fn debug_bounds(
&mut self,
window: AnyWindowHandle,
selector: &str,
) -> Result<Option<Bounds<Pixels>>>
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.
Sourcepub fn solid_quad_bounds(
&mut self,
window: AnyWindowHandle,
color: Hsla,
) -> Result<Vec<Bounds<ScaledPixels>>>
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.
Sourcepub fn glyph_bounds(
&mut self,
window: AnyWindowHandle,
color: Hsla,
) -> Result<Vec<Bounds<ScaledPixels>>>
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.
Sourcepub fn underline_bounds(
&mut self,
window: AnyWindowHandle,
color: Hsla,
) -> Result<Vec<Bounds<ScaledPixels>>>
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.
Sourcepub fn capture_screenshot(
&mut self,
window: AnyWindowHandle,
) -> Result<RgbaImage>
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.
Sourcepub fn text_system(&self) -> &Arc<TextSystem> ⓘ
pub fn text_system(&self) -> &Arc<TextSystem> ⓘ
Returns the text system.
Sourcepub fn background_executor(&self) -> &BackgroundExecutor
pub fn background_executor(&self) -> &BackgroundExecutor
Returns the background executor.
Sourcepub fn foreground_executor(&self) -> &ForegroundExecutor
pub fn foreground_executor(&self) -> &ForegroundExecutor
Returns the foreground executor.
Trait Implementations§
Source§impl AppContext for HeadlessAppContext
impl AppContext for HeadlessAppContext
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, _: &Entity<T>) -> GpuiBorrow<'a, T>where
T: 'static,
fn as_mut<'a, T>(&'a mut self, _: &Entity<T>) -> GpuiBorrow<'a, T>where
T: 'static,
Source§fn read_entity<T, R>(
&self,
handle: &Entity<T>,
read: impl FnOnce(&T, &App) -> R,
) -> Rwhere
T: 'static,
fn read_entity<T, R>(
&self,
handle: &Entity<T>,
read: 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.Source§impl Drop for HeadlessAppContext
impl Drop for HeadlessAppContext
Auto Trait Implementations§
impl !RefUnwindSafe for HeadlessAppContext
impl !Send for HeadlessAppContext
impl !Sync for HeadlessAppContext
impl !UnwindSafe for HeadlessAppContext
impl Freeze for HeadlessAppContext
impl Unpin for HeadlessAppContext
impl UnsafeUnpin for HeadlessAppContext
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, 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