#![doc(html_root_url = "https://docs.rs/keyseq/0.8.0")]
#![cfg_attr(all(feature = "winit", feature = "bevy"),
doc = include_str!("../README.md"))]
#![cfg_attr(
not(all(feature = "winit", feature = "bevy")),
doc = "Warning: Not full documentation. Please generate doc with `--all-features` option to include README."
)]
#![forbid(missing_docs)]
use bitflags::bitflags;
use std::fmt;
#[rustfmt::skip]
#[cfg(feature = "poor")]
pub mod poor {
pub use keyseq_macros::{poor_lkey as lkey,
poor_lkeyseq as lkeyseq,
poor_pkey as pkey,
poor_pkeyseq as pkeyseq};
}
#[derive(Clone, Copy, PartialOrd, PartialEq, Eq, Hash, Ord)]
#[cfg_attr(feature = "bevy", derive(bevy_reflect::Reflect))]
pub struct Modifiers(pub u8);
bitflags! {
impl Modifiers: u8 {
const NONE = 0b00000000;
const CONTROL = 0b00000001;
const ALT = 0b00000010;
const SHIFT = 0b00000100;
const SUPER = 0b00001000;
}
}
#[doc(hidden)]
pub mod _keyseq {
pub use super::Modifiers;
}
impl fmt::Debug for Modifiers {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Modifiers(")?;
if self.is_empty() {
f.write_str("NONE")?;
} else {
fmt::Display::fmt(self, f)?;
}
f.write_str(")")
}
}
impl fmt::Display for Modifiers {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut first = true;
if self.contains(Modifiers::CONTROL) {
f.write_str("Ctrl")?;
first = false;
}
if self.contains(Modifiers::ALT) {
if !first {
f.write_str("-")?;
}
f.write_str("Alt")?;
first = false;
}
if self.contains(Modifiers::SHIFT) {
if !first {
f.write_str("-")?;
}
f.write_str("Shift")?;
first = false;
}
if self.contains(Modifiers::SUPER) {
if !first {
f.write_str("-")?;
}
f.write_str("Super")?;
}
Ok(())
}
}
impl From<u8> for Modifiers {
fn from(x: u8) -> Modifiers {
Modifiers::from_bits(x).expect("Should only have first nybble set")
}
}
#[cfg(feature = "winit")]
pub mod winit;
#[cfg(feature = "bevy")]
pub mod bevy;
#[cfg(all(feature = "poor", not(feature = "permit-plus")))]
struct test_dummy;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_modifiers() {
let mods = Modifiers(1 + 2 + 4);
assert_eq!(format!("{}", mods), "Ctrl-Alt-Shift");
}
#[cfg(all(feature = "poor", feature = "permit-plus"))]
#[test]
fn permit_plus() {
assert_eq!(poor::pkey! { Ctrl+A }, (1, "A"));
}
}