Skip to content

Commit

Permalink
Renames as requested in review
Browse files Browse the repository at this point in the history
The "modifiers" field of KeyEvent becomes "mods", and there are
deprecated aliases for KeyCode and KeyModifiers, to help with migration.
  • Loading branch information
raphlinus committed Jun 29, 2020
1 parent 536da90 commit a3aa5bf
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion druid-shell/src/hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl HotKey {
/// [`KeyboardEvent`]: keyboard_types::KeyEvent
pub fn matches(&self, event: impl Borrow<KeyEvent>) -> bool {
let event = event.borrow();
self.mods == event.modifiers && self.key == event.key
self.mods == event.mods && self.key == event.key
}
}

Expand Down
8 changes: 5 additions & 3 deletions druid-shell/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, N

pub use keyboard_types::{Code, KeyState, Location};

/// The meaning (mapped value) of a keypress.
pub type KbKey = keyboard_types::Key;

/// Information about a keyboard event.
Expand All @@ -31,6 +32,7 @@ pub type KbKey = keyboard_types::Key;
/// field because that is already implicit in the event.
///
/// [`KeyboardEvent`]: keyboard_types::KeyboardEvent
#[non_exhaustive]
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct KeyEvent {
/// Whether the key is pressed or released.
Expand All @@ -42,7 +44,7 @@ pub struct KeyEvent {
/// Location for keys with multiple instances on common keyboards.
pub location: Location,
/// Flags for pressed modifier keys.
pub modifiers: Modifiers,
pub mods: Modifiers,
/// True if the key is currently auto-repeated.
pub repeat: bool,
/// Events with this flag should be ignored in a text editor
Expand Down Expand Up @@ -76,14 +78,14 @@ impl KeyEvent {
#[doc(hidden)]
/// Create a key event for testing purposes.
pub fn for_test(mods: impl Into<Modifiers>, key: impl IntoKey) -> KeyEvent {
let modifiers = mods.into();
let mods = mods.into();
let key = key.into_key();
KeyEvent {
key,
code: Code::Unidentified,
location: Location::Standard,
state: KeyState::Down,
modifiers,
mods,
is_composing: false,
repeat: false,
}
Expand Down
4 changes: 2 additions & 2 deletions druid-shell/src/platform/gtk/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ fn make_key_event(key: &EventKey, repeat: bool, state: KeyState) -> KeyEvent {
let keycode = hardware_keycode_to_keyval(hardware_keycode).unwrap_or(keyval);

let text = gdk::keyval_to_unicode(keyval);
let modifiers = get_modifiers(key.get_state());
let mods = get_modifiers(key.get_state());
let key = keycodes::raw_key_to_key(keyval).unwrap_or_else(|| {
if let Some(c) = text {
if c >= ' ' && c != '\x7f' {
Expand All @@ -878,7 +878,7 @@ fn make_key_event(key: &EventKey, repeat: bool, state: KeyState) -> KeyEvent {
key,
code,
location,
modifiers,
mods,
repeat,
is_composing,
state,
Expand Down
4 changes: 2 additions & 2 deletions druid-shell/src/platform/mac/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl KeyboardState {
let code = key_code_to_code(key_code);
let location = shared::code_to_location(code);
let raw_mods = event.modifierFlags();
let modifiers = make_modifiers(raw_mods);
let mods = make_modifiers(raw_mods);
let state = match event_type {
NSEventType::NSKeyDown => KeyState::Down,
NSEventType::NSKeyUp => KeyState::Up,
Expand Down Expand Up @@ -318,7 +318,7 @@ impl KeyboardState {
code,
key,
location,
modifiers,
mods,
state,
is_composing,
repeat,
Expand Down
4 changes: 2 additions & 2 deletions druid-shell/src/platform/web/keycodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ use crate::keyboard::{Code, KbKey, KeyEvent, KeyState, Location, Modifiers};
/// Convert a web-sys KeyboardEvent into a keyboard-types one.
pub(crate) fn convert_keyboard_event(
event: &KeyboardEvent,
modifiers: Modifiers,
mods: Modifiers,
state: KeyState,
) -> KeyEvent {
KeyEvent {
state,
key: event.key().parse().unwrap_or(KbKey::Unidentified),
code: convert_code(&event.code()),
location: convert_location(event.location()),
modifiers,
mods,
repeat: event.repeat(),
is_composing: event.is_composing(),
}
Expand Down
18 changes: 9 additions & 9 deletions druid-shell/src/platform/windows/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,16 +557,16 @@ impl KeyboardState {
let scan_code = ((lparam & SCAN_MASK) >> 16) as u32;
let vk = self.refine_vk(wparam as u8, scan_code);
if is_last_message(hwnd, msg, lparam) {
let modifiers = self.get_modifiers();
let mods = self.get_modifiers();
let code = scan_to_code(scan_code);
let key = vk_to_key(vk).unwrap_or_else(|| self.get_base_key(vk, modifiers));
let key = vk_to_key(vk).unwrap_or_else(|| self.get_base_key(vk, mods));
let repeat = (lparam & 0x4000_0000) != 0;
let is_extended = (lparam & 0x100_0000) != 0;
let location = vk_to_location(vk, is_extended);
let state = KeyState::Down;
let event = KeyEvent {
state,
modifiers,
mods,
code,
key,
is_composing: false,
Expand All @@ -582,16 +582,16 @@ impl KeyboardState {
WM_KEYUP | WM_SYSKEYUP => {
let scan_code = ((lparam & SCAN_MASK) >> 16) as u32;
let vk = self.refine_vk(wparam as u8, scan_code);
let modifiers = self.get_modifiers();
let mods = self.get_modifiers();
let code = scan_to_code(scan_code);
let key = vk_to_key(vk).unwrap_or_else(|| self.get_base_key(vk, modifiers));
let key = vk_to_key(vk).unwrap_or_else(|| self.get_base_key(vk, mods));
let repeat = false;
let is_extended = (lparam & 0x100_0000) != 0;
let location = vk_to_location(vk, is_extended);
let state = KeyState::Up;
let event = KeyEvent {
state,
modifiers,
mods,
code,
key,
is_composing: false,
Expand All @@ -604,12 +604,12 @@ impl KeyboardState {
//println!("char wparam {:x} lparam {:x}", wparam, lparam);
if is_last_message(hwnd, msg, lparam) {
let stash_vk = self.stash_vk.take();
let modifiers = self.get_modifiers();
let mods = self.get_modifiers();
let scan_code = ((lparam & SCAN_MASK) >> 16) as u32;
let vk = self.refine_vk(stash_vk.unwrap_or(0), scan_code);
let code = scan_to_code(scan_code);
let key = if self.stash_utf16.is_empty() && wparam < 0x20 {
vk_to_key(vk).unwrap_or_else(|| self.get_base_key(vk, modifiers))
vk_to_key(vk).unwrap_or_else(|| self.get_base_key(vk, mods))
} else {
self.stash_utf16.push(wparam as u16);
if let Ok(s) = String::from_utf16(&self.stash_utf16) {
Expand All @@ -625,7 +625,7 @@ impl KeyboardState {
let state = KeyState::Down;
let event = KeyEvent {
state,
modifiers,
mods,
code,
key,
is_composing: false,
Expand Down
6 changes: 3 additions & 3 deletions druid-shell/src/platform/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,14 +516,14 @@ impl Window {
pub fn handle_key_press(&self, key_press: &xproto::KeyPressEvent) -> Result<(), Error> {
let hw_keycode = key_press.detail;
let code = keycodes::hardware_keycode_to_code(hw_keycode);
let modifiers = key_mods(key_press.state);
let key = keycodes::code_to_key(code, modifiers);
let mods = key_mods(key_press.state);
let key = keycodes::code_to_key(code, mods);
let location = keycodes::code_to_location(code);
let state = KeyState::Down;
let key_event = KeyEvent {
code,
key,
modifiers,
mods,
location,
state,
repeat: false,
Expand Down
14 changes: 14 additions & 0 deletions druid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,17 @@ pub use window::{Window, WindowId};
#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
pub(crate) use event::{StateCell, StateCheckFn};

/// The meaning (mapped value) of a keypress.
///
/// Note that in previous versions, the `KeyCode` field referred to the
/// physical position of the key, rather than the mapped value. In most
/// cases, applications should dispatch based on the value instead. This
/// alias is provided to make that transition easy, but in any case make
/// an explicit choice whether to use meaning or physical location and
/// use the appropriate type.
#[deprecated(since = "0.7.0", note = "Use KbKey instead")]
pub type KeyCode = KbKey;

#[deprecated(since = "0.7.0", note = "Use Modifiers instead")]
pub type KeyModifiers = Modifiers;
4 changes: 2 additions & 2 deletions druid/src/text/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ impl TextInput for BasicTextInput {
/// Determine whether a keyboard event contains insertable text.
fn key_event_is_printable(event: &KeyEvent) -> bool {
if let KbKey::Character(_) = &event.key {
if event.modifiers.ctrl() || event.modifiers.meta() {
if event.mods.ctrl() || event.mods.meta() {
return false;
}
// On mac, Alt functions more like AltGr.
#[cfg(not(target_os = "macos"))]
{
if event.modifiers.alt() {
if event.mods.alt() {
return false;
}
}
Expand Down

0 comments on commit a3aa5bf

Please sign in to comment.