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. GPUI is currently
5//! being actively developed and improved for the [Zed code editor](https://zed.dev/), and new versions
6//! will have breaking changes. You'll probably need to use the latest stable version
7//! of rust to use GPUI.
8//!
9//! # Getting started with GPUI
10//!
11//! TODO!(docs): Write a code sample showing how to create a window and render a simple
12//! div
13//!
14//! # Drawing interesting things
15//!
16//! TODO!(docs): Expand demo to show how to draw a more interesting scene, with
17//! a counter to store state and a button to increment it.
18//!
19//! # Interacting with your application state
20//!
21//! TODO!(docs): Expand demo to show GPUI entity interactions, like subscriptions and entities
22//! maybe make a network request to show async stuff?
23//!
24//! # Conclusion
25//!
26//! TODO!(docs): Wrap up with a conclusion and links to other places? Zed / GPUI website?
27//! Discord for chatting about it? Other tutorials or references?
28
29#![deny(missing_docs)]
30#![allow(clippy::type_complexity)]
31
32#[macro_use]
33mod action;
34mod app;
35
36mod arena;
37mod assets;
38mod color;
39mod element;
40mod elements;
41mod executor;
42mod geometry;
43mod image_cache;
44mod input;
45mod interactive;
46mod key_dispatch;
47mod keymap;
48mod platform;
49pub mod prelude;
50mod scene;
51mod shared_string;
52mod shared_url;
53mod style;
54mod styled;
55mod subscription;
56mod svg_renderer;
57mod taffy;
58#[cfg(any(test, feature = "test-support"))]
59pub mod test;
60mod text_system;
61mod util;
62mod view;
63mod window;
64
65/// Do not touch, here be dragons for use by gpui_macros and such.
66#[doc(hidden)]
67pub mod private {
68 pub use linkme;
69 pub use serde;
70 pub use serde_derive;
71 pub use serde_json;
72}
73
74mod seal {
75 /// A mechanism for restricting implementations of a trait to only those in GPUI.
76 /// See: https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/
77 pub trait Sealed {}
78}
79
80pub use action::*;
81pub use anyhow::Result;
82pub use app::*;
83pub(crate) use arena::*;
84pub use assets::*;
85pub use color::*;
86pub use ctor::ctor;
87pub use element::*;
88pub use elements::*;
89pub use executor::*;
90pub use geometry::*;
91pub use gpui_macros::{register_action, test, IntoElement, Render};
92use image_cache::*;
93pub use input::*;
94pub use interactive::*;
95use key_dispatch::*;
96pub use keymap::*;
97pub use platform::*;
98pub use refineable::*;
99pub use scene::*;
100use seal::Sealed;
101pub use shared_string::*;
102pub use shared_url::*;
103pub use smol::Timer;
104pub use style::*;
105pub use styled::*;
106pub use subscription::*;
107use svg_renderer::*;
108pub use taffy::{AvailableSpace, LayoutId};
109#[cfg(any(test, feature = "test-support"))]
110pub use test::*;
111pub use text_system::*;
112pub use util::arc_cow::ArcCow;
113pub use view::*;
114pub use window::*;
115
116use std::{any::Any, borrow::BorrowMut};
117use taffy::TaffyLayoutEngine;
118
119/// The context trait, allows the different contexts in GPUI to be used
120/// interchangeably for certain operations.
121pub trait Context {
122 /// The result type for this context, used for async contexts that
123 /// can't hold a direct reference to the application context.
124 type Result<T>;
125
126 /// Create a new model in the app context.
127 fn new_model<T: 'static>(
128 &mut self,
129 build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
130 ) -> Self::Result<Model<T>>;
131
132 /// Update a model in the app context.
133 fn update_model<T, R>(
134 &mut self,
135 handle: &Model<T>,
136 update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
137 ) -> Self::Result<R>
138 where
139 T: 'static;
140
141 /// Read a model from the app context.
142 fn read_model<T, R>(
143 &self,
144 handle: &Model<T>,
145 read: impl FnOnce(&T, &AppContext) -> R,
146 ) -> Self::Result<R>
147 where
148 T: 'static;
149
150 /// Update a window for the given handle.
151 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
152 where
153 F: FnOnce(AnyView, &mut WindowContext<'_>) -> T;
154
155 /// Read a window off of the application context.
156 fn read_window<T, R>(
157 &self,
158 window: &WindowHandle<T>,
159 read: impl FnOnce(View<T>, &AppContext) -> R,
160 ) -> Result<R>
161 where
162 T: 'static;
163}
164
165/// This trait is used for the different visual contexts in GPUI that
166/// require a window to be present.
167pub trait VisualContext: Context {
168 /// Construct a new view in the window referenced by this context.
169 fn new_view<V>(
170 &mut self,
171 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
172 ) -> Self::Result<View<V>>
173 where
174 V: 'static + Render;
175
176 /// Update a view with the given callback
177 fn update_view<V: 'static, R>(
178 &mut self,
179 view: &View<V>,
180 update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
181 ) -> Self::Result<R>;
182
183 /// Replace the root view of a window with a new view.
184 fn replace_root_view<V>(
185 &mut self,
186 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
187 ) -> Self::Result<View<V>>
188 where
189 V: 'static + Render;
190
191 /// Focus a view in the window, if it implements the [`FocusableView`] trait.
192 fn focus_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
193 where
194 V: FocusableView;
195
196 /// Dismiss a view in the window, if it implements the [`ManagedView`] trait.
197 fn dismiss_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
198 where
199 V: ManagedView;
200}
201
202/// A trait that allows models and views to be interchangeable in certain operations
203pub trait Entity<T>: Sealed {
204 /// The weak reference type for this entity.
205 type Weak: 'static;
206
207 /// The ID for this entity
208 fn entity_id(&self) -> EntityId;
209
210 /// Downgrade this entity to a weak reference.
211 fn downgrade(&self) -> Self::Weak;
212
213 /// Upgrade this entity from a weak reference.
214 fn upgrade_from(weak: &Self::Weak) -> Option<Self>
215 where
216 Self: Sized;
217}
218
219/// A trait for tying together the types of a GPUI entity and the events it can
220/// emit.
221pub trait EventEmitter<E: Any>: 'static {}
222
223/// A helper trait for auto-implementing certain methods on contexts that
224/// can be used interchangeably.
225pub trait BorrowAppContext {
226 /// Set a global value on the context.
227 fn set_global<T: 'static>(&mut self, global: T);
228}
229
230impl<C> BorrowAppContext for C
231where
232 C: BorrowMut<AppContext>,
233{
234 fn set_global<G: 'static>(&mut self, global: G) {
235 self.borrow_mut().set_global(global)
236 }
237}
238
239/// A flatten equivalent for anyhow `Result`s.
240pub trait Flatten<T> {
241 /// Convert this type into a simple `Result<T>`.
242 fn flatten(self) -> Result<T>;
243}
244
245impl<T> Flatten<T> for Result<Result<T>> {
246 fn flatten(self) -> Result<T> {
247 self?
248 }
249}
250
251impl<T> Flatten<T> for Result<T> {
252 fn flatten(self) -> Result<T> {
253 self
254 }
255}