contexts.md

 1# Contexts
 2
 3GPUI 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.
 4
 5There are multiple kinds of contexts, and contexts implement the `Deref` trait so that a function taking `&mut AppContext` could be passed a `&mut WindowContext` or `&mut ViewContext` instead.
 6
 7```
 8     AppContext
 9     /        \
10ModelContext  WindowContext
11              /
12        ViewContext
13```
14
15- The `AppContext` forms the root of the hierarchy
16- `ModelContext` and `WindowContext` both dereference to `AppContext`
17- `ViewContext` dereferences to `WindowContext`
18
19## `AppContext`
20
21Provides 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`...
22
23## `WindowContext`
24
25Provides 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`.
26
27## `ModelContext<T>`
28
29Available 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.
30
31## `ViewContext<V>`
32
33Available 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.
34
35## `AsyncAppContext` and `AsyncWindowContext`
36
37Whereas 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.
38
39## `TestAppContext` and `TestVisualContext`
40
41These 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.