enya_plugin/hooks.rs
1//! Hook traits for plugins to intercept and extend host behavior.
2
3use rustc_hash::FxHashMap;
4
5use crate::types::Theme;
6
7/// Result of a command hook execution.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum CommandHookResult {
10 /// Allow the command to continue to the next handler
11 Continue,
12 /// Stop processing, command was handled
13 Handled,
14 /// Stop processing, command was blocked
15 Blocked,
16}
17
18/// Result of a keyboard hook execution.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum KeyboardHookResult {
21 /// Allow the key event to continue to the next handler
22 Continue,
23 /// Stop processing, key was handled
24 Handled,
25 /// Stop processing, key was blocked
26 Blocked,
27}
28
29/// Hook for intercepting lifecycle events.
30pub trait LifecycleHook: Send + Sync {
31 /// Called when the workspace is loaded.
32 fn on_workspace_loaded(&mut self) {}
33
34 /// Called when the workspace is about to be saved.
35 fn on_workspace_saving(&mut self) {}
36
37 /// Called when a pane is added to the workspace.
38 fn on_pane_added(&mut self, _pane_id: usize) {}
39
40 /// Called when a pane is about to be removed.
41 fn on_pane_removing(&mut self, _pane_id: usize) {}
42
43 /// Called when a pane gains focus.
44 fn on_pane_focused(&mut self, _pane_id: usize) {}
45
46 /// Called when the application is about to close.
47 fn on_closing(&mut self) {}
48
49 /// Called on each frame update (be careful with performance).
50 fn on_frame(&mut self) {}
51}
52
53/// Hook for intercepting command execution.
54pub trait CommandHook: Send + Sync {
55 /// Called before a command is executed.
56 ///
57 /// Return `CommandHookResult::Handled` to prevent the default handler.
58 fn before_command(&mut self, _command: &str, _args: &str) -> CommandHookResult {
59 CommandHookResult::Continue
60 }
61
62 /// Called after a command is executed.
63 fn after_command(&mut self, _command: &str, _args: &str, _success: bool) {}
64}
65
66/// Hook for intercepting keyboard events.
67pub trait KeyboardHook: Send + Sync {
68 /// Called when a key is pressed.
69 ///
70 /// Return `KeyboardHookResult::Handled` to prevent the default handler.
71 fn on_key_pressed(&mut self, _key: &KeyEvent) -> KeyboardHookResult {
72 KeyboardHookResult::Continue
73 }
74
75 /// Called when a key combination is pressed (e.g., Ctrl+S).
76 fn on_key_combo(&mut self, _combo: &KeyCombo) -> KeyboardHookResult {
77 KeyboardHookResult::Continue
78 }
79}
80
81/// Hook for theme-related events.
82pub trait ThemeHook: Send + Sync {
83 /// Called before a theme change is applied.
84 fn before_theme_change(&mut self, _old_theme: Theme, _new_theme: Theme) {}
85
86 /// Called after a theme change is applied.
87 fn after_theme_change(&mut self, _theme: Theme) {}
88
89 /// Optionally provide custom colors for the theme.
90 fn customize_theme(&self, _theme: Theme) -> Option<ThemeCustomization> {
91 None
92 }
93}
94
95/// Hook for pane-related events.
96pub trait PaneHook: Send + Sync {
97 /// Called when a pane is created.
98 fn on_pane_created(&mut self, _pane_id: usize, _pane_type: &str) {}
99
100 /// Called when a pane's query changes.
101 fn on_query_changed(&mut self, _pane_id: usize, _query: &str) {}
102
103 /// Called when a pane receives data.
104 fn on_data_received(&mut self, _pane_id: usize) {}
105
106 /// Called when a pane encounters an error.
107 fn on_pane_error(&mut self, _pane_id: usize, _error: &str) {}
108}
109
110/// Represents a keyboard event.
111#[derive(Debug, Clone)]
112pub struct KeyEvent {
113 /// The key code
114 pub key: egui::Key,
115 /// Whether shift was held
116 pub shift: bool,
117 /// Whether ctrl/cmd was held
118 pub ctrl: bool,
119 /// Whether alt was held
120 pub alt: bool,
121}
122
123/// Represents a key combination.
124#[derive(Debug, Clone)]
125pub struct KeyCombo {
126 /// Primary key
127 pub key: egui::Key,
128 /// Modifier keys
129 pub modifiers: egui::Modifiers,
130}
131
132/// Theme customization provided by a plugin.
133#[derive(Debug, Clone)]
134pub struct ThemeCustomization {
135 /// Custom colors (name -> color)
136 pub colors: FxHashMap<String, egui::Color32>,
137}