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