use crate::action::ActionManager;
use crate::shortcut::Key;
use crate::shortcut::Shortcut;
pub struct ActionRouter<'a> {
shortcut_mgr: &'a mut crate::shortcut::ShortcutManager,
action_mgr: &'a mut ActionManager,
}
impl<'a> ActionRouter<'a> {
pub fn new(
shortcut_mgr: &'a mut crate::shortcut::ShortcutManager,
action_mgr: &'a mut ActionManager,
) -> Self {
Self { shortcut_mgr, action_mgr }
}
pub fn connect(
&mut self,
action_id: impl Into<String>,
shortcut: Shortcut,
text: impl Into<String>,
) -> bool {
let action_id = action_id.into();
if !self.action_mgr.register_action(action_id.clone(), text) {
return false;
}
if !self.shortcut_mgr.register(action_id.clone(), shortcut.clone(), "") {
self.action_mgr.unregister_action(&action_id);
return false;
}
if self.action_mgr.bind_shortcut_type(&shortcut, action_id.clone()) {
true
} else {
self.shortcut_mgr.unregister(&action_id);
self.action_mgr.unregister_action(&action_id);
false
}
}
pub fn connect_callback<F>(
&mut self,
action_id: impl Into<String>,
shortcut: Shortcut,
text: impl Into<String>,
callback: F,
) -> bool
where
F: FnMut() + Send + Sync + 'static,
{
let action_id = action_id.into();
if !self.connect(action_id.clone(), shortcut, text) {
return false;
}
self.action_mgr
.action(&action_id)
.map(|action| {
action.connect_triggered(callback);
true
})
.unwrap_or(false)
}
pub fn connect_undo_redo<U, R>(&mut self, undo: U, redo: R) -> bool
where
U: FnMut() + Send + Sync + 'static,
R: FnMut() + Send + Sync + 'static,
{
let undo_ok = self.connect_callback("undo", Shortcut::ctrl(Key::Z), "Undo", undo);
let redo_ok = self.connect_callback("redo", Shortcut::ctrl(Key::Y), "Redo", redo);
undo_ok && redo_ok
}
pub fn bind_shortcut(&mut self, action_id: impl Into<String>, shortcut: Shortcut) -> bool {
let action_id = action_id.into();
if self.action_mgr.action(&action_id).is_none() {
return false;
}
if !self.shortcut_mgr.register(action_id.clone(), shortcut.clone(), "") {
return false;
}
if self.action_mgr.bind_shortcut_type(&shortcut, action_id.clone()) {
return true;
}
self.shortcut_mgr.unregister(&action_id);
false
}
pub fn route_action_id(&mut self, action_id: &str) -> bool {
self.action_mgr.trigger_action(action_id)
}
}