ZIm/crates/gpui/docs/contexts.md
Nathan Sobo 6fca1d2b0b
Eliminate GPUI View, ViewContext, and WindowContext types (#22632)
There's still a bit more work to do on this, but this PR is compiling
(with warnings) after eliminating the key types. When the tasks below
are complete, this will be the new narrative for GPUI:

- `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit
of state, and if `T` implements `Render`, then `Entity<T>` implements
`Element`.
- `&mut App` This replaces `AppContext` and represents the app.
- `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It
is provided by the framework when updating an entity.
- `&mut Window` Broken out of `&mut WindowContext` which no longer
exists. Every method that once took `&mut WindowContext` now takes `&mut
Window, &mut App` and every method that took `&mut ViewContext<T>` now
takes `&mut Window, &mut Context<T>`

Not pictured here are the two other failed attempts. It's been quite a
month!

Tasks:

- [x] Remove `View`, `ViewContext`, `WindowContext` and thread through
`Window`
- [x] [@cole-miller @mikayla-maki] Redraw window when entities change
- [x] [@cole-miller @mikayla-maki] Get examples and Zed running
- [x] [@cole-miller @mikayla-maki] Fix Zed rendering
- [x] [@mikayla-maki] Fix todo! macros and comments
- [x] Fix a bug where the editor would not be redrawn because of view
caching
- [x] remove publicness window.notify() and replace with
`AppContext::notify`
- [x] remove `observe_new_window_models`, replace with
`observe_new_models` with an optional window
- [x] Fix a bug where the project panel would not be redrawn because of
the wrong refresh() call being used
- [x] Fix the tests
- [x] Fix warnings by eliminating `Window` params or using `_`
- [x] Fix conflicts
- [x] Simplify generic code where possible
- [x] Rename types
- [ ] Update docs

### issues post merge

- [x] Issues switching between normal and insert mode
- [x] Assistant re-rendering failure
- [x] Vim test failures
- [x] Mac build issue



Release Notes:

- N/A

---------

Co-authored-by: Antonio Scandurra <me@as-cii.com>
Co-authored-by: Cole Miller <cole@zed.dev>
Co-authored-by: Mikayla <mikayla@zed.dev>
Co-authored-by: Joseph <joseph@zed.dev>
Co-authored-by: max <max@zed.dev>
Co-authored-by: Michael Sloan <michael@zed.dev>
Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local>
Co-authored-by: Mikayla <mikayla.c.maki@gmail.com>
Co-authored-by: joão <joao@zed.dev>
2025-01-26 03:02:45 +00:00

2.3 KiB

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.

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
  • The AppContext forms the root of the hierarchy
  • ModelContext and WindowContext both dereference to AppContext
  • ViewContext dereferences to WindowContext

AppContext

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...

WindowContext

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.

ModelContext<T>

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.

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.

AsyncAppContext and AsyncWindowContext

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 Models or Views with an async context, the calls become fallible, because the context may outlive the window or even the app itself.

TestAppContext and TestVisualContext

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.