gpui_material_foundation/
strings.rs1use std::{rc::Rc, sync::LazyLock};
2
3use gpui::{App, Global};
4use gpui_material_theme::MaterialStrings;
5
6struct ActiveMaterialStrings(Rc<MaterialStrings>);
7
8impl Global for ActiveMaterialStrings {}
9
10static DEFAULT_STRINGS: LazyLock<MaterialStrings> = LazyLock::new(MaterialStrings::default);
11
12pub(crate) fn init_default_strings(cx: &mut App) {
13 if !cx.has_global::<ActiveMaterialStrings>() {
14 cx.set_global(ActiveMaterialStrings(Rc::new(MaterialStrings::default())));
15 }
16}
17
18pub(crate) fn app_strings_handle(cx: &App) -> Rc<MaterialStrings> {
19 cx.try_global::<ActiveMaterialStrings>()
20 .map(|active| active.0.clone())
21 .unwrap_or_else(|| Rc::new(MaterialStrings::default()))
22}
23
24pub fn material_strings(cx: &App) -> &MaterialStrings {
27 cx.try_global::<ActiveMaterialStrings>()
28 .map(|active| active.0.as_ref())
29 .unwrap_or(&DEFAULT_STRINGS)
30}
31
32pub fn set_material_strings(strings: MaterialStrings, cx: &mut App) {
39 if cx
40 .try_global::<ActiveMaterialStrings>()
41 .is_some_and(|previous| *previous.0 == strings)
42 {
43 return;
44 }
45 cx.set_global(ActiveMaterialStrings(Rc::new(strings)));
46 cx.refresh_windows();
47}