use crate::auth::{AuthHandle, User};
use crate::db_access::DbAccess;
use crate::theme::Theme;
use crossterm::event::{KeyEvent, MouseEvent};
use ratatui::layout::Rect;
use ratatui::Frame;
use std::path::{Path, PathBuf};
use std::sync::Arc;
pub type PluginFactory = Arc<dyn Fn(&str, &str, &Path) -> Box<dyn Plugin + Send> + Send + Sync>;
#[derive(Debug, Clone)]
pub struct PluginCmdItem {
pub category: String,
pub label: String,
}
pub trait Plugin: Send {
fn id(&self) -> &str;
fn name(&self) -> &str;
fn init(&mut self, ctx: &mut PluginContext) -> Result<(), Box<dyn std::error::Error>>;
fn handle_key(&mut self, _key: KeyEvent) -> bool {
false
}
fn handle_mouse(&mut self, _event: &MouseEvent) -> bool {
false
}
fn render(&self, _f: &mut Frame, _area: Rect) {}
fn tick(&mut self) {}
fn on_focus(&mut self) {}
fn on_blur(&mut self) {}
fn on_theme_change(&mut self, _theme: &Theme) {}
fn on_user_update(&mut self, _user: Option<&User>) {}
fn status_hints(&self) -> Vec<(String, String)> {
vec![]
}
fn commands(&self) -> Vec<PluginCmdItem> {
vec![]
}
fn handle_palette_command(&mut self, _index: usize) {}
fn on_plugin_message(&mut self, _from: &str, _action: &str, _data: &str) {}
fn shutdown(&mut self) {}
fn binary_path(&self) -> Option<&Path> {
None
}
fn is_alive(&self) -> bool {
true
}
fn can_background(&self) -> bool {
false
}
fn set_capabilities(&mut self, _caps: Vec<String>) {}
fn persistent(&self) -> bool {
false
}
fn set_persistent(&mut self, _persistent: bool) {}
fn take_pending_esc_result(&mut self) -> Option<bool> {
None
}
fn process_pending_requests(
&mut self,
_db: &mut dyn DbAccess,
_auth: Option<&Arc<dyn AuthHandle>>,
) {
}
}
pub struct PluginContext {
pub theme: Theme,
pub auth: Option<Arc<dyn AuthHandle>>,
pub data_dir: PathBuf,
}
impl PluginContext {
pub fn new() -> Self {
PluginContext {
theme: Theme::default(),
auth: None,
data_dir: PathBuf::new(),
}
}
}
impl Default for PluginContext {
fn default() -> Self {
Self::new()
}
}