From 0c94bdc8e463dbdab4acbbd43ee755a20d039507 Mon Sep 17 00:00:00 2001 From: someone13574 <81528246+someone13574@users.noreply.github.com> Date: Fri, 31 Jan 2025 14:40:42 -0500 Subject: [PATCH] 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 --- crates/diagnostics/src/diagnostics_tests.rs | 8 +- crates/gpui/README.md | 8 +- crates/gpui/docs/contexts.md | 40 ++++---- crates/gpui/examples/set_menus.rs | 2 +- crates/gpui/src/app.rs | 91 ++++++++++--------- crates/gpui/src/app/async_context.rs | 44 ++++----- crates/gpui/src/app/context.rs | 61 +++++++------ crates/gpui/src/app/entity_map.rs | 84 ++++++++--------- crates/gpui/src/app/test_context.rs | 54 +++++------ crates/gpui/src/element.rs | 4 +- crates/gpui/src/elements/div.rs | 90 +++++++++--------- crates/gpui/src/geometry.rs | 4 +- crates/gpui/src/gpui.rs | 50 +++++----- crates/gpui/src/input.rs | 7 +- crates/gpui/src/platform/mac/window.rs | 2 +- crates/gpui/src/subscription.rs | 2 +- crates/gpui/src/test.rs | 2 +- crates/gpui/src/view.rs | 40 ++++---- crates/gpui/src/window.rs | 18 ++-- crates/gpui/src/window/prompts.rs | 2 +- crates/gpui_macros/src/derive_app_context.rs | 8 +- .../gpui_macros/src/derive_visual_context.rs | 12 +-- crates/search/src/buffer_search.rs | 4 +- crates/search/src/project_search.rs | 14 +-- crates/zed/src/zed/linux_prompts.rs | 2 +- 25 files changed, 327 insertions(+), 326 deletions(-) diff --git a/crates/diagnostics/src/diagnostics_tests.rs b/crates/diagnostics/src/diagnostics_tests.rs index 705c70e982fadb..af46b1b07c5600 100644 --- a/crates/diagnostics/src/diagnostics_tests.rs +++ b/crates/diagnostics/src/diagnostics_tests.rs @@ -150,7 +150,7 @@ async fn test_diagnostics(cx: &mut TestAppContext) { }); // Open the project diagnostics view while there are already diagnostics. - let diagnostics = window.build_model(cx, |window, cx| { + let diagnostics = window.build_entity(cx, |window, cx| { ProjectDiagnosticsEditor::new_with_context( 1, true, @@ -485,7 +485,7 @@ async fn test_diagnostics_multiple_servers(cx: &mut TestAppContext) { let cx = &mut VisualTestContext::from_window(*window, cx); let workspace = window.root(cx).unwrap(); - let diagnostics = window.build_model(cx, |window, cx| { + let diagnostics = window.build_entity(cx, |window, cx| { ProjectDiagnosticsEditor::new_with_context( 1, true, @@ -763,7 +763,7 @@ async fn test_random_diagnostics(cx: &mut TestAppContext, mut rng: StdRng) { let cx = &mut VisualTestContext::from_window(*window, cx); let workspace = window.root(cx).unwrap(); - let mutated_diagnostics = window.build_model(cx, |window, cx| { + let mutated_diagnostics = window.build_entity(cx, |window, cx| { ProjectDiagnosticsEditor::new_with_context( 1, true, @@ -870,7 +870,7 @@ async fn test_random_diagnostics(cx: &mut TestAppContext, mut rng: StdRng) { cx.run_until_parked(); log::info!("constructing reference diagnostics view"); - let reference_diagnostics = window.build_model(cx, |window, cx| { + let reference_diagnostics = window.build_entity(cx, |window, cx| { ProjectDiagnosticsEditor::new_with_context( 1, true, diff --git a/crates/gpui/README.md b/crates/gpui/README.md index 6c0a5b607c769a..9faab7b6801873 100644 --- a/crates/gpui/README.md +++ b/crates/gpui/README.md @@ -11,7 +11,7 @@ GPUI is still in active development as we work on the Zed code editor and isn't 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 view. See [gpui.rs](https://www.gpui.rs/) for a complete example. +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. ### Dependencies @@ -41,9 +41,9 @@ On macOS, GPUI uses Metal for rendering. In order to use Metal, you need to do t GPUI offers three different [registers]() depending on your needs: -- State management and communication with Models. 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. +- 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 entities. Entities are owned by GPUI and are only accessible through an owned smart pointer similar to an `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 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 GPUI to turn into pixels. See the `div` element for an all purpose swiss-army knife of rendering. +- High level, declarative UI with views. All UI in GPUI starts with a view. A view is simply an `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 GPUI to turn into pixels. See the `div` element for an all purpose swiss-army knife of rendering. - Low level, imperative UI with Elements. Elements are the building blocks of UI in GPUI, and they provide a nice wrapper around an imperative API that provides as much flexibility and control as you need. Elements have total control over how they and their child elements are rendered and can be used for making efficient views into large lists, implement custom layouting for a code editor, and anything else you can think of. See the `element` module for more information. @@ -55,7 +55,7 @@ In addition to the systems above, GPUI provides a range of smaller services that - 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., diff --git a/crates/gpui/docs/contexts.md b/crates/gpui/docs/contexts.md index 120d2c3f0565d4..9295c775c759bd 100644 --- a/crates/gpui/docs/contexts.md +++ b/crates/gpui/docs/contexts.md @@ -1,41 +1,33 @@ # Contexts -GPUI makes extensive use of _context parameters_, typically named `cx` and positioned at the end of the parameter list, unless they're before a final function parameter. A context reference provides access to application state and services. +GPUI makes extensive use of _context parameters_ (typically named `cx`) to provide access to application state and services. These contexts are references passed to functions, enabling interaction with global state, windows, entities, and system services. -There are multiple kinds of contexts, and contexts implement the `Deref` trait so that a function taking `&mut AppContext` could be passed a `&mut Window, &mut AppContext` or `&mut ViewContext` instead. +--- -``` - AppContext - / \ -ModelContext WindowContext - / - ViewContext -``` +## `App` -- The `AppContext` forms the root of the hierarchy -- `ModelContext` and `WindowContext` both dereference to `AppContext` -- `ViewContext` dereferences to `WindowContext` +The root context granting access to the application's global state. This context owns all entities' data and can be used to read or update the data referenced by an `Entity`. -## `AppContext` +## `Context` -Provides access to the global application state. All other kinds of contexts ultimately deref to an `AppContext`. You can update a `Model` by passing an `AppContext`, but you can't update a view. For that you need a `WindowContext`... +A context provided when interacting with an `Entity`, with additional methods related to that specific entity such as notifying observers and emitting events. This context dereferences into `App`, meaning any function which can take an `App` reference can also take a `Context` reference, allowing you to access the application's global state. -## `WindowContext` +## `AsyncApp` and `AsyncWindowContext` -Provides access to the state of an application window, and also derefs to an `AppContext`, so you can pass a window context reference to any method taking an app context. Obtain this context by calling `WindowHandle::update`. +Whereas the above contexts are always passed to your code as references, you can call `to_async` on the reference to create an async context, which has a static lifetime and can be held across `await` points in async code. When you interact with entities with an async context, the calls become fallible, because the context may outlive the window or even the app itself. -## `Context` +## `TestAppContext` -Available when you create or update a `Model`. It derefs to an `AppContext`, but also contains methods specific to the particular model, such as the ability to notify change observers or emit events. +These are similar to the async contexts above, but they panic if you attempt to access a non-existent app or window, and they also contain other features specific to tests. -## `ViewContext` +--- -Available when you create or update a `View`. It derefs to a `WindowContext`, but also contains methods specific to the particular view, such as the ability to notify change observers or emit events. +# Non-Context Core Types -## `AsyncApp` and `AsyncWindowContext` +## `Window` -Whereas the above contexts are always passed to your code as references, you can call `to_async` on the reference to create an async context, which has a static lifetime and can be held across `await` points in async code. When you interact with `Model`s or `View`s with an async context, the calls become fallible, because the context may outlive the window or even the app itself. +Provides access to the state of an application window. This type has a root view (an `Entity` implementing `Render`) which it can read/update, but since it is not a context, you must pass a `&mut App` (or a context which dereferences to it) to do so, along with other functions interacting with global state. You can obtain a `Window` from an `WindowHandle` by calling `WindowHandle::update`. -## `TestAppContext` and `TestVisualContext` +## `Entity` -These are similar to the async contexts above, but they panic if you attempt to access a non-existent app or window, and they also contain other features specific to tests. +A handle to a structure requiring state. This data is owned by the `App` and can be accessed and modified via references to contexts. If `T` implements `Render`, then the entity is sometimes referred to as a view. Entities can be observed by other entities and windows, allowing a closure to be called when `notify` is called on the entity's `Context`. diff --git a/crates/gpui/examples/set_menus.rs b/crates/gpui/examples/set_menus.rs index af608edaf22997..2de8e872504d1d 100644 --- a/crates/gpui/examples/set_menus.rs +++ b/crates/gpui/examples/set_menus.rs @@ -37,7 +37,7 @@ fn main() { // Associate actions using the `actions!` macro (or `impl_actions!` macro) actions!(set_menus, [Quit]); -// Define the quit function that is registered with the AppContext +// Define the quit function that is registered with the App fn quit(_: &Quit, cx: &mut App) { println!("Gracefully quitting the application . . ."); cx.quit(); diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index a41870d0b68589..ee673e578c24f0 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -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`] to help us debug any double borrows. +/// Temporary(?) wrapper around [`RefCell`] 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 bool + 'static>; type QuitHandler = Box LocalBoxFuture<'static, ()> + 'static>; type ReleaseListener = Box; -type NewModelListener = Box, &mut App) + 'static>; +type NewEntityListener = Box, &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, pub(crate) platform: Rc, @@ -241,7 +241,7 @@ pub struct App { pub(crate) globals_by_type: FxHashMap>, pub(crate) entities: EntityMap, pub(crate) window_update_stack: Vec, - pub(crate) new_model_observers: SubscriberSet, + pub(crate) new_entity_observers: SubscriberSet, pub(crate) windows: SlotMap>, pub(crate) window_handles: FxHashMap, pub(crate) focus_handles: Arc, @@ -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( &mut self, entity: &Entity, @@ -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( &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( &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, ) { - 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), ) -> Subscription { - self.new_model_observer( + self.new_entity_observer( TypeId::of::(), 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::() .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( &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( &self, @@ -1573,22 +1577,25 @@ impl AppContext for App { type Result = 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(&mut self, build_model: impl FnOnce(&mut Context<'_, T>) -> T) -> Entity { + fn new( + &mut self, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, + ) -> Entity { 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::(), 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( &mut self, reservation: Reservation, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result> { 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( &mut self, - model: &Entity, + handle: &Entity, 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, }, - ModelCreated { + EntityCreated { entity: AnyEntity, tid: TypeId, window: Option, @@ -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), } } } diff --git a/crates/gpui/src/app/async_context.rs b/crates/gpui/src/app/async_context.rs index 29edb65200436a..3e8f5cdd914b3e 100644 --- a/crates/gpui/src/app/async_context.rs +++ b/crates/gpui/src/app/async_context.rs @@ -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, @@ -25,14 +25,14 @@ impl AppContext for AsyncApp { fn new( &mut self, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result> { 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(&mut self) -> Result> { @@ -47,14 +47,14 @@ impl AppContext for AsyncApp { fn insert_entity( &mut self, reservation: Reservation, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Result> { 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( @@ -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(&self) -> Result { 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(&self, read: impl FnOnce(&G, &App) -> R) -> Result { 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(&self, read: impl FnOnce(&G, &App) -> R) -> Option { 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( &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(&mut self, update: impl FnOnce(&mut Window, &mut App) -> R) -> Result { 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( &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( &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( &mut self, @@ -326,11 +326,11 @@ impl AsyncWindowContext { impl AppContext for AsyncWindowContext { type Result = Result; - fn new(&mut self, build_model: impl FnOnce(&mut Context<'_, T>) -> T) -> Result> + fn new(&mut self, build_entity: impl FnOnce(&mut Context<'_, T>) -> T) -> Result> where T: 'static, { - self.window.update(self, |_, _, cx| cx.new(build_model)) + self.window.update(self, |_, _, cx| cx.new(build_entity)) } fn reserve_entity(&mut self) -> Result> { @@ -340,10 +340,10 @@ impl AppContext for AsyncWindowContext { fn insert_entity( &mut self, reservation: Reservation, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result> { self.window - .update(self, |_, _, cx| cx.insert_entity(reservation, build_model)) + .update(self, |_, _, cx| cx.insert_entity(reservation, build_entity)) } fn update_entity( @@ -406,10 +406,10 @@ impl VisualContext for AsyncWindowContext { fn new_window_entity( &mut self, - build_model: impl FnOnce(&mut Window, &mut Context) -> T, + build_entity: impl FnOnce(&mut Window, &mut Context) -> T, ) -> Self::Result> { 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( @@ -418,7 +418,7 @@ impl VisualContext for AsyncWindowContext { update: impl FnOnce(&mut T, &mut Window, &mut Context) -> R, ) -> Self::Result { self.window.update(self, |_, window, cx| { - view.update(cx, |model, cx| update(model, window, cx)) + view.update(cx, |entity, cx| update(entity, window, cx)) }) } diff --git a/crates/gpui/src/app/context.rs b/crates/gpui/src/app/context.rs index 80916bee9f96e6..5e892a6505a7b9 100644 --- a/crates/gpui/src/app/context.rs +++ b/crates/gpui/src/app/context.rs @@ -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, + entity_state: WeakEntity, } impl<'a, T: 'static> Context<'a, T> { - pub(crate) fn new_context(app: &'a mut App, model_state: WeakEntity) -> Self { - Self { app, model_state } + pub(crate) fn new_context(app: &'a mut App, entity_state: WeakEntity) -> 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 { 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 { - 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( &mut self, entity: &Entity, @@ -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( &mut self, entity: &Entity, @@ -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( &self, entity: &Entity, @@ -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(&self, f: impl FnOnce(WeakEntity, AsyncApp) -> Fut) -> Task 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( &mut self, observed: &Entity, @@ -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( &mut self, emitter: &Entity, @@ -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( &self, observed: &Entity, @@ -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`] to avoid leaking the view for a long-running process. + /// The given callback is invoked with a [`WeakEntity`] 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( @@ -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::(), 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; - fn new(&mut self, build_model: impl FnOnce(&mut Context<'_, U>) -> U) -> Entity { - self.app.new(build_model) + fn new( + &mut self, + build_entity: impl FnOnce(&mut Context<'_, U>) -> U, + ) -> Entity { + self.app.new(build_entity) } fn reserve_entity(&mut self) -> Reservation { @@ -676,9 +679,9 @@ impl<'a, T> AppContext for Context<'a, T> { fn insert_entity( &mut self, reservation: Reservation, - build_model: impl FnOnce(&mut Context<'_, U>) -> U, + build_entity: impl FnOnce(&mut Context<'_, U>) -> U, ) -> Self::Result> { - self.app.insert_entity(reservation, build_model) + self.app.insert_entity(reservation, build_entity) } fn update_entity( diff --git a/crates/gpui/src/app/entity_map.rs b/crates/gpui/src/app/entity_map.rs index eee161dfeaf5c1..afdb99b0586e95 100644 --- a/crates/gpui/src/app/entity_map.rs +++ b/crates/gpui/src/app/entity_map.rs @@ -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(&self, model: &Entity) -> &T { - self.assert_valid_context(model); + pub fn read(&self, entity: &Entity) -> &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::("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(Entity); -/// A dynamically typed reference to a model, which can be downcast into a `Model`. +/// A dynamically typed reference to a entity, which can be downcast into a `Entity`. 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(self) -> Result, AnyEntity> { if TypeId::of::() == 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 From> for AnyEntity { - fn from(model: Entity) -> Self { - model.any_entity + fn from(entity: Entity) -> 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 Entity { }) } - /// 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 Entity { 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( &self, cx: &C, @@ -425,11 +425,11 @@ impl Entity { 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( &self, cx: &mut C, @@ -441,7 +441,7 @@ impl Entity { 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( @@ -467,7 +467,7 @@ impl Clone for Entity { impl std::fmt::Debug for Entity { 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::()) .finish() @@ -494,7 +494,7 @@ impl PartialEq> for Entity { } } -/// 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 { 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 From> for AnyWeakEntity { - fn from(model: WeakEntity) -> Self { - model.any_entity + fn from(entity: WeakEntity) -> 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 { #[deref] @@ -629,13 +629,13 @@ impl Clone for WeakEntity { } impl WeakEntity { - /// 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> { // 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( @@ -654,7 +654,7 @@ impl WeakEntity { ) } - /// 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( @@ -670,11 +670,11 @@ impl WeakEntity { 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(&self, cx: &C, read: impl FnOnce(&T, &App) -> R) -> Result diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rs index 46c4cfbbb0f34a..b14e572b505a61 100644 --- a/crates/gpui/src/app/test_context.rs +++ b/crates/gpui/src/app/test_context.rs @@ -34,10 +34,10 @@ impl AppContext for TestAppContext { fn new( &mut self, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result> { let mut app = self.app.borrow_mut(); - app.new(build_model) + app.new(build_entity) } fn reserve_entity(&mut self) -> Self::Result> { @@ -48,10 +48,10 @@ impl AppContext for TestAppContext { fn insert_entity( &mut self, reservation: crate::Reservation, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result> { let mut app = self.app.borrow_mut(); - app.insert_entity(reservation, build_model) + app.insert_entity(reservation, build_entity) } fn update_entity( @@ -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(&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(&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( &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(&mut self, entity: &Entity) -> impl Stream { 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>( &mut self, entity: &Entity, @@ -464,7 +464,7 @@ impl TestAppContext { let (tx, rx) = futures::channel::mpsc::unbounded(); entity .update(self, |_, cx: &mut Context| { - 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( &mut self, - model: &Entity, + entity: &Entity, mut predicate: impl FnMut(&mut T, &mut Context) -> 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 Entity { - /// 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(&self, cx: &mut TestAppContext) -> impl Future where Event: Send + Clone + 'static, @@ -560,7 +560,7 @@ impl Entity { .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(&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 { self.cx.test_window(self.window).0.lock().title.clone() } @@ -865,9 +865,9 @@ impl AppContext for VisualTestContext { fn new( &mut self, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result> { - self.cx.new(build_model) + self.cx.new(build_entity) } fn reserve_entity(&mut self) -> Self::Result> { @@ -877,9 +877,9 @@ impl AppContext for VisualTestContext { fn insert_entity( &mut self, reservation: crate::Reservation, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result> { - self.cx.insert_entity(reservation, build_model) + self.cx.insert_entity(reservation, build_entity) } fn update_entity( @@ -945,11 +945,11 @@ impl VisualContext for VisualTestContext { fn new_window_entity( &mut self, - build_model: impl FnOnce(&mut Window, &mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Window, &mut Context<'_, T>) -> T, ) -> Self::Result> { 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( + pub fn build_entity( &self, cx: &mut TestAppContext, build_view: impl FnOnce(&mut Window, &mut Context) -> V, diff --git a/crates/gpui/src/element.rs b/crates/gpui/src/element.rs index 2975568f5ac66d..c4e70d78852a46 100644 --- a/crates/gpui/src/element.rs +++ b/crates/gpui/src/element.rs @@ -114,8 +114,8 @@ pub trait IntoElement: Sized { impl 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; diff --git a/crates/gpui/src/elements/div.rs b/crates/gpui/src/elements/div.rs index b40bf2182e2868..4f12a6837ab8b5 100644 --- a/crates/gpui/src/elements/div.rs +++ b/crates/gpui/src/elements/div.rs @@ -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( &mut self, listener: impl Fn(&DragMoveEvent, &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( &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(&mut self, listener: impl Fn(&A, &mut Window, &mut App) + 'static) { self.action_listeners.push(( TypeId::of::(), @@ -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(&mut self, listener: impl Fn(&T, &mut Window, &mut App) + 'static) { self.drop_listeners.push(( TypeId::of::(), @@ -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( &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( mut self, listener: impl Fn(&DragMoveEvent, &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( 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( 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( 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( 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, diff --git a/crates/gpui/src/geometry.rs b/crates/gpui/src/geometry.rs index e5361a47330517..11b4a58554bafb 100644 --- a/crates/gpui/src/geometry.rs +++ b/crates/gpui/src/geometry.rs @@ -2846,7 +2846,7 @@ impl From 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 `` element in browsers), @@ -2855,7 +2855,7 @@ impl From 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); diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs index d13fa2da1a3e43..a6d96633bef780 100644 --- a/crates/gpui/src/gpui.rs +++ b/crates/gpui/src/gpui.rs @@ -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; - /// Create a new model in the app context. + /// Create a new entity in the app context. fn new( &mut self, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result>; - /// 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(&mut self) -> Self::Result>; - /// 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( &mut self, reservation: Reservation, - build_model: impl FnOnce(&mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Context<'_, T>) -> T, ) -> Self::Result>; - /// Update a model in the app context. + /// Update a entity in the app context. fn update_entity( &mut self, handle: &Entity, @@ -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( &self, handle: &Entity, @@ -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(pub(crate) Slot); impl Reservation { - /// 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( &mut self, - model: &Entity, + entity: &Entity, update: impl FnOnce(&mut T, &mut Window, &mut Context) -> R, ) -> Self::Result; /// Update a view with the given callback fn new_window_entity( &mut self, - build_model: impl FnOnce(&mut Window, &mut Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut Window, &mut Context<'_, T>) -> T, ) -> Self::Result>; /// 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(&mut self, model: &Entity) -> Self::Result<()> + /// Focus a entity in the window, if it implements the [`Focusable`] trait. + fn focus(&mut self, entity: &Entity) -> Self::Result<()> where V: Focusable; } diff --git a/crates/gpui/src/input.rs b/crates/gpui/src/input.rs index 38b5c15ecde9c7..41a63bd8a36283 100644 --- a/crates/gpui/src/input.rs +++ b/crates/gpui/src/input.rs @@ -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`]. -/// 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>; } -/// 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 { view: Entity, @@ -72,8 +72,7 @@ pub struct ElementInputHandler { } impl ElementInputHandler { - /// 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, view: Entity) -> Self { diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index 35b1f598b346ab..9d874b4f9b85e2 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -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 } diff --git a/crates/gpui/src/subscription.rs b/crates/gpui/src/subscription.rs index 700a248998b131..8a22dd2bbef66c 100644 --- a/crates/gpui/src/subscription.rs +++ b/crates/gpui/src/subscription.rs @@ -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(); diff --git a/crates/gpui/src/test.rs b/crates/gpui/src/test.rs index 16803d34a4faa4..f5b30916975a9b 100644 --- a/crates/gpui/src/test.rs +++ b/crates/gpui/src/test.rs @@ -101,7 +101,7 @@ impl futures::Stream for Observation { } } -/// 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(entity: &Entity, cx: &mut TestAppContext) -> Observation<()> { let (tx, rx) = smol::channel::unbounded(); let _subscription = cx.update(|cx| { diff --git a/crates/gpui/src/view.rs b/crates/gpui/src/view.rs index 178d1ddbc8d23d..9997e0713bb7a8 100644 --- a/crates/gpui/src/view.rs +++ b/crates/gpui/src/view.rs @@ -68,10 +68,10 @@ impl Element for Entity { } } -/// 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, } @@ -79,7 +79,7 @@ pub struct AnyView { impl From> for AnyView { fn from(value: Entity) -> Self { AnyView { - model: value.into_any(), + entity: value.into_any(), render: any_view::render::, cached_style: None, } @@ -88,8 +88,8 @@ impl From> 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(self) -> Result, 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 { - 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 From> for AnyWeakView { fn from(view: WeakEntity) -> Self { AnyWeakView { - model: view.into(), + entity: view.into(), render: any_view::render::, } } @@ -310,14 +310,14 @@ impl From> 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() } } diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index 543f680d989bef..19254d280d1a71 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -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( &mut self, observed: &Entity, @@ -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( &mut self, entity: &Entity, @@ -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( &self, entity: &Entity, @@ -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(&mut self, style: Option, 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. diff --git a/crates/gpui/src/window/prompts.rs b/crates/gpui/src/window/prompts.rs index f9e59005ef01d2..32a4c8d36b706b 100644 --- a/crates/gpui/src/window/prompts.rs +++ b/crates/gpui/src/window/prompts.rs @@ -68,7 +68,7 @@ pub struct RenderablePromptHandle { pub(crate) view: Box, } -/// 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, diff --git a/crates/gpui_macros/src/derive_app_context.rs b/crates/gpui_macros/src/derive_app_context.rs index 165b60040b7252..9414943d60b0b9 100644 --- a/crates/gpui_macros/src/derive_app_context.rs +++ b/crates/gpui_macros/src/derive_app_context.rs @@ -25,9 +25,9 @@ pub fn derive_app_context(input: TokenStream) -> TokenStream { fn new( &mut self, - build_model: impl FnOnce(&mut gpui::Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut gpui::Context<'_, T>) -> T, ) -> Self::Result> { - self.#app_variable.new(build_model) + self.#app_variable.new(build_entity) } fn reserve_entity(&mut self) -> Self::Result> { @@ -37,9 +37,9 @@ pub fn derive_app_context(input: TokenStream) -> TokenStream { fn insert_entity( &mut self, reservation: gpui::Reservation, - build_model: impl FnOnce(&mut gpui::Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut gpui::Context<'_, T>) -> T, ) -> Self::Result> { - self.#app_variable.insert_entity(reservation, build_model) + self.#app_variable.insert_entity(reservation, build_entity) } fn update_entity( diff --git a/crates/gpui_macros/src/derive_visual_context.rs b/crates/gpui_macros/src/derive_visual_context.rs index 7a43b351284dd0..71e2a532943606 100644 --- a/crates/gpui_macros/src/derive_visual_context.rs +++ b/crates/gpui_macros/src/derive_visual_context.rs @@ -34,17 +34,17 @@ pub fn derive_visual_context(input: TokenStream) -> TokenStream { fn update_window_entity( &mut self, - model: &gpui::Entity, + entity: &gpui::Entity, update: impl FnOnce(&mut T, &mut gpui::Window, &mut gpui::Context) -> R, ) -> Self::Result { - gpui::AppContext::update_entity(self.#app_variable, model, |entity, cx| update(entity, self.#window_variable, cx)) + gpui::AppContext::update_entity(self.#app_variable, entity, |entity, cx| update(entity, self.#window_variable, cx)) } fn new_window_entity( &mut self, - build_model: impl FnOnce(&mut gpui::Window, &mut gpui::Context<'_, T>) -> T, + build_entity: impl FnOnce(&mut gpui::Window, &mut gpui::Context<'_, T>) -> T, ) -> Self::Result> { - gpui::AppContext::new(self.#app_variable, |cx| build_model(self.#window_variable, cx)) + gpui::AppContext::new(self.#app_variable, |cx| build_entity(self.#window_variable, cx)) } fn replace_root_view( @@ -57,11 +57,11 @@ pub fn derive_visual_context(input: TokenStream) -> TokenStream { self.#window_variable.replace_root(self.#app_variable, build_view) } - fn focus(&mut self, model: &gpui::Entity) -> Self::Result<()> + fn focus(&mut self, entity: &gpui::Entity) -> Self::Result<()> where V: gpui::Focusable, { - let focus_handle = gpui::Focusable::focus_handle(model, self.#app_variable); + let focus_handle = gpui::Focusable::focus_handle(entity, self.#app_variable); self.#window_variable.focus(&focus_handle) } } diff --git a/crates/search/src/buffer_search.rs b/crates/search/src/buffer_search.rs index 671ffb070225d0..d8cba0d0a60af2 100644 --- a/crates/search/src/buffer_search.rs +++ b/crates/search/src/buffer_search.rs @@ -1845,11 +1845,11 @@ mod tests { let buffer = cx.new(|cx| Buffer::local(buffer_text, cx)); let window = cx.add_window(|_, _| gpui::Empty); - let editor = window.build_model(cx, |window, cx| { + let editor = window.build_entity(cx, |window, cx| { Editor::for_buffer(buffer.clone(), None, window, cx) }); - let search_bar = window.build_model(cx, |window, cx| { + let search_bar = window.build_entity(cx, |window, cx| { let mut search_bar = BufferSearchBar::new(window, cx); search_bar.set_active_pane_item(Some(&editor), window, cx); search_bar.show(window, cx); diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index f6fb6de2bae4f0..130531b732ff0a 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -2337,7 +2337,7 @@ pub mod tests { let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await; let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx)); let workspace = window; - let search_bar = window.build_model(cx, |_, _| ProjectSearchBar::new()); + let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new()); let active_item = cx.read(|cx| { workspace @@ -2577,7 +2577,7 @@ pub mod tests { let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await; let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx)); let workspace = window; - let search_bar = window.build_model(cx, |_, _| ProjectSearchBar::new()); + let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new()); let active_item = cx.read(|cx| { workspace @@ -2879,7 +2879,7 @@ pub mod tests { }); let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx)); let workspace = window.root(cx).unwrap(); - let search_bar = window.build_model(cx, |_, _| ProjectSearchBar::new()); + let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new()); let active_item = cx.read(|cx| { workspace @@ -2997,7 +2997,7 @@ pub mod tests { let project = Project::test(fs.clone(), ["/dir".as_ref()], cx).await; let window = cx.add_window(|window, cx| Workspace::test_new(project, window, cx)); let workspace = window.root(cx).unwrap(); - let search_bar = window.build_model(cx, |_, _| ProjectSearchBar::new()); + let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new()); window .update(cx, { @@ -3333,8 +3333,8 @@ pub mod tests { .update(cx, |this, _, _| this.panes().to_owned()) .unwrap(); - let search_bar_1 = window.build_model(cx, |_, _| ProjectSearchBar::new()); - let search_bar_2 = window.build_model(cx, |_, _| ProjectSearchBar::new()); + let search_bar_1 = window.build_entity(cx, |_, _| ProjectSearchBar::new()); + let search_bar_2 = window.build_entity(cx, |_, _| ProjectSearchBar::new()); assert_eq!(panes.len(), 1); let first_pane = panes.first().cloned().unwrap(); @@ -3587,7 +3587,7 @@ pub mod tests { .focus_handle(cx) .contains_focused(window, cx)) .unwrap()); - let search_bar = window.build_model(cx, |_, _| ProjectSearchBar::new()); + let search_bar = window.build_entity(cx, |_, _| ProjectSearchBar::new()); window .update(cx, { let search_bar = search_bar.clone(); diff --git a/crates/zed/src/zed/linux_prompts.rs b/crates/zed/src/zed/linux_prompts.rs index 067bcb20d83440..01b1e53aa8eade 100644 --- a/crates/zed/src/zed/linux_prompts.rs +++ b/crates/zed/src/zed/linux_prompts.rs @@ -15,7 +15,7 @@ use workspace::ui::StyledExt; pub fn init(cx: &mut App) { cx.set_prompt_builder(fallback_prompt_renderer) } -/// 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,