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}
16
17impl PluginServiceBridge for EditorServiceBridge {
18    fn as_any(&self) -> &dyn Any {
19        self
20    }
21
22    fn translate(&self, plugin_name: &str, key: &str, args: &HashMap<String, String>) -> String {
23        i18n::translate_plugin_string(plugin_name, key, args)
24    }
25
26    fn current_locale(&self) -> String {
27        i18n::current_locale()
28    }
29
30    fn set_js_execution_state(&self, state: String) {
31        signal_handler::set_js_execution_state(state);
32    }
33
34    fn clear_js_execution_state(&self) {
35        signal_handler::clear_js_execution_state();
36    }
37
38    fn get_theme_schema(&self) -> serde_json::Value {
39        theme::get_theme_schema()
40    }
41
42    fn get_builtin_themes(&self) -> serde_json::Value {
43        theme::get_builtin_themes()
44    }
45
46    fn register_plugin_strings(
47        &self,
48        plugin_name: &str,
49        strings: HashMap<String, HashMap<String, String>>,
50    ) {
51        i18n::register_plugin_strings(plugin_name, strings);
52    }
53
54    fn unregister_plugin_strings(&self, plugin_name: &str) {
55        i18n::unregister_plugin_strings(plugin_name);
56    }
57
58    fn register_command(&self, command: fresh_core::command::Command) {
59        // Convert fresh_core::command::Command to crate::input::commands::Command
60        use crate::input::commands::{Command as EditorCommand, CommandSource};
61        use crate::input::keybindings::{Action, KeyContext};
62
63        let editor_command = EditorCommand {
64            name: command.name,
65            description: command.description,
66            action: Action::PluginAction(command.action_name),
67            contexts: vec![KeyContext::Global],
68            custom_contexts: command.custom_contexts,
69            source: CommandSource::Plugin(command.plugin_name),
70        };
71        self.command_registry
72            .read()
73            .unwrap()
74            .register(editor_command);
75    }
76
77    fn unregister_command(&self, name: &str) {
78        self.command_registry.read().unwrap().unregister(name);
79    }
80
81    fn unregister_commands_by_prefix(&self, prefix: &str) {
82        self.command_registry
83            .read()
84            .unwrap()
85            .unregister_by_prefix(prefix);
86    }
87
88    fn unregister_commands_by_plugin(&self, plugin_name: &str) {
89        self.command_registry
90            .read()
91            .unwrap()
92            .unregister_by_plugin(plugin_name);
93    }
94
95    fn plugins_dir(&self) -> PathBuf {
96        self.dir_context.plugins_dir()
97    }
98
99    fn config_dir(&self) -> PathBuf {
100        self.dir_context.config_dir.clone()
101    }
102}