gpui: Update docs to reflect removal of View, ViewContext, WindowContext (#24008)

This PR updates function signatures, docstrings, and gpui's other
documentation to reflect it's new state following the merge of `Model`
and `View` into `Entity` as well as the removal of `WindowContext`.

Release Notes:

- N/A
This commit is contained in:
someone13574
2025-01-31 11:40:42 -08:00
committed by GitHub
parent 027fe1b4b5
commit 0c94bdc8e4
25 changed files with 330 additions and 329 deletions
+22 -22
View File
@@ -10,9 +10,9 @@ use std::{future::Future, rc::Weak};
use super::Context;
/// An async-friendly version of [AppContext] with a static lifetime so it can be held across `await` points in async code.
/// You're provided with an instance when calling [AppContext::spawn], and you can also create one with [AppContext::to_async].
/// Internally, this holds a weak reference to an `AppContext`, so its methods are fallible to protect against cases where the [AppContext] is dropped.
/// 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.
#[derive(Clone)]
pub struct AsyncApp {
pub(crate) app: Weak<AppCell>,
@@ -25,14 +25,14 @@ impl AppContext for AsyncApp {
fn new<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Context<'_, T>) -> T,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Self::Result<Entity<T>> {
let app = self
.app
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut app = app.borrow_mut();
Ok(app.new(build_model))
Ok(app.new(build_entity))
}
fn reserve_entity<T: 'static>(&mut self) -> Result<Reservation<T>> {
@@ -47,14 +47,14 @@ impl AppContext for AsyncApp {
fn insert_entity<T: 'static>(
&mut self,
reservation: Reservation<T>,
build_model: impl FnOnce(&mut Context<'_, T>) -> T,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Result<Entity<T>> {
let app = self
.app
.upgrade()
.ok_or_else(|| anyhow!("app was released"))?;
let mut app = app.borrow_mut();
Ok(app.insert_entity(reservation, build_model))
Ok(app.insert_entity(reservation, build_entity))
}
fn update_entity<T: 'static, R>(
@@ -181,7 +181,7 @@ impl AsyncApp {
}
/// Determine whether global state of the specified type has been assigned.
/// Returns an error if the `AppContext` has been dropped.
/// Returns an error if the `App` has been dropped.
pub fn has_global<G: Global>(&self) -> Result<bool> {
let app = self
.app
@@ -194,7 +194,7 @@ impl AsyncApp {
/// 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 `AppContext` has been dropped.
/// 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
@@ -209,14 +209,14 @@ impl AsyncApp {
/// 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 `AppContext` has been dropped.
/// 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 = app.borrow_mut();
Some(read(app.try_global()?, &app))
}
/// A convenience method for [AppContext::update_global]
/// A convenience method for [App::update_global]
/// for updating the global state of the specified type.
pub fn update_global<G: Global, R>(
&self,
@@ -251,13 +251,13 @@ impl AsyncWindowContext {
self.window
}
/// A convenience method for [`AppContext::update_window`].
/// A convenience method for [`App::update_window`].
pub fn update<R>(&mut self, update: impl FnOnce(&mut Window, &mut App) -> R) -> Result<R> {
self.app
.update_window(self.window, |_, window, cx| update(window, cx))
}
/// A convenience method for [`AppContext::update_window`].
/// A convenience method for [`App::update_window`].
pub fn update_root<R>(
&mut self,
update: impl FnOnce(AnyView, &mut Window, &mut App) -> R,
@@ -272,7 +272,7 @@ impl AsyncWindowContext {
.ok();
}
/// A convenience method for [`AppContext::global`].
/// A convenience method for [`App::global`].
pub fn read_global<G: Global, R>(
&mut self,
read: impl FnOnce(&G, &Window, &App) -> R,
@@ -281,7 +281,7 @@ impl AsyncWindowContext {
.update(self, |_, window, cx| read(cx.global(), window, cx))
}
/// A convenience method for [`AppContext::update_global`].
/// A convenience method for [`App::update_global`].
/// for updating the global state of the specified type.
pub fn update_global<G, R>(
&mut self,
@@ -326,11 +326,11 @@ impl AsyncWindowContext {
impl AppContext for AsyncWindowContext {
type Result<T> = Result<T>;
fn new<T>(&mut self, build_model: impl FnOnce(&mut Context<'_, T>) -> T) -> Result<Entity<T>>
fn new<T>(&mut self, build_entity: impl FnOnce(&mut Context<'_, T>) -> T) -> Result<Entity<T>>
where
T: 'static,
{
self.window.update(self, |_, _, cx| cx.new(build_model))
self.window.update(self, |_, _, cx| cx.new(build_entity))
}
fn reserve_entity<T: 'static>(&mut self) -> Result<Reservation<T>> {
@@ -340,10 +340,10 @@ impl AppContext for AsyncWindowContext {
fn insert_entity<T: 'static>(
&mut self,
reservation: Reservation<T>,
build_model: impl FnOnce(&mut Context<'_, T>) -> T,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Self::Result<Entity<T>> {
self.window
.update(self, |_, _, cx| cx.insert_entity(reservation, build_model))
.update(self, |_, _, cx| cx.insert_entity(reservation, build_entity))
}
fn update_entity<T: 'static, R>(
@@ -406,10 +406,10 @@ impl VisualContext for AsyncWindowContext {
fn new_window_entity<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Window, &mut Context<T>) -> T,
build_entity: impl FnOnce(&mut Window, &mut Context<T>) -> T,
) -> Self::Result<Entity<T>> {
self.window
.update(self, |_, window, cx| cx.new(|cx| build_model(window, cx)))
.update(self, |_, window, cx| cx.new(|cx| build_entity(window, cx)))
}
fn update_window_entity<T: 'static, R>(
@@ -418,7 +418,7 @@ impl VisualContext for AsyncWindowContext {
update: impl FnOnce(&mut T, &mut Window, &mut Context<T>) -> R,
) -> Self::Result<R> {
self.window.update(self, |_, window, cx| {
view.update(cx, |model, cx| update(model, window, cx))
view.update(cx, |entity, cx| update(entity, window, cx))
})
}
+32 -29
View File
@@ -15,39 +15,39 @@ use std::{
use super::{App, AsyncWindowContext, Entity, KeystrokeEvent};
/// The app context, with specialized behavior for the given model.
/// The app context, with specialized behavior for the given entity.
#[derive(Deref, DerefMut)]
pub struct Context<'a, T> {
#[deref]
#[deref_mut]
app: &'a mut App,
model_state: WeakEntity<T>,
entity_state: WeakEntity<T>,
}
impl<'a, T: 'static> Context<'a, T> {
pub(crate) fn new_context(app: &'a mut App, model_state: WeakEntity<T>) -> Self {
Self { app, model_state }
pub(crate) fn new_context(app: &'a mut App, entity_state: WeakEntity<T>) -> Self {
Self { app, entity_state }
}
/// The entity id of the model backing this context.
/// The entity id of the entity backing this context.
pub fn entity_id(&self) -> EntityId {
self.model_state.entity_id
self.entity_state.entity_id
}
/// Returns a handle to the model belonging to this context.
/// Returns a handle to the entity belonging to this context.
pub fn entity(&self) -> Entity<T> {
self.weak_entity()
.upgrade()
.expect("The entity must be alive if we have a model context")
.expect("The entity must be alive if we have a entity context")
}
/// Returns a weak handle to the model belonging to this context.
/// Returns a weak handle to the entity belonging to this context.
pub fn weak_entity(&self) -> WeakEntity<T> {
self.model_state.clone()
self.entity_state.clone()
}
/// Arranges for the given function to be called whenever [`ModelContext::notify`] or
/// [`ViewContext::notify`](crate::ViewContext::notify) is called with the given model or view.
/// Arranges for the given function to be called whenever [`Context::notify`] is
/// called with the given entity.
pub fn observe<W>(
&mut self,
entity: &Entity<W>,
@@ -68,7 +68,7 @@ impl<'a, T: 'static> Context<'a, T> {
})
}
/// Subscribe to an event type from another model or view
/// Subscribe to an event type from another entity
pub fn subscribe<T2, Evt>(
&mut self,
entity: &Entity<T2>,
@@ -90,13 +90,13 @@ impl<'a, T: 'static> Context<'a, T> {
})
}
/// Register a callback to be invoked when GPUI releases this model.
/// Register a callback to be invoked when GPUI releases this entity.
pub fn on_release(&self, on_release: impl FnOnce(&mut T, &mut App) + 'static) -> Subscription
where
T: 'static,
{
let (subscription, activate) = self.app.release_listeners.insert(
self.model_state.entity_id,
self.entity_state.entity_id,
Box::new(move |this, cx| {
let this = this.downcast_mut().expect("invalid entity type");
on_release(this, cx);
@@ -106,7 +106,7 @@ impl<'a, T: 'static> Context<'a, T> {
subscription
}
/// Register a callback to be run on the release of another model or view
/// Register a callback to be run on the release of another entity
pub fn observe_release<T2>(
&self,
entity: &Entity<T2>,
@@ -175,13 +175,13 @@ impl<'a, T: 'static> Context<'a, T> {
subscription
}
/// Tell GPUI that this model has changed and observers of it should be notified.
/// Tell GPUI that this entity has changed and observers of it should be notified.
pub fn notify(&mut self) {
self.app.notify(self.model_state.entity_id);
self.app.notify(self.entity_state.entity_id);
}
/// Spawn the future returned by the given function.
/// The function is provided a weak handle to the model owned by this context and a context that can be held across await points.
/// The function is provided a weak handle to the entity owned by this context and a context that can be held across await points.
/// The returned task must be held or detached.
pub fn spawn<Fut, R>(&self, f: impl FnOnce(WeakEntity<T>, AsyncApp) -> Fut) -> Task<R>
where
@@ -238,7 +238,7 @@ impl<'a, T: 'static> Context<'a, T> {
});
}
/// Observe another model or view for changes to its state, as tracked by [`ModelContext::notify`].
/// Observe another entity for changes to its state, as tracked by [`Context::notify`].
pub fn observe_in<V2>(
&mut self,
observed: &Entity<V2>,
@@ -274,9 +274,9 @@ impl<'a, T: 'static> Context<'a, T> {
)
}
/// Subscribe to events emitted by another model or view.
/// Subscribe to events emitted by another entity.
/// The entity to which you're subscribing must implement the [`EventEmitter`] trait.
/// The callback will be invoked with a reference to the current view, a handle to the emitting entity (either a [`View`] or [`Model`]), the event, and a view context for the current view.
/// The callback will be invoked with a reference to the current view, a handle to the emitting `Entity`, the event, a mutable reference to the `Window`, and the context for the entity.
pub fn subscribe_in<Emitter, Evt>(
&mut self,
emitter: &Entity<Emitter>,
@@ -329,7 +329,7 @@ impl<'a, T: 'static> Context<'a, T> {
self.app.observe_release_in(&entity, window, on_release)
}
/// Register a callback to be invoked when the given Model or View is released.
/// Register a callback to be invoked when the given Entity is released.
pub fn observe_release_in<T2>(
&self,
observed: &Entity<T2>,
@@ -580,7 +580,7 @@ impl<'a, T: 'static> Context<'a, T> {
}
/// Schedule a future to be run asynchronously.
/// The given callback is invoked with a [`WeakModel<V>`] to avoid leaking the view for a long-running process.
/// The given callback is invoked with a [`WeakEntity<V>`] to avoid leaking the view for a long-running process.
/// It's also given an [`AsyncWindowContext`], which can be used to access the state of the view across await points.
/// The returned future will be polled on the main thread.
pub fn spawn_in<Fut, R>(
@@ -655,7 +655,7 @@ impl<'a, T> Context<'a, T> {
Evt: 'static,
{
self.app.pending_effects.push_back(Effect::Emit {
emitter: self.model_state.entity_id,
emitter: self.entity_state.entity_id,
event_type: TypeId::of::<Evt>(),
event: Box::new(event),
});
@@ -665,8 +665,11 @@ impl<'a, T> Context<'a, T> {
impl<'a, T> AppContext for Context<'a, T> {
type Result<U> = U;
fn new<U: 'static>(&mut self, build_model: impl FnOnce(&mut Context<'_, U>) -> U) -> Entity<U> {
self.app.new(build_model)
fn new<U: 'static>(
&mut self,
build_entity: impl FnOnce(&mut Context<'_, U>) -> U,
) -> Entity<U> {
self.app.new(build_entity)
}
fn reserve_entity<U: 'static>(&mut self) -> Reservation<U> {
@@ -676,9 +679,9 @@ impl<'a, T> AppContext for Context<'a, T> {
fn insert_entity<U: 'static>(
&mut self,
reservation: Reservation<U>,
build_model: impl FnOnce(&mut Context<'_, U>) -> U,
build_entity: impl FnOnce(&mut Context<'_, U>) -> U,
) -> Self::Result<Entity<U>> {
self.app.insert_entity(reservation, build_model)
self.app.insert_entity(reservation, build_entity)
}
fn update_entity<U: 'static, R>(
+42 -42
View File
@@ -25,7 +25,7 @@ use collections::HashMap;
use super::Context;
slotmap::new_key_type! {
/// A unique identifier for a model or view across the application.
/// A unique identifier for a entity across the application.
pub struct EntityId;
}
@@ -97,9 +97,9 @@ impl EntityMap {
let mut accessed_entities = self.accessed_entities.borrow_mut();
accessed_entities.insert(slot.entity_id);
let model = slot.0;
self.entities.insert(model.entity_id, Box::new(entity));
model
let handle = slot.0;
self.entities.insert(handle.entity_id, Box::new(entity));
handle
}
/// Move an entity to the stack.
@@ -127,21 +127,21 @@ impl EntityMap {
.insert(lease.pointer.entity_id, lease.entity.take().unwrap());
}
pub fn read<T: 'static>(&self, model: &Entity<T>) -> &T {
self.assert_valid_context(model);
pub fn read<T: 'static>(&self, entity: &Entity<T>) -> &T {
self.assert_valid_context(entity);
let mut accessed_entities = self.accessed_entities.borrow_mut();
accessed_entities.insert(model.entity_id);
accessed_entities.insert(entity.entity_id);
self.entities
.get(model.entity_id)
.get(entity.entity_id)
.and_then(|entity| entity.downcast_ref())
.unwrap_or_else(|| double_lease_panic::<T>("read"))
}
fn assert_valid_context(&self, model: &AnyEntity) {
fn assert_valid_context(&self, entity: &AnyEntity) {
debug_assert!(
Weak::ptr_eq(&model.entity_map, &Arc::downgrade(&self.ref_counts)),
"used a model with the wrong context"
Weak::ptr_eq(&entity.entity_map, &Arc::downgrade(&self.ref_counts)),
"used a entity with the wrong context"
);
}
@@ -216,7 +216,7 @@ impl<'a, T> Drop for Lease<'a, T> {
#[derive(Deref, DerefMut)]
pub(crate) struct Slot<T>(Entity<T>);
/// A dynamically typed reference to a model, which can be downcast into a `Model<T>`.
/// A dynamically typed reference to a entity, which can be downcast into a `Entity<T>`.
pub struct AnyEntity {
pub(crate) entity_id: EntityId,
pub(crate) entity_type: TypeId,
@@ -241,17 +241,17 @@ impl AnyEntity {
}
}
/// Returns the id associated with this model.
/// Returns the id associated with this entity.
pub fn entity_id(&self) -> EntityId {
self.entity_id
}
/// Returns the [TypeId] associated with this model.
/// Returns the [TypeId] associated with this entity.
pub fn entity_type(&self) -> TypeId {
self.entity_type
}
/// Converts this model handle into a weak variant, which does not prevent it from being released.
/// Converts this entity handle into a weak variant, which does not prevent it from being released.
pub fn downgrade(&self) -> AnyWeakEntity {
AnyWeakEntity {
entity_id: self.entity_id,
@@ -260,8 +260,8 @@ impl AnyEntity {
}
}
/// Converts this model handle into a strongly-typed model handle of the given type.
/// If this model handle is not of the specified type, returns itself as an error variant.
/// Converts this entity handle into a strongly-typed entity handle of the given type.
/// If this entity handle is not of the specified type, returns itself as an error variant.
pub fn downcast<T: 'static>(self) -> Result<Entity<T>, AnyEntity> {
if TypeId::of::<T>() == self.entity_type {
Ok(Entity {
@@ -281,9 +281,9 @@ impl Clone for AnyEntity {
let count = entity_map
.counts
.get(self.entity_id)
.expect("detected over-release of a model");
.expect("detected over-release of a entity");
let prev_count = count.fetch_add(1, SeqCst);
assert_ne!(prev_count, 0, "Detected over-release of a model.");
assert_ne!(prev_count, 0, "Detected over-release of a entity.");
}
Self {
@@ -311,7 +311,7 @@ impl Drop for AnyEntity {
.get(self.entity_id)
.expect("detected over-release of a handle.");
let prev_count = count.fetch_sub(1, SeqCst);
assert_ne!(prev_count, 0, "Detected over-release of a model.");
assert_ne!(prev_count, 0, "Detected over-release of a entity.");
if prev_count == 1 {
// We were the last reference to this entity, so we can remove it.
let mut entity_map = RwLockUpgradableReadGuard::upgrade(entity_map);
@@ -330,8 +330,8 @@ impl Drop for AnyEntity {
}
impl<T> From<Entity<T>> for AnyEntity {
fn from(model: Entity<T>) -> Self {
model.any_entity
fn from(entity: Entity<T>) -> Self {
entity.any_entity
}
}
@@ -351,7 +351,7 @@ impl Eq for AnyEntity {}
impl std::fmt::Debug for AnyEntity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AnyModel")
f.debug_struct("AnyEntity")
.field("entity_id", &self.entity_id.as_u64())
.finish()
}
@@ -406,7 +406,7 @@ impl<T: 'static> Entity<T> {
})
}
/// Convert this into a dynamically typed model.
/// Convert this into a dynamically typed entity.
pub fn into_any(self) -> AnyEntity {
self.any_entity
}
@@ -416,7 +416,7 @@ impl<T: 'static> Entity<T> {
cx.entities.read(self)
}
/// Read the entity referenced by this model with the given function.
/// Read the entity referenced by this handle with the given function.
pub fn read_with<R, C: AppContext>(
&self,
cx: &C,
@@ -425,11 +425,11 @@ impl<T: 'static> Entity<T> {
cx.read_entity(self, f)
}
/// Updates the entity referenced by this model with the given function.
/// Updates the entity referenced by this handle with the given function.
///
/// The update function receives a context appropriate for its environment.
/// When updating in an `AppContext`, it receives a `ModelContext`.
/// When updating in a `WindowContext`, it receives a `ViewContext`.
/// When updating in an `App`, it receives a `Context`.
/// When updating in a `Window`, it receives a `Window` and a `Context`.
pub fn update<C, R>(
&self,
cx: &mut C,
@@ -441,7 +441,7 @@ impl<T: 'static> Entity<T> {
cx.update_entity(self, update)
}
/// Updates the entity referenced by this model with the given function if
/// Updates the entity referenced by this handle with the given function if
/// the referenced entity still exists, within a visual context that has a window.
/// Returns an error if the entity has been released.
pub fn update_in<C, R>(
@@ -467,7 +467,7 @@ impl<T> Clone for Entity<T> {
impl<T> std::fmt::Debug for Entity<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Model")
f.debug_struct("Entity")
.field("entity_id", &self.any_entity.entity_id)
.field("entity_type", &type_name::<T>())
.finish()
@@ -494,7 +494,7 @@ impl<T> PartialEq<WeakEntity<T>> for Entity<T> {
}
}
/// A type erased, weak reference to a model.
/// A type erased, weak reference to a entity.
#[derive(Clone)]
pub struct AnyWeakEntity {
pub(crate) entity_id: EntityId,
@@ -508,7 +508,7 @@ impl AnyWeakEntity {
self.entity_id
}
/// Check if this weak handle can be upgraded, or if the model has already been dropped
/// Check if this weak handle can be upgraded, or if the entity has already been dropped
pub fn is_upgradable(&self) -> bool {
let ref_count = self
.entity_ref_counts
@@ -518,7 +518,7 @@ impl AnyWeakEntity {
ref_count > 0
}
/// Upgrade this weak model reference to a strong reference.
/// Upgrade this weak entity reference to a strong reference.
pub fn upgrade(&self) -> Option<AnyEntity> {
let ref_counts = &self.entity_ref_counts.upgrade()?;
let ref_counts = ref_counts.read();
@@ -546,7 +546,7 @@ impl AnyWeakEntity {
})
}
/// Assert that model referenced by this weak handle has been released.
/// Assert that entity referenced by this weak handle has been released.
#[cfg(any(test, feature = "test-support"))]
pub fn assert_released(&self) {
self.entity_ref_counts
@@ -579,8 +579,8 @@ impl std::fmt::Debug for AnyWeakEntity {
}
impl<T> From<WeakEntity<T>> for AnyWeakEntity {
fn from(model: WeakEntity<T>) -> Self {
model.any_entity
fn from(entity: WeakEntity<T>) -> Self {
entity.any_entity
}
}
@@ -598,7 +598,7 @@ impl PartialEq for AnyWeakEntity {
impl Eq for AnyWeakEntity {}
/// A weak reference to a model of the given type.
/// A weak reference to a entity of the given type.
#[derive(Deref, DerefMut)]
pub struct WeakEntity<T> {
#[deref]
@@ -629,13 +629,13 @@ impl<T> Clone for WeakEntity<T> {
}
impl<T: 'static> WeakEntity<T> {
/// Upgrade this weak model reference into a strong model reference
/// Upgrade this weak entity reference into a strong entity reference
pub fn upgrade(&self) -> Option<Entity<T>> {
// Delegate to the trait implementation to keep behavior in one place.
Entity::upgrade_from(self)
}
/// Updates the entity referenced by this model with the given function if
/// Updates the entity referenced by this handle with the given function if
/// the referenced entity still exists. Returns an error if the entity has
/// been released.
pub fn update<C, R>(
@@ -654,7 +654,7 @@ impl<T: 'static> WeakEntity<T> {
)
}
/// Updates the entity referenced by this model with the given function if
/// Updates the entity referenced by this handle with the given function if
/// the referenced entity still exists, within a visual context that has a window.
/// Returns an error if the entity has been released.
pub fn update_in<C, R>(
@@ -670,11 +670,11 @@ impl<T: 'static> WeakEntity<T> {
let this = self.upgrade().ok_or_else(|| anyhow!("entity released"))?;
crate::Flatten::flatten(window.update(cx, |_, window, cx| {
this.update(cx, |model, cx| update(model, window, cx))
this.update(cx, |entity, cx| update(entity, window, cx))
}))
}
/// Reads the entity referenced by this model with the given function if
/// Reads the entity referenced by this handle with the given function if
/// the referenced entity still exists. Returns an error if the entity has
/// been released.
pub fn read_with<C, R>(&self, cx: &C, read: impl FnOnce(&T, &App) -> R) -> Result<R>
+27 -27
View File
@@ -34,10 +34,10 @@ impl AppContext for TestAppContext {
fn new<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Context<'_, T>) -> T,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Self::Result<Entity<T>> {
let mut app = self.app.borrow_mut();
app.new(build_model)
app.new(build_entity)
}
fn reserve_entity<T: 'static>(&mut self) -> Self::Result<crate::Reservation<T>> {
@@ -48,10 +48,10 @@ impl AppContext for TestAppContext {
fn insert_entity<T: 'static>(
&mut self,
reservation: crate::Reservation<T>,
build_model: impl FnOnce(&mut Context<'_, T>) -> T,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Self::Result<Entity<T>> {
let mut app = self.app.borrow_mut();
app.insert_entity(reservation, build_model)
app.insert_entity(reservation, build_entity)
}
fn update_entity<T: 'static, R>(
@@ -178,13 +178,13 @@ impl TestAppContext {
&self.foreground_executor
}
/// Gives you an `&mut AppContext` for the duration of the closure
/// Gives you an `&mut App` for the duration of the closure
pub fn update<R>(&self, f: impl FnOnce(&mut App) -> R) -> R {
let mut cx = self.app.borrow_mut();
cx.update(f)
}
/// Gives you an `&AppContext` for the duration of the closure
/// Gives you an `&App` for the duration of the closure
pub fn read<R>(&self, f: impl FnOnce(&App) -> R) -> R {
let cx = self.app.borrow();
f(&cx)
@@ -231,7 +231,7 @@ impl TestAppContext {
}
/// Adds a new window, and returns its root view and a `VisualTestContext` which can be used
/// as a `WindowContext` for the rest of the test. Typically you would shadow this context with
/// as a `Window` and `App` for the rest of the test. Typically you would shadow this context with
/// the returned one. `let (view, cx) = cx.add_window_view(...);`
pub fn add_window_view<F, V>(
&mut self,
@@ -436,7 +436,7 @@ impl TestAppContext {
.clone()
}
/// Returns a stream of notifications whenever the View or Model is updated.
/// Returns a stream of notifications whenever the Entity is updated.
pub fn notifications<T: 'static>(&mut self, entity: &Entity<T>) -> impl Stream<Item = ()> {
let (tx, rx) = futures::channel::mpsc::unbounded();
self.update(|cx| {
@@ -453,7 +453,7 @@ impl TestAppContext {
rx
}
/// Returns a stream of events emitted by the given Model.
/// Returns a stream of events emitted by the given Entity.
pub fn events<Evt, T: 'static + EventEmitter<Evt>>(
&mut self,
entity: &Entity<T>,
@@ -464,7 +464,7 @@ impl TestAppContext {
let (tx, rx) = futures::channel::mpsc::unbounded();
entity
.update(self, |_, cx: &mut Context<T>| {
cx.subscribe(entity, move |_model, _handle, event, _cx| {
cx.subscribe(entity, move |_entity, _handle, event, _cx| {
let _ = tx.unbounded_send(event.clone());
})
})
@@ -476,23 +476,23 @@ impl TestAppContext {
/// don't need to jump in at a specific time).
pub async fn condition<T: 'static>(
&mut self,
model: &Entity<T>,
entity: &Entity<T>,
mut predicate: impl FnMut(&mut T, &mut Context<T>) -> bool,
) {
let timer = self.executor().timer(Duration::from_secs(3));
let mut notifications = self.notifications(model);
let mut notifications = self.notifications(entity);
use futures::FutureExt as _;
use smol::future::FutureExt as _;
async {
loop {
if model.update(self, &mut predicate) {
if entity.update(self, &mut predicate) {
return Ok(());
}
if notifications.next().await.is_none() {
bail!("model dropped")
bail!("entity dropped")
}
}
}
@@ -509,7 +509,7 @@ impl TestAppContext {
}
impl<T: 'static> Entity<T> {
/// Block until the next event is emitted by the model, then return it.
/// Block until the next event is emitted by the entity, then return it.
pub fn next_event<Event>(&self, cx: &mut TestAppContext) -> impl Future<Output = Event>
where
Event: Send + Clone + 'static,
@@ -560,7 +560,7 @@ impl<V: 'static> Entity<V> {
.await
.expect("next notification timed out");
drop(subscription);
notification.expect("model dropped while test was waiting for its next notification")
notification.expect("entity dropped while test was waiting for its next notification")
}
}
}
@@ -639,8 +639,8 @@ use derive_more::{Deref, DerefMut};
use super::{Context, Entity};
#[derive(Deref, DerefMut, Clone)]
/// A VisualTestContext is the test-equivalent of a `WindowContext`. It allows you to
/// run window-specific test code.
/// A VisualTestContext is the test-equivalent of a `Window` and `App`. It allows you to
/// run window-specific test code. It can be dereferenced to a `TextAppContext`.
pub struct VisualTestContext {
#[deref]
#[deref_mut]
@@ -650,7 +650,7 @@ pub struct VisualTestContext {
}
impl VisualTestContext {
/// Provides the `WindowContext` for the duration of the closure.
/// Provides a `Window` and `App` for the duration of the closure.
pub fn update<R>(&mut self, f: impl FnOnce(&mut Window, &mut App) -> R) -> R {
self.cx
.update_window(self.window, |_, window, cx| f(window, cx))
@@ -680,7 +680,7 @@ impl VisualTestContext {
self.cx.dispatch_action(self.window, action)
}
/// Read the title off the window (set by `WindowContext#set_window_title`)
/// Read the title off the window (set by `Window#set_window_title`)
pub fn window_title(&mut self) -> Option<String> {
self.cx.test_window(self.window).0.lock().title.clone()
}
@@ -865,9 +865,9 @@ impl AppContext for VisualTestContext {
fn new<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Context<'_, T>) -> T,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Self::Result<Entity<T>> {
self.cx.new(build_model)
self.cx.new(build_entity)
}
fn reserve_entity<T: 'static>(&mut self) -> Self::Result<crate::Reservation<T>> {
@@ -877,9 +877,9 @@ impl AppContext for VisualTestContext {
fn insert_entity<T: 'static>(
&mut self,
reservation: crate::Reservation<T>,
build_model: impl FnOnce(&mut Context<'_, T>) -> T,
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
) -> Self::Result<Entity<T>> {
self.cx.insert_entity(reservation, build_model)
self.cx.insert_entity(reservation, build_entity)
}
fn update_entity<T, R>(
@@ -945,11 +945,11 @@ impl VisualContext for VisualTestContext {
fn new_window_entity<T: 'static>(
&mut self,
build_model: impl FnOnce(&mut Window, &mut Context<'_, T>) -> T,
build_entity: impl FnOnce(&mut Window, &mut Context<'_, T>) -> T,
) -> Self::Result<Entity<T>> {
self.window
.update(&mut self.cx, |_, window, cx| {
cx.new(|cx| build_model(window, cx))
cx.new(|cx| build_entity(window, cx))
})
.unwrap()
}
@@ -991,7 +991,7 @@ impl VisualContext for VisualTestContext {
impl AnyWindowHandle {
/// Creates the given view in this window.
pub fn build_model<V: Render + 'static>(
pub fn build_entity<V: Render + 'static>(
&self,
cx: &mut TestAppContext,
build_view: impl FnOnce(&mut Window, &mut Context<V>) -> V,