# Contexts
Most GPUI callbacks receive a `cx` argument. It is a *context*: access to the
application, sometimes paired with information about the entity or window
being processed. The context type tells you which state and services the
callback may use.
| [`&mut App`](crate::App) | In the [`Application::run`](crate::Application::run) callback and in element event handlers | Everything application-wide: entities, windows, globals, key bindings, platform services |
| [`&mut Context<T>`](crate::Context) | While creating, updating or rendering the entity `T` | `notify`, `emit`, `observe`, `subscribe`, `listener` and `spawn` for that entity |
| [`&mut Window`](crate::Window) | Next to the app context in render functions, event handlers and window updates | The window's focus, size, scale factor, input state and paint methods |
| [`&mut AsyncApp`](crate::AsyncApp) | Inside futures started with `spawn` | Access to the app across `.await` points |
| [`&mut AsyncWindowContext`](crate::AsyncWindowContext) | Inside futures started with `spawn_in` | The same, plus one window |
## `App`: the application
[`App`](crate::App) is the application-wide context. It owns entities and
windows, key bindings, global values and the connection to the operating
system. A function that takes `&mut App` can use those application-wide
services:
```standalone_crate
use gpui::{App, AppContext, KeyBinding, actions};
actions!(app, [Quit]);
fn init(cx: &mut App) {
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
cx.on_action(|_: &Quit, cx| cx.quit());
}
# gpui::TestApp::new().update(init);
```
## `Context<T>`: working on one entity
When you create or update an entity, GPUI gives you a
[`Context<T>`](crate::Context). It dereferences to `App`, so it can do
everything `App` can, and it knows which entity it belongs to. That knowledge
is what lets it offer methods like these without naming the entity again:
* [`notify`](crate::Context::notify) marks this entity as changed.
* [`emit`](crate::Context::emit) sends an event from this entity.
* [`observe`](crate::Context::observe) and
[`subscribe`](crate::Context::subscribe) register callbacks that receive this
entity mutably.
* [`listener`](crate::Context::listener) wraps an element event handler so
that it receives this entity mutably.
* [`spawn`](crate::Context::spawn) starts async work with a weak handle to this
entity.
```
# use gpui::{AppContext, Context, TestApp};
struct Clock {
ticks: u64,
}
impl Clock {
fn tick(&mut self, cx: &mut Context<Self>) {
self.ticks += 1;
cx.notify();
}
}
# TestApp::new().update(|cx| {
count.update(cx, |count, cx| {
*count = 0;
cx.notify();
});
}
# gpui::TestApp::new().update(|cx| {
# let count = cx.new(|_| 5_u64);
# reset(&count, cx);
# assert_eq!(*count.read(cx), 0);
# });
```
[`VisualContext`](crate::VisualContext) extends `AppContext` with operations
that need a window. Import both through [`gpui::prelude`](crate::prelude).
## Borrowing through a context
A context is a mutable borrow of the application, so the borrow checker applies
to it. You cannot hold a reference returned by
[`Entity::read`](crate::Entity::read) while you use the same context mutably:
```compile_fail
# use gpui::{AppContext, TestApp};
# TestApp::new().update(|cx| {
let name = cx.new(|_| String::from("GPUI"));
let borrowed: &String = name.read(cx);
name.update(cx, |name, _| name.push('!')); // error: `cx` is already borrowed
println!("{borrowed}");
# });
```
Copy what you need out of the entity first, or do the work inside a single
update. This rule is what makes entity access safe without locks: there is only
ever one `&mut App`, so only one piece of code can be changing state at a time.
Next, [the frame](crate::_documentation::concepts::the_frame) follows a change from `notify` to the
pixels on screen.