From 32ad486a8e7e658c3c12ca29bf73c53b4cfb0dd9 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 14 Nov 2023 19:52:51 -0700 Subject: [PATCH] Document contexts --- crates/gpui2/docs/contexts.md | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/crates/gpui2/docs/contexts.md b/crates/gpui2/docs/contexts.md index e69de29bb2..763a902517 100644 --- a/crates/gpui2/docs/contexts.md +++ b/crates/gpui2/docs/contexts.md @@ -0,0 +1,41 @@ +# 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 WindowContext` 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` 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` + +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. + +## `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. + +## `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 `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. + +## `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.