staff_mode.rs

 1use gpui::MutableAppContext;
 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 MutableAppContext) + 'static>(
17    cx: &mut MutableAppContext,
18    mut init: F,
19) {
20    if **cx.default_global::<StaffMode>() {
21        init(cx)
22    } else {
23        let mut once = Some(());
24        cx.observe_global::<StaffMode, _>(move |cx| {
25            if **cx.global::<StaffMode>() && once.take().is_some() {
26                init(cx);
27            }
28        })
29        .detach();
30    }
31}
32
33/// Immediately checks and runs the init function if the staff mode is not enabled.
34/// This is only included for symettry with staff_mode() above
35pub fn not_staff_mode<F: FnOnce(&mut MutableAppContext) + 'static>(
36    cx: &mut MutableAppContext,
37    init: F,
38) {
39    if !**cx.default_global::<StaffMode>() {
40        init(cx)
41    }
42}