-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Support for Key Modifiers to the Hotkeys (#582)
This adds support for key modifiers to the `livesplit-hotkey` crate. Instead of specifying a `KeyCode`, you now specify a `Hotkey` which consists of a `KeyCode` and a set of `Modifiers`. All the implementations support modifiers. However while `wasm-web` and `macOS` natively provide the state of the modifiers to us, the other platforms manually track their state.
- Loading branch information
Showing
16 changed files
with
880 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use core::{fmt, str::FromStr}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{KeyCode, Modifiers}; | ||
|
||
/// A hotkey is a combination of a key code and a set of modifiers. | ||
#[derive(Eq, PartialEq, Hash, Copy, Clone)] | ||
pub struct Hotkey { | ||
/// The key code of the hotkey. | ||
pub key_code: KeyCode, | ||
/// The modifiers of the hotkey. | ||
pub modifiers: Modifiers, | ||
} | ||
|
||
impl fmt::Debug for Hotkey { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Display::fmt(self, f) | ||
} | ||
} | ||
|
||
impl fmt::Display for Hotkey { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
if self.modifiers.is_empty() { | ||
f.write_str(self.key_code.name()) | ||
} else { | ||
write!(f, "{} + {}", self.modifiers, self.key_code.name()) | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for Hotkey { | ||
type Err = (); | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
if let Some((modifiers, key_code)) = s.rsplit_once('+') { | ||
let modifiers = modifiers.trim_end().parse()?; | ||
let key_code = key_code.trim_start().parse()?; | ||
Ok(Self { | ||
key_code, | ||
modifiers, | ||
}) | ||
} else { | ||
let key_code = s.parse()?; | ||
Ok(Self { | ||
key_code, | ||
modifiers: Modifiers::empty(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for Hotkey { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
if self.modifiers.is_empty() { | ||
self.key_code.serialize(serializer) | ||
} else { | ||
serializer.collect_str(self) | ||
} | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Hotkey { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
deserializer.deserialize_str(HotkeyVisitor) | ||
} | ||
} | ||
|
||
struct HotkeyVisitor; | ||
|
||
impl<'de> serde::de::Visitor<'de> for HotkeyVisitor { | ||
type Value = Hotkey; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
formatter.write_str("a valid hotkey") | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Hotkey::from_str(v).map_err(|()| serde::de::Error::custom("invalid hotkey")) | ||
} | ||
} | ||
|
||
impl From<KeyCode> for Hotkey { | ||
fn from(key_code: KeyCode) -> Self { | ||
Self { | ||
key_code, | ||
modifiers: Modifiers::empty(), | ||
} | ||
} | ||
} |
Oops, something went wrong.