use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
use super::KeyboardEvent;
use crate::{
app::{Accelerator, SyntheticModifiers},
core::NodeId,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum EditCommand {
Undo,
Redo,
Cut,
Copy,
Paste,
SelectAll,
}
const APPLE: bool = cfg!(any(target_os = "macos", target_os = "ios"));
impl EditCommand {
pub const ALL: [Self; 6] = [
Self::Undo,
Self::Redo,
Self::Cut,
Self::Copy,
Self::Paste,
Self::SelectAll,
];
pub fn of_key(event: &KeyboardEvent) -> Option<Self> {
Self::of_key_on(event, APPLE)
}
pub(crate) fn of_key_on(event: &KeyboardEvent, apple: bool) -> Option<Self> {
if event.alt {
return None;
}
let named = |name: &str| event.key == name || event.code == name;
if named("Insert") && event.ctrl && !event.shift {
return Some(Self::Copy);
}
if named("Insert") && event.shift && !event.ctrl {
return Some(Self::Paste);
}
if named("Delete") && event.shift && !event.ctrl {
return Some(Self::Cut);
}
let shortcut = if apple { event.meta } else { event.ctrl };
if !shortcut {
return None;
}
let mut chars = event.key.chars();
let letter = match (chars.next(), chars.next()) {
(Some(letter), None) if letter.is_ascii_alphabetic() => letter.to_ascii_lowercase(),
_ => match event.code.strip_prefix("Key").map(str::as_bytes) {
Some([letter]) => letter.to_ascii_lowercase() as char,
_ => return None,
},
};
match letter {
'z' if event.shift => Some(Self::Redo),
'z' => Some(Self::Undo),
'y' if !event.shift && !apple => Some(Self::Redo),
'x' => Some(Self::Cut),
'c' => Some(Self::Copy),
'v' => Some(Self::Paste),
'a' if !event.shift => Some(Self::SelectAll),
_ => None,
}
}
pub fn accelerator(self) -> Accelerator {
let modifiers = if APPLE {
SyntheticModifiers::default().meta()
} else {
SyntheticModifiers::default().ctrl()
};
match self {
Self::Undo => Accelerator::new("z", modifiers),
Self::Redo if APPLE => Accelerator::new("z", modifiers.shift()),
Self::Redo => Accelerator::new("y", modifiers),
Self::Cut => Accelerator::new("x", modifiers),
Self::Copy => Accelerator::new("c", modifiers),
Self::Paste => Accelerator::new("v", modifiers),
Self::SelectAll => Accelerator::new("a", modifiers),
}
}
pub(crate) fn keyboard_event(self) -> KeyboardEvent {
let accelerator = self.accelerator();
let m = accelerator.modifiers;
let code = format!("Key{}", accelerator.key.to_ascii_uppercase());
let key = if m.shift {
accelerator.key.to_ascii_uppercase()
} else {
accelerator.key.to_string()
};
KeyboardEvent::new(key, code, m.shift, m.ctrl, m.alt, m.meta, NodeId::UNASSIGNED)
}
}
#[derive(Clone)]
pub struct EditEvent {
command: EditCommand,
pub target_id: NodeId,
pub focus_id: NodeId,
passed: Arc<AtomicBool>,
}
impl EditEvent {
pub(crate) fn new(command: EditCommand, focus_id: NodeId) -> Self {
Self {
command,
target_id: focus_id,
focus_id,
passed: Arc::new(AtomicBool::new(false)),
}
}
pub(crate) fn at(&self, target_id: NodeId) -> Self {
Self {
command: self.command,
target_id,
focus_id: self.focus_id,
passed: Arc::clone(&self.passed),
}
}
pub fn command(&self) -> EditCommand {
self.command
}
pub fn pass_on(&self) {
self.passed.store(true, Ordering::Relaxed);
}
pub fn passed_on(&self) -> bool {
self.passed.load(Ordering::Relaxed)
}
pub(crate) fn reset(&self) {
self.passed.store(false, Ordering::Relaxed);
}
}
impl std::fmt::Debug for EditEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EditEvent")
.field("command", &self.command)
.field("target_id", &self.target_id)
.field("focus_id", &self.focus_id)
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
fn key(key: &str, code: &str, shift: bool, ctrl: bool, alt: bool, meta: bool) -> KeyboardEvent {
KeyboardEvent::new(key, code, shift, ctrl, alt, meta, NodeId::UNASSIGNED)
}
#[test]
fn apple_takes_command_and_has_no_y() {
let of = |event: &KeyboardEvent| EditCommand::of_key_on(event, true);
assert_eq!(
of(&key("z", "KeyZ", false, false, false, true)),
Some(EditCommand::Undo)
);
assert_eq!(of(&key("Z", "KeyZ", true, false, false, true)), Some(EditCommand::Redo));
assert_eq!(
of(&key("я", "KeyZ", false, false, false, true)),
Some(EditCommand::Undo)
);
assert_eq!(
of(&key("z", "KeyZ", false, true, false, false)),
None,
"Ctrl is no shortcut on a Mac"
);
assert_eq!(
of(&key("y", "KeyY", false, false, false, true)),
None,
"Cmd+Y is no redo on a Mac"
);
assert_eq!(of(&key("z", "KeyZ", false, false, true, true)), None, "Option held");
assert_eq!(
of(&key("с", "KeyC", false, false, false, true)),
Some(EditCommand::Copy)
);
assert_eq!(
of(&key("м", "KeyV", false, false, false, true)),
Some(EditCommand::Paste)
);
assert_eq!(
of(&key("a", "KeyA", false, false, false, true)),
Some(EditCommand::SelectAll)
);
assert_eq!(
of(&key("c", "KeyC", false, true, false, false)),
None,
"Ctrl+C is no copy on a Mac"
);
}
#[test]
fn elsewhere_takes_ctrl_and_y() {
let of = |event: &KeyboardEvent| EditCommand::of_key_on(event, false);
assert_eq!(
of(&key("z", "KeyZ", false, true, false, false)),
Some(EditCommand::Undo)
);
assert_eq!(
of(&key("y", "KeyY", false, true, false, false)),
Some(EditCommand::Redo)
);
assert_eq!(of(&key("Y", "KeyY", true, true, false, false)), None, "Ctrl+Shift+Y");
assert_eq!(
of(&key("z", "KeyZ", false, false, false, true)),
None,
"Meta is no shortcut here"
);
}
#[test]
fn a_german_keyboard_is_read_by_the_letters_it_says() {
let of = |event: &KeyboardEvent| EditCommand::of_key_on(event, false);
assert_eq!(
of(&key("z", "KeyY", false, true, false, false)),
Some(EditCommand::Undo)
);
assert_eq!(
of(&key("y", "KeyZ", false, true, false, false)),
Some(EditCommand::Redo)
);
}
#[test]
fn a_command_without_a_key_reports_its_accelerator() {
use EditCommand::*;
for command in [Undo, Redo, Cut, Copy, Paste, SelectAll] {
assert_eq!(EditCommand::of_key(&command.keyboard_event()), Some(command));
let accelerator = command.accelerator();
assert!(accelerator.matches(&command.keyboard_event()), "{command:?}");
}
}
}