registry.rs

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