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.rs134
1 files changed, 128 insertions, 6 deletions
diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs
index 03d583fb..b6a0b64b 100644
--- a/winit/src/conversion.rs
+++ b/winit/src/conversion.rs
@@ -3,11 +3,107 @@
//! [`winit`]: https://github.com/rust-windowing/winit
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
use crate::{
- input::{keyboard::KeyCode, mouse, ButtonState},
- MouseCursor,
+ input::{
+ keyboard::{self, KeyCode, ModifiersState},
+ mouse, ButtonState,
+ },
+ window, Event, Mode, MouseCursor,
};
-/// Convert a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon.
+/// 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`]:
+pub fn fullscreen(
+ monitor: winit::monitor::MonitorHandle,
+ mode: Mode,
+) -> Option<winit::window::Fullscreen> {
+ match mode {
+ Mode::Windowed => None,
+ Mode::Fullscreen => {
+ Some(winit::window::Fullscreen::Borderless(monitor))
+ }
+ }
+}
+
+/// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
@@ -23,7 +119,7 @@ pub fn mouse_cursor(mouse_cursor: MouseCursor) -> winit::window::CursorIcon {
}
}
-/// Convert a `MouseButton` from [`winit`] to an [`iced_native`] mouse button.
+/// Converts a `MouseButton` from [`winit`] to an [`iced_native`] mouse button.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
@@ -36,7 +132,7 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
}
}
-/// Convert an `ElementState` from [`winit`] to an [`iced_native`] button state.
+/// Converts an `ElementState` from [`winit`] to an [`iced_native`] button state.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
@@ -47,7 +143,23 @@ pub fn button_state(element_state: winit::event::ElementState) -> ButtonState {
}
}
-/// Convert a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code.
+/// Converts some `ModifiersState` from [`winit`] to an [`iced_native`]
+/// modifiers state.
+///
+/// [`winit`]: https://github.com/rust-windowing/winit
+/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
+pub fn modifiers_state(
+ modifiers: winit::event::ModifiersState,
+) -> ModifiersState {
+ ModifiersState {
+ shift: modifiers.shift(),
+ control: modifiers.ctrl(),
+ alt: modifiers.alt(),
+ logo: modifiers.logo(),
+ }
+}
+
+/// 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
@@ -220,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,
+ }
+}