Revise Key and KeyCode enums

Split `Key` into clear categories, like `Named`, `Dead`, Character`, `Unidentified`
removing the `#[non_exhaustive]` from the `Key` itself.

Similar action was done for the `KeyCode`.

Fixes: #2995
Co-authored-by: Kirill Chibisov <contact@kchibisov.com>
This commit is contained in:
Diggory Hardy 2023-10-19 15:27:49 +01:00 committed by GitHub
parent b9e1e96eaa
commit acfeff5327
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 1456 additions and 1277 deletions

View file

@ -11,7 +11,7 @@ use web_sys::{
use crate::dpi::{LogicalPosition, PhysicalPosition, PhysicalSize};
use crate::error::OsError as RootOE;
use crate::event::{Force, InnerSizeWriter, MouseButton, MouseScrollDelta};
use crate::keyboard::{Key, KeyCode, KeyLocation, ModifiersState};
use crate::keyboard::{Key, KeyLocation, ModifiersState, PhysicalKey};
use crate::platform_impl::{OsError, PlatformSpecificWindowBuilderAttributes};
use crate::window::{WindowAttributes, WindowId as RootWindowId};
@ -258,7 +258,7 @@ impl Canvas {
pub fn on_keyboard_release<F>(&mut self, mut handler: F, prevent_default: bool)
where
F: 'static + FnMut(KeyCode, Key, Option<SmolStr>, KeyLocation, bool, ModifiersState),
F: 'static + FnMut(PhysicalKey, Key, Option<SmolStr>, KeyLocation, bool, ModifiersState),
{
self.on_keyboard_release =
Some(self.common.add_event("keyup", move |event: KeyboardEvent| {
@ -280,7 +280,7 @@ impl Canvas {
pub fn on_keyboard_press<F>(&mut self, mut handler: F, prevent_default: bool)
where
F: 'static + FnMut(KeyCode, Key, Option<SmolStr>, KeyLocation, bool, ModifiersState),
F: 'static + FnMut(PhysicalKey, Key, Option<SmolStr>, KeyLocation, bool, ModifiersState),
{
self.on_keyboard_press = Some(self.common.add_transient_event(
"keydown",

View file

@ -1,6 +1,6 @@
use crate::dpi::LogicalPosition;
use crate::event::{MouseButton, MouseScrollDelta};
use crate::keyboard::{Key, KeyCode, KeyLocation, ModifiersState};
use crate::keyboard::{Key, KeyLocation, ModifiersState, NamedKey, PhysicalKey};
use once_cell::unsync::OnceCell;
use smol_str::SmolStr;
@ -149,9 +149,9 @@ pub fn mouse_scroll_delta(
}
}
pub fn key_code(event: &KeyboardEvent) -> KeyCode {
pub fn key_code(event: &KeyboardEvent) -> PhysicalKey {
let code = event.code();
KeyCode::from_key_code_attribute_value(&code)
PhysicalKey::from_key_code_attribute_value(&code)
}
pub fn key(event: &KeyboardEvent) -> Key {
@ -163,9 +163,9 @@ pub fn key_text(event: &KeyboardEvent) -> Option<SmolStr> {
let key = Key::from_key_attribute_value(&key);
match &key {
Key::Character(text) => Some(text.clone()),
Key::Tab => Some(SmolStr::new("\t")),
Key::Enter => Some(SmolStr::new("\r")),
Key::Space => Some(SmolStr::new(" ")),
Key::Named(NamedKey::Tab) => Some(SmolStr::new("\t")),
Key::Named(NamedKey::Enter) => Some(SmolStr::new("\r")),
Key::Named(NamedKey::Space) => Some(SmolStr::new(" ")),
_ => None,
}
.map(SmolStr::new)