theme_extension.rs

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