diff --git a/.rules b/.rules index 7c98c65d7e0eaf3ed0d57898dbd8acee28a220ae..136c4de46af26eaeec1aba8476721f36b20cebf2 100644 --- a/.rules +++ b/.rules @@ -71,9 +71,9 @@ When `read_with`, `update`, or `update_in` are used with an async context, the All use of entities and UI rendering occurs on a single foreground thread. -`cx.spawn(async move |cx| ...)` runs an async closure on the foreground thread. Within the closure, `cx` is an async context like `AsyncApp` or `AsyncWindowContext`. +`cx.spawn(async move |cx| ...)` runs an async closure on the foreground thread. Within the closure, `cx` is `&mut AsyncApp`. -When the outer cx is a `Context`, the use of `spawn` instead looks like `cx.spawn(async move |handle, cx| ...)`, where `handle: WeakEntity`. +When the outer cx is a `Context`, the use of `spawn` instead looks like `cx.spawn(async move |this, cx| ...)`, where `this: WeakEntity` and `cx: &mut AsyncApp`. To do work on other threads, `cx.background_spawn(async move { ... })` is used. Often this background task is awaited on by a foreground task which uses the results to update state. @@ -103,7 +103,7 @@ impl Render for TextWithBorder { Since `impl IntoElement for SharedString` exists, it can be used as an argument to `child`. `SharedString` is used to avoid copying strings, and is either an `&'static str` or `Arc`. -UI components that are constructed just to be turned into elements can instead implement the `RenderOnce` trait, which is similar to `Render`, but its `render` method takes ownership of `self`. Types that implement this trait can use `#[derive(IntoElement)]` to use them directly as children. +UI components that are constructed just to be turned into elements can instead implement the `RenderOnce` trait, which is similar to `Render`, but its `render` method takes ownership of `self` and receives `&mut App` instead of `&mut Context`. Types that implement this trait can use `#[derive(IntoElement)]` to use them directly as children. The style methods on elements are similar to those used by Tailwind CSS. @@ -129,21 +129,11 @@ When a view's state has changed in a way that may affect its rendering, it shoul ## Entity events -While updating an entity (`cx: Context`), it can emit an event using `cx.emit(event)`. Entities register which events they can emit by declaring `impl EventEmittor for EntityType {}`. +While updating an entity (`cx: Context`), it can emit an event using `cx.emit(event)`. Entities register which events they can emit by declaring `impl EventEmitter for EntityType {}`. Other entities can then register a callback to handle these events by doing `cx.subscribe(other_entity, |this, other_entity, event, cx| ...)`. This will return a `Subscription` which deregisters the callback when dropped. Typically `cx.subscribe` happens when creating a new entity and the subscriptions are stored in a `_subscriptions: Vec` field. -## Recent API changes - -GPUI has had some changes to its APIs. Always write code using the new APIs: - -* `spawn` methods now take async closures (`AsyncFn`), and so should be called like `cx.spawn(async move |cx| ...)`. -* Use `Entity`. This replaces `Model` and `View` which no longer exist and should NEVER be used. -* Use `App` references. This replaces `AppContext` which no longer exists and should NEVER be used. -* Use `Context` references. This replaces `ModelContext` which no longer exists and should NEVER be used. -* `Window` is now passed around explicitly. The new interface adds a `Window` reference parameter to some methods, and adds some new "*_in" methods for plumbing `Window`. The old types `WindowContext` and `ViewContext` should NEVER be used. - - -## General guidelines +## Build guidelines - Use `./script/clippy` instead of `cargo clippy` +- Always use `-q` when invoking `cargo`