summaryrefslogtreecommitdiffstats
path: root/winit/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-09 05:23:21 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-02-09 05:23:21 +0100
commit3efede26627121138316e8375a594979c92a9a43 (patch)
treed79bb51ad904e9fc01718d237c665ae64a0fd3c7 /winit/src
parent9a73c3a88d92262b4e59c1f061b1c56e533e2b0b (diff)
downloadiced-3efede26627121138316e8375a594979c92a9a43.tar.gz
iced-3efede26627121138316e8375a594979c92a9a43.tar.bz2
iced-3efede26627121138316e8375a594979c92a9a43.zip
Add `conversion::window_event` to `iced_winit`
Diffstat (limited to 'winit/src')
-rw-r--r--winit/src/conversion.rs92
1 files changed, 90 insertions, 2 deletions
diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs
index af0c4c9f..b6a0b64b 100644
--- a/winit/src/conversion.rs
+++ b/winit/src/conversion.rs
@@ -4,12 +4,90 @@
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
use crate::{
input::{
- keyboard::{KeyCode, ModifiersState},
+ keyboard::{self, KeyCode, ModifiersState},
mouse, ButtonState,
},
- Mode, MouseCursor,
+ window, Event, Mode, MouseCursor,
};
+/// Converts a winit window event into an iced event.
+pub fn window_event(
+ event: winit::event::WindowEvent<'_>,
+ scale_factor: f64,
+ modifiers: winit::event::ModifiersState,
+) -> Option<Event> {
+ use winit::event::WindowEvent;
+
+ match event {
+ WindowEvent::Resized(new_size) => {
+ let logical_size = new_size.to_logical(scale_factor);
+
+ Some(Event::Window(window::Event::Resized {
+ width: logical_size.width,
+ height: logical_size.height,
+ }))
+ }
+ WindowEvent::CursorMoved { position, .. } => {
+ let position = position.to_logical::<f64>(scale_factor);
+
+ Some(Event::Mouse(mouse::Event::CursorMoved {
+ x: position.x as f32,
+ y: position.y as f32,
+ }))
+ }
+ WindowEvent::MouseInput { button, state, .. } => {
+ Some(Event::Mouse(mouse::Event::Input {
+ button: mouse_button(button),
+ state: button_state(state),
+ }))
+ }
+ WindowEvent::MouseWheel { delta, .. } => match delta {
+ winit::event::MouseScrollDelta::LineDelta(delta_x, delta_y) => {
+ Some(Event::Mouse(mouse::Event::WheelScrolled {
+ delta: mouse::ScrollDelta::Lines {
+ x: delta_x,
+ y: delta_y,
+ },
+ }))
+ }
+ winit::event::MouseScrollDelta::PixelDelta(position) => {
+ Some(Event::Mouse(mouse::Event::WheelScrolled {
+ delta: mouse::ScrollDelta::Pixels {
+ x: position.x as f32,
+ y: position.y as f32,
+ },
+ }))
+ }
+ },
+ WindowEvent::ReceivedCharacter(c) if !is_private_use_character(c) => {
+ Some(Event::Keyboard(keyboard::Event::CharacterReceived(c)))
+ }
+ WindowEvent::KeyboardInput {
+ input:
+ winit::event::KeyboardInput {
+ virtual_keycode: Some(virtual_keycode),
+ state,
+ ..
+ },
+ ..
+ } => Some(Event::Keyboard(keyboard::Event::Input {
+ key_code: key_code(virtual_keycode),
+ state: button_state(state),
+ modifiers: modifiers_state(modifiers),
+ })),
+ WindowEvent::HoveredFile(path) => {
+ Some(Event::Window(window::Event::FileHovered(path)))
+ }
+ WindowEvent::DroppedFile(path) => {
+ Some(Event::Window(window::Event::FileDropped(path)))
+ }
+ WindowEvent::HoveredFileCancelled => {
+ Some(Event::Window(window::Event::FilesHoveredLeft))
+ }
+ _ => None,
+ }
+}
+
/// Converts a [`Mode`] to a [`winit`] fullscreen mode.
///
/// [`Mode`]:
@@ -254,3 +332,13 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
winit::event::VirtualKeyCode::Cut => KeyCode::Cut,
}
}
+
+// As defined in: http://www.unicode.org/faq/private_use.html
+pub(crate) fn is_private_use_character(c: char) -> bool {
+ match c {
+ '\u{E000}'..='\u{F8FF}'
+ | '\u{F0000}'..='\u{FFFFD}'
+ | '\u{100000}'..='\u{10FFFD}' => true,
+ _ => false,
+ }
+}