registry.rs

 1use crate::{zed_pro_family, ThemeFamily, ThemeVariant};
 2use anyhow::{anyhow, Result};
 3use gpui2::SharedString;
 4use std::{collections::HashMap, sync::Arc};
 5
 6pub struct ThemeRegistry {
 7    themes: HashMap<SharedString, Arc<ThemeVariant>>,
 8}
 9
10impl ThemeRegistry {
11    fn insert_theme_families(&mut self, families: impl IntoIterator<Item = ThemeFamily>) {
12        for family in families.into_iter() {
13            self.insert_themes(family.themes);
14        }
15    }
16
17    fn insert_themes(&mut self, themes: impl IntoIterator<Item = ThemeVariant>) {
18        for theme in themes.into_iter() {
19            self.themes.insert(theme.name.clone(), Arc::new(theme));
20        }
21    }
22
23    pub fn list_names(&self, _staff: bool) -> impl Iterator<Item = SharedString> + '_ {
24        self.themes.keys().cloned()
25    }
26
27    pub fn list(&self, _staff: bool) -> impl Iterator<Item = SharedString> + '_ {
28        self.themes.values().map(|theme| theme.name.clone())
29    }
30
31    pub fn get(&self, name: &str) -> Result<Arc<ThemeVariant>> {
32        self.themes
33            .get(name)
34            .ok_or_else(|| anyhow!("theme not found: {}", name))
35            .cloned()
36    }
37}
38
39impl Default for ThemeRegistry {
40    fn default() -> Self {
41        let mut this = Self {
42            themes: HashMap::default(),
43        };
44
45        this.insert_theme_families([zed_pro_family()]);
46
47        this
48    }
49}