Skip to main content

gpui_material_foundation/
strings.rs

1use 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
24/// Returns the application-wide [`MaterialStrings`] table, or [`MaterialStrings::default`]
25/// if no custom strings have been installed yet.
26pub 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
32/// Replaces the application-wide [`MaterialStrings`] table and refreshes open windows
33/// when the values change.
34///
35/// Subtree overrides installed with [`crate::ThemeScope::strings`] take precedence over
36/// the application global, and explicit per-component label setters always take precedence
37/// over both.
38pub 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}