use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::{
error::Error,
fmt::{Display, Write},
};
use crate::{OperatingSystem, PlatformKeyboardMapper};
pub trait AsKeystroke {
fn as_keystroke(&self) -> &Keystroke;
}
#[derive(Clone, Debug, Eq, PartialEq, Default, Deserialize, Hash)]
pub struct Keystroke {
pub modifiers: Modifiers,
pub key: String,
pub key_char: Option<String>,
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct KeybindingKeystroke {
inner: Keystroke,
#[cfg(target_os = "windows")]
display_modifiers: Modifiers,
#[cfg(target_os = "windows")]
display_key: String,
}
#[derive(Debug)]
pub struct InvalidKeystrokeError {
pub keystroke: String,
}
impl Error for InvalidKeystrokeError {}
impl Display for InvalidKeystrokeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Invalid keystroke \"{}\". {}",
self.keystroke, KEYSTROKE_PARSE_EXPECTED_MESSAGE
)
}
}
pub const KEYSTROKE_PARSE_EXPECTED_MESSAGE: &str = "Expected a sequence of modifiers \
(`ctrl`, `alt`, `shift`, `fn`, `cmd`, `super`, or `win`) \
followed by a key, separated by `-`.";
impl Keystroke {
pub fn should_match(&self, target: &KeybindingKeystroke) -> bool {
#[cfg(not(target_os = "windows"))]
if let Some(key_char) = self
.key_char
.as_ref()
.filter(|key_char| key_char != &&self.key)
{
let ime_modifiers = Modifiers {
control: self.modifiers.control,
platform: self.modifiers.platform,
..Default::default()
};
if &target.inner.key == key_char && target.inner.modifiers == ime_modifiers {
return true;
}
}
#[cfg(target_os = "windows")]
if let Some(key_char) = self
.key_char
.as_ref()
.filter(|key_char| key_char != &&self.key)
{
if &target.inner.key == key_char && target.inner.modifiers == Modifiers::none() {
return true;
}
}
target.inner.modifiers == self.modifiers && target.inner.key == self.key
}
pub fn parse(source: &'static str) -> std::result::Result<Self, InvalidKeystrokeError> {
Self::parse_internal(source, None)
}
pub fn parse_with_os(
source: &str,
os: OperatingSystem,
) -> std::result::Result<Self, InvalidKeystrokeError> {
Self::parse_internal(source, Some(os))
}
fn parse_internal(
source: &str,
os: Option<OperatingSystem>,
) -> std::result::Result<Self, InvalidKeystrokeError> {
let mut modifiers = Modifiers::none();
let mut key = None;
let mut key_char = None;
let mut components = source.split('-').peekable();
while let Some(component) = components.next() {
if component.eq_ignore_ascii_case("ctrl") {
modifiers.control = true;
continue;
}
if component.eq_ignore_ascii_case("alt") {
modifiers.alt = true;
continue;
}
if component.eq_ignore_ascii_case("shift") {
modifiers.shift = true;
continue;
}
if component.eq_ignore_ascii_case("fn") {
modifiers.function = true;
continue;
}
if component.eq_ignore_ascii_case("secondary") {
let os = os.ok_or_else(|| InvalidKeystrokeError {
keystroke: source.to_owned(),
})?;
if os == OperatingSystem::Mac {
modifiers.platform = true;
} else {
modifiers.control = true;
};
continue;
}
let is_platform = component.eq_ignore_ascii_case("cmd")
|| component.eq_ignore_ascii_case("super")
|| component.eq_ignore_ascii_case("win");
if is_platform {
modifiers.platform = true;
continue;
}
let mut key_str = component.to_string();
if let Some(next) = components.peek() {
if next.is_empty() && source.ends_with('-') {
key = Some(String::from("-"));
break;
} else if next.len() > 1 && next.starts_with('>') {
key = Some(key_str);
key_char = Some(String::from(&next[1..]));
components.next();
} else {
return Err(InvalidKeystrokeError {
keystroke: source.to_owned(),
});
}
continue;
}
if component.len() == 1 && component.as_bytes()[0].is_ascii_uppercase() {
modifiers.shift = true;
key_str.make_ascii_lowercase();
} else {
key_str.make_ascii_lowercase()
}
key = Some(key_str);
}
key = key.or_else(|| {
use std::mem;
if mem::take(&mut modifiers.shift) {
Some("shift".to_string())
} else if mem::take(&mut modifiers.control) {
Some("control".to_string())
} else if mem::take(&mut modifiers.alt) {
Some("alt".to_string())
} else if mem::take(&mut modifiers.platform) {
Some("platform".to_string())
} else if mem::take(&mut modifiers.function) {
Some("function".to_string())
} else {
None
}
});
let key = key.ok_or_else(|| InvalidKeystrokeError {
keystroke: source.to_owned(),
})?;
Ok(Keystroke {
modifiers,
key,
key_char,
})
}
pub fn unparse(&self, os: OperatingSystem) -> String {
unparse(&self.modifiers, &self.key, os)
}
pub fn display(&self, os: OperatingSystem) -> KeystrokeDisplay<'_> {
KeystrokeDisplay {
modifiers: &self.modifiers,
key: &self.key,
os,
}
}
pub fn is_ime_in_progress(&self) -> bool {
self.key_char.is_none()
&& (is_printable_key(&self.key) || self.key.is_empty())
&& !(self.modifiers.platform
|| self.modifiers.control
|| self.modifiers.function
|| self.modifiers.alt)
}
pub fn with_simulated_ime(mut self) -> Self {
if self.key_char.is_none()
&& !self.modifiers.platform
&& !self.modifiers.control
&& !self.modifiers.function
&& !self.modifiers.alt
{
self.key_char = match self.key.as_str() {
"space" => Some(" ".into()),
"tab" => Some("\t".into()),
"enter" => Some("\n".into()),
key if !is_printable_key(key) || key.is_empty() => None,
key => {
if self.modifiers.shift {
Some(key.to_uppercase())
} else {
Some(key.into())
}
}
}
}
self
}
}
impl KeybindingKeystroke {
pub fn display(&self, os: OperatingSystem) -> KeystrokeDisplay<'_> {
KeystrokeDisplay {
modifiers: self.modifiers(),
key: self.key(),
os,
}
}
#[cfg(target_os = "windows")]
#[expect(missing_docs)]
pub fn new(inner: Keystroke, display_modifiers: Modifiers, display_key: String) -> Self {
KeybindingKeystroke {
inner,
display_modifiers,
display_key,
}
}
pub fn new_with_mapper(
inner: Keystroke,
use_key_equivalents: bool,
keyboard_mapper: &dyn PlatformKeyboardMapper,
) -> Self {
keyboard_mapper.map_key_equivalent(inner, use_key_equivalents)
}
pub fn from_keystroke(keystroke: Keystroke) -> Self {
#[cfg(target_os = "windows")]
{
let key = keystroke.key.clone();
let modifiers = keystroke.modifiers;
KeybindingKeystroke {
inner: keystroke,
display_modifiers: modifiers,
display_key: key,
}
}
#[cfg(not(target_os = "windows"))]
{
KeybindingKeystroke { inner: keystroke }
}
}
pub fn inner(&self) -> &Keystroke {
&self.inner
}
pub fn modifiers(&self) -> &Modifiers {
#[cfg(target_os = "windows")]
{
&self.display_modifiers
}
#[cfg(not(target_os = "windows"))]
{
&self.inner.modifiers
}
}
pub fn key(&self) -> &str {
#[cfg(target_os = "windows")]
{
&self.display_key
}
#[cfg(not(target_os = "windows"))]
{
&self.inner.key
}
}
pub fn set_modifiers(&mut self, modifiers: Modifiers) {
self.inner.modifiers = modifiers;
#[cfg(target_os = "windows")]
{
self.display_modifiers = modifiers;
}
}
pub fn set_key(&mut self, key: String) {
#[cfg(target_os = "windows")]
{
self.display_key = key.clone();
}
self.inner.key = key;
}
pub fn unparse(&self, os: OperatingSystem) -> String {
#[cfg(target_os = "windows")]
{
unparse(&self.display_modifiers, &self.display_key, os)
}
#[cfg(not(target_os = "windows"))]
{
unparse(&self.inner.modifiers, &self.inner.key, os)
}
}
pub fn remove_key_char(&mut self) {
self.inner.key_char = None;
}
}
fn is_printable_key(key: &str) -> bool {
!matches!(
key,
"f1" | "f2"
| "f3"
| "f4"
| "f5"
| "f6"
| "f7"
| "f8"
| "f9"
| "f10"
| "f11"
| "f12"
| "f13"
| "f14"
| "f15"
| "f16"
| "f17"
| "f18"
| "f19"
| "f20"
| "f21"
| "f22"
| "f23"
| "f24"
| "f25"
| "f26"
| "f27"
| "f28"
| "f29"
| "f30"
| "f31"
| "f32"
| "f33"
| "f34"
| "f35"
| "backspace"
| "delete"
| "left"
| "right"
| "up"
| "down"
| "pageup"
| "pagedown"
| "insert"
| "home"
| "end"
| "back"
| "forward"
| "escape"
)
}
pub struct KeystrokeDisplay<'a> {
modifiers: &'a Modifiers,
key: &'a str,
os: OperatingSystem,
}
impl std::fmt::Display for KeystrokeDisplay<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
display_modifiers(self.modifiers, self.os, f)?;
display_key(self.key, self.os, f)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize, Hash, JsonSchema)]
pub struct Modifiers {
#[serde(default)]
pub control: bool,
#[serde(default)]
pub alt: bool,
#[serde(default)]
pub shift: bool,
#[serde(default)]
pub platform: bool,
#[serde(default)]
pub function: bool,
}
impl Modifiers {
pub fn modified(&self) -> bool {
self.control || self.alt || self.shift || self.platform || self.function
}
pub fn secondary(&self, cx: &crate::App) -> bool {
if cx.platform_info().is_mac() {
self.platform
} else {
self.control
}
}
pub fn number_of_modifiers(&self) -> u8 {
self.control as u8
+ self.alt as u8
+ self.shift as u8
+ self.platform as u8
+ self.function as u8
}
pub fn none() -> Modifiers {
Default::default()
}
pub fn command() -> Modifiers {
Modifiers {
platform: true,
..Default::default()
}
}
pub fn secondary_key(cx: &crate::App) -> Modifiers {
if cx.platform_info().is_mac() {
Modifiers {
platform: true,
..Default::default()
}
} else {
Modifiers {
control: true,
..Default::default()
}
}
}
pub fn windows() -> Modifiers {
Modifiers {
platform: true,
..Default::default()
}
}
pub fn super_key() -> Modifiers {
Modifiers {
platform: true,
..Default::default()
}
}
pub fn control() -> Modifiers {
Modifiers {
control: true,
..Default::default()
}
}
pub fn alt() -> Modifiers {
Modifiers {
alt: true,
..Default::default()
}
}
pub fn shift() -> Modifiers {
Modifiers {
shift: true,
..Default::default()
}
}
pub fn function() -> Modifiers {
Modifiers {
function: true,
..Default::default()
}
}
pub fn command_shift() -> Modifiers {
Modifiers {
shift: true,
platform: true,
..Default::default()
}
}
pub fn control_shift() -> Modifiers {
Modifiers {
shift: true,
control: true,
..Default::default()
}
}
pub fn is_subset_of(&self, other: &Modifiers) -> bool {
(*other & *self) == *self
}
}
impl std::ops::BitOr for Modifiers {
type Output = Self;
fn bitor(mut self, other: Self) -> Self::Output {
self |= other;
self
}
}
impl std::ops::BitOrAssign for Modifiers {
fn bitor_assign(&mut self, other: Self) {
self.control |= other.control;
self.alt |= other.alt;
self.shift |= other.shift;
self.platform |= other.platform;
self.function |= other.function;
}
}
impl std::ops::BitXor for Modifiers {
type Output = Self;
fn bitxor(mut self, rhs: Self) -> Self::Output {
self ^= rhs;
self
}
}
impl std::ops::BitXorAssign for Modifiers {
fn bitxor_assign(&mut self, other: Self) {
self.control ^= other.control;
self.alt ^= other.alt;
self.shift ^= other.shift;
self.platform ^= other.platform;
self.function ^= other.function;
}
}
impl std::ops::BitAnd for Modifiers {
type Output = Self;
fn bitand(mut self, rhs: Self) -> Self::Output {
self &= rhs;
self
}
}
impl std::ops::BitAndAssign for Modifiers {
fn bitand_assign(&mut self, other: Self) {
self.control &= other.control;
self.alt &= other.alt;
self.shift &= other.shift;
self.platform &= other.platform;
self.function &= other.function;
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize, Hash, JsonSchema)]
pub struct Capslock {
#[serde(default)]
pub on: bool,
}
impl AsKeystroke for Keystroke {
fn as_keystroke(&self) -> &Keystroke {
self
}
}
impl AsKeystroke for KeybindingKeystroke {
fn as_keystroke(&self) -> &Keystroke {
&self.inner
}
}
impl<T: AsKeystroke + ?Sized> AsKeystroke for &T {
fn as_keystroke(&self) -> &Keystroke {
(**self).as_keystroke()
}
}
fn display_modifiers(
modifiers: &Modifiers,
os: OperatingSystem,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
if modifiers.control {
f.write_str(if os == OperatingSystem::Mac {
"^"
} else {
"ctrl-"
})?;
}
if modifiers.alt {
f.write_str(if os == OperatingSystem::Mac {
"⌥"
} else {
"alt-"
})?;
}
if modifiers.platform {
f.write_str(match os {
OperatingSystem::Mac => "⌘",
OperatingSystem::Windows => "⊞",
OperatingSystem::Linux | OperatingSystem::FreeBsd => "❖",
OperatingSystem::Ios | OperatingSystem::Android | OperatingSystem::Unknown => "cmd-",
})?;
}
if modifiers.shift {
f.write_str(if os == OperatingSystem::Mac {
"⇧"
} else {
"shift-"
})?;
}
Ok(())
}
fn display_key(
key: &str,
os: OperatingSystem,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
let key = match key {
"backspace" if os == OperatingSystem::Mac => '⌫',
"up" if os == OperatingSystem::Mac => '↑',
"down" if os == OperatingSystem::Mac => '↓',
"left" if os == OperatingSystem::Mac => '←',
"right" if os == OperatingSystem::Mac => '→',
"tab" if os == OperatingSystem::Mac => '⇥',
"escape" if os == OperatingSystem::Mac => '⎋',
"shift" if os == OperatingSystem::Mac => '⇧',
"control" if os == OperatingSystem::Mac => '⌃',
"alt" if os == OperatingSystem::Mac => '⌥',
"platform" if os == OperatingSystem::Mac => '⌘',
key if key.len() == 1 => key.chars().next().unwrap().to_ascii_uppercase(),
key => return f.write_str(key),
};
f.write_char(key)
}
#[inline]
fn unparse(modifiers: &Modifiers, key: &str, os: OperatingSystem) -> String {
let mut result = String::new();
if modifiers.function {
result.push_str("fn-");
}
if modifiers.control {
result.push_str("ctrl-");
}
if modifiers.alt {
result.push_str("alt-");
}
if modifiers.platform {
result.push_str(match os {
OperatingSystem::Windows => "win-",
OperatingSystem::Linux
| OperatingSystem::FreeBsd
| OperatingSystem::Android
| OperatingSystem::Unknown => "super-",
OperatingSystem::Mac | OperatingSystem::Ios => "cmd-",
});
}
if modifiers.shift {
result.push_str("shift-");
}
result.push_str(&key);
result
}