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