1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use crate::auth::AuthHandle;
use crate::db_access::DbAccess;
use crate::event::Event;
use crate::plugin::{Plugin, PluginCmdItem, PluginContext, PluginFactory};
use crate::registry_config::RegistryConfig;
use crate::theme::Theme;
use crossterm::event::KeyEvent;
use ratatui::layout::Rect;
use ratatui::Frame;
use std::sync::Arc;
/// Manages the lifecycle, dispatch, and palette-command registry for all
/// loaded plugins. Extracted from the monolithic `Santui` struct so that
/// Santui itself only owns a single `PluginManager` field.
pub(crate) struct PluginManager {
plugins: Vec<Box<dyn Plugin + Send>>,
active_idx: Option<usize>,
/// Global index → (plugin_index, local_command_index, command).
plugin_commands: Vec<(usize, usize, PluginCmdItem)>,
/// Factory for recreating plugins during hot-reload.
plugin_factory: Option<PluginFactory>,
/// Last known modification times for each plugin's binary, parallel to
/// `plugins`. `None` for in-process plugins or when stat failed.
mtimes: Vec<Option<SystemTime>>,
/// Dynamic palette items from enabled registry plugins: (category, plugin_id, name).
dynamic_items: Vec<(String, String, String)>,
/// Capabilities declared in registry.toml, keyed by plugin id.
plugin_capabilities: HashMap<String, Vec<String>>,
/// Plugin ids that were installed via the registry (from registry.toml).
/// Used by `reap_unregistered` to avoid killing built-in plugins.
registry_plugin_ids: HashSet<String>,
/// Santui data directory (platform-standard). Set from main.rs.
data_dir: PathBuf,
/// Last mtime of registry.toml, used for change detection.
registry_mtime: Option<SystemTime>,
/// Throttle: only stat plugin binaries every N frames.
reload_skip: u32,
/// Names of plugins that have crashed since last check.
crashed_plugins: Vec<String>,
}
impl PluginManager {
pub fn new() -> Self {
PluginManager {
plugins: Vec::new(),
active_idx: None,
plugin_commands: Vec::new(),
plugin_factory: None,
mtimes: Vec::new(),
dynamic_items: Vec::new(),
plugin_capabilities: HashMap::new(),
registry_plugin_ids: HashSet::new(),
data_dir: PathBuf::new(),
registry_mtime: None,
reload_skip: 0,
crashed_plugins: Vec::new(),
}
}
/// Store the santui data directory.
pub fn set_data_dir(&mut self, dir: PathBuf) {
self.data_dir = dir;
}
/// Get the santui data directory.
pub fn data_dir(&self) -> &Path {
&self.data_dir
}
/// Store the plugin factory so we can recreate plugins during hot-reload.
pub fn set_persistent(&mut self, id: &str, persistent: bool) {
if let Some(plugin) = self.plugins.iter_mut().find(|p| p.id() == id) {
plugin.set_persistent(persistent);
}
}
pub fn set_factory(&mut self, factory: PluginFactory) {
self.plugin_factory = Some(factory);
}
// ------------------------------------------------------------------
// Registration & lifecycle
// ------------------------------------------------------------------
pub fn register(&mut self, plugin: Box<dyn Plugin + Send>) {
self.mtimes.push(stat_mtime(plugin.binary_path()));
self.plugins.push(plugin);
}
/// Create and register a plugin from the factory without initialising it.
/// The plugin will be initialised during the next `init_all` call.
pub fn register_new(&mut self, id: &str, name: &str, path: &Path) {
if let Some(ref factory) = self.plugin_factory {
let plugin = factory(id, name, path);
self.register(plugin);
}
}
pub fn len(&self) -> usize {
self.plugins.len()
}
pub fn init_all(&mut self, ctx: &mut PluginContext) -> Result<(), Box<dyn std::error::Error>> {
for p in &mut self.plugins {
p.init(ctx)?;
}
self.refresh_commands();
Ok(())
}
pub fn tick_all(&mut self) {
self.crashed_plugins.clear();
for p in &mut self.plugins {
p.tick();
if !p.is_alive() {
self.crashed_plugins.push(p.name().to_string());
}
}
}
/// Process any pending `PluginRequest`s for all plugins.
/// Called once per main loop iteration (after tick and after key events).
pub fn process_all_requests(
&mut self,
db: &mut dyn DbAccess,
auth: &Option<Arc<dyn AuthHandle>>,
) {
let auth_ref = auth.as_ref();
for p in &mut self.plugins {
p.process_pending_requests(db, auth_ref);
}
}
pub fn crashed_plugins(&self) -> &[String] {
&self.crashed_plugins
}
/// Use the internal factory to create a plugin from (id, name, binary_path),
/// initialise it, register it, and return its index.
pub fn spawn_and_init(
&mut self,
id: &str,
name: &str,
binary_path: &std::path::Path,
capabilities: &[String],
ctx: &mut PluginContext,
) -> Result<usize, Box<dyn std::error::Error>> {
let factory = self
.plugin_factory
.as_ref()
.ok_or_else::<Box<dyn std::error::Error>, _>(|| "no factory".into())?;
let mut plugin = factory(id, name, binary_path);
plugin.set_capabilities(capabilities.to_vec());
plugin.init(ctx)?;
let idx = self.plugins.len();
self.mtimes.push(stat_mtime(plugin.binary_path()));
self.plugins.push(plugin);
Ok(idx)
}
// ------------------------------------------------------------------
// Active plugin
// ------------------------------------------------------------------
pub fn active(&self) -> Option<usize> {
self.active_idx
}
pub fn set_active(&mut self, idx: Option<usize>) {
if self.active_idx == idx {
return;
}
if let Some(old) = self.active_idx {
if old < self.plugins.len() {
self.plugins[old].on_blur();
}
}
self.active_idx = idx;
if let Some(new) = idx {
if new < self.plugins.len() {
self.plugins[new].on_focus();
}
}
}
// ------------------------------------------------------------------
// Dispatch to a specific plugin
// ------------------------------------------------------------------
pub fn handle_key(&mut self, idx: usize, key: KeyEvent) -> bool {
if idx < self.plugins.len() {
self.plugins[idx].handle_key(key)
} else {
false
}
}
pub fn render(&self, idx: usize, f: &mut Frame, area: Rect) {
if idx < self.plugins.len() {
self.plugins[idx].render(f, area);
}
}
pub fn status_hints(&self, idx: usize) -> Vec<(String, String)> {
if idx < self.plugins.len() {
self.plugins[idx].status_hints()
} else {
vec![]
}
}
/// Check if a pending Esc response has been resolved for plugin `idx`.
/// Returns `Some(consumed)` if resolved, `None` if still pending.
pub fn drain_esc_result(&mut self, idx: usize) -> Option<bool> {
if idx < self.plugins.len() {
self.plugins[idx].take_pending_esc_result()
} else {
None
}
}
/// Shut down an IPC plugin and remove it from the managed set.
///
/// Background-capable plugins (e.g., audio players) are kept alive — only
/// blurred and removed from the active display.
///
/// In-process plugins (no binary path) are *not* removed — only the active
/// index is cleared so the home screen regains focus.
pub fn shutdown_and_remove(&mut self, idx: usize) {
if idx >= self.plugins.len() {
return;
}
// Persistent plugins (e.g., registry) stay loaded so their palette
// entries and background processes survive.
if self.plugins[idx].persistent() {
self.plugins[idx].on_blur();
self.active_idx = None;
return;
}
// Background-capable plugins: blur but keep alive.
if self.plugins[idx].can_background() {
self.plugins[idx].on_blur();
self.active_idx = None;
return;
}
// Only remove out-of-process plugins; keep in-process plugins loaded.
if self.plugins[idx].binary_path().is_some() {
self.plugins[idx].on_blur();
self.plugins[idx].shutdown();
self.plugins.remove(idx);
self.mtimes.remove(idx);
self.refresh_commands();
}
if self.active_idx == Some(idx) {
self.active_idx = None;
}
}
/// Dispatch a palette command that originated from the given plugin.
pub fn handle_palette_command(&mut self, plugin_idx: usize, local_idx: usize) {
if plugin_idx < self.plugins.len() {
self.plugins[plugin_idx].handle_palette_command(local_idx);
}
}
// ------------------------------------------------------------------
// Queries
// ------------------------------------------------------------------
pub fn find_by_id(&self, id: &str) -> Option<usize> {
self.plugins.iter().position(|p| p.id() == id)
}
// ------------------------------------------------------------------
// Hot-reload
// ------------------------------------------------------------------
/// Check every plugin's binary mtime and reload any that have changed.
/// Called once per frame from the event loop.
pub fn check_reloads(&mut self, ctx: &mut PluginContext) {
self.reload_skip = self.reload_skip.saturating_sub(1);
if self.reload_skip > 0 {
return;
}
self.reload_skip = 30;
for idx in 0..self.plugins.len() {
if let Err(e) = self.reload_plugin(idx, ctx) {
let name = self.plugins[idx].name().to_string();
log::error!("[santui] Failed to reload plugin `{name}`: {e}");
}
}
}
/// Reload the plugin at `idx` if its binary has changed on disk.
/// In-process plugins (no binary path) are skipped.
fn reload_plugin(
&mut self,
idx: usize,
ctx: &mut PluginContext,
) -> Result<(), Box<dyn std::error::Error>> {
let path = match self.plugins[idx].binary_path() {
Some(p) => p.to_path_buf(),
None => return Ok(()),
};
let factory = match self.plugin_factory.as_ref() {
Some(f) => f,
None => return Ok(()),
};
let current_mtime = stat_mtime(Some(&path));
let stored = self.mtimes.get(idx).copied().flatten();
if current_mtime == stored {
return Ok(());
}
// Binary changed — recreate the plugin.
let id = self.plugins[idx].id().to_string();
let name = self.plugins[idx].name().to_string();
// Shut down the old plugin first so no two plugin processes are
// alive at the same time, preventing an orphan if kill() fails.
self.plugins[idx].shutdown();
// Drop the old plugin now (kills child process) before spawning a
// new one. If init() fails below, at worst we are left without a
// running plugin (the palette entry still exists).
self.plugins[idx] = factory(&id, &name, &path);
// Restore capabilities from the cached map.
if let Some(caps) = self.plugin_capabilities.get(&id) {
self.plugins[idx].set_capabilities(caps.clone());
}
// Clear stale commands before refreshing.
self.mtimes[idx] = None;
// Update stored mtime *before* init, so if init fails the mtime is
// still bumped and we don't retry on every frame.
if let Some(mtime) = current_mtime {
if idx < self.mtimes.len() {
self.mtimes[idx] = Some(mtime);
}
}
self.plugins[idx].init(ctx)?;
self.refresh_commands();
Ok(())
}
// ------------------------------------------------------------------
// Broadcasts
// ------------------------------------------------------------------
pub fn on_theme_change_all(&mut self, theme: &Theme) {
for p in &mut self.plugins {
p.on_theme_change(theme);
}
}
// ------------------------------------------------------------------
// Crash recovery
// ------------------------------------------------------------------
/// Recreate a plugin that has crashed using the stored factory.
/// Returns an error if the plugin is in-process (no binary path) or
/// no factory is registered.
pub fn restart_plugin(
&mut self,
idx: usize,
ctx: &mut PluginContext,
) -> Result<(), Box<dyn std::error::Error>> {
let path = match self.plugins[idx].binary_path() {
Some(p) => p.to_path_buf(),
None => return Err("cannot restart in-process plugin".into()),
};
let factory = self
.plugin_factory
.as_ref()
.ok_or_else::<Box<dyn std::error::Error>, _>(|| "no factory".into())?;
let id = self.plugins[idx].id().to_string();
let name = self.plugins[idx].name().to_string();
self.plugins[idx].shutdown();
self.plugins[idx] = factory(&id, &name, &path);
if let Some(caps) = self.plugin_capabilities.get(&id) {
self.plugins[idx].set_capabilities(caps.clone());
}
self.mtimes[idx] = stat_mtime(Some(&path));
self.plugins[idx].init(ctx)?;
self.refresh_commands();
log::info!("[santui] Restarted plugin `{name}`");
Ok(())
}
/// Restart all plugins that have crashed. Returns the number of
/// successfully restarted plugins.
pub fn restart_crashed(&mut self, ctx: &mut PluginContext) -> usize {
let mut count = 0;
let mut i = 0;
while i < self.plugins.len() {
if !self.plugins[i].is_alive() && self.restart_plugin(i, ctx).is_ok() {
count += 1;
}
i += 1;
}
count
}
/// Process a batch of events from the EventBus.
pub fn process_events(&mut self, events: &[Event]) {
for event in events {
match event {
Event::PluginMessage {
from,
to,
action,
data,
} => {
if let Some(idx) = self.find_by_id(to) {
self.plugins[idx].on_plugin_message(from, action, data);
}
}
Event::ThemeChanged(theme) => {
self.on_theme_change_all(theme);
}
}
}
}
pub fn on_user_update_all(&mut self, user: Option<&crate::auth::User>) {
for p in &mut self.plugins {
p.on_user_update(user);
}
}
// ------------------------------------------------------------------
// Palette commands
// ------------------------------------------------------------------
pub fn commands(&self) -> &[(usize, usize, PluginCmdItem)] {
&self.plugin_commands
}
pub fn refresh_commands(&mut self) {
self.plugin_commands.clear();
for (i, plugin) in self.plugins.iter().enumerate() {
for (local_idx, cmd) in plugin.commands().into_iter().enumerate() {
self.plugin_commands.push((i, local_idx, cmd));
}
}
}
pub fn dynamic_items(&self) -> &[(String, String, String)] {
&self.dynamic_items
}
/// Poll `registry.toml` for changes and update dynamic palette items.
/// Called once per frame. Returns true if items changed.
pub fn poll_registry_installed(&mut self) -> bool {
let path = self.data_dir.join("registry.toml");
let current_mtime = match std::fs::metadata(&path) {
Ok(m) => m.modified().ok(),
Err(_) => {
self.dynamic_items.clear();
return false;
}
};
if current_mtime == self.registry_mtime {
return false;
}
self.registry_mtime = current_mtime;
self.read_registry_installed()
}
/// Re-read `registry.toml` and rebuild `dynamic_items` and `plugin_capabilities`.
/// Returns true if items changed.
pub fn read_registry_installed(&mut self) -> bool {
let path = self.data_dir.join("registry.toml");
let cfg = RegistryConfig::load(&path);
let old = std::mem::take(&mut self.dynamic_items);
self.plugin_capabilities.clear();
let old_registry_ids = std::mem::take(&mut self.registry_plugin_ids);
if let Some(ref cfg) = cfg {
for installed in &cfg.plugins {
let id = if !installed.id.is_empty() {
installed.id.clone()
} else {
match installed
.path
.file_stem()
.and_then(|s| s.to_str())
.map(|s| s.trim_end_matches(".exe").to_string())
{
Some(id) => id,
None => continue,
}
};
self.registry_plugin_ids.insert(id.clone());
if !installed.enabled {
continue;
}
let name = if !installed.name.is_empty() {
installed.name.clone()
} else {
id.trim_start_matches("santui-")
.replace('-', " ")
.split_ascii_whitespace()
.map(|w| {
let mut c = w.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().to_string() + c.as_str(),
}
})
.collect::<Vec<_>>()
.join(" ")
};
self.plugin_capabilities
.insert(id.clone(), installed.capabilities.clone());
if !self
.dynamic_items
.iter()
.any(|(_, existing_id, _)| *existing_id == id)
{
self.dynamic_items
.push(("Plugins".into(), id.clone(), name));
}
}
self.reap_unregistered(&old_registry_ids);
}
self.dynamic_items != old
}
/// Shut down and remove any loaded out-of-process plugins that were
/// previously installed via the registry but are no longer present in the
/// new config (deleted or disabled). `old_ids` is the set of registry
/// plugin ids from the *previous* read.
fn reap_unregistered(&mut self, old_ids: &HashSet<String>) {
let current: HashSet<&str> = self
.dynamic_items
.iter()
.map(|(_, id, _)| id.as_str())
.collect();
let mut i = self.plugins.len();
while i > 0 {
i -= 1;
let id = self.plugins[i].id();
if self.plugins[i].binary_path().is_some()
&& old_ids.contains(id)
&& !current.contains(id)
{
self.plugins[i].on_blur();
self.plugins[i].shutdown();
self.plugins.remove(i);
self.mtimes.remove(i);
}
}
self.refresh_commands();
}
}
impl Default for PluginManager {
fn default() -> Self {
Self::new()
}
}
/// An item in the home-screen carousel.
/// Represents a plugin that is either already loaded or available in the registry.
#[derive(Debug, Clone)]
pub struct CarouselItem {
pub id: String,
pub name: String,
/// Index into `self.plugins` if the plugin is already loaded.
pub plugin_idx: Option<usize>,
}
impl PluginManager {
/// Build a unified carousel list from loaded plugins + enabled registry items.
///
/// Order:
/// 1. All currently loaded plugins (in registration order).
/// 2. Enabled registry plugins whose id is not already loaded (deduplicated).
pub fn carousel_items(&self) -> Vec<CarouselItem> {
let mut items: Vec<CarouselItem> = self
.plugins
.iter()
.enumerate()
.map(|(i, p)| CarouselItem {
id: p.id().to_string(),
name: p.name().to_string(),
plugin_idx: Some(i),
})
.collect();
// Append registry items that aren't already loaded.
let loaded_ids: HashSet<&str> = self.plugins.iter().map(|p| p.id()).collect();
for (_, id, name) in &self.dynamic_items {
if !loaded_ids.contains(id.as_str()) {
items.push(CarouselItem {
id: id.clone(),
name: name.clone(),
plugin_idx: None,
});
}
}
items
}
}
/// Helper: resolve `SystemTime` from an optional path.
fn stat_mtime(path: Option<&Path>) -> Option<SystemTime> {
path.and_then(|p| std::fs::metadata(p).ok())
.and_then(|m| m.modified().ok())
}