Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge KeyEventExtra into KeyEvent #4029

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/changelog/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ changelog entry.
- On X11, use bottom-right corner for IME hotspot in `Window::set_ime_cursor_area`.
- On macOS and iOS, no longer emit `ScaleFactorChanged` upon window creation.
- On macOS, no longer emit `Focused` upon window creation.
- Removed `KeyEventExtModifierSupplement`, and made the fields `text_with_all_modifiers` and
`key_without_modifiers` public on `KeyEvent` instead.

### Removed

Expand Down
41 changes: 27 additions & 14 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::error::RequestError;
use crate::event_loop::AsyncRequestSerial;
use crate::keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState};
use crate::platform_impl;
#[cfg(doc)]
use crate::window::Window;
use crate::window::{ActivationToken, Theme, WindowId};
Expand Down Expand Up @@ -764,12 +763,6 @@ pub struct KeyEvent {
/// you somehow see this in the wild, we'd like to know :)
pub physical_key: keyboard::PhysicalKey,

// Allowing `broken_intra_doc_links` for `logical_key`, because
// `key_without_modifiers` is not available on all platforms
#[cfg_attr(
not(any(windows_platform, macos_platform, x11_platform, wayland_platform)),
allow(rustdoc::broken_intra_doc_links)
)]
/// This value is affected by all modifiers except <kbd>Ctrl</kbd>.
///
/// This has two use cases:
Expand All @@ -785,7 +778,7 @@ pub struct KeyEvent {
/// - **Web:** Dead keys might be reported as the real key instead of `Dead` depending on the
/// browser/OS.
///
/// [`key_without_modifiers`]: crate::platform::modifier_supplement::KeyEventExtModifierSupplement::key_without_modifiers
/// [`key_without_modifiers`]: Self::key_without_modifiers
pub logical_key: keyboard::Key,

/// Contains the text produced by this keypress.
Expand All @@ -806,7 +799,7 @@ pub struct KeyEvent {
/// This is `None` if the current keypress cannot
/// be interpreted as text.
///
/// See also: `text_with_all_modifiers()`
/// See also [`text_with_all_modifiers`][Self::text_with_all_modifiers].
pub text: Option<SmolStr>,

/// Contains the location of this key on the keyboard.
Expand Down Expand Up @@ -862,13 +855,33 @@ pub struct KeyEvent {
/// ```
pub repeat: bool,

/// Platform-specific key event information.
/// Identical to [`text`][Self::text] but this is affected by <kbd>Ctrl</kbd>.
///
/// For example, pressing <kbd>Ctrl</kbd>+<kbd>a</kbd> produces `Some("\x01")`.
///
/// ## Platform-specific
///
/// - **Android:** Unimplemented, this field is always the same value as `text`.
/// - **iOS:** Unimplemented, this field is always the same value as `text`.
/// - **Web:** Unsupported, this field is always the same value as `text`.
Comment on lines +864 to +866
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to always set None here on these platforms, but I felt this choice was more consistent with key_without_modifiers (unless we change that to be an Option).

pub text_with_all_modifiers: Option<SmolStr>,

/// This value ignores all modifiers including, but not limited to <kbd>Shift</kbd>,
/// <kbd>Caps Lock</kbd>, and <kbd>Ctrl</kbd>. In most cases this means that the
/// unicode character in the resulting string is lowercase.
///
/// This is useful for key-bindings / shortcut key combinations.
///
/// On Windows, Linux and macOS, this type contains the key without modifiers and the text with
/// all modifiers applied.
/// In case [`logical_key`][Self::logical_key] reports [`Dead`][keyboard::Key::Dead],
/// this will still report the key as `Character` according to the current keyboard
/// layout. This value cannot be `Dead`.
///
/// ## Platform-specific
///
/// On Android, iOS, Redox and Web, this type is a no-op.
pub(crate) platform_specific: platform_impl::KeyEventExtra,
/// - **Android:** Unimplemented, this field is always the same value as `logical_key`.
/// - **iOS:** Unimplemented, this field is always the same value as `logical_key`.
/// - **Web:** Unsupported, this field is always the same value as `logical_key`.
pub key_without_modifiers: keyboard::Key,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to make this an Option<keyboard::Key>, I'm not familiar enough with it to say what would be best.

}

/// Describes keyboard modifiers event.
Expand Down
10 changes: 0 additions & 10 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,5 @@ pub mod run_on_demand;
))]
pub mod pump_events;

#[cfg(any(
windows_platform,
macos_platform,
x11_platform,
wayland_platform,
orbital_platform,
docsrs
))]
pub mod modifier_supplement;

#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform, docsrs))]
pub mod scancode;
35 changes: 0 additions & 35 deletions src/platform/modifier_supplement.rs

This file was deleted.

6 changes: 2 additions & 4 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ impl RedrawRequester {
}
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct KeyEventExtra {}

pub struct EventLoop {
pub(crate) android_app: AndroidApp,
window_target: ActiveEventLoop,
Expand Down Expand Up @@ -479,7 +476,8 @@ impl EventLoop {
location: keycodes::to_location(keycode),
repeat: key.repeat_count() > 0,
text: None,
platform_specific: KeyEventExtra {},
text_with_all_modifiers: None,
key_without_modifiers: keycodes::to_logical(key_char, keycode),
},
is_synthetic: false,
};
Expand Down
9 changes: 2 additions & 7 deletions src/platform_impl/apple/appkit/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ use crate::keyboard::{
PhysicalKey,
};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct KeyEventExtra {
pub text_with_all_modifiers: Option<SmolStr>,
pub key_without_modifiers: Key,
}

/// Ignores ALL modifiers.
pub fn get_modifierless_char(scancode: u16) -> Key {
let mut string = [0; 16];
Expand Down Expand Up @@ -172,7 +166,8 @@ pub(crate) fn create_key_event(
repeat: is_repeat,
state,
text,
platform_specific: KeyEventExtra { text_with_all_modifiers, key_without_modifiers },
text_with_all_modifiers,
key_without_modifiers,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/apple/appkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod window;
mod window_delegate;

pub(crate) use self::cursor::CustomCursor as PlatformCustomCursor;
pub(crate) use self::event::{physicalkey_to_scancode, scancode_to_physicalkey, KeyEventExtra};
pub(crate) use self::event::{physicalkey_to_scancode, scancode_to_physicalkey};
pub(crate) use self::event_loop::{
ActiveEventLoop, EventLoop, PlatformSpecificEventLoopAttributes,
};
Expand Down
3 changes: 0 additions & 3 deletions src/platform_impl/apple/uikit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ pub(crate) use crate::cursor::{
pub(crate) use crate::icon::NoIcon as PlatformIcon;
pub(crate) use crate::platform_impl::Fullscreen;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct KeyEventExtra {}

#[derive(Debug)]
pub enum OsError {}

Expand Down
11 changes: 8 additions & 3 deletions src/platform_impl/apple/uikit/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate::event::{
TouchPhase, WindowEvent,
};
use crate::keyboard::{Key, KeyCode, KeyLocation, NamedKey, NativeKeyCode, PhysicalKey};
use crate::platform_impl::KeyEventExtra;
use crate::window::WindowAttributes;

pub struct WinitViewState {
Expand Down Expand Up @@ -664,7 +663,12 @@ impl WinitView {
physical_key: PhysicalKey::Unidentified(
NativeKeyCode::Unidentified,
),
platform_specific: KeyEventExtra {},
text_with_all_modifiers: if state == ElementState::Pressed {
Some(text.clone())
} else {
None
},
key_without_modifiers: Key::Character(text.clone()),
},
is_synthetic: false,
device_id: None,
Expand All @@ -690,10 +694,11 @@ impl WinitView {
state,
logical_key: Key::Named(NamedKey::Backspace),
physical_key: PhysicalKey::Code(KeyCode::Backspace),
platform_specific: KeyEventExtra {},
repeat: false,
location: KeyLocation::Standard,
text: None,
text_with_all_modifiers: None,
key_without_modifiers: Key::Named(NamedKey::Backspace),
},
is_synthetic: false,
},
Expand Down
14 changes: 10 additions & 4 deletions src/platform_impl/linux/common/xkb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use {x11_dl::xlib_xcb::xcb_connection_t, xkbcommon_dl::x11::xkbcommon_x11_handle

use crate::event::{ElementState, KeyEvent};
use crate::keyboard::{Key, KeyLocation};
use crate::platform_impl::KeyEventExtra;
use crate::utils::Lazy;

mod compose;
Expand Down Expand Up @@ -198,9 +197,16 @@ impl KeyContext<'_> {
let (key_without_modifiers, _) = event.key_without_modifiers();
let text_with_all_modifiers = event.text_with_all_modifiers();

let platform_specific = KeyEventExtra { text_with_all_modifiers, key_without_modifiers };

KeyEvent { physical_key, logical_key, text, location, state, repeat, platform_specific }
KeyEvent {
physical_key,
logical_key,
text,
location,
state,
repeat,
text_with_all_modifiers,
key_without_modifiers,
}
}

fn keysym_to_utf8_raw(&mut self, keysym: u32) -> Option<SmolStr> {
Expand Down
9 changes: 0 additions & 9 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use std::time::Duration;
#[cfg(x11_platform)]
use std::{ffi::CStr, mem::MaybeUninit, os::raw::*, sync::Arc, sync::Mutex};

use smol_str::SmolStr;

pub(crate) use self::common::xkb::{physicalkey_to_scancode, scancode_to_physicalkey};
#[cfg(x11_platform)]
use self::x11::{XConnection, XError, XNotSupported};
Expand All @@ -23,7 +21,6 @@ use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::error::{EventLoopError, NotSupportedError};
use crate::event_loop::ActiveEventLoop;
pub(crate) use crate::icon::RgbaIcon as PlatformIcon;
use crate::keyboard::Key;
use crate::platform::pump_events::PumpStatus;
#[cfg(x11_platform)]
use crate::platform::x11::{WindowType as XWindowType, XlibErrorHook};
Expand Down Expand Up @@ -205,12 +202,6 @@ impl VideoModeHandle {
}
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct KeyEventExtra {
pub text_with_all_modifiers: Option<SmolStr>,
pub key_without_modifiers: Key,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub(crate) enum PlatformCustomCursor {
#[cfg(wayland_platform)]
Expand Down
9 changes: 3 additions & 6 deletions src/platform_impl/orbital/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use orbclient::{
use smol_str::SmolStr;

use super::{
KeyEventExtra, MonitorHandle, PlatformSpecificEventLoopAttributes, RedoxSocket, TimeSocket,
WindowProperties,
MonitorHandle, PlatformSpecificEventLoopAttributes, RedoxSocket, TimeSocket, WindowProperties,
};
use crate::application::ApplicationHandler;
use crate::error::{EventLoopError, NotSupportedError, RequestError};
Expand Down Expand Up @@ -372,10 +371,8 @@ impl EventLoop {
state: element_state(pressed),
repeat: false,
text,
platform_specific: KeyEventExtra {
key_without_modifiers,
text_with_all_modifiers,
},
key_without_modifiers,
text_with_all_modifiers,
},
is_synthetic: false,
};
Expand Down
9 changes: 0 additions & 9 deletions src/platform_impl/orbital/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
use std::num::{NonZeroU16, NonZeroU32};
use std::{fmt, str};

use smol_str::SmolStr;

pub(crate) use self::event_loop::{ActiveEventLoop, EventLoop};
use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::keyboard::Key;
mod event_loop;

pub use self::window::Window;
Expand Down Expand Up @@ -185,9 +182,3 @@ impl VideoModeHandle {
self.monitor.clone()
}
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct KeyEventExtra {
pub key_without_modifiers: Key,
pub text_with_all_modifiers: Option<SmolStr>,
}
16 changes: 9 additions & 7 deletions src/platform_impl/web/event_loop/window_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::sync::Arc;

use web_sys::Element;

use super::super::lock;
use super::super::monitor::MonitorPermissionFuture;
use super::super::{lock, KeyEventExtra};
use super::runner::EventWrapper;
use super::{backend, runner};
use crate::error::{NotSupportedError, RequestError};
Expand Down Expand Up @@ -143,12 +143,13 @@ impl ActiveEventLoop {
device_id: None,
event: KeyEvent {
physical_key,
logical_key,
text,
logical_key: logical_key.clone(),
text: text.clone(),
location,
state: ElementState::Pressed,
repeat,
platform_specific: KeyEventExtra,
text_with_all_modifiers: text,
key_without_modifiers: logical_key,
},
is_synthetic: false,
},
Expand Down Expand Up @@ -177,12 +178,13 @@ impl ActiveEventLoop {
device_id: None,
event: KeyEvent {
physical_key,
logical_key,
text,
logical_key: logical_key.clone(),
text: text.clone(),
location,
state: ElementState::Released,
repeat,
platform_specific: KeyEventExtra,
text_with_all_modifiers: text,
key_without_modifiers: logical_key,
},
is_synthetic: false,
},
Expand Down
3 changes: 0 additions & 3 deletions src/platform_impl/web/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ use smol_str::SmolStr;

use crate::keyboard::{Key, KeyCode, NamedKey, NativeKey, NativeKeyCode, PhysicalKey};

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub(crate) struct KeyEventExtra;

impl Key {
pub(crate) fn from_key_attribute_value(kav: &str) -> Self {
Key::Named(match kav {
Expand Down
Loading
Loading