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:
+49
-42
@@ -46,10 +46,10 @@ mod entity_map;
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
mod test_context;
|
||||
|
||||
/// The duration for which futures returned from [AppContext::on_app_context] or [ModelContext::on_app_quit] can run before the application fully quits.
|
||||
/// The duration for which futures returned from [Context::on_app_quit] can run before the application fully quits.
|
||||
pub const SHUTDOWN_TIMEOUT: Duration = Duration::from_millis(100);
|
||||
|
||||
/// Temporary(?) wrapper around [`RefCell<AppContext>`] to help us debug any double borrows.
|
||||
/// Temporary(?) wrapper around [`RefCell<App>`] to help us debug any double borrows.
|
||||
/// Strongly consider removing after stabilization.
|
||||
#[doc(hidden)]
|
||||
pub struct AppCell {
|
||||
@@ -219,11 +219,11 @@ pub(crate) type KeystrokeObserver =
|
||||
Box<dyn FnMut(&KeystrokeEvent, &mut Window, &mut App) -> bool + 'static>;
|
||||
type QuitHandler = Box<dyn FnOnce(&mut App) -> LocalBoxFuture<'static, ()> + 'static>;
|
||||
type ReleaseListener = Box<dyn FnOnce(&mut dyn Any, &mut App) + 'static>;
|
||||
type NewModelListener = Box<dyn FnMut(AnyEntity, &mut Option<&mut Window>, &mut App) + 'static>;
|
||||
type NewEntityListener = Box<dyn FnMut(AnyEntity, &mut Option<&mut Window>, &mut App) + 'static>;
|
||||
|
||||
/// Contains the state of the full application, and passed as a reference to a variety of callbacks.
|
||||
/// Other contexts such as [ModelContext], [WindowContext], and [ViewContext] deref to this type, making it the most general context type.
|
||||
/// You need a reference to an `AppContext` to access the state of a [Model].
|
||||
/// Other [Context] derefs to this type.
|
||||
/// You need a reference to an `App` to access the state of a [Entity].
|
||||
pub struct App {
|
||||
pub(crate) this: Weak<AppCell>,
|
||||
pub(crate) platform: Rc<dyn Platform>,
|
||||
@@ -241,7 +241,7 @@ pub struct App {
|
||||
pub(crate) globals_by_type: FxHashMap<TypeId, Box<dyn Any>>,
|
||||
pub(crate) entities: EntityMap,
|
||||
pub(crate) window_update_stack: Vec<WindowId>,
|
||||
pub(crate) new_model_observers: SubscriberSet<TypeId, NewModelListener>,
|
||||
pub(crate) new_entity_observers: SubscriberSet<TypeId, NewEntityListener>,
|
||||
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
|
||||
pub(crate) window_handles: FxHashMap<WindowId, AnyWindowHandle>,
|
||||
pub(crate) focus_handles: Arc<FocusMap>,
|
||||
@@ -305,7 +305,7 @@ impl App {
|
||||
http_client,
|
||||
globals_by_type: FxHashMap::default(),
|
||||
entities,
|
||||
new_model_observers: SubscriberSet::new(),
|
||||
new_entity_observers: SubscriberSet::new(),
|
||||
windows: SlotMap::with_key(),
|
||||
window_update_stack: Vec::new(),
|
||||
window_handles: FxHashMap::default(),
|
||||
@@ -359,7 +359,7 @@ impl App {
|
||||
app
|
||||
}
|
||||
|
||||
/// Quit the application gracefully. Handlers registered with [`ModelContext::on_app_quit`]
|
||||
/// Quit the application gracefully. Handlers registered with [`Context::on_app_quit`]
|
||||
/// will be given 100ms to complete before exiting.
|
||||
pub fn shutdown(&mut self) {
|
||||
let mut futures = Vec::new();
|
||||
@@ -426,7 +426,7 @@ impl App {
|
||||
result
|
||||
}
|
||||
|
||||
/// Arrange a callback to be invoked when the given model or view calls `notify` on its respective context.
|
||||
/// Arrange a callback to be invoked when the given entity calls `notify` on its respective context.
|
||||
pub fn observe<W>(
|
||||
&mut self,
|
||||
entity: &Entity<W>,
|
||||
@@ -510,7 +510,7 @@ impl App {
|
||||
)
|
||||
}
|
||||
|
||||
/// Arrange for the given callback to be invoked whenever the given model or view emits an event of a given type.
|
||||
/// Arrange for the given callback to be invoked whenever the given entity emits an event of a given type.
|
||||
/// The callback is provided a handle to the emitting entity and a reference to the emitted event.
|
||||
pub fn subscribe<T, Event>(
|
||||
&mut self,
|
||||
@@ -588,7 +588,7 @@ impl App {
|
||||
}
|
||||
|
||||
/// Opens a new window with the given option and the root view returned by the given function.
|
||||
/// The function is invoked with a `WindowContext`, which can be used to interact with window-specific
|
||||
/// The function is invoked with a `Window`, which can be used to interact with window-specific
|
||||
/// functionality.
|
||||
pub fn open_window<V: 'static + Render>(
|
||||
&mut self,
|
||||
@@ -823,7 +823,7 @@ impl App {
|
||||
self.pending_effects.push_back(effect);
|
||||
}
|
||||
|
||||
/// Called at the end of [`AppContext::update`] to complete any side effects
|
||||
/// Called at the end of [`App::update`] to complete any side effects
|
||||
/// such as notifying observers, emitting events, etc. Effects can themselves
|
||||
/// cause effects, so we continue looping until all effects are processed.
|
||||
fn flush_effects(&mut self) {
|
||||
@@ -854,12 +854,12 @@ impl App {
|
||||
Effect::Defer { callback } => {
|
||||
self.apply_defer_effect(callback);
|
||||
}
|
||||
Effect::ModelCreated {
|
||||
Effect::EntityCreated {
|
||||
entity,
|
||||
tid,
|
||||
window,
|
||||
} => {
|
||||
self.apply_model_created_effect(entity, tid, window);
|
||||
self.apply_entity_created_effect(entity, tid, window);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -967,13 +967,13 @@ impl App {
|
||||
callback(self);
|
||||
}
|
||||
|
||||
fn apply_model_created_effect(
|
||||
fn apply_entity_created_effect(
|
||||
&mut self,
|
||||
entity: AnyEntity,
|
||||
tid: TypeId,
|
||||
window: Option<WindowId>,
|
||||
) {
|
||||
self.new_model_observers.clone().retain(&tid, |observer| {
|
||||
self.new_entity_observers.clone().retain(&tid, |observer| {
|
||||
if let Some(id) = window {
|
||||
self.update_window_id(id, {
|
||||
let entity = entity.clone();
|
||||
@@ -1172,8 +1172,12 @@ impl App {
|
||||
self.globals_by_type.insert(global_type, lease.global);
|
||||
}
|
||||
|
||||
pub(crate) fn new_model_observer(&self, key: TypeId, value: NewModelListener) -> Subscription {
|
||||
let (subscription, activate) = self.new_model_observers.insert(key, value);
|
||||
pub(crate) fn new_entity_observer(
|
||||
&self,
|
||||
key: TypeId,
|
||||
value: NewEntityListener,
|
||||
) -> Subscription {
|
||||
let (subscription, activate) = self.new_entity_observers.insert(key, value);
|
||||
activate();
|
||||
subscription
|
||||
}
|
||||
@@ -1184,18 +1188,18 @@ impl App {
|
||||
&self,
|
||||
on_new: impl 'static + Fn(&mut T, Option<&mut Window>, &mut Context<T>),
|
||||
) -> Subscription {
|
||||
self.new_model_observer(
|
||||
self.new_entity_observer(
|
||||
TypeId::of::<T>(),
|
||||
Box::new(
|
||||
move |any_model: AnyEntity, window: &mut Option<&mut Window>, cx: &mut App| {
|
||||
any_model
|
||||
move |any_entity: AnyEntity, window: &mut Option<&mut Window>, cx: &mut App| {
|
||||
any_entity
|
||||
.downcast::<T>()
|
||||
.unwrap()
|
||||
.update(cx, |model_state, cx| {
|
||||
.update(cx, |entity_state, cx| {
|
||||
if let Some(window) = window {
|
||||
on_new(model_state, Some(window), cx);
|
||||
on_new(entity_state, Some(window), cx);
|
||||
} else {
|
||||
on_new(model_state, None, cx);
|
||||
on_new(entity_state, None, cx);
|
||||
}
|
||||
})
|
||||
},
|
||||
@@ -1203,7 +1207,7 @@ impl App {
|
||||
)
|
||||
}
|
||||
|
||||
/// Observe the release of a model or view. The callback is invoked after the model or view
|
||||
/// Observe the release of a entity. The callback is invoked after the entity
|
||||
/// has no more strong references but before it has been dropped.
|
||||
pub fn observe_release<T>(
|
||||
&self,
|
||||
@@ -1224,7 +1228,7 @@ impl App {
|
||||
subscription
|
||||
}
|
||||
|
||||
/// Observe the release of a model or view. The callback is invoked after the model or view
|
||||
/// Observe the release of a entity. The callback is invoked after the entity
|
||||
/// has no more strong references but before it has been dropped.
|
||||
pub fn observe_release_in<T>(
|
||||
&self,
|
||||
@@ -1573,22 +1577,25 @@ impl AppContext for App {
|
||||
type Result<T> = T;
|
||||
|
||||
/// Build an entity that is owned by the application. The given function will be invoked with
|
||||
/// a `ModelContext` and must return an object representing the entity. A `Model` handle will be returned,
|
||||
/// a `Context` and must return an object representing the entity. A `Entity` handle will be returned,
|
||||
/// which can be used to access the entity in a context.
|
||||
fn new<T: 'static>(&mut self, build_model: impl FnOnce(&mut Context<'_, T>) -> T) -> Entity<T> {
|
||||
fn new<T: 'static>(
|
||||
&mut self,
|
||||
build_entity: impl FnOnce(&mut Context<'_, T>) -> T,
|
||||
) -> Entity<T> {
|
||||
self.update(|cx| {
|
||||
let slot = cx.entities.reserve();
|
||||
let model = slot.clone();
|
||||
let entity = build_model(&mut Context::new_context(cx, slot.downgrade()));
|
||||
let handle = slot.clone();
|
||||
let entity = build_entity(&mut Context::new_context(cx, slot.downgrade()));
|
||||
|
||||
cx.push_effect(Effect::ModelCreated {
|
||||
entity: model.clone().into_any(),
|
||||
cx.push_effect(Effect::EntityCreated {
|
||||
entity: handle.clone().into_any(),
|
||||
tid: TypeId::of::<T>(),
|
||||
window: cx.window_update_stack.last().cloned(),
|
||||
});
|
||||
|
||||
cx.entities.insert(slot, entity);
|
||||
model
|
||||
handle
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1599,27 +1606,27 @@ impl AppContext for App {
|
||||
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.update(|cx| {
|
||||
let slot = reservation.0;
|
||||
let entity = build_model(&mut Context::new_context(cx, slot.downgrade()));
|
||||
let entity = build_entity(&mut Context::new_context(cx, slot.downgrade()));
|
||||
cx.entities.insert(slot, entity)
|
||||
})
|
||||
}
|
||||
|
||||
/// Updates the entity referenced by the given model. The function is passed a mutable reference to the
|
||||
/// entity along with a `ModelContext` for the entity.
|
||||
/// Updates the entity referenced by the given handle. The function is passed a mutable reference to the
|
||||
/// entity along with a `Context` for the entity.
|
||||
fn update_entity<T: 'static, R>(
|
||||
&mut self,
|
||||
model: &Entity<T>,
|
||||
handle: &Entity<T>,
|
||||
update: impl FnOnce(&mut T, &mut Context<'_, T>) -> R,
|
||||
) -> R {
|
||||
self.update(|cx| {
|
||||
let mut entity = cx.entities.lease(model);
|
||||
let mut entity = cx.entities.lease(handle);
|
||||
let result = update(
|
||||
&mut entity,
|
||||
&mut Context::new_context(cx, model.downgrade()),
|
||||
&mut Context::new_context(cx, handle.downgrade()),
|
||||
);
|
||||
cx.entities.end_lease(entity);
|
||||
result
|
||||
@@ -1701,7 +1708,7 @@ pub(crate) enum Effect {
|
||||
Defer {
|
||||
callback: Box<dyn FnOnce(&mut App) + 'static>,
|
||||
},
|
||||
ModelCreated {
|
||||
EntityCreated {
|
||||
entity: AnyEntity,
|
||||
tid: TypeId,
|
||||
window: Option<WindowId>,
|
||||
@@ -1718,7 +1725,7 @@ impl std::fmt::Debug for Effect {
|
||||
write!(f, "NotifyGlobalObservers({:?})", global_type)
|
||||
}
|
||||
Effect::Defer { .. } => write!(f, "Defer(..)"),
|
||||
Effect::ModelCreated { entity, .. } => write!(f, "ModelCreated({:?})", entity),
|
||||
Effect::EntityCreated { entity, .. } => write!(f, "EntityCreated({:?})", entity),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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>(
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -114,8 +114,8 @@ pub trait IntoElement: Sized {
|
||||
|
||||
impl<T: IntoElement> FluentBuilder for T {}
|
||||
|
||||
/// An object that can be drawn to the screen. This is the trait that distinguishes `Views` from
|
||||
/// models. Views are drawn to the screen and care about the current window's state, models are not and do not.
|
||||
/// An object that can be drawn to the screen. This is the trait that distinguishes "views" from
|
||||
/// other entities. Views are `Entity`'s which `impl Render` and drawn to the screen.
|
||||
pub trait Render: 'static + Sized {
|
||||
/// Render this view into an element tree.
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<'_, Self>) -> impl IntoElement;
|
||||
|
||||
@@ -84,7 +84,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to the mouse down event for the given mouse button, during the bubble phase
|
||||
/// The imperative API equivalent of [`InteractiveElement::on_mouse_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to the view state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to the view state from this callback.
|
||||
pub fn on_mouse_down(
|
||||
&mut self,
|
||||
button: MouseButton,
|
||||
@@ -104,7 +104,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to the mouse down event for any button, during the capture phase
|
||||
/// The imperative API equivalent of [`InteractiveElement::capture_any_mouse_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn capture_any_mouse_down(
|
||||
&mut self,
|
||||
listener: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -120,7 +120,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to the mouse down event for any button, during the bubble phase
|
||||
/// the imperative API equivalent to [`InteractiveElement::on_any_mouse_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_any_mouse_down(
|
||||
&mut self,
|
||||
listener: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -136,7 +136,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to the mouse up event for the given button, during the bubble phase
|
||||
/// the imperative API equivalent to [`InteractiveElement::on_mouse_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_mouse_up(
|
||||
&mut self,
|
||||
button: MouseButton,
|
||||
@@ -156,7 +156,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to the mouse up event for any button, during the capture phase
|
||||
/// the imperative API equivalent to [`InteractiveElement::capture_any_mouse_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn capture_any_mouse_up(
|
||||
&mut self,
|
||||
listener: impl Fn(&MouseUpEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -172,7 +172,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to the mouse up event for any button, during the bubble phase
|
||||
/// the imperative API equivalent to [`Interactivity::on_any_mouse_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_any_mouse_up(
|
||||
&mut self,
|
||||
listener: impl Fn(&MouseUpEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -189,7 +189,7 @@ impl Interactivity {
|
||||
/// when the mouse is outside of the bounds of this element.
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_mouse_down_out`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_mouse_down_out(
|
||||
&mut self,
|
||||
listener: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -206,7 +206,7 @@ impl Interactivity {
|
||||
/// when the mouse is outside of the bounds of this element.
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_mouse_up_out`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_mouse_up_out(
|
||||
&mut self,
|
||||
button: MouseButton,
|
||||
@@ -226,7 +226,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to the mouse move event, during the bubble phase
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_mouse_move`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_mouse_move(
|
||||
&mut self,
|
||||
listener: impl Fn(&MouseMoveEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -245,7 +245,7 @@ impl Interactivity {
|
||||
/// UIs that don't conform to a drag and drop style interaction, like resizing.
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_drag_move`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_drag_move<T>(
|
||||
&mut self,
|
||||
listener: impl Fn(&DragMoveEvent<T>, &mut Window, &mut App) + 'static,
|
||||
@@ -276,7 +276,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to scroll wheel events during the bubble phase
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_scroll_wheel`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_scroll_wheel(
|
||||
&mut self,
|
||||
listener: impl Fn(&ScrollWheelEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -292,7 +292,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to an action dispatch during the capture phase
|
||||
/// The imperative API equivalent to [`InteractiveElement::capture_action`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn capture_action<A: Action>(
|
||||
&mut self,
|
||||
listener: impl Fn(&A, &mut Window, &mut App) + 'static,
|
||||
@@ -313,7 +313,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to an action dispatch during the bubble phase
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_action`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_action<A: Action>(&mut self, listener: impl Fn(&A, &mut Window, &mut App) + 'static) {
|
||||
self.action_listeners.push((
|
||||
TypeId::of::<A>(),
|
||||
@@ -331,7 +331,7 @@ impl Interactivity {
|
||||
/// action bindings to their users.
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_boxed_action`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_boxed_action(
|
||||
&mut self,
|
||||
action: &dyn Action,
|
||||
@@ -351,7 +351,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to key down events during the bubble phase
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_key_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_key_down(
|
||||
&mut self,
|
||||
listener: impl Fn(&KeyDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -367,7 +367,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to key down events during the capture phase
|
||||
/// The imperative API equivalent to [`InteractiveElement::capture_key_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn capture_key_down(
|
||||
&mut self,
|
||||
listener: impl Fn(&KeyDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -383,7 +383,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to key up events during the bubble phase
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_key_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_key_up(&mut self, listener: impl Fn(&KeyUpEvent, &mut Window, &mut App) + 'static) {
|
||||
self.key_up_listeners
|
||||
.push(Box::new(move |event, phase, window, cx| {
|
||||
@@ -396,7 +396,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to key up events during the capture phase
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_key_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn capture_key_up(
|
||||
&mut self,
|
||||
listener: impl Fn(&KeyUpEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -412,7 +412,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to modifiers changing events.
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_modifiers_changed`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_modifiers_changed(
|
||||
&mut self,
|
||||
listener: impl Fn(&ModifiersChangedEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -426,7 +426,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to drop events of the given type, whether or not the drag started on this element
|
||||
/// The imperative API equivalent to [`InteractiveElement::on_drop`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_drop<T: 'static>(&mut self, listener: impl Fn(&T, &mut Window, &mut App) + 'static) {
|
||||
self.drop_listeners.push((
|
||||
TypeId::of::<T>(),
|
||||
@@ -448,7 +448,7 @@ impl Interactivity {
|
||||
/// Bind the given callback to click events of this element
|
||||
/// The imperative API equivalent to [`StatefulInteractiveElement::on_click`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_click(&mut self, listener: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static)
|
||||
where
|
||||
Self: Sized,
|
||||
@@ -464,7 +464,7 @@ impl Interactivity {
|
||||
/// the [`Self::on_drag_move`] API
|
||||
/// The imperative API equivalent to [`StatefulInteractiveElement::on_drag`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_drag<T, W>(
|
||||
&mut self,
|
||||
value: T,
|
||||
@@ -490,7 +490,7 @@ impl Interactivity {
|
||||
/// passed to the callback is true when the hover starts and false when it ends.
|
||||
/// The imperative API equivalent to [`StatefulInteractiveElement::on_drag`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
pub fn on_hover(&mut self, listener: impl Fn(&bool, &mut Window, &mut App) + 'static)
|
||||
where
|
||||
Self: Sized,
|
||||
@@ -611,7 +611,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to the mouse down event for the given mouse button,
|
||||
/// the fluent API equivalent to [`Interactivity::on_mouse_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to the view state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to the view state from this callback.
|
||||
fn on_mouse_down(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
@@ -642,7 +642,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to the mouse down event for any button, during the capture phase
|
||||
/// the fluent API equivalent to [`Interactivity::capture_any_mouse_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn capture_any_mouse_down(
|
||||
mut self,
|
||||
listener: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -654,7 +654,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to the mouse down event for any button, during the capture phase
|
||||
/// the fluent API equivalent to [`Interactivity::on_any_mouse_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_any_mouse_down(
|
||||
mut self,
|
||||
listener: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -666,7 +666,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to the mouse up event for the given button, during the bubble phase
|
||||
/// the fluent API equivalent to [`Interactivity::on_mouse_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_mouse_up(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
@@ -679,7 +679,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to the mouse up event for any button, during the capture phase
|
||||
/// the fluent API equivalent to [`Interactivity::capture_any_mouse_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn capture_any_mouse_up(
|
||||
mut self,
|
||||
listener: impl Fn(&MouseUpEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -692,7 +692,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// when the mouse is outside of the bounds of this element.
|
||||
/// The fluent API equivalent to [`Interactivity::on_mouse_down_out`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_mouse_down_out(
|
||||
mut self,
|
||||
listener: impl Fn(&MouseDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -705,7 +705,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// when the mouse is outside of the bounds of this element.
|
||||
/// The fluent API equivalent to [`Interactivity::on_mouse_up_out`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_mouse_up_out(
|
||||
mut self,
|
||||
button: MouseButton,
|
||||
@@ -718,7 +718,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to the mouse move event, during the bubble phase
|
||||
/// The fluent API equivalent to [`Interactivity::on_mouse_move`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_mouse_move(
|
||||
mut self,
|
||||
listener: impl Fn(&MouseMoveEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -733,7 +733,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// UIs that don't conform to a drag and drop style interaction, like resizing.
|
||||
/// The fluent API equivalent to [`Interactivity::on_drag_move`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_drag_move<T: 'static>(
|
||||
mut self,
|
||||
listener: impl Fn(&DragMoveEvent<T>, &mut Window, &mut App) + 'static,
|
||||
@@ -745,7 +745,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to scroll wheel events during the bubble phase
|
||||
/// The fluent API equivalent to [`Interactivity::on_scroll_wheel`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_scroll_wheel(
|
||||
mut self,
|
||||
listener: impl Fn(&ScrollWheelEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -757,7 +757,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Capture the given action, before normal action dispatch can fire
|
||||
/// The fluent API equivalent to [`Interactivity::on_scroll_wheel`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn capture_action<A: Action>(
|
||||
mut self,
|
||||
listener: impl Fn(&A, &mut Window, &mut App) + 'static,
|
||||
@@ -769,7 +769,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to an action dispatch during the bubble phase
|
||||
/// The fluent API equivalent to [`Interactivity::on_action`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_action<A: Action>(
|
||||
mut self,
|
||||
listener: impl Fn(&A, &mut Window, &mut App) + 'static,
|
||||
@@ -783,7 +783,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// action bindings to their users.
|
||||
/// The fluent API equivalent to [`Interactivity::on_boxed_action`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_boxed_action(
|
||||
mut self,
|
||||
action: &dyn Action,
|
||||
@@ -796,7 +796,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to key down events during the bubble phase
|
||||
/// The fluent API equivalent to [`Interactivity::on_key_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_key_down(
|
||||
mut self,
|
||||
listener: impl Fn(&KeyDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -808,7 +808,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to key down events during the capture phase
|
||||
/// The fluent API equivalent to [`Interactivity::capture_key_down`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn capture_key_down(
|
||||
mut self,
|
||||
listener: impl Fn(&KeyDownEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -820,7 +820,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to key up events during the bubble phase
|
||||
/// The fluent API equivalent to [`Interactivity::on_key_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_key_up(
|
||||
mut self,
|
||||
listener: impl Fn(&KeyUpEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -832,7 +832,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to key up events during the capture phase
|
||||
/// The fluent API equivalent to [`Interactivity::capture_key_up`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn capture_key_up(
|
||||
mut self,
|
||||
listener: impl Fn(&KeyUpEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -844,7 +844,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to modifiers changing events.
|
||||
/// The fluent API equivalent to [`Interactivity::on_modifiers_changed`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_modifiers_changed(
|
||||
mut self,
|
||||
listener: impl Fn(&ModifiersChangedEvent, &mut Window, &mut App) + 'static,
|
||||
@@ -891,7 +891,7 @@ pub trait InteractiveElement: Sized {
|
||||
/// Bind the given callback to drop events of the given type, whether or not the drag started on this element
|
||||
/// The fluent API equivalent to [`Interactivity::on_drop`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_drop<T: 'static>(
|
||||
mut self,
|
||||
listener: impl Fn(&T, &mut Window, &mut App) + 'static,
|
||||
@@ -992,7 +992,7 @@ pub trait StatefulInteractiveElement: InteractiveElement {
|
||||
/// Bind the given callback to click events of this element
|
||||
/// The fluent API equivalent to [`Interactivity::on_click`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_click(mut self, listener: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
@@ -1007,7 +1007,7 @@ pub trait StatefulInteractiveElement: InteractiveElement {
|
||||
/// The callback also has access to the offset of triggering click from the origin of parent element.
|
||||
/// The fluent API equivalent to [`Interactivity::on_drag`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_drag<T, W>(
|
||||
mut self,
|
||||
value: T,
|
||||
@@ -1026,7 +1026,7 @@ pub trait StatefulInteractiveElement: InteractiveElement {
|
||||
/// passed to the callback is true when the hover starts and false when it ends.
|
||||
/// The fluent API equivalent to [`Interactivity::on_hover`]
|
||||
///
|
||||
/// See [`ViewContext::listener`](crate::ViewContext::listener) to get access to a view's state from this callback.
|
||||
/// See [`Context::listener`](crate::Context::listener) to get access to a view's state from this callback.
|
||||
fn on_hover(mut self, listener: impl Fn(&bool, &mut Window, &mut App) + 'static) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
|
||||
@@ -2846,7 +2846,7 @@ impl From<ScaledPixels> for u32 {
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a length in rems, a unit based on the font-size of the window, which can be assigned with [`WindowContext::set_rem_size`][set_rem_size].
|
||||
/// Represents a length in rems, a unit based on the font-size of the window, which can be assigned with [`Window::set_rem_size`][set_rem_size].
|
||||
///
|
||||
/// Rems are used for defining lengths that are scalable and consistent across different UI elements.
|
||||
/// The value of `1rem` is typically equal to the font-size of the root element (often the `<html>` element in browsers),
|
||||
@@ -2855,7 +2855,7 @@ impl From<ScaledPixels> for u32 {
|
||||
///
|
||||
/// For example, if the root element's font-size is `16px`, then `1rem` equals `16px`. A length of `2rems` would then be `32px`.
|
||||
///
|
||||
/// [set_rem_size]: crate::WindowContext::set_rem_size
|
||||
/// [set_rem_size]: crate::Window::set_rem_size
|
||||
#[derive(Clone, Copy, Default, Add, Sub, Mul, Div, Neg, PartialEq)]
|
||||
pub struct Rems(pub f32);
|
||||
|
||||
|
||||
+25
-25
@@ -12,24 +12,24 @@
|
||||
//! gpui = { git = "https://github.com/zed-industries/zed" }
|
||||
//! ```
|
||||
//!
|
||||
//! Everything in GPUI starts with an [`App`]. You can create one with [`App::new`], and
|
||||
//! kick off your application by passing a callback to [`App::run`]. Inside this callback,
|
||||
//! you can create a new window with [`AppContext::open_window`], and register your first root
|
||||
//! Everything in GPUI starts with an [`Application`]. You can create one with [`Application::new`], and
|
||||
//! kick off your application by passing a callback to [`Application::run`]. Inside this callback,
|
||||
//! you can create a new window with [`App::open_window`], and register your first root
|
||||
//! view. See [gpui.rs](https://www.gpui.rs/) for a complete example.
|
||||
//!
|
||||
//! ## The Big Picture
|
||||
//!
|
||||
//! GPUI offers three different [registers](https://en.wikipedia.org/wiki/Register_(sociolinguistics)) depending on your needs:
|
||||
//!
|
||||
//! - State management and communication with Models. Whenever you need to store application state
|
||||
//! - State management and communication with [`Entity`]'s. Whenever you need to store application state
|
||||
//! that communicates between different parts of your application, you'll want to use GPUI's
|
||||
//! models. Models are owned by GPUI and are only accessible through an owned smart pointer
|
||||
//! similar to an [`Rc`]. See the [`app::model_context`] module for more information.
|
||||
//! entities. Entities are owned by GPUI and are only accessible through an owned smart pointer
|
||||
//! similar to an [`std::rc::Rc`]. See the [`app::context`] module for more information.
|
||||
//!
|
||||
//! - High level, declarative UI with Views. All UI in GPUI starts with a View. A view is simply
|
||||
//! a model that can be rendered, via the [`Render`] trait. At the start of each frame, GPUI
|
||||
//! - High level, declarative UI with views. All UI in GPUI starts with a view. A view is simply
|
||||
//! a [`Entity`] that can be rendered, by implementing the [`Render`] trait. At the start of each frame, GPUI
|
||||
//! will call this render method on the root view of a given window. Views build a tree of
|
||||
//! `elements`, lay them out and style them with a tailwind-style API, and then give them to
|
||||
//! [`Element`]s, lay them out and style them with a tailwind-style API, and then give them to
|
||||
//! GPUI to turn into pixels. See the [`elements::Div`] element for an all purpose swiss-army
|
||||
//! knife for UI.
|
||||
//!
|
||||
@@ -49,7 +49,7 @@
|
||||
//!
|
||||
//! - Actions are user-defined structs that are used for converting keystrokes into logical operations in your UI.
|
||||
//! Use this for implementing keyboard shortcuts, such as cmd-q. See the [`action`] module for more information.
|
||||
//! - Platform services, such as `quit the app` or `open a URL` are available as methods on the [`app::AppContext`].
|
||||
//! - Platform services, such as `quit the app` or `open a URL` are available as methods on the [`app::App`].
|
||||
//! - An async executor that is integrated with the platform's event loop. See the [`executor`] module for more information.,
|
||||
//! - The [gpui::test] macro provides a convenient way to write tests for your GPUI applications. Tests also have their
|
||||
//! own kind of context, a [`TestAppContext`] which provides ways of simulating common platform input. See [`app::test_context`]
|
||||
@@ -165,26 +165,26 @@ pub trait AppContext {
|
||||
/// can't hold a direct reference to the application context.
|
||||
type Result<T>;
|
||||
|
||||
/// Create a new model in the app context.
|
||||
/// Create a new entity in the app context.
|
||||
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>>;
|
||||
|
||||
/// Reserve a slot for a model to be inserted later.
|
||||
/// The returned [Reservation] allows you to obtain the [EntityId] for the future model.
|
||||
/// Reserve a slot for a entity to be inserted later.
|
||||
/// The returned [Reservation] allows you to obtain the [EntityId] for the future entity.
|
||||
fn reserve_entity<T: 'static>(&mut self) -> Self::Result<Reservation<T>>;
|
||||
|
||||
/// Insert a new model in the app context based on a [Reservation] previously obtained from [`reserve_entity`].
|
||||
/// Insert a new entity in the app context based on a [Reservation] previously obtained from [`reserve_entity`].
|
||||
///
|
||||
/// [`reserve_entity`]: Self::reserve_entity
|
||||
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>>;
|
||||
|
||||
/// Update a model in the app context.
|
||||
/// Update a entity in the app context.
|
||||
fn update_entity<T, R>(
|
||||
&mut self,
|
||||
handle: &Entity<T>,
|
||||
@@ -193,7 +193,7 @@ pub trait AppContext {
|
||||
where
|
||||
T: 'static;
|
||||
|
||||
/// Read a model from the app context.
|
||||
/// Read a entity from the app context.
|
||||
fn read_entity<T, R>(
|
||||
&self,
|
||||
handle: &Entity<T>,
|
||||
@@ -227,12 +227,12 @@ pub trait AppContext {
|
||||
G: Global;
|
||||
}
|
||||
|
||||
/// Returned by [Context::reserve_entity] to later be passed to [Context::insert_model].
|
||||
/// Allows you to obtain the [EntityId] for a model before it is created.
|
||||
/// Returned by [Context::reserve_entity] to later be passed to [Context::insert_entity].
|
||||
/// Allows you to obtain the [EntityId] for a entity before it is created.
|
||||
pub struct Reservation<T>(pub(crate) Slot<T>);
|
||||
|
||||
impl<T: 'static> Reservation<T> {
|
||||
/// Returns the [EntityId] that will be associated with the model once it is inserted.
|
||||
/// Returns the [EntityId] that will be associated with the entity once it is inserted.
|
||||
pub fn entity_id(&self) -> EntityId {
|
||||
self.0.entity_id()
|
||||
}
|
||||
@@ -247,14 +247,14 @@ pub trait VisualContext: AppContext {
|
||||
/// Update a view with the given callback
|
||||
fn update_window_entity<T: 'static, R>(
|
||||
&mut self,
|
||||
model: &Entity<T>,
|
||||
entity: &Entity<T>,
|
||||
update: impl FnOnce(&mut T, &mut Window, &mut Context<T>) -> R,
|
||||
) -> Self::Result<R>;
|
||||
|
||||
/// Update a view with the given callback
|
||||
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>>;
|
||||
|
||||
/// Replace the root view of a window with a new view.
|
||||
@@ -265,8 +265,8 @@ pub trait VisualContext: AppContext {
|
||||
where
|
||||
V: 'static + Render;
|
||||
|
||||
/// Focus a model in the window, if it implements the [`Focusable`] trait.
|
||||
fn focus<V>(&mut self, model: &Entity<V>) -> Self::Result<()>
|
||||
/// Focus a entity in the window, if it implements the [`Focusable`] trait.
|
||||
fn focus<V>(&mut self, entity: &Entity<V>) -> Self::Result<()>
|
||||
where
|
||||
V: Focusable;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::ops::Range;
|
||||
/// Implement this trait to allow views to handle textual input when implementing an editor, field, etc.
|
||||
///
|
||||
/// Once your view implements this trait, you can use it to construct an [`ElementInputHandler<V>`].
|
||||
/// This input handler can then be assigned during paint by calling [`WindowContext::handle_input`].
|
||||
/// This input handler can then be assigned during paint by calling [`Window::handle_input`].
|
||||
///
|
||||
/// See [`InputHandler`] for details on how to implement each method.
|
||||
pub trait EntityInputHandler: 'static + Sized {
|
||||
@@ -64,7 +64,7 @@ pub trait EntityInputHandler: 'static + Sized {
|
||||
) -> Option<Bounds<Pixels>>;
|
||||
}
|
||||
|
||||
/// The canonical implementation of [`PlatformInputHandler`]. Call [`WindowContext::handle_input`]
|
||||
/// The canonical implementation of [`PlatformInputHandler`]. Call [`Window::handle_input`]
|
||||
/// with an instance during your element's paint.
|
||||
pub struct ElementInputHandler<V> {
|
||||
view: Entity<V>,
|
||||
@@ -72,8 +72,7 @@ pub struct ElementInputHandler<V> {
|
||||
}
|
||||
|
||||
impl<V: 'static> ElementInputHandler<V> {
|
||||
/// Used in [`Element::paint`][element_paint] with the element's bounds and a view context for its
|
||||
/// containing view.
|
||||
/// Used in [`Element::paint`][element_paint] with the element's bounds, a `Window`, and a `App` context.
|
||||
///
|
||||
/// [element_paint]: crate::Element::paint
|
||||
pub fn new(element_bounds: Bounds<Pixels>, view: Entity<V>) -> Self {
|
||||
|
||||
@@ -949,7 +949,7 @@ impl PlatformWindow for MacWindow {
|
||||
unsafe { self.0.lock().native_window.isKeyWindow() == YES }
|
||||
}
|
||||
|
||||
// is_hovered is unused on macOS. See WindowContext::is_window_hovered.
|
||||
// is_hovered is unused on macOS. See Window::is_window_hovered.
|
||||
fn is_hovered(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ impl Subscription {
|
||||
}
|
||||
|
||||
/// Detaches the subscription from this handle. The callback will
|
||||
/// continue to be invoked until the views or models it has been
|
||||
/// continue to be invoked until the entities it has been
|
||||
/// subscribed to are dropped
|
||||
pub fn detach(mut self) {
|
||||
self.unsubscribe.take();
|
||||
|
||||
@@ -101,7 +101,7 @@ impl<T: 'static> futures::Stream for Observation<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// observe returns a stream of the change events from the given `View` or `Model`
|
||||
/// observe returns a stream of the change events from the given `Entity`
|
||||
pub fn observe<T: 'static>(entity: &Entity<T>, cx: &mut TestAppContext) -> Observation<()> {
|
||||
let (tx, rx) = smol::channel::unbounded();
|
||||
let _subscription = cx.update(|cx| {
|
||||
|
||||
+20
-20
@@ -68,10 +68,10 @@ impl<V: Render> Element for Entity<V> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A dynamically-typed handle to a view, which can be downcast to a [View] for a specific type.
|
||||
/// A dynamically-typed handle to a view, which can be downcast to a [Entity] for a specific type.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AnyView {
|
||||
model: AnyEntity,
|
||||
entity: AnyEntity,
|
||||
render: fn(&AnyView, &mut Window, &mut App) -> AnyElement,
|
||||
cached_style: Option<StyleRefinement>,
|
||||
}
|
||||
@@ -79,7 +79,7 @@ pub struct AnyView {
|
||||
impl<V: Render> From<Entity<V>> for AnyView {
|
||||
fn from(value: Entity<V>) -> Self {
|
||||
AnyView {
|
||||
model: value.into_any(),
|
||||
entity: value.into_any(),
|
||||
render: any_view::render::<V>,
|
||||
cached_style: None,
|
||||
}
|
||||
@@ -88,8 +88,8 @@ impl<V: Render> From<Entity<V>> for AnyView {
|
||||
|
||||
impl AnyView {
|
||||
/// Indicate that this view should be cached when using it as an element.
|
||||
/// When using this method, the view's previous layout and paint will be recycled from the previous frame if [ViewContext::notify] has not been called since it was rendered.
|
||||
/// The one exception is when [WindowContext::refresh] is called, in which case caching is ignored.
|
||||
/// When using this method, the view's previous layout and paint will be recycled from the previous frame if [Context::notify] has not been called since it was rendered.
|
||||
/// The one exception is when [Window::refresh] is called, in which case caching is ignored.
|
||||
pub fn cached(mut self, style: StyleRefinement) -> Self {
|
||||
self.cached_style = Some(style);
|
||||
self
|
||||
@@ -98,18 +98,18 @@ impl AnyView {
|
||||
/// Convert this to a weak handle.
|
||||
pub fn downgrade(&self) -> AnyWeakView {
|
||||
AnyWeakView {
|
||||
model: self.model.downgrade(),
|
||||
entity: self.entity.downgrade(),
|
||||
render: self.render,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this to a [View] of a specific type.
|
||||
/// Convert this to a [Entity] of a specific type.
|
||||
/// If this handle does not contain a view of the specified type, returns itself in an `Err` variant.
|
||||
pub fn downcast<T: 'static>(self) -> Result<Entity<T>, Self> {
|
||||
match self.model.downcast() {
|
||||
Ok(model) => Ok(model),
|
||||
Err(model) => Err(Self {
|
||||
model,
|
||||
match self.entity.downcast() {
|
||||
Ok(entity) => Ok(entity),
|
||||
Err(entity) => Err(Self {
|
||||
entity,
|
||||
render: self.render,
|
||||
cached_style: self.cached_style,
|
||||
}),
|
||||
@@ -118,18 +118,18 @@ impl AnyView {
|
||||
|
||||
/// Gets the [TypeId] of the underlying view.
|
||||
pub fn entity_type(&self) -> TypeId {
|
||||
self.model.entity_type
|
||||
self.entity.entity_type
|
||||
}
|
||||
|
||||
/// Gets the entity id of this handle.
|
||||
pub fn entity_id(&self) -> EntityId {
|
||||
self.model.entity_id()
|
||||
self.entity.entity_id()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for AnyView {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.model == other.model
|
||||
self.entity == other.entity
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,16 +283,16 @@ impl IntoElement for AnyView {
|
||||
|
||||
/// A weak, dynamically-typed view handle that does not prevent the view from being released.
|
||||
pub struct AnyWeakView {
|
||||
model: AnyWeakEntity,
|
||||
entity: AnyWeakEntity,
|
||||
render: fn(&AnyView, &mut Window, &mut App) -> AnyElement,
|
||||
}
|
||||
|
||||
impl AnyWeakView {
|
||||
/// Convert to a strongly-typed handle if the referenced view has not yet been released.
|
||||
pub fn upgrade(&self) -> Option<AnyView> {
|
||||
let model = self.model.upgrade()?;
|
||||
let entity = self.entity.upgrade()?;
|
||||
Some(AnyView {
|
||||
model,
|
||||
entity,
|
||||
render: self.render,
|
||||
cached_style: None,
|
||||
})
|
||||
@@ -302,7 +302,7 @@ impl AnyWeakView {
|
||||
impl<V: 'static + Render> From<WeakEntity<V>> for AnyWeakView {
|
||||
fn from(view: WeakEntity<V>) -> Self {
|
||||
AnyWeakView {
|
||||
model: view.into(),
|
||||
entity: view.into(),
|
||||
render: any_view::render::<V>,
|
||||
}
|
||||
}
|
||||
@@ -310,14 +310,14 @@ impl<V: 'static + Render> From<WeakEntity<V>> for AnyWeakView {
|
||||
|
||||
impl PartialEq for AnyWeakView {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.model == other.model
|
||||
self.entity == other.entity
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for AnyWeakView {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("AnyWeakView")
|
||||
.field("entity_id", &self.model.entity_id)
|
||||
.field("entity_id", &self.entity.entity_id)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ impl WindowFocusEvent {
|
||||
}
|
||||
}
|
||||
|
||||
/// This is provided when subscribing for `ViewContext::on_focus_out` events.
|
||||
/// This is provided when subscribing for `Context::on_focus_out` events.
|
||||
pub struct FocusOutEvent {
|
||||
/// A weak focus handle representing what was blurred.
|
||||
pub blurred: WeakFocusHandle,
|
||||
@@ -423,7 +423,7 @@ impl HitboxId {
|
||||
}
|
||||
|
||||
/// A rectangular region that potentially blocks hitboxes inserted prior.
|
||||
/// See [WindowContext::insert_hitbox] for more details.
|
||||
/// See [Window::insert_hitbox] for more details.
|
||||
#[derive(Clone, Debug, Deref)]
|
||||
pub struct Hitbox {
|
||||
/// A unique identifier for the hitbox.
|
||||
@@ -1193,9 +1193,9 @@ impl Window {
|
||||
});
|
||||
}
|
||||
|
||||
/// Subscribe to events emitted by a model or view.
|
||||
/// Subscribe to events emitted by a entity.
|
||||
/// The entity to which you're subscribing must implement the [`EventEmitter`] trait.
|
||||
/// The callback will be invoked a handle to the emitting entity (either a [`View`] or [`Model`]), the event, and a window context for the current window.
|
||||
/// The callback will be invoked a handle to the emitting entity, the event, and a window context for the current window.
|
||||
pub fn observe<T: 'static>(
|
||||
&mut self,
|
||||
observed: &Entity<T>,
|
||||
@@ -1222,9 +1222,9 @@ impl Window {
|
||||
)
|
||||
}
|
||||
|
||||
/// Subscribe to events emitted by a model or view.
|
||||
/// Subscribe to events emitted by a entity.
|
||||
/// The entity to which you're subscribing must implement the [`EventEmitter`] trait.
|
||||
/// The callback will be invoked a handle to the emitting entity (either a [`View`] or [`Model`]), the event, and a window context for the current window.
|
||||
/// The callback will be invoked a handle to the emitting entity, the event, and a window context for the current window.
|
||||
pub fn subscribe<Emitter, Evt>(
|
||||
&mut self,
|
||||
entity: &Entity<Emitter>,
|
||||
@@ -1259,7 +1259,7 @@ impl Window {
|
||||
)
|
||||
}
|
||||
|
||||
/// 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<T>(
|
||||
&self,
|
||||
entity: &Entity<T>,
|
||||
@@ -1914,7 +1914,7 @@ impl Window {
|
||||
}
|
||||
|
||||
/// Push a text style onto the stack, and call a function with that style active.
|
||||
/// Use [`AppContext::text_style`] to get the current, combined text style. This method
|
||||
/// Use [`Window::text_style`] to get the current, combined text style. This method
|
||||
/// should only be called as part of element drawing.
|
||||
pub fn with_text_style<F, R>(&mut self, style: Option<TextStyleRefinement>, f: F) -> R
|
||||
where
|
||||
@@ -4071,7 +4071,7 @@ impl From<(&'static str, u32)> for ElementId {
|
||||
}
|
||||
|
||||
/// A rectangle to be rendered in the window at the given position and size.
|
||||
/// Passed as an argument [`WindowContext::paint_quad`].
|
||||
/// Passed as an argument [`Window::paint_quad`].
|
||||
#[derive(Clone)]
|
||||
pub struct PaintQuad {
|
||||
/// The bounds of the quad within the window.
|
||||
|
||||
@@ -68,7 +68,7 @@ pub struct RenderablePromptHandle {
|
||||
pub(crate) view: Box<dyn PromptViewHandle>,
|
||||
}
|
||||
|
||||
/// Use this function in conjunction with [AppContext::set_prompt_renderer] to force
|
||||
/// Use this function in conjunction with [App::set_prompt_builder] to force
|
||||
/// GPUI to always use the fallback prompt renderer.
|
||||
pub fn fallback_prompt_renderer(
|
||||
level: PromptLevel,
|
||||
|
||||
Reference in New Issue
Block a user