use super::super::Context;
use crate::actions::{action_id, Actions, Chord, Keymap};
use std::hash::Hash;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ActionSource {
Keyboard,
Menu,
ContextMenu,
Button,
Programmatic,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingChord {
pub strokes: Chord,
pub text: String,
}
pub struct ActionsHandle<'a> {
context: &'a mut Context,
}
impl<'a> ActionsHandle<'a> {
pub(in crate::context) fn new(context: &'a mut Context) -> Self {
Self { context }
}
pub fn set_enabled(&mut self, id: impl Hash, enabled: bool) {
let id = action_id(id);
self.context.actions.frame.entry(id).or_default().enabled = Some(enabled);
}
pub fn set_checked(&mut self, id: impl Hash, checked: bool) {
let id = action_id(id);
self.context.actions.frame.entry(id).or_default().checked = Some(checked);
}
pub fn is_enabled(&self, id: impl Hash) -> bool {
self.context.action_enabled(action_id(id))
}
pub fn checked(&self, id: impl Hash) -> Option<bool> {
self.context.flags(action_id(id)).checked
}
pub fn triggered(&mut self, id: impl Hash) -> bool {
self.take(id).is_some()
}
pub fn take(&mut self, id: impl Hash) -> Option<ActionSource> {
let id = action_id(id);
let events = &mut self.context.actions.events;
let at = events.iter().position(|event| event.id == id)?;
Some(events.remove(at).source)
}
pub fn trigger(&mut self, id: impl Hash) -> bool {
self.context
.fire_action(action_id(id), ActionSource::Programmatic)
}
pub fn set_context(&mut self, context: impl Into<String>) {
self.context.actions.context = Some(context.into());
}
pub fn pending_chord(&self) -> Option<PendingChord> {
self.context.pending_chord()
}
pub fn shortcut_text(&mut self, id: impl Hash) -> Option<String> {
let view = self.context.action_view(action_id(id))?;
(!view.shortcut.is_empty()).then_some(view.shortcut)
}
pub fn registry(&self) -> &Actions {
&self.context.actions.registry
}
pub fn registry_mut(&mut self) -> &mut Actions {
&mut self.context.actions.registry
}
pub fn keymap(&self) -> &Keymap {
self.context.actions.registry.keymap()
}
pub fn keymap_mut(&mut self) -> &mut Keymap {
self.context.actions.registry.keymap_mut()
}
}