Skip to main content

fresh/services/plugins/
bridge.rs

1use crate::config_io::DirectoryContext;
2use crate::i18n;
3use crate::input::command_registry::CommandRegistry;
4use crate::services::signal_handler;
5use crate::view::theme;
6use fresh_core::services::PluginServiceBridge;
7use std::any::Any;
8use std::collections::HashMap;
9use std::path::PathBuf;
10use std::sync::{Arc, RwLock};
11
12pub struct EditorServiceBridge {
13    pub command_registry: Arc<RwLock<CommandRegistry>>,
14    pub dir_context: DirectoryContext,
15    pub theme_cache: Arc<RwLock<std::collections::HashMap<String, serde_json::Value>>>,
16}
17
18impl PluginServiceBridge for EditorServiceBridge {
19    fn as_any(&self) -> &dyn Any {
20        self
21    }
22
23    fn translate(&self, plugin_name: &str, key: &str, args: &HashMap<String, String>) -> String {
24        i18n::translate_plugin_string(plugin_name, key, args)
25    }
26
27    fn current_locale(&self) -> String {
28        i18n::current_locale()
29    }
30
31    fn set_js_execution_state(&self, state: String) {
32        signal_handler::set_js_execution_state(state);
33    }
34
35    fn clear_js_execution_state(&self) {
36        signal_handler::clear_js_execution_state();
37    }
38
39    fn get_theme_schema(&self) -> serde_json::Value {
40        theme::get_theme_schema()
41    }
42
43    fn get_builtin_themes(&self) -> serde_json::Value {
44        theme::get_builtin_themes()
45    }
46
47    fn get_all_themes(&self) -> serde_json::Value {
48        let cache = self.theme_cache.read().unwrap();
49        let map: serde_json::Map<String, serde_json::Value> =
50            cache.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
51        serde_json::Value::Object(map)
52    }
53
54    fn register_plugin_strings(
55        &self,
56        plugin_name: &str,
57        strings: HashMap<String, HashMap<String, String>>,
58    ) {
59        i18n::register_plugin_strings(plugin_name, strings);
60    }
61
62    fn unregister_plugin_strings(&self, plugin_name: &str) {
63        i18n::unregister_plugin_strings(plugin_name);
64    }
65
66    fn register_command(&self, command: fresh_core::command::Command) {
67        // Convert fresh_core::command::Command to crate::input::commands::Command
68        use crate::input::commands::{Command as EditorCommand, CommandSource};
69        use crate::input::keybindings::{Action, KeyContext};
70
71        let editor_command = EditorCommand {
72            name: command.name,
73            description: command.description,
74            action: Action::PluginAction(command.action_name),
75            contexts: vec![KeyContext::Global],
76            custom_contexts: command.custom_contexts,
77            source: CommandSource::Plugin(command.plugin_name),
78        };
79        self.command_registry
80            .read()
81            .unwrap()
82            .register(editor_command);
83    }
84
85    fn unregister_command(&self, name: &str) {
86        self.command_registry.read().unwrap().unregister(name);
87    }
88
89    fn unregister_commands_by_prefix(&self, prefix: &str) {
90        self.command_registry
91            .read()
92            .unwrap()
93            .unregister_by_prefix(prefix);
94    }
95
96    fn unregister_commands_by_plugin(&self, plugin_name: &str) {
97        self.command_registry
98            .read()
99            .unwrap()
100            .unregister_by_plugin(plugin_name);
101    }
102
103    fn plugins_dir(&self) -> PathBuf {
104        self.dir_context.plugins_dir()
105    }
106
107    fn config_dir(&self) -> PathBuf {
108        self.dir_context.config_dir.clone()
109    }
110
111    fn data_dir(&self) -> PathBuf {
112        self.dir_context.data_dir.clone()
113    }
114
115    fn get_theme_data(&self, key_or_name: &str) -> Option<serde_json::Value> {
116        let cache = self.theme_cache.read().unwrap();
117        // Exact key match
118        if let Some(v) = cache.get(key_or_name) {
119            return Some(v.clone());
120        }
121        // Fallback: match by theme name inside the cached values
122        let normalized = key_or_name.to_lowercase().replace(['_', ' '], "-");
123        cache
124            .values()
125            .find(|v| {
126                v.get("name")
127                    .and_then(|n| n.as_str())
128                    .is_some_and(|n| n.to_lowercase().replace(['_', ' '], "-") == normalized)
129            })
130            .cloned()
131    }
132
133    fn save_theme_file(&self, name: &str, content: &str) -> Result<String, String> {
134        let themes_dir = self.dir_context.themes_dir();
135        if !themes_dir.exists() {
136            std::fs::create_dir_all(&themes_dir).map_err(|e| e.to_string())?;
137        }
138        let path = themes_dir.join(format!("{}.json", name));
139        std::fs::write(&path, content).map_err(|e| e.to_string())?;
140        Ok(path.to_string_lossy().to_string())
141    }
142
143    fn theme_file_exists(&self, name: &str) -> bool {
144        let themes_dir = self.dir_context.themes_dir();
145        themes_dir.join(format!("{}.json", name)).exists()
146    }
147}