2023-04-04 03:16:45 +00:00
|
|
|
use gpui::MutableAppContext;
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct StaffMode(pub bool);
|
|
|
|
|
|
|
|
impl std::ops::Deref for StaffMode {
|
|
|
|
type Target = bool;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 04:38:26 +00:00
|
|
|
/// Despite what the type system requires me to tell you, the init function will only be called a once
|
|
|
|
/// as soon as we know that the staff mode is enabled.
|
2023-04-04 04:45:18 +00:00
|
|
|
pub fn staff_mode<F: FnMut(&mut MutableAppContext) + 'static>(
|
2023-04-04 03:16:45 +00:00
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
mut init: F,
|
|
|
|
) {
|
2023-04-04 04:45:18 +00:00
|
|
|
if **cx.default_global::<StaffMode>() {
|
2023-04-04 03:16:45 +00:00
|
|
|
init(cx)
|
|
|
|
} else {
|
|
|
|
let mut once = Some(());
|
|
|
|
cx.observe_global::<StaffMode, _>(move |cx| {
|
|
|
|
if **cx.global::<StaffMode>() && once.take().is_some() {
|
|
|
|
init(cx);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.detach();
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 04:38:26 +00:00
|
|
|
|
|
|
|
/// Immediately checks and runs the init function if the staff mode is not enabled.
|
2023-04-04 04:45:18 +00:00
|
|
|
pub fn not_staff_mode<F: FnOnce(&mut MutableAppContext) + 'static>(
|
2023-04-04 04:38:26 +00:00
|
|
|
cx: &mut MutableAppContext,
|
|
|
|
init: F,
|
|
|
|
) {
|
2023-04-04 04:45:18 +00:00
|
|
|
if !**cx.default_global::<StaffMode>() {
|
2023-04-04 04:38:26 +00:00
|
|
|
init(cx)
|
|
|
|
}
|
|
|
|
}
|