summaryrefslogtreecommitdiffstats
path: root/winit/src/conversion.rs
diff options
context:
space:
mode:
Diffstat (limited to 'winit/src/conversion.rs')
-rw-r--r--winit/src/conversion.rs62
1 files changed, 52 insertions, 10 deletions
diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs
index 6102b4b3..f073c474 100644
--- a/winit/src/conversion.rs
+++ b/winit/src/conversion.rs
@@ -2,10 +2,11 @@
//!
//! [`winit`]: https://github.com/rust-windowing/winit
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
-use crate::{
- keyboard::{self, KeyCode, Modifiers},
- mouse, window, Event, Mode, Point,
-};
+use crate::keyboard;
+use crate::mouse;
+use crate::touch;
+use crate::window;
+use crate::{Event, Mode, Point};
/// Converts a winit window event into an iced event.
pub fn window_event(
@@ -36,8 +37,7 @@ pub fn window_event(
let position = position.to_logical::<f64>(scale_factor);
Some(Event::Mouse(mouse::Event::CursorMoved {
- x: position.x as f32,
- y: position.y as f32,
+ position: Point::new(position.x as f32, position.y as f32),
}))
}
WindowEvent::CursorEntered { .. } => {
@@ -118,6 +118,9 @@ pub fn window_event(
WindowEvent::HoveredFileCancelled => {
Some(Event::Window(window::Event::FilesHoveredLeft))
}
+ WindowEvent::Touch(touch) => {
+ Some(Event::Touch(touch_event(*touch, scale_factor)))
+ }
_ => None,
}
}
@@ -170,7 +173,9 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
winit::event::MouseButton::Left => mouse::Button::Left,
winit::event::MouseButton::Right => mouse::Button::Right,
winit::event::MouseButton::Middle => mouse::Button::Middle,
- winit::event::MouseButton::Other(other) => mouse::Button::Other(other),
+ winit::event::MouseButton::Other(other) => {
+ mouse::Button::Other(other as u8)
+ }
}
}
@@ -179,8 +184,10 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
-pub fn modifiers(modifiers: winit::event::ModifiersState) -> Modifiers {
- Modifiers {
+pub fn modifiers(
+ modifiers: winit::event::ModifiersState,
+) -> keyboard::Modifiers {
+ keyboard::Modifiers {
shift: modifiers.shift(),
control: modifiers.ctrl(),
alt: modifiers.alt(),
@@ -198,11 +205,46 @@ pub fn cursor_position(
Point::new(logical_position.x, logical_position.y)
}
+/// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event.
+///
+/// [`winit`]: https://github.com/rust-windowing/winit
+/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
+pub fn touch_event(
+ touch: winit::event::Touch,
+ scale_factor: f64,
+) -> touch::Event {
+ let id = touch::Finger(touch.id);
+ let position = {
+ let location = touch.location.to_logical::<f64>(scale_factor);
+
+ Point::new(location.x as f32, location.y as f32)
+ };
+
+ match touch.phase {
+ winit::event::TouchPhase::Started => {
+ touch::Event::FingerPressed { id, position }
+ }
+ winit::event::TouchPhase::Moved => {
+ touch::Event::FingerMoved { id, position }
+ }
+ winit::event::TouchPhase::Ended => {
+ touch::Event::FingerLifted { id, position }
+ }
+ winit::event::TouchPhase::Cancelled => {
+ touch::Event::FingerLost { id, position }
+ }
+ }
+}
+
/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
-pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
+pub fn key_code(
+ virtual_keycode: winit::event::VirtualKeyCode,
+) -> keyboard::KeyCode {
+ use keyboard::KeyCode;
+
match virtual_keycode {
winit::event::VirtualKeyCode::Key1 => KeyCode::Key1,
winit::event::VirtualKeyCode::Key2 => KeyCode::Key2,