use super::{Context, DiagnosticKind, Id};
use crate::actions::{Actions, Chord, Kbd, Mods, Stroke};
use std::{
collections::{HashMap, HashSet},
time::Duration,
};
use winit::keyboard::KeyCode;
mod dispatch;
mod handle;
pub use handle::{ActionSource, ActionsHandle, PendingChord};
#[derive(Clone, Copy, Debug, Default)]
struct Flags {
enabled: Option<bool>,
checked: Option<bool>,
}
struct Event {
id: Id,
source: ActionSource,
queued: u64,
}
struct Pending {
strokes: Vec<Stroke>,
deadline: crate::time::Instant,
}
#[derive(Default)]
pub(crate) struct ActionRuntime {
registry: Actions,
frame: HashMap<Id, Flags>,
published: HashMap<Id, Flags>,
context: Option<String>,
published_context: String,
events: Vec<Event>,
chord: Option<Pending>,
swallowed: HashSet<KeyCode>,
pressed: HashMap<KeyCode, (KeyCode, Mods)>,
}
pub(crate) struct ActionView {
pub(crate) title: String,
pub(crate) description: String,
pub(crate) shortcut: String,
pub(crate) enabled: bool,
pub(crate) checked: Option<bool>,
pub(crate) icon: Option<crate::ImageSource>,
}
impl ActionRuntime {
pub(super) fn begin_pass(&mut self, now: crate::time::Instant) {
self.frame.clear();
self.context = None;
if self
.chord
.as_ref()
.is_some_and(|chord| chord.deadline <= now)
{
self.chord = None;
}
}
pub(super) fn finish_frame(&mut self, frame: u64) {
self.published = std::mem::take(&mut self.frame);
self.published_context = self.context.clone().unwrap_or_default();
self.events.retain(|event| event.queued >= frame);
self.pressed.clear();
}
pub(crate) fn pressed_key(&self, code: KeyCode) -> Option<(KeyCode, Mods)> {
self.pressed.get(&code).copied()
}
pub(super) fn focus_lost(&mut self) {
self.chord = None;
self.swallowed.clear();
}
pub(super) fn release_swallowed(&mut self, code: KeyCode) -> bool {
self.swallowed.remove(&code)
}
}
impl Context {
#[track_caller]
pub fn set_actions(&mut self, actions: Actions) {
if actions.is_empty() {
super::invalid_value(
"Context::set_actions",
"no actions registered; keys, menus and buttons have nothing to run".into(),
);
}
self.actions.registry = actions;
self.actions.chord = None;
self.request_repaint();
}
pub fn action_registry(&self) -> &Actions {
&self.actions.registry
}
pub fn pending_chord(&self) -> Option<PendingChord> {
let strokes = self.pending_strokes()?;
let platform = self.action_platform();
let text = format!("{} …", Kbd::new(&strokes).platform(platform));
Some(PendingChord { strokes, text })
}
pub fn actions(&mut self) -> ActionsHandle<'_> {
ActionsHandle::new(self)
}
fn flags(&self, id: Id) -> Flags {
let source = if self.in_pass {
&self.actions.frame
} else {
&self.actions.published
};
source.get(&id).copied().unwrap_or_default()
}
pub(crate) fn action_enabled(&self, id: Id) -> bool {
self.flags(id).enabled.unwrap_or(true)
}
pub(crate) fn fire_action(&mut self, id: Id, source: ActionSource) -> bool {
if self.actions.registry.action(id).is_none() {
self.unknown_action(id);
return false;
}
if !self.action_enabled(id) {
return false;
}
let queued = self.frame;
self.actions.events.push(Event { id, source, queued });
self.request_repaint();
true
}
fn unknown_action(&mut self, id: Id) {
self.report(DiagnosticKind::InvalidUsage, Some(id), None, || {
"unknown action: none was declared with this value".into()
});
}
pub(crate) fn action_view(&mut self, id: Id) -> Option<ActionView> {
let Some(action) = self.actions.registry.action(id) else {
self.unknown_action(id);
return None;
};
let keymap = self.actions.registry.keymap();
let shortcut = keymap
.shortcut_of(id, &self.actions.published_context)
.map(|chord| Kbd::new(chord).platform(keymap.platform()).to_string())
.unwrap_or_default();
let flags = self.flags(id);
Some(ActionView {
title: action.title.clone(),
description: action.description.clone(),
shortcut,
enabled: flags.enabled.unwrap_or(true),
checked: flags.checked,
icon: action.icon.clone(),
})
}
pub(crate) fn action_platform(&self) -> crate::Platform {
self.actions.registry.keymap().platform()
}
pub(crate) fn chord_timeout(&self) -> Duration {
self.actions.registry.keymap().chord_timeout()
}
pub(crate) fn pending_strokes(&self) -> Option<Chord> {
let pending = self.actions.chord.as_ref()?;
Some(Chord::new(pending.strokes.iter().copied()))
}
}