use gpui::{AppContext, Subscription, ViewContext}; #[derive(Default)] struct FeatureFlags { flags: Vec, staff: bool, } impl FeatureFlags { fn has_flag(&self, flag: &str) -> bool { self.staff || self.flags.iter().find(|f| f.as_str() == flag).is_some() } } pub trait FeatureFlag { const NAME: &'static str; } pub enum ChannelsAlpha {} impl FeatureFlag for ChannelsAlpha { const NAME: &'static str = "channels_alpha"; } pub trait FeatureFlagViewExt { fn observe_flag(&mut self, callback: F) -> Subscription where F: Fn(bool, &mut V, &mut ViewContext) + 'static; } impl FeatureFlagViewExt for ViewContext<'_, '_, V> { fn observe_flag(&mut self, callback: F) -> Subscription where F: Fn(bool, &mut V, &mut ViewContext) + 'static, { self.observe_global::(move |v, cx| { let feature_flags = cx.global::(); callback(feature_flags.has_flag(::NAME), v, cx); }) } } pub trait FeatureFlagAppExt { fn update_flags(&mut self, staff: bool, flags: Vec); fn set_staff(&mut self, staff: bool); fn has_flag(&self) -> bool; fn is_staff(&self) -> bool; } impl FeatureFlagAppExt for AppContext { fn update_flags(&mut self, staff: bool, flags: Vec) { self.update_default_global::(|feature_flags, _| { feature_flags.staff = staff; feature_flags.flags = flags; }) } fn set_staff(&mut self, staff: bool) { self.update_default_global::(|feature_flags, _| { feature_flags.staff = staff; }) } fn has_flag(&self) -> bool { if self.has_global::() { self.global::().has_flag(T::NAME) } else { false } } fn is_staff(&self) -> bool { if self.has_global::() { return self.global::().staff; } else { false } } }