Skip to main content

gpui_material_theme/
strings.rs

1use std::sync::{Arc, LazyLock};
2
3/// Pure-value table of built-in default labels and accessibility copy.
4///
5/// Uses shared [`Arc<str>`] storage (matching [`crate::FontFamilies`]) so cloning
6/// and scoping strings across subtrees does not allocate on the heap.
7///
8/// All fields default to the library's English labels so existing applications
9/// behave unchanged until they install custom strings via
10/// `set_material_strings` or `ThemeScope::strings`. Explicit per-component label
11/// setters always take precedence over `MaterialStrings`.
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct MaterialStrings {
14    /// Generic dismiss label (defaults to `"Dismiss"`, e.g. `Banner`).
15    pub dismiss: Arc<str>,
16    /// Accessible label for dismissing a notification message (defaults to `"Dismiss message"`, e.g. `Snackbar`).
17    pub dismiss_message: Arc<str>,
18    /// Accessible label for clicking a modal backdrop scrim (defaults to `"Dismiss overlay"`, e.g. `Scrim`).
19    pub dismiss_overlay: Arc<str>,
20    /// Generic close label (defaults to `"Close"`, e.g. `Sheet`, `CaptionButtons`).
21    pub close: Arc<str>,
22    /// Accessible label for closing floating action menus (defaults to `"Close actions"`, e.g. `FabMenu`).
23    pub close_actions: Arc<str>,
24    /// Accessible label for closing search views (defaults to `"Close search"`, e.g. `SearchView`).
25    pub close_search: Arc<str>,
26    /// Generic cancellation label (defaults to `"Cancel"`, e.g. `Dialog`).
27    pub cancel: Arc<str>,
28    /// Generic confirmation label (defaults to `"OK"`, e.g. `DatePickerDialog`, `TimePickerDialog`).
29    pub confirm: Arc<str>,
30    /// Accessible label for clearing a text input (defaults to `"Clear"`, e.g. `TextField`).
31    pub clear: Arc<str>,
32    /// Accessible label for revealing masked password text (defaults to `"Show password"`, e.g. `TextField`).
33    pub show_password: Arc<str>,
34    /// Accessible label for hiding revealed password text (defaults to `"Hide password"`, e.g. `TextField`).
35    pub hide_password: Arc<str>,
36    /// Accessible label for opening dropdown options (defaults to `"Open options"`, e.g. `ExposedDropdown`).
37    pub open_options: Arc<str>,
38    /// Accessible label for closing dropdown options (defaults to `"Close options"`, e.g. `ExposedDropdown`).
39    pub close_options: Arc<str>,
40    /// Empty-state message when a filterable dropdown has no matches (defaults to `"No matching options"`, e.g. `ExposedDropdown`).
41    pub no_matching_options: Arc<str>,
42    /// Accessible label for overflow menu triggers (defaults to `"More actions"`, e.g. `AppBarRow`, `AppBarColumn`, `ButtonGroup`, `Toolbar`).
43    pub more_actions: Arc<str>,
44    /// Accessible label for app bar action strips (defaults to `"App bar actions"`, e.g. `AppBarRow`, `AppBarColumn`).
45    pub app_bar_actions: Arc<str>,
46    /// Accessible label for bottom app bar landmarks (defaults to `"Bottom app bar"`, e.g. `BottomAppBar`).
47    pub bottom_app_bar: Arc<str>,
48    /// Accessible label for breadcrumb navigation landmarks (defaults to `"Breadcrumb"`, e.g. `Breadcrumbs`).
49    pub breadcrumb: Arc<str>,
50    /// Accessible label for collapsed breadcrumb overflow triggers (defaults to `"More breadcrumbs"`, e.g. `Breadcrumbs`).
51    pub more_breadcrumbs: Arc<str>,
52    /// Default navigation landmark and modal sheet title (defaults to `"Navigation"`, e.g. `NavigationRail`).
53    pub navigation: Arc<str>,
54    /// Accessible label for stepper containers (defaults to `"Steps"`, e.g. `Stepper`).
55    pub steps: Arc<str>,
56    /// Label for refresh actions and loading indicators (defaults to `"Refresh"`, e.g. `PullToRefresh`).
57    pub refresh: Arc<str>,
58    /// Accessible label for bottom sheet resize drag handles (defaults to `"Resize bottom sheet"`, e.g. `Sheet`).
59    pub resize_bottom_sheet: Arc<str>,
60    /// Label for expanding a collapsible bottom sheet (defaults to `"Expand"`, e.g. `Sheet`).
61    pub expand: Arc<str>,
62    /// Label for collapsing an expanded bottom sheet (defaults to `"Collapse"`, e.g. `Sheet`).
63    pub collapse: Arc<str>,
64    /// Default accessible label for drop targets (defaults to `"Drop target"`, e.g. `DropTarget`).
65    pub drop_target: Arc<str>,
66    /// Default prompt heading for drop targets (defaults to `"Drop files here"`, e.g. `DropTarget`).
67    pub drop_files_here: Arc<str>,
68    /// Default loading status label for search views (defaults to `"Searching"`, e.g. `SearchView`).
69    pub searching: Arc<str>,
70    /// Default empty-result label for search views (defaults to `"No results"`, e.g. `SearchView`).
71    pub no_results: Arc<str>,
72    /// Default idle prompt for search views (defaults to `"Enter a search term"`, e.g. `SearchView`).
73    pub search_prompt: Arc<str>,
74    /// Accessible label for window minimize caption buttons (defaults to `"Minimize"`, e.g. `CaptionButtons`).
75    pub minimize: Arc<str>,
76    /// Accessible label for window maximize caption buttons (defaults to `"Maximize"`, e.g. `CaptionButtons`).
77    pub maximize: Arc<str>,
78    /// Accessible label for window restore caption buttons (defaults to `"Restore"`, e.g. `CaptionButtons`).
79    pub restore: Arc<str>,
80}
81
82impl MaterialStrings {
83    /// Sets [`Self::dismiss`].
84    pub fn dismiss(mut self, value: impl Into<Arc<str>>) -> Self {
85        self.dismiss = value.into();
86        self
87    }
88
89    /// Sets [`Self::dismiss_message`].
90    pub fn dismiss_message(mut self, value: impl Into<Arc<str>>) -> Self {
91        self.dismiss_message = value.into();
92        self
93    }
94
95    /// Sets [`Self::dismiss_overlay`].
96    pub fn dismiss_overlay(mut self, value: impl Into<Arc<str>>) -> Self {
97        self.dismiss_overlay = value.into();
98        self
99    }
100
101    /// Sets [`Self::close`].
102    pub fn close(mut self, value: impl Into<Arc<str>>) -> Self {
103        self.close = value.into();
104        self
105    }
106
107    /// Sets [`Self::close_actions`].
108    pub fn close_actions(mut self, value: impl Into<Arc<str>>) -> Self {
109        self.close_actions = value.into();
110        self
111    }
112
113    /// Sets [`Self::close_search`].
114    pub fn close_search(mut self, value: impl Into<Arc<str>>) -> Self {
115        self.close_search = value.into();
116        self
117    }
118
119    /// Sets [`Self::cancel`].
120    pub fn cancel(mut self, value: impl Into<Arc<str>>) -> Self {
121        self.cancel = value.into();
122        self
123    }
124
125    /// Sets [`Self::confirm`].
126    pub fn confirm(mut self, value: impl Into<Arc<str>>) -> Self {
127        self.confirm = value.into();
128        self
129    }
130
131    /// Sets [`Self::clear`].
132    pub fn clear(mut self, value: impl Into<Arc<str>>) -> Self {
133        self.clear = value.into();
134        self
135    }
136
137    /// Sets [`Self::show_password`].
138    pub fn show_password(mut self, value: impl Into<Arc<str>>) -> Self {
139        self.show_password = value.into();
140        self
141    }
142
143    /// Sets [`Self::hide_password`].
144    pub fn hide_password(mut self, value: impl Into<Arc<str>>) -> Self {
145        self.hide_password = value.into();
146        self
147    }
148
149    /// Sets [`Self::open_options`].
150    pub fn open_options(mut self, value: impl Into<Arc<str>>) -> Self {
151        self.open_options = value.into();
152        self
153    }
154
155    /// Sets [`Self::close_options`].
156    pub fn close_options(mut self, value: impl Into<Arc<str>>) -> Self {
157        self.close_options = value.into();
158        self
159    }
160
161    /// Sets [`Self::no_matching_options`].
162    pub fn no_matching_options(mut self, value: impl Into<Arc<str>>) -> Self {
163        self.no_matching_options = value.into();
164        self
165    }
166
167    /// Sets [`Self::more_actions`].
168    pub fn more_actions(mut self, value: impl Into<Arc<str>>) -> Self {
169        self.more_actions = value.into();
170        self
171    }
172
173    /// Sets [`Self::app_bar_actions`].
174    pub fn app_bar_actions(mut self, value: impl Into<Arc<str>>) -> Self {
175        self.app_bar_actions = value.into();
176        self
177    }
178
179    /// Sets [`Self::bottom_app_bar`].
180    pub fn bottom_app_bar(mut self, value: impl Into<Arc<str>>) -> Self {
181        self.bottom_app_bar = value.into();
182        self
183    }
184
185    /// Sets [`Self::breadcrumb`].
186    pub fn breadcrumb(mut self, value: impl Into<Arc<str>>) -> Self {
187        self.breadcrumb = value.into();
188        self
189    }
190
191    /// Sets [`Self::more_breadcrumbs`].
192    pub fn more_breadcrumbs(mut self, value: impl Into<Arc<str>>) -> Self {
193        self.more_breadcrumbs = value.into();
194        self
195    }
196
197    /// Sets [`Self::navigation`].
198    pub fn navigation(mut self, value: impl Into<Arc<str>>) -> Self {
199        self.navigation = value.into();
200        self
201    }
202
203    /// Sets [`Self::steps`].
204    pub fn steps(mut self, value: impl Into<Arc<str>>) -> Self {
205        self.steps = value.into();
206        self
207    }
208
209    /// Sets [`Self::refresh`].
210    pub fn refresh(mut self, value: impl Into<Arc<str>>) -> Self {
211        self.refresh = value.into();
212        self
213    }
214
215    /// Sets [`Self::resize_bottom_sheet`].
216    pub fn resize_bottom_sheet(mut self, value: impl Into<Arc<str>>) -> Self {
217        self.resize_bottom_sheet = value.into();
218        self
219    }
220
221    /// Sets [`Self::expand`].
222    pub fn expand(mut self, value: impl Into<Arc<str>>) -> Self {
223        self.expand = value.into();
224        self
225    }
226
227    /// Sets [`Self::collapse`].
228    pub fn collapse(mut self, value: impl Into<Arc<str>>) -> Self {
229        self.collapse = value.into();
230        self
231    }
232
233    /// Sets [`Self::drop_target`].
234    pub fn drop_target(mut self, value: impl Into<Arc<str>>) -> Self {
235        self.drop_target = value.into();
236        self
237    }
238
239    /// Sets [`Self::drop_files_here`].
240    pub fn drop_files_here(mut self, value: impl Into<Arc<str>>) -> Self {
241        self.drop_files_here = value.into();
242        self
243    }
244
245    /// Sets [`Self::searching`].
246    pub fn searching(mut self, value: impl Into<Arc<str>>) -> Self {
247        self.searching = value.into();
248        self
249    }
250
251    /// Sets [`Self::no_results`].
252    pub fn no_results(mut self, value: impl Into<Arc<str>>) -> Self {
253        self.no_results = value.into();
254        self
255    }
256
257    /// Sets [`Self::search_prompt`].
258    pub fn search_prompt(mut self, value: impl Into<Arc<str>>) -> Self {
259        self.search_prompt = value.into();
260        self
261    }
262
263    /// Sets [`Self::minimize`].
264    pub fn minimize(mut self, value: impl Into<Arc<str>>) -> Self {
265        self.minimize = value.into();
266        self
267    }
268
269    /// Sets [`Self::maximize`].
270    pub fn maximize(mut self, value: impl Into<Arc<str>>) -> Self {
271        self.maximize = value.into();
272        self
273    }
274
275    /// Sets [`Self::restore`].
276    pub fn restore(mut self, value: impl Into<Arc<str>>) -> Self {
277        self.restore = value.into();
278        self
279    }
280}
281
282impl Default for MaterialStrings {
283    fn default() -> Self {
284        static DEFAULT: LazyLock<MaterialStrings> = LazyLock::new(|| MaterialStrings {
285            dismiss: Arc::from("Dismiss"),
286            dismiss_message: Arc::from("Dismiss message"),
287            dismiss_overlay: Arc::from("Dismiss overlay"),
288            close: Arc::from("Close"),
289            close_actions: Arc::from("Close actions"),
290            close_search: Arc::from("Close search"),
291            cancel: Arc::from("Cancel"),
292            confirm: Arc::from("OK"),
293            clear: Arc::from("Clear"),
294            show_password: Arc::from("Show password"),
295            hide_password: Arc::from("Hide password"),
296            open_options: Arc::from("Open options"),
297            close_options: Arc::from("Close options"),
298            no_matching_options: Arc::from("No matching options"),
299            more_actions: Arc::from("More actions"),
300            app_bar_actions: Arc::from("App bar actions"),
301            bottom_app_bar: Arc::from("Bottom app bar"),
302            breadcrumb: Arc::from("Breadcrumb"),
303            more_breadcrumbs: Arc::from("More breadcrumbs"),
304            navigation: Arc::from("Navigation"),
305            steps: Arc::from("Steps"),
306            refresh: Arc::from("Refresh"),
307            resize_bottom_sheet: Arc::from("Resize bottom sheet"),
308            expand: Arc::from("Expand"),
309            collapse: Arc::from("Collapse"),
310            drop_target: Arc::from("Drop target"),
311            drop_files_here: Arc::from("Drop files here"),
312            searching: Arc::from("Searching"),
313            no_results: Arc::from("No results"),
314            search_prompt: Arc::from("Enter a search term"),
315            minimize: Arc::from("Minimize"),
316            maximize: Arc::from("Maximize"),
317            restore: Arc::from("Restore"),
318        });
319        DEFAULT.clone()
320    }
321}
322
323#[cfg(feature = "serde")]
324impl serde::Serialize for MaterialStrings {
325    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
326        #[derive(serde::Serialize)]
327        #[serde(rename = "MaterialStrings")]
328        struct SerializedMaterialStrings<'a> {
329            dismiss: &'a str,
330            dismiss_message: &'a str,
331            dismiss_overlay: &'a str,
332            close: &'a str,
333            close_actions: &'a str,
334            close_search: &'a str,
335            cancel: &'a str,
336            confirm: &'a str,
337            clear: &'a str,
338            show_password: &'a str,
339            hide_password: &'a str,
340            open_options: &'a str,
341            close_options: &'a str,
342            no_matching_options: &'a str,
343            more_actions: &'a str,
344            app_bar_actions: &'a str,
345            bottom_app_bar: &'a str,
346            breadcrumb: &'a str,
347            more_breadcrumbs: &'a str,
348            navigation: &'a str,
349            steps: &'a str,
350            refresh: &'a str,
351            resize_bottom_sheet: &'a str,
352            expand: &'a str,
353            collapse: &'a str,
354            drop_target: &'a str,
355            drop_files_here: &'a str,
356            searching: &'a str,
357            no_results: &'a str,
358            search_prompt: &'a str,
359            minimize: &'a str,
360            maximize: &'a str,
361            restore: &'a str,
362        }
363
364        SerializedMaterialStrings {
365            dismiss: self.dismiss.as_ref(),
366            dismiss_message: self.dismiss_message.as_ref(),
367            dismiss_overlay: self.dismiss_overlay.as_ref(),
368            close: self.close.as_ref(),
369            close_actions: self.close_actions.as_ref(),
370            close_search: self.close_search.as_ref(),
371            cancel: self.cancel.as_ref(),
372            confirm: self.confirm.as_ref(),
373            clear: self.clear.as_ref(),
374            show_password: self.show_password.as_ref(),
375            hide_password: self.hide_password.as_ref(),
376            open_options: self.open_options.as_ref(),
377            close_options: self.close_options.as_ref(),
378            no_matching_options: self.no_matching_options.as_ref(),
379            more_actions: self.more_actions.as_ref(),
380            app_bar_actions: self.app_bar_actions.as_ref(),
381            bottom_app_bar: self.bottom_app_bar.as_ref(),
382            breadcrumb: self.breadcrumb.as_ref(),
383            more_breadcrumbs: self.more_breadcrumbs.as_ref(),
384            navigation: self.navigation.as_ref(),
385            steps: self.steps.as_ref(),
386            refresh: self.refresh.as_ref(),
387            resize_bottom_sheet: self.resize_bottom_sheet.as_ref(),
388            expand: self.expand.as_ref(),
389            collapse: self.collapse.as_ref(),
390            drop_target: self.drop_target.as_ref(),
391            drop_files_here: self.drop_files_here.as_ref(),
392            searching: self.searching.as_ref(),
393            no_results: self.no_results.as_ref(),
394            search_prompt: self.search_prompt.as_ref(),
395            minimize: self.minimize.as_ref(),
396            maximize: self.maximize.as_ref(),
397            restore: self.restore.as_ref(),
398        }
399        .serialize(serializer)
400    }
401}
402
403#[cfg(feature = "serde")]
404impl<'de> serde::Deserialize<'de> for MaterialStrings {
405    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
406        #[derive(Default, serde::Deserialize)]
407        #[serde(rename = "MaterialStrings", default)]
408        struct SerializedMaterialStrings {
409            dismiss: Option<String>,
410            dismiss_message: Option<String>,
411            dismiss_overlay: Option<String>,
412            close: Option<String>,
413            close_actions: Option<String>,
414            close_search: Option<String>,
415            cancel: Option<String>,
416            confirm: Option<String>,
417            clear: Option<String>,
418            show_password: Option<String>,
419            hide_password: Option<String>,
420            open_options: Option<String>,
421            close_options: Option<String>,
422            no_matching_options: Option<String>,
423            more_actions: Option<String>,
424            app_bar_actions: Option<String>,
425            bottom_app_bar: Option<String>,
426            breadcrumb: Option<String>,
427            more_breadcrumbs: Option<String>,
428            navigation: Option<String>,
429            steps: Option<String>,
430            refresh: Option<String>,
431            resize_bottom_sheet: Option<String>,
432            expand: Option<String>,
433            collapse: Option<String>,
434            drop_target: Option<String>,
435            drop_files_here: Option<String>,
436            searching: Option<String>,
437            no_results: Option<String>,
438            search_prompt: Option<String>,
439            minimize: Option<String>,
440            maximize: Option<String>,
441            restore: Option<String>,
442        }
443
444        let raw = SerializedMaterialStrings::deserialize(deserializer)?;
445        let defaults = Self::default();
446        Ok(Self {
447            dismiss: raw.dismiss.map(Into::into).unwrap_or(defaults.dismiss),
448            dismiss_message: raw
449                .dismiss_message
450                .map(Into::into)
451                .unwrap_or(defaults.dismiss_message),
452            dismiss_overlay: raw
453                .dismiss_overlay
454                .map(Into::into)
455                .unwrap_or(defaults.dismiss_overlay),
456            close: raw.close.map(Into::into).unwrap_or(defaults.close),
457            close_actions: raw
458                .close_actions
459                .map(Into::into)
460                .unwrap_or(defaults.close_actions),
461            close_search: raw
462                .close_search
463                .map(Into::into)
464                .unwrap_or(defaults.close_search),
465            cancel: raw.cancel.map(Into::into).unwrap_or(defaults.cancel),
466            confirm: raw.confirm.map(Into::into).unwrap_or(defaults.confirm),
467            clear: raw.clear.map(Into::into).unwrap_or(defaults.clear),
468            show_password: raw
469                .show_password
470                .map(Into::into)
471                .unwrap_or(defaults.show_password),
472            hide_password: raw
473                .hide_password
474                .map(Into::into)
475                .unwrap_or(defaults.hide_password),
476            open_options: raw
477                .open_options
478                .map(Into::into)
479                .unwrap_or(defaults.open_options),
480            close_options: raw
481                .close_options
482                .map(Into::into)
483                .unwrap_or(defaults.close_options),
484            no_matching_options: raw
485                .no_matching_options
486                .map(Into::into)
487                .unwrap_or(defaults.no_matching_options),
488            more_actions: raw
489                .more_actions
490                .map(Into::into)
491                .unwrap_or(defaults.more_actions),
492            app_bar_actions: raw
493                .app_bar_actions
494                .map(Into::into)
495                .unwrap_or(defaults.app_bar_actions),
496            bottom_app_bar: raw
497                .bottom_app_bar
498                .map(Into::into)
499                .unwrap_or(defaults.bottom_app_bar),
500            breadcrumb: raw
501                .breadcrumb
502                .map(Into::into)
503                .unwrap_or(defaults.breadcrumb),
504            more_breadcrumbs: raw
505                .more_breadcrumbs
506                .map(Into::into)
507                .unwrap_or(defaults.more_breadcrumbs),
508            navigation: raw
509                .navigation
510                .map(Into::into)
511                .unwrap_or(defaults.navigation),
512            steps: raw.steps.map(Into::into).unwrap_or(defaults.steps),
513            refresh: raw.refresh.map(Into::into).unwrap_or(defaults.refresh),
514            resize_bottom_sheet: raw
515                .resize_bottom_sheet
516                .map(Into::into)
517                .unwrap_or(defaults.resize_bottom_sheet),
518            expand: raw.expand.map(Into::into).unwrap_or(defaults.expand),
519            collapse: raw.collapse.map(Into::into).unwrap_or(defaults.collapse),
520            drop_target: raw
521                .drop_target
522                .map(Into::into)
523                .unwrap_or(defaults.drop_target),
524            drop_files_here: raw
525                .drop_files_here
526                .map(Into::into)
527                .unwrap_or(defaults.drop_files_here),
528            searching: raw.searching.map(Into::into).unwrap_or(defaults.searching),
529            no_results: raw
530                .no_results
531                .map(Into::into)
532                .unwrap_or(defaults.no_results),
533            search_prompt: raw
534                .search_prompt
535                .map(Into::into)
536                .unwrap_or(defaults.search_prompt),
537            minimize: raw.minimize.map(Into::into).unwrap_or(defaults.minimize),
538            maximize: raw.maximize.map(Into::into).unwrap_or(defaults.maximize),
539            restore: raw.restore.map(Into::into).unwrap_or(defaults.restore),
540        })
541    }
542}