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 struct TerminalInlineAssist {}
33impl FeatureFlag for TerminalInlineAssist {
34    const NAME: &'static str = "terminal-inline-assist";
35}
36
37pub struct GroupedDiagnostics {}
38impl FeatureFlag for GroupedDiagnostics {
39    const NAME: &'static str = "grouped-diagnostics";
40}
41
42pub trait FeatureFlagViewExt<V: 'static> {
43    fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
44    where
45        F: Fn(bool, &mut V, &mut ViewContext<V>) + Send + Sync + 'static;
46}
47
48impl<V> FeatureFlagViewExt<V> for ViewContext<'_, V>
49where
50    V: 'static,
51{
52    fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
53    where
54        F: Fn(bool, &mut V, &mut ViewContext<V>) + 'static,
55    {
56        self.observe_global::<FeatureFlags>(move |v, cx| {
57            let feature_flags = cx.global::<FeatureFlags>();
58            callback(feature_flags.has_flag(<T as FeatureFlag>::NAME), v, cx);
59        })
60    }
61}
62
63pub trait FeatureFlagAppExt {
64    fn update_flags(&mut self, staff: bool, flags: Vec<String>);
65    fn set_staff(&mut self, staff: bool);
66    fn has_flag<T: FeatureFlag>(&self) -> bool;
67    fn is_staff(&self) -> bool;
68}
69
70impl FeatureFlagAppExt for AppContext {
71    fn update_flags(&mut self, staff: bool, flags: Vec<String>) {
72        let feature_flags = self.default_global::<FeatureFlags>();
73        feature_flags.staff = staff;
74        feature_flags.flags = flags;
75    }
76
77    fn set_staff(&mut self, staff: bool) {
78        let feature_flags = self.default_global::<FeatureFlags>();
79        feature_flags.staff = staff;
80    }
81
82    fn has_flag<T: FeatureFlag>(&self) -> bool {
83        self.try_global::<FeatureFlags>()
84            .map(|flags| flags.has_flag(T::NAME))
85            .unwrap_or(false)
86    }
87
88    fn is_staff(&self) -> bool {
89        self.try_global::<FeatureFlags>()
90            .map(|flags| flags.staff)
91            .unwrap_or(false)
92    }
93}