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