theme_extension.rs

 1use std::path::PathBuf;
 2use std::sync::Arc;
 3
 4use anyhow::Result;
 5use extension::{ExtensionHostProxy, ExtensionThemeProxy};
 6use fs::Fs;
 7use gpui::{App, BackgroundExecutor, SharedString, Task};
 8use theme::{ThemeRegistry, deserialize_icon_theme};
 9use theme_settings;
10
11pub fn init(
12    extension_host_proxy: Arc<ExtensionHostProxy>,
13    theme_registry: Arc<ThemeRegistry>,
14    executor: BackgroundExecutor,
15) {
16    extension_host_proxy.register_theme_proxy(ThemeRegistryProxy {
17        theme_registry,
18        executor,
19    });
20}
21
22struct ThemeRegistryProxy {
23    theme_registry: Arc<ThemeRegistry>,
24    executor: BackgroundExecutor,
25}
26
27impl ExtensionThemeProxy for ThemeRegistryProxy {
28    fn set_extensions_loaded(&self) {
29        self.theme_registry.set_extensions_loaded();
30    }
31
32    fn list_theme_names(&self, theme_path: PathBuf, fs: Arc<dyn Fs>) -> Task<Result<Vec<String>>> {
33        self.executor.spawn(async move {
34            let themes =
35                theme_settings::deserialize_user_theme(&fs.load_bytes(&theme_path).await?)?;
36            Ok(themes.themes.into_iter().map(|theme| theme.name).collect())
37        })
38    }
39
40    fn remove_user_themes(&self, themes: Vec<SharedString>) {
41        self.theme_registry.remove_user_themes(&themes);
42    }
43
44    fn load_user_theme(&self, theme_path: PathBuf, fs: Arc<dyn Fs>) -> Task<Result<()>> {
45        let theme_registry = self.theme_registry.clone();
46        self.executor.spawn(async move {
47            theme_settings::load_user_theme(&theme_registry, &fs.load_bytes(&theme_path).await?)
48        })
49    }
50
51    fn reload_current_theme(&self, cx: &mut App) {
52        theme_settings::reload_theme(cx)
53    }
54
55    fn list_icon_theme_names(
56        &self,
57        icon_theme_path: PathBuf,
58        fs: Arc<dyn Fs>,
59    ) -> Task<Result<Vec<String>>> {
60        self.executor.spawn(async move {
61            let icon_theme_family =
62                theme::deserialize_icon_theme(&fs.load_bytes(&icon_theme_path).await?)?;
63            Ok(icon_theme_family
64                .themes
65                .into_iter()
66                .map(|theme| theme.name)
67                .collect())
68        })
69    }
70
71    fn remove_icon_themes(&self, icon_themes: Vec<SharedString>) {
72        self.theme_registry.remove_icon_themes(&icon_themes);
73    }
74
75    fn load_icon_theme(
76        &self,
77        icon_theme_path: PathBuf,
78        icons_root_dir: PathBuf,
79        fs: Arc<dyn Fs>,
80    ) -> Task<Result<()>> {
81        let theme_registry = self.theme_registry.clone();
82        self.executor.spawn(async move {
83            let icon_theme_family =
84                deserialize_icon_theme(&fs.load_bytes(&icon_theme_path).await?)?;
85            theme_registry.load_icon_theme(icon_theme_family, &icons_root_dir)
86        })
87    }
88
89    fn reload_current_icon_theme(&self, cx: &mut App) {
90        theme_settings::reload_icon_theme(cx)
91    }
92}