1use gpui::AppContext;
2
3#[derive(Debug, Default)]
4pub struct StaffMode(pub bool);
5
6impl std::ops::Deref for StaffMode {
7 type Target = bool;
8
9 fn deref(&self) -> &Self::Target {
10 &self.0
11 }
12}
13
14/// Despite what the type system requires me to tell you, the init function will only be called a once
15/// as soon as we know that the staff mode is enabled.
16pub fn staff_mode<F: FnMut(&mut AppContext) + 'static>(cx: &mut AppContext, mut init: F) {
17 if **cx.default_global::<StaffMode>() {
18 init(cx)
19 } else {
20 let mut once = Some(());
21 cx.observe_global::<StaffMode, _>(move |cx| {
22 if **cx.global::<StaffMode>() && once.take().is_some() {
23 init(cx);
24 }
25 })
26 .detach();
27 }
28}
29
30/// Immediately checks and runs the init function if the staff mode is not enabled.
31/// This is only included for symettry with staff_mode() above
32pub fn not_staff_mode<F: FnOnce(&mut AppContext) + 'static>(cx: &mut AppContext, init: F) {
33 if !**cx.default_global::<StaffMode>() {
34 init(cx)
35 }
36}