feature_flags.rs

 1use gpui::{AppContext, Global, Subscription, ViewContext};
 2
 3#[derive(Default)]
 4struct FeatureFlags {
 5    flags: Vec<String>,
 6    staff: bool,
 7}
 8
 9impl FeatureFlags {
10    fn has_flag(&self, flag: &str) -> bool {
11        self.staff || self.flags.iter().any(|f| f.as_str() == flag)
12    }
13}
14
15impl Global for FeatureFlags {}
16
17/// To create a feature flag, implement this trait on a trivial type and use it as
18/// a generic parameter when called [`FeatureFlagAppExt::has_flag`].
19///
20/// Feature flags are always enabled for members of Zed staff. To disable this behavior
21/// so you can test flags being disabled, set ZED_DISABLE_STAFF=1 in your environment,
22/// which will force Zed to treat the current user as non-staff.
23pub trait FeatureFlag {
24    const NAME: &'static str;
25}
26
27pub struct Remoting {}
28impl FeatureFlag for Remoting {
29    const NAME: &'static str = "remoting";
30}
31
32pub trait FeatureFlagViewExt<V: 'static> {
33    fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
34    where
35        F: Fn(bool, &mut V, &mut ViewContext<V>) + Send + Sync + 'static;
36}
37
38impl<V> FeatureFlagViewExt<V> for ViewContext<'_, V>
39where
40    V: 'static,
41{
42    fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
43    where
44        F: Fn(bool, &mut V, &mut ViewContext<V>) + 'static,
45    {
46        self.observe_global::<FeatureFlags>(move |v, cx| {
47            let feature_flags = cx.global::<FeatureFlags>();
48            callback(feature_flags.has_flag(<T as FeatureFlag>::NAME), v, cx);
49        })
50    }
51}
52
53pub trait FeatureFlagAppExt {
54    fn update_flags(&mut self, staff: bool, flags: Vec<String>);
55    fn set_staff(&mut self, staff: bool);
56    fn has_flag<T: FeatureFlag>(&self) -> bool;
57    fn is_staff(&self) -> bool;
58}
59
60impl FeatureFlagAppExt for AppContext {
61    fn update_flags(&mut self, staff: bool, flags: Vec<String>) {
62        let feature_flags = self.default_global::<FeatureFlags>();
63        feature_flags.staff = staff;
64        feature_flags.flags = flags;
65    }
66
67    fn set_staff(&mut self, staff: bool) {
68        let feature_flags = self.default_global::<FeatureFlags>();
69        feature_flags.staff = staff;
70    }
71
72    fn has_flag<T: FeatureFlag>(&self) -> bool {
73        self.try_global::<FeatureFlags>()
74            .map(|flags| flags.has_flag(T::NAME))
75            .unwrap_or(false)
76    }
77
78    fn is_staff(&self) -> bool {
79        self.try_global::<FeatureFlags>()
80            .map(|flags| flags.staff)
81            .unwrap_or(false)
82    }
83}