1use futures::channel::oneshot;
2use futures::{select_biased, FutureExt};
3use gpui::{App, Context, Global, Subscription, Task, Window};
4use std::cell::RefCell;
5use std::rc::Rc;
6use std::sync::LazyLock;
7use std::time::Duration;
8use std::{future::Future, pin::Pin, task::Poll};
9
10#[derive(Default)]
11struct FeatureFlags {
12 flags: Vec<String>,
13 staff: bool,
14}
15
16pub static ZED_DISABLE_STAFF: LazyLock<bool> = LazyLock::new(|| {
17 std::env::var("ZED_DISABLE_STAFF").map_or(false, |value| !value.is_empty() && value != "0")
18});
19
20impl FeatureFlags {
21 fn has_flag<T: FeatureFlag>(&self) -> bool {
22 if self.staff && T::enabled_for_staff() {
23 return true;
24 }
25
26 #[cfg(debug_assertions)]
27 if T::enabled_in_development() {
28 return true;
29 }
30
31 self.flags.iter().any(|f| f.as_str() == T::NAME)
32 }
33}
34
35impl Global for FeatureFlags {}
36
37/// To create a feature flag, implement this trait on a trivial type and use it as
38/// a generic parameter when called [`FeatureFlagAppExt::has_flag`].
39///
40/// Feature flags are enabled for members of Zed staff by default. To disable this behavior
41/// so you can test flags being disabled, set ZED_DISABLE_STAFF=1 in your environment,
42/// which will force Zed to treat the current user as non-staff.
43pub trait FeatureFlag {
44 const NAME: &'static str;
45
46 /// Returns whether this feature flag is enabled for Zed staff.
47 fn enabled_for_staff() -> bool {
48 true
49 }
50
51 fn enabled_in_development() -> bool {
52 Self::enabled_for_staff() && !*ZED_DISABLE_STAFF
53 }
54}
55
56pub struct Assistant2FeatureFlag;
57
58impl FeatureFlag for Assistant2FeatureFlag {
59 const NAME: &'static str = "assistant2";
60}
61
62pub struct PredictEditsFeatureFlag;
63impl FeatureFlag for PredictEditsFeatureFlag {
64 const NAME: &'static str = "predict-edits";
65}
66
67/// A feature flag that controls things that shouldn't go live until the predictive edits launch.
68pub struct PredictEditsLaunchFeatureFlag;
69impl FeatureFlag for PredictEditsLaunchFeatureFlag {
70 const NAME: &'static str = "predict-edits-launch";
71}
72
73pub struct PredictEditsRateCompletionsFeatureFlag;
74impl FeatureFlag for PredictEditsRateCompletionsFeatureFlag {
75 const NAME: &'static str = "predict-edits-rate-completions";
76}
77
78/// A feature flag that controls whether "non eager mode" (holding `alt` to preview) is publicized.
79pub struct PredictEditsNonEagerModeFeatureFlag;
80impl FeatureFlag for PredictEditsNonEagerModeFeatureFlag {
81 const NAME: &'static str = "predict-edits-non-eager-mode";
82
83 fn enabled_for_staff() -> bool {
84 // Don't show to staff so it doesn't leak into media for the launch.
85 false
86 }
87}
88
89pub struct GitUiFeatureFlag;
90impl FeatureFlag for GitUiFeatureFlag {
91 const NAME: &'static str = "git-ui";
92}
93
94pub struct Remoting {}
95impl FeatureFlag for Remoting {
96 const NAME: &'static str = "remoting";
97}
98
99pub struct LanguageModels {}
100impl FeatureFlag for LanguageModels {
101 const NAME: &'static str = "language-models";
102}
103
104pub struct LlmClosedBeta {}
105impl FeatureFlag for LlmClosedBeta {
106 const NAME: &'static str = "llm-closed-beta";
107}
108
109pub struct ZedPro {}
110impl FeatureFlag for ZedPro {
111 const NAME: &'static str = "zed-pro";
112}
113
114pub struct NotebookFeatureFlag;
115
116impl FeatureFlag for NotebookFeatureFlag {
117 const NAME: &'static str = "notebooks";
118}
119
120pub struct AutoCommand {}
121impl FeatureFlag for AutoCommand {
122 const NAME: &'static str = "auto-command";
123
124 fn enabled_for_staff() -> bool {
125 false
126 }
127}
128
129pub trait FeatureFlagViewExt<V: 'static> {
130 fn observe_flag<T: FeatureFlag, F>(&mut self, window: &Window, callback: F) -> Subscription
131 where
132 F: Fn(bool, &mut V, &mut Window, &mut Context<V>) + Send + Sync + 'static;
133
134 fn when_flag_enabled<T: FeatureFlag>(
135 &mut self,
136 window: &mut Window,
137 callback: impl Fn(&mut V, &mut Window, &mut Context<V>) + Send + Sync + 'static,
138 );
139}
140
141impl<V> FeatureFlagViewExt<V> for Context<'_, V>
142where
143 V: 'static,
144{
145 fn observe_flag<T: FeatureFlag, F>(&mut self, window: &Window, callback: F) -> Subscription
146 where
147 F: Fn(bool, &mut V, &mut Window, &mut Context<V>) + 'static,
148 {
149 self.observe_global_in::<FeatureFlags>(window, move |v, window, cx| {
150 let feature_flags = cx.global::<FeatureFlags>();
151 callback(feature_flags.has_flag::<T>(), v, window, cx);
152 })
153 }
154
155 fn when_flag_enabled<T: FeatureFlag>(
156 &mut self,
157 window: &mut Window,
158 callback: impl Fn(&mut V, &mut Window, &mut Context<V>) + Send + Sync + 'static,
159 ) {
160 if self
161 .try_global::<FeatureFlags>()
162 .is_some_and(|f| f.has_flag::<T>())
163 || cfg!(debug_assertions) && T::enabled_in_development()
164 {
165 self.defer_in(window, move |view, window, cx| {
166 callback(view, window, cx);
167 });
168 return;
169 }
170 let subscription = Rc::new(RefCell::new(None));
171 let inner = self.observe_global_in::<FeatureFlags>(window, {
172 let subscription = subscription.clone();
173 move |v, window, cx| {
174 let feature_flags = cx.global::<FeatureFlags>();
175 if feature_flags.has_flag::<T>() {
176 callback(v, window, cx);
177 subscription.take();
178 }
179 }
180 });
181 subscription.borrow_mut().replace(inner);
182 }
183}
184
185pub trait FeatureFlagAppExt {
186 fn wait_for_flag<T: FeatureFlag>(&mut self) -> WaitForFlag;
187
188 /// Waits for the specified feature flag to resolve, up to the given timeout.
189 fn wait_for_flag_or_timeout<T: FeatureFlag>(&mut self, timeout: Duration) -> Task<bool>;
190
191 fn update_flags(&mut self, staff: bool, flags: Vec<String>);
192 fn set_staff(&mut self, staff: bool);
193 fn has_flag<T: FeatureFlag>(&self) -> bool;
194 fn is_staff(&self) -> bool;
195
196 fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
197 where
198 F: FnMut(bool, &mut App) + 'static;
199}
200
201impl FeatureFlagAppExt for App {
202 fn update_flags(&mut self, staff: bool, flags: Vec<String>) {
203 let feature_flags = self.default_global::<FeatureFlags>();
204 feature_flags.staff = staff;
205 feature_flags.flags = flags;
206 }
207
208 fn set_staff(&mut self, staff: bool) {
209 let feature_flags = self.default_global::<FeatureFlags>();
210 feature_flags.staff = staff;
211 }
212
213 fn has_flag<T: FeatureFlag>(&self) -> bool {
214 self.try_global::<FeatureFlags>()
215 .map(|flags| flags.has_flag::<T>())
216 .unwrap_or(false)
217 }
218
219 fn is_staff(&self) -> bool {
220 self.try_global::<FeatureFlags>()
221 .map(|flags| flags.staff)
222 .unwrap_or(false)
223 }
224
225 fn observe_flag<T: FeatureFlag, F>(&mut self, mut callback: F) -> Subscription
226 where
227 F: FnMut(bool, &mut App) + 'static,
228 {
229 self.observe_global::<FeatureFlags>(move |cx| {
230 let feature_flags = cx.global::<FeatureFlags>();
231 callback(feature_flags.has_flag::<T>(), cx);
232 })
233 }
234
235 fn wait_for_flag<T: FeatureFlag>(&mut self) -> WaitForFlag {
236 let (tx, rx) = oneshot::channel::<bool>();
237 let mut tx = Some(tx);
238 let subscription: Option<Subscription>;
239
240 match self.try_global::<FeatureFlags>() {
241 Some(feature_flags) => {
242 subscription = None;
243 tx.take().unwrap().send(feature_flags.has_flag::<T>()).ok();
244 }
245 None => {
246 subscription = Some(self.observe_global::<FeatureFlags>(move |cx| {
247 let feature_flags = cx.global::<FeatureFlags>();
248 if let Some(tx) = tx.take() {
249 tx.send(feature_flags.has_flag::<T>()).ok();
250 }
251 }));
252 }
253 }
254
255 WaitForFlag(rx, subscription)
256 }
257
258 fn wait_for_flag_or_timeout<T: FeatureFlag>(&mut self, timeout: Duration) -> Task<bool> {
259 let wait_for_flag = self.wait_for_flag::<T>();
260
261 self.spawn(|_cx| async move {
262 let mut wait_for_flag = wait_for_flag.fuse();
263 let mut timeout = FutureExt::fuse(smol::Timer::after(timeout));
264
265 select_biased! {
266 is_enabled = wait_for_flag => is_enabled,
267 _ = timeout => false,
268 }
269 })
270 }
271}
272
273pub struct WaitForFlag(oneshot::Receiver<bool>, Option<Subscription>);
274
275impl Future for WaitForFlag {
276 type Output = bool;
277
278 fn poll(mut self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
279 self.0.poll_unpin(cx).map(|result| {
280 self.1.take();
281 result.unwrap_or(false)
282 })
283 }
284}