zed/crates/staff_mode/src/staff_mode.rs
2023-04-06 15:54:44 -06:00

36 lines
1 KiB
Rust

use gpui::AppContext;
#[derive(Debug, Default)]
pub struct StaffMode(pub bool);
impl std::ops::Deref for StaffMode {
type Target = bool;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// 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.
pub fn staff_mode<F: FnMut(&mut AppContext) + 'static>(cx: &mut AppContext, mut init: F) {
if **cx.default_global::<StaffMode>() {
init(cx)
} else {
let mut once = Some(());
cx.observe_global::<StaffMode, _>(move |cx| {
if **cx.global::<StaffMode>() && once.take().is_some() {
init(cx);
}
})
.detach();
}
}
/// Immediately checks and runs the init function if the staff mode is not enabled.
/// This is only included for symettry with staff_mode() above
pub fn not_staff_mode<F: FnOnce(&mut AppContext) + 'static>(cx: &mut AppContext, init: F) {
if !**cx.default_global::<StaffMode>() {
init(cx)
}
}