feature_flags.rs

  1mod flags;
  2
  3use std::cell::RefCell;
  4use std::rc::Rc;
  5use std::sync::LazyLock;
  6
  7use gpui::{App, Context, Global, Subscription, Window};
  8
  9pub use flags::*;
 10
 11#[derive(Default)]
 12struct FeatureFlags {
 13    flags: Vec<String>,
 14    staff: bool,
 15}
 16
 17pub static ZED_DISABLE_STAFF: LazyLock<bool> = LazyLock::new(|| {
 18    std::env::var("ZED_DISABLE_STAFF").is_ok_and(|value| !value.is_empty() && value != "0")
 19});
 20
 21impl FeatureFlags {
 22    fn has_flag<T: FeatureFlag>(&self) -> bool {
 23        if T::enabled_for_all() {
 24            return true;
 25        }
 26
 27        if (cfg!(debug_assertions) || self.staff) && !*ZED_DISABLE_STAFF && T::enabled_for_staff() {
 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    /// Returns whether this feature flag is enabled for everyone.
 52    ///
 53    /// This is generally done on the server, but we provide this as a way to entirely enable a feature flag client-side
 54    /// without needing to remove all of the call sites.
 55    fn enabled_for_all() -> bool {
 56        false
 57    }
 58}
 59
 60pub trait FeatureFlagViewExt<V: 'static> {
 61    fn observe_flag<T: FeatureFlag, F>(&mut self, window: &Window, callback: F) -> Subscription
 62    where
 63        F: Fn(bool, &mut V, &mut Window, &mut Context<V>) + Send + Sync + 'static;
 64
 65    fn when_flag_enabled<T: FeatureFlag>(
 66        &mut self,
 67        window: &mut Window,
 68        callback: impl Fn(&mut V, &mut Window, &mut Context<V>) + Send + Sync + 'static,
 69    );
 70}
 71
 72impl<V> FeatureFlagViewExt<V> for Context<'_, V>
 73where
 74    V: 'static,
 75{
 76    fn observe_flag<T: FeatureFlag, F>(&mut self, window: &Window, callback: F) -> Subscription
 77    where
 78        F: Fn(bool, &mut V, &mut Window, &mut Context<V>) + 'static,
 79    {
 80        self.observe_global_in::<FeatureFlags>(window, move |v, window, cx| {
 81            let feature_flags = cx.global::<FeatureFlags>();
 82            callback(feature_flags.has_flag::<T>(), v, window, cx);
 83        })
 84    }
 85
 86    fn when_flag_enabled<T: FeatureFlag>(
 87        &mut self,
 88        window: &mut Window,
 89        callback: impl Fn(&mut V, &mut Window, &mut Context<V>) + Send + Sync + 'static,
 90    ) {
 91        if self
 92            .try_global::<FeatureFlags>()
 93            .is_some_and(|f| f.has_flag::<T>())
 94        {
 95            self.defer_in(window, move |view, window, cx| {
 96                callback(view, window, cx);
 97            });
 98            return;
 99        }
100        let subscription = Rc::new(RefCell::new(None));
101        let inner = self.observe_global_in::<FeatureFlags>(window, {
102            let subscription = subscription.clone();
103            move |v, window, cx| {
104                let feature_flags = cx.global::<FeatureFlags>();
105                if feature_flags.has_flag::<T>() {
106                    callback(v, window, cx);
107                    subscription.take();
108                }
109            }
110        });
111        subscription.borrow_mut().replace(inner);
112    }
113}
114
115#[derive(Debug)]
116pub struct OnFlagsReady {
117    pub is_staff: bool,
118}
119
120pub trait FeatureFlagAppExt {
121    fn update_flags(&mut self, staff: bool, flags: Vec<String>);
122    fn set_staff(&mut self, staff: bool);
123    fn has_flag<T: FeatureFlag>(&self) -> bool;
124    fn is_staff(&self) -> bool;
125
126    fn on_flags_ready<F>(&mut self, callback: F) -> Subscription
127    where
128        F: FnMut(OnFlagsReady, &mut App) + 'static;
129
130    fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
131    where
132        F: FnMut(bool, &mut App) + 'static;
133}
134
135impl FeatureFlagAppExt for App {
136    fn update_flags(&mut self, staff: bool, flags: Vec<String>) {
137        let feature_flags = self.default_global::<FeatureFlags>();
138        feature_flags.staff = staff;
139        feature_flags.flags = flags;
140    }
141
142    fn set_staff(&mut self, staff: bool) {
143        let feature_flags = self.default_global::<FeatureFlags>();
144        feature_flags.staff = staff;
145    }
146
147    fn has_flag<T: FeatureFlag>(&self) -> bool {
148        self.try_global::<FeatureFlags>()
149            .map(|flags| flags.has_flag::<T>())
150            .unwrap_or_else(|| {
151                (cfg!(debug_assertions) && T::enabled_for_staff() && !*ZED_DISABLE_STAFF)
152                    || T::enabled_for_all()
153            })
154    }
155
156    fn is_staff(&self) -> bool {
157        self.try_global::<FeatureFlags>()
158            .map(|flags| flags.staff)
159            .unwrap_or(false)
160    }
161
162    fn on_flags_ready<F>(&mut self, mut callback: F) -> Subscription
163    where
164        F: FnMut(OnFlagsReady, &mut App) + 'static,
165    {
166        self.observe_global::<FeatureFlags>(move |cx| {
167            let feature_flags = cx.global::<FeatureFlags>();
168            callback(
169                OnFlagsReady {
170                    is_staff: feature_flags.staff,
171                },
172                cx,
173            );
174        })
175    }
176
177    fn observe_flag<T: FeatureFlag, F>(&mut self, mut callback: F) -> Subscription
178    where
179        F: FnMut(bool, &mut App) + 'static,
180    {
181        self.observe_global::<FeatureFlags>(move |cx| {
182            let feature_flags = cx.global::<FeatureFlags>();
183            callback(feature_flags.has_flag::<T>(), cx);
184        })
185    }
186}