1mod app;
2mod assets;
3mod color;
4mod element;
5mod elements;
6mod executor;
7mod geometry;
8mod platform;
9mod scene;
10mod style;
11mod style_helpers;
12mod styled;
13mod svg_renderer;
14mod taffy;
15mod text_system;
16mod util;
17mod view;
18mod window;
19
20pub use anyhow::Result;
21pub use app::*;
22pub use assets::*;
23pub use color::*;
24pub use element::*;
25pub use elements::*;
26pub use executor::*;
27pub use geometry::*;
28pub use gpui3_macros::*;
29pub use svg_renderer::*;
30
31pub use platform::*;
32pub use refineable::*;
33pub use scene::*;
34pub use serde;
35pub use serde_json;
36pub use smallvec;
37pub use smol::Timer;
38use std::{
39 ops::{Deref, DerefMut},
40 sync::Arc,
41};
42pub use style::*;
43pub use style_helpers::*;
44pub use styled::*;
45use taffy::TaffyLayoutEngine;
46pub use taffy::{AvailableSpace, LayoutId};
47pub use text_system::*;
48pub use util::arc_cow::ArcCow;
49pub use view::*;
50pub use window::*;
51
52pub trait Context {
53 type EntityContext<'a, 'w, T: 'static + Send + Sync>;
54 type Result<T>;
55
56 fn entity<T: Send + Sync + 'static>(
57 &mut self,
58 build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
59 ) -> Self::Result<Handle<T>>;
60
61 fn update_entity<T: Send + Sync + 'static, R>(
62 &mut self,
63 handle: &Handle<T>,
64 update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
65 ) -> Self::Result<R>;
66}
67
68#[repr(transparent)]
69pub struct MainThread<T>(T);
70
71impl<T> MainThread<T> {
72 fn new(value: T) -> Self {
73 Self(value)
74 }
75}
76
77impl<T> Deref for MainThread<T> {
78 type Target = T;
79
80 fn deref(&self) -> &Self::Target {
81 &self.0
82 }
83}
84
85impl<T> DerefMut for MainThread<T> {
86 fn deref_mut(&mut self) -> &mut Self::Target {
87 &mut self.0
88 }
89}
90
91pub trait BorrowAppContext {
92 fn app_mut(&mut self) -> &mut AppContext;
93
94 fn with_text_style<F, R>(&mut self, style: TextStyleRefinement, f: F) -> R
95 where
96 F: FnOnce(&mut Self) -> R,
97 {
98 self.app_mut().push_text_style(style);
99 let result = f(self);
100 self.app_mut().pop_text_style();
101 result
102 }
103
104 fn with_state<T: Send + Sync + 'static, F, R>(&mut self, state: T, f: F) -> R
105 where
106 F: FnOnce(&mut Self) -> R,
107 {
108 self.app_mut().push_state(state);
109 let result = f(self);
110 self.app_mut().pop_state::<T>();
111 result
112 }
113}
114
115pub trait Flatten<T> {
116 fn flatten(self) -> Result<T>;
117}
118
119impl<T> Flatten<T> for Result<Result<T>> {
120 fn flatten(self) -> Result<T> {
121 self?
122 }
123}
124
125impl<T> Flatten<T> for Result<T> {
126 fn flatten(self) -> Result<T> {
127 self
128 }
129}
130
131#[derive(Clone, Eq, PartialEq, Hash)]
132pub struct SharedString(ArcCow<'static, str>);
133
134impl Default for SharedString {
135 fn default() -> Self {
136 Self(ArcCow::Owned("".into()))
137 }
138}
139
140impl AsRef<str> for SharedString {
141 fn as_ref(&self) -> &str {
142 &self.0
143 }
144}
145
146impl std::fmt::Debug for SharedString {
147 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
148 self.0.fmt(f)
149 }
150}
151
152impl std::fmt::Display for SharedString {
153 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
154 write!(f, "{}", self.0.as_ref())
155 }
156}
157
158impl<T: Into<ArcCow<'static, str>>> From<T> for SharedString {
159 fn from(value: T) -> Self {
160 Self(value.into())
161 }
162}
163
164pub enum Reference<'a, T> {
165 Immutable(&'a T),
166 Mutable(&'a mut T),
167}
168
169impl<'a, T> Deref for Reference<'a, T> {
170 type Target = T;
171
172 fn deref(&self) -> &Self::Target {
173 match self {
174 Reference::Immutable(target) => target,
175 Reference::Mutable(target) => target,
176 }
177 }
178}
179
180impl<'a, T> DerefMut for Reference<'a, T> {
181 fn deref_mut(&mut self) -> &mut Self::Target {
182 match self {
183 Reference::Immutable(_) => {
184 panic!("cannot mutably deref an immutable reference. this is a bug in GPUI.");
185 }
186 Reference::Mutable(target) => target,
187 }
188 }
189}
190
191pub(crate) struct MainThreadOnly<T: ?Sized> {
192 dispatcher: Arc<dyn PlatformDispatcher>,
193 value: Arc<T>,
194}
195
196impl<T: ?Sized> Clone for MainThreadOnly<T> {
197 fn clone(&self) -> Self {
198 Self {
199 dispatcher: self.dispatcher.clone(),
200 value: self.value.clone(),
201 }
202 }
203}
204
205/// Allows a value to be accessed only on the main thread, allowing a non-`Send` type
206/// to become `Send`.
207impl<T: 'static + ?Sized> MainThreadOnly<T> {
208 pub(crate) fn new(value: Arc<T>, dispatcher: Arc<dyn PlatformDispatcher>) -> Self {
209 Self { dispatcher, value }
210 }
211
212 pub(crate) fn borrow_on_main_thread(&self) -> &T {
213 assert!(self.dispatcher.is_main_thread());
214 &self.value
215 }
216}
217
218unsafe impl<T: ?Sized> Send for MainThreadOnly<T> {}