Skip to content

Commit

Permalink
gpui: Update docs to reflect removal of View, ViewContext, WindowCont…
Browse files Browse the repository at this point in the history
…ext (#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
  • Loading branch information
someone13574 authored Jan 31, 2025
1 parent 027fe1b commit 0c94bdc
Show file tree
Hide file tree
Showing 25 changed files with 327 additions and 326 deletions.
8 changes: 4 additions & 4 deletions crates/diagnostics/src/diagnostics_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions crates/gpui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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](<https://en.wikipedia.org/wiki/Register_(sociolinguistics)>) 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.

Expand All @@ -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.,

Expand Down
40 changes: 16 additions & 24 deletions crates/gpui/docs/contexts.md
Original file line number Diff line number Diff line change
@@ -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<T>`.

## `AppContext`
## `Context<T>`

Provides access to the global application state. All other kinds of contexts ultimately deref to an `AppContext`. You can update a `Model<T>` 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<T>`, 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<T>` 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<T>`
## `TestAppContext`

Available when you create or update a `Model<T>`. 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<V>`
---

Available when you create or update a `View<V>`. 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<T>`

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`.
2 changes: 1 addition & 1 deletion crates/gpui/examples/set_menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 0c94bdc

Please sign in to comment.