1//! # Welcome to GPUI!
2//!
3//! GPUI is a hybrid immediate and retained mode, GPU accelerated, UI framework
4//! for Rust, designed to support a wide variety of applications.
5//!
6//! ## Getting Started
7//!
8//! GPUI is still in active development as we work on the Zed code editor and isn't yet on crates.io.
9//! You'll also need to use the latest version of stable rust. Add the following to your Cargo.toml:
10//!
11//! ```
12//! gpui = { git = "https://github.com/zed-industries/zed" }
13//! ```
14//!
15//! Everything in GPUI starts with an [`App`]. You can create one with [`App::new`], and
16//! kick off your application by passing a callback to [`App::run`]. Inside this callback,
17//! you can create a new window with [`AppContext::open_window`], and register your first root
18//! view. See [gpui.rs](https://www.gpui.rs/) for a complete example.
19//!
20//! ## The Big Picture
21//!
22//! GPUI offers three different [registers](https://en.wikipedia.org/wiki/Register_(sociolinguistics)) depending on your needs:
23//!
24//! - State management and communication with Models. Whenever you need to store application state
25//! that communicates between different parts of your application, you'll want to use GPUI's
26//! models. Models are owned by GPUI and are only accessible through an owned smart pointer
27//! similar to an [`Rc`]. See the [`app::model_context`] module for more information.
28//!
29//! - High level, declarative UI with Views. All UI in GPUI starts with a View. A view is simply
30//! a model that can be rendered, via the [`Render`] trait. At the start of each frame, GPUI
31//! will call this render method on the root view of a given window. Views build a tree of
32//! `elements`, lay them out and style them with a tailwind-style API, and then give them to
33//! GPUI to turn into pixels. See the [`elements::Div`] element for an all purpose swiss-army
34//! knife for UI.
35//!
36//! - Low level, imperative UI with Elements. Elements are the building blocks of UI in GPUI, and they
37//! provide a nice wrapper around an imperative API that provides as much flexibility and control as
38//! you need. Elements have total control over how they and their child elements are rendered and and
39//! can be used for making efficient views into large lists, implement custom layouting for a code editor,
40//! and anything else you can think of. See the [`element`] module for more information.
41//!
42//! Each of these registers has one or more corresponding contexts that can be accessed from all GPUI services.
43//! This context is your main interface to GPUI, and is used extensively throughout the framework.
44//!
45//! ## Other Resources
46//!
47//! In addition to the systems above, GPUI provides a range of smaller services that are useful for building
48//! complex applications:
49//!
50//! - Actions are user-defined structs that are used for converting keystrokes into logical operations in your UI.
51//! Use this for implementing keyboard shortcuts, such as cmd-q. See the [`action`] module for more information.
52//! - Platform services, such as `quit the app` or `open a URL` are available as methods on the [`app::AppContext`].
53//! - An async executor that is integrated with the platform's event loop. See the [`executor`] module for more information.,
54//! - The [gpui::test] macro provides a convenient way to write tests for your GPUI applications. Tests also have their
55//! own kind of context, a [`TestAppContext`] which provides ways of simulating common platform input. See [`app::test_context`]
56//! and [`test`] modules for more details.
57//!
58//! Currently, the best way to learn about these APIs is to read the Zed source code, ask us about it at a fireside hack, or drop
59//! a question in the [Zed Discord](https://discord.gg/U4qhCEhMXP). We're working on improving the documentation, creating more examples,
60//! and will be publishing more guides to GPUI on our [blog](https://zed.dev/blog).
61
62#![deny(missing_docs)]
63#![allow(clippy::type_complexity)]
64
65#[macro_use]
66mod action;
67mod app;
68
69mod arena;
70mod assets;
71mod color;
72mod element;
73mod elements;
74mod executor;
75mod geometry;
76mod image_cache;
77mod input;
78mod interactive;
79mod key_dispatch;
80mod keymap;
81mod platform;
82pub mod prelude;
83mod scene;
84mod shared_string;
85mod shared_uri;
86mod style;
87mod styled;
88mod subscription;
89mod svg_renderer;
90mod taffy;
91#[cfg(any(test, feature = "test-support"))]
92pub mod test;
93mod text_system;
94mod util;
95mod view;
96mod window;
97
98/// Do not touch, here be dragons for use by gpui_macros and such.
99#[doc(hidden)]
100pub mod private {
101 pub use linkme;
102 pub use serde;
103 pub use serde_derive;
104 pub use serde_json;
105}
106
107mod seal {
108 /// A mechanism for restricting implementations of a trait to only those in GPUI.
109 /// See: https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/
110 pub trait Sealed {}
111}
112
113pub use action::*;
114pub use anyhow::Result;
115pub use app::*;
116pub(crate) use arena::*;
117pub use assets::*;
118pub use color::*;
119pub use ctor::ctor;
120pub use element::*;
121pub use elements::*;
122pub use executor::*;
123pub use geometry::*;
124pub use gpui_macros::{register_action, test, IntoElement, Render};
125use image_cache::*;
126pub use input::*;
127pub use interactive::*;
128use key_dispatch::*;
129pub use keymap::*;
130pub use platform::*;
131pub use refineable::*;
132pub use scene::*;
133use seal::Sealed;
134pub use shared_string::*;
135pub use shared_uri::*;
136pub use smol::Timer;
137pub use style::*;
138pub use styled::*;
139pub use subscription::*;
140use svg_renderer::*;
141pub use taffy::{AvailableSpace, LayoutId};
142#[cfg(any(test, feature = "test-support"))]
143pub use test::*;
144pub use text_system::*;
145pub use util::arc_cow::ArcCow;
146pub use view::*;
147pub use window::*;
148
149use std::{any::Any, borrow::BorrowMut};
150use taffy::TaffyLayoutEngine;
151
152/// The context trait, allows the different contexts in GPUI to be used
153/// interchangeably for certain operations.
154pub trait Context {
155 /// The result type for this context, used for async contexts that
156 /// can't hold a direct reference to the application context.
157 type Result<T>;
158
159 /// Create a new model in the app context.
160 fn new_model<T: 'static>(
161 &mut self,
162 build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
163 ) -> Self::Result<Model<T>>;
164
165 /// Update a model in the app context.
166 fn update_model<T, R>(
167 &mut self,
168 handle: &Model<T>,
169 update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
170 ) -> Self::Result<R>
171 where
172 T: 'static;
173
174 /// Read a model from the app context.
175 fn read_model<T, R>(
176 &self,
177 handle: &Model<T>,
178 read: impl FnOnce(&T, &AppContext) -> R,
179 ) -> Self::Result<R>
180 where
181 T: 'static;
182
183 /// Update a window for the given handle.
184 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
185 where
186 F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
187
188 /// Read a window off of the application context.
189 fn read_window<T, R>(
190 &self,
191 window: &WindowHandle<T>,
192 read: impl FnOnce(View<T>, &AppContext) -> R,
193 ) -> Result<R>
194 where
195 T: 'static;
196}
197
198/// This trait is used for the different visual contexts in GPUI that
199/// require a window to be present.
200pub trait VisualContext: Context {
201 /// Construct a new view in the window referenced by this context.
202 fn new_view<V>(
203 &mut self,
204 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
205 ) -> Self::Result<View<V>>
206 where
207 V: 'static + Render;
208
209 /// Update a view with the given callback
210 fn update_view<V: 'static, R>(
211 &mut self,
212 view: &View<V>,
213 update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
214 ) -> Self::Result<R>;
215
216 /// Replace the root view of a window with a new view.
217 fn replace_root_view<V>(
218 &mut self,
219 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
220 ) -> Self::Result<View<V>>
221 where
222 V: 'static + Render;
223
224 /// Focus a view in the window, if it implements the [`FocusableView`] trait.
225 fn focus_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
226 where
227 V: FocusableView;
228
229 /// Dismiss a view in the window, if it implements the [`ManagedView`] trait.
230 fn dismiss_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
231 where
232 V: ManagedView;
233}
234
235/// A trait that allows models and views to be interchangeable in certain operations
236pub trait Entity<T>: Sealed {
237 /// The weak reference type for this entity.
238 type Weak: 'static;
239
240 /// The ID for this entity
241 fn entity_id(&self) -> EntityId;
242
243 /// Downgrade this entity to a weak reference.
244 fn downgrade(&self) -> Self::Weak;
245
246 /// Upgrade this entity from a weak reference.
247 fn upgrade_from(weak: &Self::Weak) -> Option<Self>
248 where
249 Self: Sized;
250}
251
252/// A trait for tying together the types of a GPUI entity and the events it can
253/// emit.
254pub trait EventEmitter<E: Any>: 'static {}
255
256/// A helper trait for auto-implementing certain methods on contexts that
257/// can be used interchangeably.
258pub trait BorrowAppContext {
259 /// Set a global value on the context.
260 fn set_global<T: Global>(&mut self, global: T);
261}
262
263impl<C> BorrowAppContext for C
264where
265 C: BorrowMut<AppContext>,
266{
267 fn set_global<G: Global>(&mut self, global: G) {
268 self.borrow_mut().set_global(global)
269 }
270}
271
272/// A flatten equivalent for anyhow `Result`s.
273pub trait Flatten<T> {
274 /// Convert this type into a simple `Result<T>`.
275 fn flatten(self) -> Result<T>;
276}
277
278impl<T> Flatten<T> for Result<Result<T>> {
279 fn flatten(self) -> Result<T> {
280 self?
281 }
282}
283
284impl<T> Flatten<T> for Result<T> {
285 fn flatten(self) -> Result<T> {
286 self
287 }
288}
289
290/// A marker trait for types that can be stored in GPUI's global state.
291///
292/// Implement this on types you want to store in the context as a global.
293pub trait Global: 'static {}