aura-gui 0.2.2

UI nativa acelerada por GPU para escritorio, Android e iOS, en un solo crate: estilo tipo Tailwind, componentes, sombras, animaciones y agentes de IA. Reposo a cero. Licencia AGPL-3.0 o comercial.
//! Traducción de eventos de winit a los tipos independientes de plataforma de Aura.

use crate::core::{CursorStyle, Modifiers, MouseButton};
use winit::event::{KeyEvent, MouseButton as WinitButton};
use winit::keyboard::{Key, ModifiersState, NamedKey};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use winit::platform::modifier_supplement::KeyEventExtModifierSupplement;
use winit::window::CursorIcon;

pub fn modifiers(state: ModifiersState) -> Modifiers {
    Modifiers {
        shift: state.shift_key(),
        control: state.control_key(),
        alt: state.alt_key(),
        command: state.super_key(),
    }
}

pub fn mouse_button(button: WinitButton) -> Option<MouseButton> {
    match button {
        WinitButton::Left => Some(MouseButton::Left),
        WinitButton::Right => Some(MouseButton::Right),
        WinitButton::Middle => Some(MouseButton::Middle),
        _ => None,
    }
}

/// `mod` es Cmd en macOS y Ctrl en el resto, para que los atajos se escriban una vez.
fn primary_pressed(state: ModifiersState) -> bool {
    if cfg!(target_os = "macos") { state.super_key() } else { state.control_key() }
}

fn key_name(key: &Key) -> Option<String> {
    let name = match key {
        Key::Named(named) => match named {
            NamedKey::ArrowLeft => "left",
            NamedKey::ArrowRight => "right",
            NamedKey::ArrowUp => "up",
            NamedKey::ArrowDown => "down",
            NamedKey::Home => "home",
            NamedKey::End => "end",
            NamedKey::PageUp => "pageup",
            NamedKey::PageDown => "pagedown",
            NamedKey::Enter => "enter",
            NamedKey::Backspace => "backspace",
            NamedKey::Delete => "delete",
            NamedKey::Tab => "tab",
            NamedKey::Escape => "escape",
            NamedKey::Space => "space",
            NamedKey::Insert => "insert",
            NamedKey::F1 => "f1",
            NamedKey::F2 => "f2",
            NamedKey::F3 => "f3",
            NamedKey::F4 => "f4",
            NamedKey::F5 => "f5",
            NamedKey::F6 => "f6",
            NamedKey::F7 => "f7",
            NamedKey::F8 => "f8",
            NamedKey::F9 => "f9",
            NamedKey::F10 => "f10",
            NamedKey::F11 => "f11",
            NamedKey::F12 => "f12",
            _ => return None,
        }
        .to_owned(),
        Key::Character(text) => text.to_lowercase(),
        _ => return None,
    };
    Some(name)
}

/// Texto de la pulsación como cadena de atajo: `mod-shift-p`, `alt-left`, `enter`.
pub fn keystroke(event: &KeyEvent, state: ModifiersState) -> Option<String> {
    let mut parts = String::new();
    let primary = primary_pressed(state);
    if primary {
        parts.push_str("mod-");
    }
    if state.control_key() && !primary {
        parts.push_str("ctrl-");
    }
    if state.alt_key() {
        parts.push_str("alt-");
    }
    if state.shift_key() {
        parts.push_str("shift-");
    }
    // Sin modificadores: con Shift, "]" seguiría siendo "]" y no "}". Los teclados de Android e iOS no lo dan.
    #[cfg(not(any(target_os = "android", target_os = "ios")))]
    let key = event.key_without_modifiers();
    #[cfg(any(target_os = "android", target_os = "ios"))]
    let key = event.logical_key.clone();
    parts.push_str(&key_name(&key)?);
    Some(parts)
}

pub fn cursor_icon(cursor: CursorStyle) -> CursorIcon {
    match cursor {
        CursorStyle::Arrow => CursorIcon::Default,
        CursorStyle::PointingHand => CursorIcon::Pointer,
        CursorStyle::IBeam => CursorIcon::Text,
        CursorStyle::ResizeColumn => CursorIcon::ColResize,
        CursorStyle::ResizeRow => CursorIcon::RowResize,
        CursorStyle::Grabbing => CursorIcon::Grabbing,
        CursorStyle::NotAllowed => CursorIcon::NotAllowed,
    }
}