use async_trait::async_trait;
use origin_domain::Result;
use std::fmt::Debug;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Shortcut {
pub id: String,
pub accelerator: String,
}
impl Shortcut {
pub fn new(id: impl Into<String>, accelerator: impl Into<String>) -> Self {
Self {
id: id.into(),
accelerator: accelerator.into(),
}
}
}
#[async_trait]
pub trait GlobalShortcutService: Debug + Send + Sync + 'static {
async fn register(&self, shortcut: Shortcut) -> Result<()>;
async fn unregister(&self, id: &str) -> Result<()>;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct NoopGlobalShortcutService;
#[async_trait]
impl GlobalShortcutService for NoopGlobalShortcutService {
async fn register(&self, shortcut: Shortcut) -> Result<()> {
tracing::debug!(id = %shortcut.id, accelerator = %shortcut.accelerator, "global shortcut — dropped (noop)");
Ok(())
}
async fn unregister(&self, id: &str) -> Result<()> {
tracing::debug!(id, "global shortcut unregistered — dropped (noop)");
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn the_noop_service_accepts_registration_and_removal() {
let shortcuts: &dyn GlobalShortcutService = &NoopGlobalShortcutService;
shortcuts
.register(Shortcut::new("quick-capture", "CmdOrCtrl+Shift+Space"))
.await
.unwrap();
shortcuts.unregister("quick-capture").await.unwrap();
shortcuts.unregister("never-registered").await.unwrap();
}
}