winit-core: move event

This commit is contained in:
Kirill Chibisov 2025-05-03 20:21:45 +09:00
parent 056421546a
commit 79fa4061cb
11 changed files with 46 additions and 36 deletions

View file

@ -299,10 +299,8 @@ pub use rwh_06 as raw_window_handle;
pub mod application; pub mod application;
#[cfg(any(doc, doctest, test))] #[cfg(any(doc, doctest, test))]
pub mod changelog; pub mod changelog;
pub use winit_core::cursor;
pub mod event;
pub mod event_loop; pub mod event_loop;
pub use winit_core::{error, icon, keyboard, monitor, window}; pub use winit_core::{cursor, error, event, icon, keyboard, monitor, window};
#[macro_use] #[macro_use]
mod os_error; mod os_error;
mod platform_impl; mod platform_impl;

View file

@ -330,7 +330,7 @@ pub(super) fn event_mods(event: &NSEvent) -> Modifiers {
pressed_mods.set(ModifiersKeys::LMETA, flags.contains(NX_DEVICELCMDKEYMASK)); pressed_mods.set(ModifiersKeys::LMETA, flags.contains(NX_DEVICELCMDKEYMASK));
pressed_mods.set(ModifiersKeys::RMETA, flags.contains(NX_DEVICERCMDKEYMASK)); pressed_mods.set(ModifiersKeys::RMETA, flags.contains(NX_DEVICERCMDKEYMASK));
Modifiers { state, pressed_mods } Modifiers::new(state, pressed_mods)
} }
pub(super) fn dummy_event() -> Option<Retained<NSEvent>> { pub(super) fn dummy_event() -> Option<Retained<NSEvent>> {

View file

@ -1096,7 +1096,7 @@ fn mouse_button(event: &NSEvent) -> MouseButton {
// we're getting from the operating system, which makes it // we're getting from the operating system, which makes it
// impossible to provide such events as extra in `KeyEvent`. // impossible to provide such events as extra in `KeyEvent`.
fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Retained<NSEvent> { fn replace_event(event: &NSEvent, option_as_alt: OptionAsAlt) -> Retained<NSEvent> {
let ev_mods = event_mods(event).state; let ev_mods = event_mods(event).state();
let ignore_alt_characters = match option_as_alt { let ignore_alt_characters = match option_as_alt {
OptionAsAlt::OnlyLeft if lalt_pressed(event) => true, OptionAsAlt::OnlyLeft if lalt_pressed(event) => true,
OptionAsAlt::OnlyRight if ralt_pressed(event) => true, OptionAsAlt::OnlyRight if ralt_pressed(event) => true,

View file

@ -267,7 +267,7 @@ impl EventState {
pressed_mods pressed_mods
.set(ModifiersKeys::RMETA, self.keyboard.contains(KeyboardModifierState::RMETA)); .set(ModifiersKeys::RMETA, self.keyboard.contains(KeyboardModifierState::RMETA));
Modifiers { state, pressed_mods } Modifiers::new(state, pressed_mods)
} }
} }

View file

@ -24,6 +24,7 @@ use crate::platform::web::{PollStrategy, WaitUntilStrategy};
use crate::platform_impl::platform::backend::{EventListenerHandle, SafeAreaHandle}; use crate::platform_impl::platform::backend::{EventListenerHandle, SafeAreaHandle};
use crate::platform_impl::platform::r#async::DispatchRunner; use crate::platform_impl::platform::r#async::DispatchRunner;
use crate::platform_impl::platform::window::Inner; use crate::platform_impl::platform::window::Inner;
use crate::platform_impl::web::web_sys::event::mouse_button_to_id;
use crate::window::WindowId; use crate::window::WindowId;
#[derive(Debug)] #[derive(Debug)]
@ -325,7 +326,10 @@ impl Shared {
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id, device_id,
event: DeviceEvent::Button { button: button.to_id().into(), state }, event: DeviceEvent::Button {
button: mouse_button_to_id(button).into(),
state,
},
}); });
return; return;
@ -374,7 +378,7 @@ impl Shared {
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: event::mkdid(event.pointer_id()), device_id: event::mkdid(event.pointer_id()),
event: DeviceEvent::Button { event: DeviceEvent::Button {
button: button.to_id().into(), button: mouse_button_to_id(button).into(),
state: ElementState::Pressed, state: ElementState::Pressed,
}, },
}); });
@ -393,7 +397,7 @@ impl Shared {
runner.send_event(Event::DeviceEvent { runner.send_event(Event::DeviceEvent {
device_id: event::mkdid(event.pointer_id()), device_id: event::mkdid(event.pointer_id()),
event: DeviceEvent::Button { event: DeviceEvent::Button {
button: button.to_id().into(), button: mouse_button_to_id(button).into(),
state: ElementState::Released, state: ElementState::Released,
}, },
}); });

View file

@ -68,16 +68,14 @@ pub fn mouse_button(event: &MouseEvent) -> Option<MouseButton> {
} }
} }
impl MouseButton { pub fn mouse_button_to_id(button: MouseButton) -> u16 {
pub fn to_id(self) -> u16 { match button {
match self { MouseButton::Left => 0,
MouseButton::Left => 0, MouseButton::Right => 1,
MouseButton::Right => 1, MouseButton::Middle => 2,
MouseButton::Middle => 2, MouseButton::Back => 3,
MouseButton::Back => 3, MouseButton::Forward => 4,
MouseButton::Forward => 4, MouseButton::Other(value) => value,
MouseButton::Other(value) => value,
}
} }
} }

View file

@ -10,6 +10,7 @@ use crate::dpi::PhysicalPosition;
use crate::event::{ButtonSource, DeviceId, ElementState, Force, PointerKind, PointerSource}; use crate::event::{ButtonSource, DeviceId, ElementState, Force, PointerKind, PointerSource};
use crate::keyboard::ModifiersState; use crate::keyboard::ModifiersState;
use crate::platform_impl::web::event::mkdid; use crate::platform_impl::web::event::mkdid;
use crate::platform_impl::web::web_sys::event::mouse_button_to_id;
#[allow(dead_code)] #[allow(dead_code)]
pub(super) struct PointerHandler { pub(super) struct PointerHandler {
@ -89,7 +90,7 @@ impl PointerHandler {
finger_id, finger_id,
force: Some(Force::Normalized(event.pressure().into())), force: Some(Force::Normalized(event.pressure().into())),
}, },
PointerKind::Unknown => ButtonSource::Unknown(button.to_id()), PointerKind::Unknown => ButtonSource::Unknown(mouse_button_to_id(button)),
}; };
handler( handler(
@ -142,7 +143,7 @@ impl PointerHandler {
finger_id, finger_id,
force: Some(Force::Normalized(event.pressure().into())), force: Some(Force::Normalized(event.pressure().into())),
}, },
PointerKind::Unknown => ButtonSource::Unknown(button.to_id()), PointerKind::Unknown => ButtonSource::Unknown(mouse_button_to_id(button)),
}; };
handler( handler(
@ -206,7 +207,7 @@ impl PointerHandler {
let button = match kind { let button = match kind {
PointerKind::Mouse => ButtonSource::Mouse(button), PointerKind::Mouse => ButtonSource::Mouse(button),
PointerKind::Touch(finger_id) => { PointerKind::Touch(finger_id) => {
let button_id = button.to_id(); let button_id = mouse_button_to_id(button);
if button_id != 1 { if button_id != 1 {
tracing::error!("unexpected touch button id: {button_id}"); tracing::error!("unexpected touch button id: {button_id}");

View file

@ -399,7 +399,7 @@ impl Event {
} => Event::BufferedScaleFactorChanged( } => Event::BufferedScaleFactorChanged(
window_id.into_raw() as HWND, window_id.into_raw() as HWND,
scale_factor, scale_factor,
*surface_size_writer.new_surface_size.upgrade().unwrap().lock().unwrap(), surface_size_writer.surface_size().unwrap(),
), ),
event => event, event => event,
} }

View file

@ -4,13 +4,13 @@ use std::sync::{Mutex, Weak};
#[cfg(not(web_platform))] #[cfg(not(web_platform))]
use std::time::Instant; use std::time::Instant;
use dpi::{PhysicalPosition, PhysicalSize};
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use smol_str::SmolStr; use smol_str::SmolStr;
#[cfg(web_platform)] #[cfg(web_platform)]
use web_time::Instant; use web_time::Instant;
use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::error::RequestError; use crate::error::RequestError;
use crate::event_loop::AsyncRequestSerial; use crate::event_loop::AsyncRequestSerial;
use crate::keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState}; use crate::keyboard::{self, ModifiersKeyState, ModifiersKeys, ModifiersState};
@ -563,16 +563,14 @@ impl DeviceId {
/// Convert the [`DeviceId`] into the underlying integer. /// Convert the [`DeviceId`] into the underlying integer.
/// ///
/// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic. /// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic.
#[allow(dead_code)] pub const fn into_raw(self) -> i64 {
pub(crate) const fn into_raw(self) -> i64 {
self.0 self.0
} }
/// Construct a [`DeviceId`] from the underlying integer. /// Construct a [`DeviceId`] from the underlying integer.
/// ///
/// This should only be called with integers returned from [`DeviceId::into_raw`]. /// This should only be called with integers returned from [`DeviceId::into_raw`].
#[allow(dead_code)] pub const fn from_raw(id: i64) -> Self {
pub(crate) const fn from_raw(id: i64) -> Self {
Self(id) Self(id)
} }
} }
@ -588,16 +586,14 @@ impl FingerId {
/// Convert the [`FingerId`] into the underlying integer. /// Convert the [`FingerId`] into the underlying integer.
/// ///
/// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic. /// This is useful if you need to pass the ID across an FFI boundary, or store it in an atomic.
#[allow(dead_code)] pub const fn into_raw(self) -> usize {
pub(crate) const fn into_raw(self) -> usize {
self.0 self.0
} }
/// Construct a [`FingerId`] from the underlying integer. /// Construct a [`FingerId`] from the underlying integer.
/// ///
/// This should only be called with integers returned from [`FingerId::into_raw`]. /// This should only be called with integers returned from [`FingerId::into_raw`].
#[allow(dead_code)] pub const fn from_raw(id: usize) -> Self {
pub(crate) const fn from_raw(id: usize) -> Self {
Self(id) Self(id)
} }
} }
@ -832,6 +828,11 @@ pub struct Modifiers {
} }
impl Modifiers { impl Modifiers {
/// Create a new modifiers from state and pressed mods.
pub fn new(state: ModifiersState, pressed_mods: ModifiersKeys) -> Self {
Self { state, pressed_mods }
}
/// The state of the modifiers. /// The state of the modifiers.
pub fn state(&self) -> ModifiersState { pub fn state(&self) -> ModifiersState {
self.state self.state
@ -1092,7 +1093,7 @@ pub struct SurfaceSizeWriter {
impl SurfaceSizeWriter { impl SurfaceSizeWriter {
#[cfg(not(orbital_platform))] #[cfg(not(orbital_platform))]
pub(crate) fn new(new_surface_size: Weak<Mutex<PhysicalSize<u32>>>) -> Self { pub fn new(new_surface_size: Weak<Mutex<PhysicalSize<u32>>>) -> Self {
Self { new_surface_size } Self { new_surface_size }
} }
@ -1108,6 +1109,15 @@ impl SurfaceSizeWriter {
Err(RequestError::Ignored) Err(RequestError::Ignored)
} }
} }
/// Get the currently stashed surface size.
pub fn surface_size(&self) -> Result<PhysicalSize<u32>, RequestError> {
if let Some(inner) = self.new_surface_size.upgrade() {
Ok(*inner.lock().unwrap())
} else {
Err(RequestError::Ignored)
}
}
} }
impl PartialEq for SurfaceSizeWriter { impl PartialEq for SurfaceSizeWriter {
@ -1122,7 +1132,7 @@ impl Eq for SurfaceSizeWriter {}
mod tests { mod tests {
use std::collections::{BTreeSet, HashSet}; use std::collections::{BTreeSet, HashSet};
use crate::dpi::PhysicalPosition; use dpi::PhysicalPosition;
use crate::event; use crate::event;
macro_rules! foreach_event { macro_rules! foreach_event {

View file

@ -286,8 +286,6 @@ pub struct AsyncRequestSerial {
} }
impl AsyncRequestSerial { impl AsyncRequestSerial {
// TODO(kchibisov): Remove `cfg` when the clipboard will be added.
#[allow(dead_code)]
pub fn get() -> Self { pub fn get() -> Self {
static CURRENT_SERIAL: AtomicUsize = AtomicUsize::new(0); static CURRENT_SERIAL: AtomicUsize = AtomicUsize::new(0);
// NOTE: We rely on wrap around here, while the user may just request // NOTE: We rely on wrap around here, while the user may just request

View file

@ -3,6 +3,7 @@ pub mod as_any;
pub mod cursor; pub mod cursor;
#[macro_use] #[macro_use]
pub mod error; pub mod error;
pub mod event;
pub mod event_loop; pub mod event_loop;
pub mod icon; pub mod icon;
pub mod keyboard; pub mod keyboard;