tilo-presentation 0.5.2

Native presentation feedback, input provenance and clock observations behind a portable host boundary.
Documentation
//! The key an X11 or Wayland keysym names.
//!
//! A host that reads the keyboard itself (X11, Wayland, a KMS session through
//! libinput and xkbcommon) delivers a press as a keysym; the vocabulary is
//! winit's own, and a build that runs no such host has no keysyms.

use winit::keyboard::{Key, KeyLocation};

/// The modifier keys held with a press, as an engine and the toolkit both read them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[allow(
    clippy::struct_excessive_bools,
    reason = "one flag per physical modifier key, mirroring winit's own \
              modifiers state; a browser app constructs this by field name \
              across the crate boundary, so reshaping it into flags is out \
              of scope for a lint fix"
)]
pub struct KeyModifiers {
    pub ctrl: bool,
    pub shift: bool,
    pub alt: bool,
    pub meta: bool,
}

/// The key `keysym` names and where on the keyboard it sits; `None` where
/// this build has no X11 or Wayland host, so no keysym names anything here.
///
/// A keysym naming no key winit knows is [`Key::Unidentified`]: the caller
/// takes the key's text instead.
#[cfg(target_os = "linux")]
#[must_use]
pub fn keysym_key(keysym: u32) -> Option<(Key, KeyLocation)> {
    Some(winit::platform::scancode::keysym_to_key(keysym))
}

#[cfg(not(target_os = "linux"))]
#[must_use]
pub const fn keysym_key(_keysym: u32) -> Option<(Key, KeyLocation)> {
    None
}

#[cfg(all(test, target_os = "linux"))]
mod tests {
    use super::*;
    use winit::keyboard::NamedKey;

    #[test]
    fn a_named_keysym_names_the_key_winit_names() {
        let (key, _) = keysym_key(0xff0d).expect("no host lacks keysyms here");
        assert_eq!(key, Key::Named(NamedKey::Enter));
    }

    #[test]
    fn a_typed_character_is_no_named_key() {
        let (key, _) = keysym_key(u32::from('с')).expect("no host lacks keysyms here");
        assert!(matches!(key, Key::Unidentified(_)), "{key:?}");
    }
}