From e19a07d40049f40f36d879a498fab4ce63778b27 Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Mon, 11 Nov 2019 20:29:58 -0800 Subject: Added initial touch events to support iOS --- winit/src/conversion.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b6a0b64b..2fc76c9d 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -5,7 +5,7 @@ use crate::{ input::{ keyboard::{self, KeyCode, ModifiersState}, - mouse, ButtonState, + mouse, touch, ButtonState, }, window, Event, Mode, MouseCursor, }; @@ -84,6 +84,9 @@ pub fn window_event( WindowEvent::HoveredFileCancelled => { Some(Event::Window(window::Event::FilesHoveredLeft)) } + WindowEvent::Touch(touch) => { + Some(Event::Touch(touch_event(touch))) + } _ => None, } } @@ -342,3 +345,24 @@ pub(crate) fn is_private_use_character(c: char) -> bool { _ => false, } } +pub(crate) fn touch_event(touch: winit::event::Touch) -> touch::Touch { + let location = touch.location; + match touch.phase { + winit::event::TouchPhase::Started => touch::Touch::Started { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Ended => touch::Touch::Ended { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Moved => touch::Touch::Moved { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Cancelled => touch::Touch::Cancelled { + x: location.x as f32, + y: location.y as f32, + }, + } +} -- cgit From d3572e1b819ff4d40de4f39f48eab71b9d0d4d5e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 19 Mar 2020 12:17:16 +0100 Subject: Turn `Touch` into a struct and add finger id --- winit/src/conversion.rs | 51 ++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 2fc76c9d..1d008d05 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -5,9 +5,9 @@ use crate::{ input::{ keyboard::{self, KeyCode, ModifiersState}, - mouse, touch, ButtonState, + mouse, touch, ButtonState, Touch, }, - window, Event, Mode, MouseCursor, + window, Event, Mode, MouseCursor, Point, }; /// Converts a winit window event into an iced event. @@ -31,8 +31,7 @@ pub fn window_event( let position = position.to_logical::(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::MouseInput { button, state, .. } => { @@ -84,9 +83,7 @@ pub fn window_event( WindowEvent::HoveredFileCancelled => { Some(Event::Window(window::Event::FilesHoveredLeft)) } - WindowEvent::Touch(touch) => { - Some(Event::Touch(touch_event(touch))) - } + WindowEvent::Touch(touch) => Some(Event::Touch(touch_event(touch))), _ => None, } } @@ -162,6 +159,25 @@ pub fn modifiers_state( } } +/// 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) -> Touch { + let phase = match touch.phase { + winit::event::TouchPhase::Started => touch::Phase::Started, + winit::event::TouchPhase::Moved => touch::Phase::Moved, + winit::event::TouchPhase::Ended => touch::Phase::Ended, + winit::event::TouchPhase::Cancelled => touch::Phase::Canceled, + }; + + Touch { + finger: touch::Finger(touch.id), + position: Point::new(touch.location.x as f32, touch.location.y as f32), + phase, + } +} + /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit @@ -345,24 +361,3 @@ pub(crate) fn is_private_use_character(c: char) -> bool { _ => false, } } -pub(crate) fn touch_event(touch: winit::event::Touch) -> touch::Touch { - let location = touch.location; - match touch.phase { - winit::event::TouchPhase::Started => touch::Touch::Started { - x: location.x as f32, - y: location.y as f32, - }, - winit::event::TouchPhase::Ended => touch::Touch::Ended { - x: location.x as f32, - y: location.y as f32, - }, - winit::event::TouchPhase::Moved => touch::Touch::Moved { - x: location.x as f32, - y: location.y as f32, - }, - winit::event::TouchPhase::Cancelled => touch::Touch::Cancelled { - x: location.x as f32, - y: location.y as f32, - }, - } -} -- cgit From 3bdf931925067acbaabf040f6c437a54640ed1a0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 15 Dec 2020 06:38:46 +0100 Subject: Turn `Touch` into a `touch::Event` enum --- winit/src/conversion.rs | 50 +++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index e6fc4b96..7a6ab2de 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -2,12 +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, - touch::{self, Touch}, - 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( @@ -183,8 +182,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(), @@ -206,18 +207,23 @@ pub fn cursor_position( /// /// [`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) -> Touch { - let phase = match touch.phase { - winit::event::TouchPhase::Started => touch::Phase::Started, - winit::event::TouchPhase::Moved => touch::Phase::Moved, - winit::event::TouchPhase::Ended => touch::Phase::Ended, - winit::event::TouchPhase::Cancelled => touch::Phase::Canceled, - }; +pub fn touch_event(touch: winit::event::Touch) -> touch::Event { + let id = touch::Finger(touch.id); + let position = Point::new(touch.location.x as f32, touch.location.y as f32); - Touch { - finger: touch::Finger(touch.id), - position: Point::new(touch.location.x as f32, touch.location.y as f32), - phase, + 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 } + } } } @@ -225,7 +231,11 @@ pub fn touch_event(touch: winit::event::Touch) -> Touch { /// /// [`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, -- cgit From 277ae74d6817ee6192b5f7a44de19dcbdf8d58f7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 15 Dec 2020 06:58:15 +0100 Subject: Produce logical coordinates for `touch::Event` --- winit/src/conversion.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 7a6ab2de..f073c474 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -118,7 +118,9 @@ pub fn window_event( WindowEvent::HoveredFileCancelled => { Some(Event::Window(window::Event::FilesHoveredLeft)) } - WindowEvent::Touch(touch) => Some(Event::Touch(touch_event(*touch))), + WindowEvent::Touch(touch) => { + Some(Event::Touch(touch_event(*touch, scale_factor))) + } _ => None, } } @@ -207,9 +209,16 @@ pub fn cursor_position( /// /// [`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) -> touch::Event { +pub fn touch_event( + touch: winit::event::Touch, + scale_factor: f64, +) -> touch::Event { let id = touch::Finger(touch.id); - let position = Point::new(touch.location.x as f32, touch.location.y as f32); + let position = { + let location = touch.location.to_logical::(scale_factor); + + Point::new(location.x as f32, location.y as f32) + }; match touch.phase { winit::event::TouchPhase::Started => { -- cgit From 0b140488b425a7d1fd45ca41592de25b28d3ac17 Mon Sep 17 00:00:00 2001 From: cossonleo Date: Fri, 15 Jan 2021 22:40:16 +0800 Subject: add focus event --- winit/src/conversion.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index f073c474..5265b844 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -121,6 +121,9 @@ pub fn window_event( WindowEvent::Touch(touch) => { Some(Event::Touch(touch_event(*touch, scale_factor))) } + WindowEvent::Focused(focused) => { + Some(Event::Window(window::Event::Focused(*focused))) + } _ => None, } } -- cgit From 45dc02e9bd0b4f2c6cc65781b850f460cddf6171 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 15 Jan 2021 18:21:44 +0100 Subject: Split `window::Event::Focused` into two variants --- winit/src/conversion.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 5265b844..0e04b35d 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -109,6 +109,11 @@ pub fn window_event( WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard( keyboard::Event::ModifiersChanged(self::modifiers(*new_modifiers)), )), + WindowEvent::Focused(focused) => Some(Event::Window(if *focused { + window::Event::Focused + } else { + window::Event::Unfocused + })), WindowEvent::HoveredFile(path) => { Some(Event::Window(window::Event::FileHovered(path.clone()))) } @@ -121,9 +126,6 @@ pub fn window_event( WindowEvent::Touch(touch) => { Some(Event::Touch(touch_event(*touch, scale_factor))) } - WindowEvent::Focused(focused) => { - Some(Event::Window(window::Event::Focused(*focused))) - } _ => None, } } -- cgit From 00de9d0c9ba20b313ffb459ed291ea2b85e53d32 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Mar 2021 21:33:57 +0200 Subject: Add `CloseRequested` variant to `window::Event` --- winit/src/conversion.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 0e04b35d..0fa27413 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -33,6 +33,9 @@ pub fn window_event( height: logical_size.height, })) } + WindowEvent::CloseRequested => { + Some(Event::Window(window::Event::CloseRequested)) + } WindowEvent::CursorMoved { position, .. } => { let position = position.to_logical::(scale_factor); -- cgit From 84c0c9bc7ab858793183560739c8fd6087e22f6e Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Fri, 9 Apr 2021 09:00:29 -0700 Subject: use Mode::Hidden instead --- winit/src/conversion.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 0fa27413..b850a805 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -141,13 +141,21 @@ pub fn fullscreen( mode: Mode, ) -> Option { match mode { - Mode::Windowed => None, + Mode::Windowed | Mode::Hidden => None, Mode::Fullscreen => { Some(winit::window::Fullscreen::Borderless(monitor)) } } } +/// Converts a [`Mode`] to a visibility flag. +pub fn visible(mode: Mode) -> bool { + match mode { + Mode::Windowed | Mode::Fullscreen => true, + Mode::Hidden => false, + } +} + /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit -- cgit From 9fc5ad23edca93553137100d167de7b69e88f785 Mon Sep 17 00:00:00 2001 From: Richard Date: Mon, 5 Jul 2021 16:23:44 -0300 Subject: Initial menu implementation --- winit/src/conversion.rs | 224 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 223 insertions(+), 1 deletion(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b850a805..da09ac9d 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -6,7 +6,7 @@ use crate::keyboard; use crate::mouse; use crate::touch; use crate::window; -use crate::{Event, Mode, Point}; +use crate::{Event, Menu, MenuEntry, Mode, Point}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -156,6 +156,51 @@ pub fn visible(mode: Mode) -> bool { } } +/// Converts a `Hotkey` from [`iced_native`] to a [`winit`] Hotkey. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native +fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { + use winit::event::ModifiersState; + + let mut modifiers = ModifiersState::empty(); + modifiers.set(ModifiersState::CTRL, hotkey.modifiers.control); + modifiers.set(ModifiersState::SHIFT, hotkey.modifiers.shift); + modifiers.set(ModifiersState::ALT, hotkey.modifiers.alt); + modifiers.set(ModifiersState::LOGO, hotkey.modifiers.logo); + + winit::window::Hotkey::new(modifiers, to_virtual_keycode(hotkey.key)) +} + +/// Converts a `Menu` from [`iced_native`] to a [`winit`] menu. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native +pub fn menu(menu: Menu) -> winit::window::Menu { + let mut converted = winit::window::Menu::new(); + + for item in menu.iter() { + match item { + MenuEntry::Item { + content, hotkey, .. + } => { + let hotkey: Option<&keyboard::Hotkey> = hotkey.as_ref().into(); + converted.add_item( + 0, + content, + hotkey.map(|h| self::hotkey(*h)), + ); + } + MenuEntry::Dropdown { content, submenu } => { + converted.add_dropdown(content, self::menu(submenu)); + } + MenuEntry::Separator => converted.add_separator(), + } + } + + converted +} + /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit @@ -252,6 +297,183 @@ pub fn touch_event( } } +/// Converts a `KeyCode` from [`iced_native`] to an [`winit`] key code. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native +fn to_virtual_keycode( + keycode: keyboard::KeyCode, +) -> winit::event::VirtualKeyCode { + use keyboard::KeyCode; + use winit::event::VirtualKeyCode; + + match keycode { + KeyCode::Key1 => VirtualKeyCode::Key1, + KeyCode::Key2 => VirtualKeyCode::Key2, + KeyCode::Key3 => VirtualKeyCode::Key3, + KeyCode::Key4 => VirtualKeyCode::Key4, + KeyCode::Key5 => VirtualKeyCode::Key5, + KeyCode::Key6 => VirtualKeyCode::Key6, + KeyCode::Key7 => VirtualKeyCode::Key7, + KeyCode::Key8 => VirtualKeyCode::Key8, + KeyCode::Key9 => VirtualKeyCode::Key9, + KeyCode::Key0 => VirtualKeyCode::Key0, + KeyCode::A => VirtualKeyCode::A, + KeyCode::B => VirtualKeyCode::B, + KeyCode::C => VirtualKeyCode::C, + KeyCode::D => VirtualKeyCode::D, + KeyCode::E => VirtualKeyCode::E, + KeyCode::F => VirtualKeyCode::F, + KeyCode::G => VirtualKeyCode::G, + KeyCode::H => VirtualKeyCode::H, + KeyCode::I => VirtualKeyCode::I, + KeyCode::J => VirtualKeyCode::J, + KeyCode::K => VirtualKeyCode::K, + KeyCode::L => VirtualKeyCode::L, + KeyCode::M => VirtualKeyCode::M, + KeyCode::N => VirtualKeyCode::N, + KeyCode::O => VirtualKeyCode::O, + KeyCode::P => VirtualKeyCode::P, + KeyCode::Q => VirtualKeyCode::Q, + KeyCode::R => VirtualKeyCode::R, + KeyCode::S => VirtualKeyCode::S, + KeyCode::T => VirtualKeyCode::T, + KeyCode::U => VirtualKeyCode::U, + KeyCode::V => VirtualKeyCode::V, + KeyCode::W => VirtualKeyCode::W, + KeyCode::X => VirtualKeyCode::X, + KeyCode::Y => VirtualKeyCode::Y, + KeyCode::Z => VirtualKeyCode::Z, + KeyCode::Escape => VirtualKeyCode::Escape, + KeyCode::F1 => VirtualKeyCode::F1, + KeyCode::F2 => VirtualKeyCode::F2, + KeyCode::F3 => VirtualKeyCode::F3, + KeyCode::F4 => VirtualKeyCode::F4, + KeyCode::F5 => VirtualKeyCode::F5, + KeyCode::F6 => VirtualKeyCode::F6, + KeyCode::F7 => VirtualKeyCode::F7, + KeyCode::F8 => VirtualKeyCode::F8, + KeyCode::F9 => VirtualKeyCode::F9, + KeyCode::F10 => VirtualKeyCode::F10, + KeyCode::F11 => VirtualKeyCode::F11, + KeyCode::F12 => VirtualKeyCode::F12, + KeyCode::F13 => VirtualKeyCode::F13, + KeyCode::F14 => VirtualKeyCode::F14, + KeyCode::F15 => VirtualKeyCode::F15, + KeyCode::F16 => VirtualKeyCode::F16, + KeyCode::F17 => VirtualKeyCode::F17, + KeyCode::F18 => VirtualKeyCode::F18, + KeyCode::F19 => VirtualKeyCode::F19, + KeyCode::F20 => VirtualKeyCode::F20, + KeyCode::F21 => VirtualKeyCode::F21, + KeyCode::F22 => VirtualKeyCode::F22, + KeyCode::F23 => VirtualKeyCode::F23, + KeyCode::F24 => VirtualKeyCode::F24, + KeyCode::Snapshot => VirtualKeyCode::Snapshot, + KeyCode::Scroll => VirtualKeyCode::Scroll, + KeyCode::Pause => VirtualKeyCode::Pause, + KeyCode::Insert => VirtualKeyCode::Insert, + KeyCode::Home => VirtualKeyCode::Home, + KeyCode::Delete => VirtualKeyCode::Delete, + KeyCode::End => VirtualKeyCode::End, + KeyCode::PageDown => VirtualKeyCode::PageDown, + KeyCode::PageUp => VirtualKeyCode::PageUp, + KeyCode::Left => VirtualKeyCode::Left, + KeyCode::Up => VirtualKeyCode::Up, + KeyCode::Right => VirtualKeyCode::Right, + KeyCode::Down => VirtualKeyCode::Down, + KeyCode::Backspace => VirtualKeyCode::Back, + KeyCode::Enter => VirtualKeyCode::Return, + KeyCode::Space => VirtualKeyCode::Space, + KeyCode::Compose => VirtualKeyCode::Compose, + KeyCode::Caret => VirtualKeyCode::Caret, + KeyCode::Numlock => VirtualKeyCode::Numlock, + KeyCode::Numpad0 => VirtualKeyCode::Numpad0, + KeyCode::Numpad1 => VirtualKeyCode::Numpad1, + KeyCode::Numpad2 => VirtualKeyCode::Numpad2, + KeyCode::Numpad3 => VirtualKeyCode::Numpad3, + KeyCode::Numpad4 => VirtualKeyCode::Numpad4, + KeyCode::Numpad5 => VirtualKeyCode::Numpad5, + KeyCode::Numpad6 => VirtualKeyCode::Numpad6, + KeyCode::Numpad7 => VirtualKeyCode::Numpad7, + KeyCode::Numpad8 => VirtualKeyCode::Numpad8, + KeyCode::Numpad9 => VirtualKeyCode::Numpad9, + KeyCode::AbntC1 => VirtualKeyCode::AbntC1, + KeyCode::AbntC2 => VirtualKeyCode::AbntC2, + KeyCode::NumpadAdd => VirtualKeyCode::NumpadAdd, + KeyCode::Plus => VirtualKeyCode::Plus, + KeyCode::Apostrophe => VirtualKeyCode::Apostrophe, + KeyCode::Apps => VirtualKeyCode::Apps, + KeyCode::At => VirtualKeyCode::At, + KeyCode::Ax => VirtualKeyCode::Ax, + KeyCode::Backslash => VirtualKeyCode::Backslash, + KeyCode::Calculator => VirtualKeyCode::Calculator, + KeyCode::Capital => VirtualKeyCode::Capital, + KeyCode::Colon => VirtualKeyCode::Colon, + KeyCode::Comma => VirtualKeyCode::Comma, + KeyCode::Convert => VirtualKeyCode::Convert, + KeyCode::NumpadDecimal => VirtualKeyCode::NumpadDecimal, + KeyCode::NumpadDivide => VirtualKeyCode::NumpadDivide, + KeyCode::Equals => VirtualKeyCode::Equals, + KeyCode::Grave => VirtualKeyCode::Grave, + KeyCode::Kana => VirtualKeyCode::Kana, + KeyCode::Kanji => VirtualKeyCode::Kanji, + KeyCode::LAlt => VirtualKeyCode::LAlt, + KeyCode::LBracket => VirtualKeyCode::LBracket, + KeyCode::LControl => VirtualKeyCode::LControl, + KeyCode::LShift => VirtualKeyCode::LShift, + KeyCode::LWin => VirtualKeyCode::LWin, + KeyCode::Mail => VirtualKeyCode::Mail, + KeyCode::MediaSelect => VirtualKeyCode::MediaSelect, + KeyCode::MediaStop => VirtualKeyCode::MediaStop, + KeyCode::Minus => VirtualKeyCode::Minus, + KeyCode::NumpadMultiply => VirtualKeyCode::NumpadMultiply, + KeyCode::Mute => VirtualKeyCode::Mute, + KeyCode::MyComputer => VirtualKeyCode::MyComputer, + KeyCode::NavigateForward => VirtualKeyCode::NavigateForward, + KeyCode::NavigateBackward => VirtualKeyCode::NavigateBackward, + KeyCode::NextTrack => VirtualKeyCode::NextTrack, + KeyCode::NoConvert => VirtualKeyCode::NoConvert, + KeyCode::NumpadComma => VirtualKeyCode::NumpadComma, + KeyCode::NumpadEnter => VirtualKeyCode::NumpadEnter, + KeyCode::NumpadEquals => VirtualKeyCode::NumpadEquals, + KeyCode::OEM102 => VirtualKeyCode::OEM102, + KeyCode::Period => VirtualKeyCode::Period, + KeyCode::PlayPause => VirtualKeyCode::PlayPause, + KeyCode::Power => VirtualKeyCode::Power, + KeyCode::PrevTrack => VirtualKeyCode::PrevTrack, + KeyCode::RAlt => VirtualKeyCode::RAlt, + KeyCode::RBracket => VirtualKeyCode::RBracket, + KeyCode::RControl => VirtualKeyCode::RControl, + KeyCode::RShift => VirtualKeyCode::RShift, + KeyCode::RWin => VirtualKeyCode::RWin, + KeyCode::Semicolon => VirtualKeyCode::Semicolon, + KeyCode::Slash => VirtualKeyCode::Slash, + KeyCode::Sleep => VirtualKeyCode::Sleep, + KeyCode::Stop => VirtualKeyCode::Stop, + KeyCode::NumpadSubtract => VirtualKeyCode::NumpadSubtract, + KeyCode::Sysrq => VirtualKeyCode::Sysrq, + KeyCode::Tab => VirtualKeyCode::Tab, + KeyCode::Underline => VirtualKeyCode::Underline, + KeyCode::Unlabeled => VirtualKeyCode::Unlabeled, + KeyCode::VolumeDown => VirtualKeyCode::VolumeDown, + KeyCode::VolumeUp => VirtualKeyCode::VolumeUp, + KeyCode::Wake => VirtualKeyCode::Wake, + KeyCode::WebBack => VirtualKeyCode::WebBack, + KeyCode::WebFavorites => VirtualKeyCode::WebFavorites, + KeyCode::WebForward => VirtualKeyCode::WebForward, + KeyCode::WebHome => VirtualKeyCode::WebHome, + KeyCode::WebRefresh => VirtualKeyCode::WebRefresh, + KeyCode::WebSearch => VirtualKeyCode::WebSearch, + KeyCode::WebStop => VirtualKeyCode::WebStop, + KeyCode::Yen => VirtualKeyCode::Yen, + KeyCode::Copy => VirtualKeyCode::Copy, + KeyCode::Paste => VirtualKeyCode::Paste, + KeyCode::Cut => VirtualKeyCode::Cut, + KeyCode::Asterisk => VirtualKeyCode::Asterisk, + } +} + /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit -- cgit From 1428e9180ae9f4edbf22514bb74c5c7e9df9c712 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 21:38:54 +0200 Subject: Make `Menu` API a bit more functional --- winit/src/conversion.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index da09ac9d..02c21c59 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -3,10 +3,11 @@ //! [`winit`]: https://github.com/rust-windowing/winit //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native use crate::keyboard; +use crate::menu::{self, Menu}; use crate::mouse; use crate::touch; use crate::window; -use crate::{Event, Menu, MenuEntry, Mode, Point}; +use crate::{Event, Mode, Point}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -181,7 +182,7 @@ pub fn menu(menu: Menu) -> winit::window::Menu { for item in menu.iter() { match item { - MenuEntry::Item { + menu::Entry::Item { content, hotkey, .. } => { let hotkey: Option<&keyboard::Hotkey> = hotkey.as_ref().into(); @@ -191,10 +192,10 @@ pub fn menu(menu: Menu) -> winit::window::Menu { hotkey.map(|h| self::hotkey(*h)), ); } - MenuEntry::Dropdown { content, submenu } => { + menu::Entry::Dropdown { content, submenu } => { converted.add_dropdown(content, self::menu(submenu)); } - MenuEntry::Separator => converted.add_separator(), + menu::Entry::Separator => converted.add_separator(), } } -- cgit From b57d567981bb7ef5f9ff397c210778f211d2de5b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 22:01:57 +0200 Subject: Use `bitflags` for `keyboard::Modifiers` --- winit/src/conversion.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 02c21c59..3aa88c5c 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -165,10 +165,10 @@ fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { use winit::event::ModifiersState; let mut modifiers = ModifiersState::empty(); - modifiers.set(ModifiersState::CTRL, hotkey.modifiers.control); - modifiers.set(ModifiersState::SHIFT, hotkey.modifiers.shift); - modifiers.set(ModifiersState::ALT, hotkey.modifiers.alt); - modifiers.set(ModifiersState::LOGO, hotkey.modifiers.logo); + modifiers.set(ModifiersState::CTRL, hotkey.modifiers.control()); + modifiers.set(ModifiersState::SHIFT, hotkey.modifiers.shift()); + modifiers.set(ModifiersState::ALT, hotkey.modifiers.alt()); + modifiers.set(ModifiersState::LOGO, hotkey.modifiers.logo()); winit::window::Hotkey::new(modifiers, to_virtual_keycode(hotkey.key)) } @@ -249,12 +249,14 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { pub fn modifiers( modifiers: winit::event::ModifiersState, ) -> keyboard::Modifiers { - keyboard::Modifiers { - shift: modifiers.shift(), - control: modifiers.ctrl(), - alt: modifiers.alt(), - logo: modifiers.logo(), - } + let mut result = keyboard::Modifiers::empty(); + + result.set(keyboard::Modifiers::SHIFT, modifiers.shift()); + result.set(keyboard::Modifiers::CTRL, modifiers.ctrl()); + result.set(keyboard::Modifiers::ALT, modifiers.alt()); + result.set(keyboard::Modifiers::LOGO, modifiers.logo()); + + result } /// Converts a physical cursor position to a logical `Point`. -- cgit From b3ff522c182590ffcd03113b5147fa4599559059 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 22:11:06 +0200 Subject: Simplify `Hotkey` conversion in `conversion::menu` --- winit/src/conversion.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 3aa88c5c..4dc8bbf5 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -185,12 +185,7 @@ pub fn menu(menu: Menu) -> winit::window::Menu { menu::Entry::Item { content, hotkey, .. } => { - let hotkey: Option<&keyboard::Hotkey> = hotkey.as_ref().into(); - converted.add_item( - 0, - content, - hotkey.map(|h| self::hotkey(*h)), - ); + converted.add_item(0, content, hotkey.map(self::hotkey)); } menu::Entry::Dropdown { content, submenu } => { converted.add_dropdown(content, self::menu(submenu)); -- cgit From 31997d255f263a0f47f5af0d8560f3d5bd37f077 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 12 Jul 2021 22:28:18 +0200 Subject: Store and synchronize `Menu` in `application::State` --- winit/src/conversion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 4dc8bbf5..22118bf5 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -177,7 +177,7 @@ fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn menu(menu: Menu) -> winit::window::Menu { +pub fn menu(menu: &Menu) -> winit::window::Menu { let mut converted = winit::window::Menu::new(); for item in menu.iter() { -- cgit From f3b056a6fc06248aa068549fc47ab6864829b875 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 20:46:23 +0200 Subject: Generate unique identifiers for entries in `conversion::menu` --- winit/src/conversion.rs | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 22118bf5..3c483086 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -178,22 +178,40 @@ fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native pub fn menu(menu: &Menu) -> winit::window::Menu { - let mut converted = winit::window::Menu::new(); - - for item in menu.iter() { - match item { - menu::Entry::Item { - content, hotkey, .. - } => { - converted.add_item(0, content, hotkey.map(self::hotkey)); - } - menu::Entry::Dropdown { content, submenu } => { - converted.add_dropdown(content, self::menu(submenu)); + fn menu_i( + starting_id: usize, + menu: &Menu, + ) -> (winit::window::Menu, usize) { + let mut id = starting_id; + let mut converted = winit::window::Menu::new(); + + for item in menu.iter() { + match item { + menu::Entry::Item { + content, hotkey, .. + } => { + converted.add_item(id, content, hotkey.map(self::hotkey)); + + id += 1; + } + menu::Entry::Dropdown { content, submenu } => { + let (submenu, n_children) = menu_i(id, submenu); + + converted.add_dropdown(content, submenu); + + id += n_children; + } + menu::Entry::Separator => { + converted.add_separator(); + } } - menu::Entry::Separator => converted.add_separator(), } + + (converted, id - starting_id) } + let (converted, _) = menu_i(0, menu); + converted } -- cgit From 6221adf2b1b1e8150931d4175e1e36870d45f6e5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 20:55:21 +0200 Subject: Draft `conversion::menu_message` in `iced_winit` ... and wire it up to the runtime loop --- winit/src/conversion.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 3c483086..51db615b 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -215,6 +215,17 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { converted } +/// Given a [`Menu`] and an identifier of a [`menu::Entry`], it returns the +/// `Message` that should be produced when that entry is activated. +pub fn menu_message( + _menu: &Menu, + id: isize, +) -> Option { + println!("Menu entry activated: {}", id); + + None +} + /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit -- cgit From 4abaee8b2354a666a75e4eb5ec9cc9a744936813 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 21:11:13 +0200 Subject: Use `Menu::default` for root level menu in `conversion::menu` --- winit/src/conversion.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 51db615b..75419006 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -179,11 +179,11 @@ fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey { /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native pub fn menu(menu: &Menu) -> winit::window::Menu { fn menu_i( + converted: &mut winit::window::Menu, starting_id: usize, menu: &Menu, - ) -> (winit::window::Menu, usize) { + ) -> usize { let mut id = starting_id; - let mut converted = winit::window::Menu::new(); for item in menu.iter() { match item { @@ -195,9 +195,11 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { id += 1; } menu::Entry::Dropdown { content, submenu } => { - let (submenu, n_children) = menu_i(id, submenu); + let mut converted_submenu = winit::window::Menu::new(); + let n_children = + menu_i(&mut converted_submenu, id, submenu); - converted.add_dropdown(content, submenu); + converted.add_dropdown(content, converted_submenu); id += n_children; } @@ -207,10 +209,11 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { } } - (converted, id - starting_id) + id - starting_id } - let (converted, _) = menu_i(0, menu); + let mut converted = winit::window::Menu::default(); + let _ = menu_i(&mut converted, 0, menu); converted } -- cgit From 5df2a92f28be1d53d32d5b42a6645459b7d78efe Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 21:15:07 +0200 Subject: Force `Application::Message` to implement `Clone` A `Message` should represent an application event (e.g. user interactions, command results, subscription results...). Therefore, it should always consist of pure, cloneable data. --- winit/src/conversion.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 75419006..687a037e 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -223,7 +223,10 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { pub fn menu_message( _menu: &Menu, id: isize, -) -> Option { +) -> Option +where + Message: Clone, +{ println!("Menu entry activated: {}", id); None -- cgit From 2e7eac7d2167b492149e064927e764eca67f98fe Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 13 Jul 2021 21:31:34 +0200 Subject: Implement `conversion::menu_message` --- winit/src/conversion.rs | 50 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 687a037e..dc7f90ad 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -220,16 +220,54 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { /// Given a [`Menu`] and an identifier of a [`menu::Entry`], it returns the /// `Message` that should be produced when that entry is activated. -pub fn menu_message( - _menu: &Menu, - id: isize, -) -> Option +pub fn menu_message(menu: &Menu, id: isize) -> Option where Message: Clone, { - println!("Menu entry activated: {}", id); + use std::convert::TryFrom; - None + fn find_message( + target: usize, + starting_id: usize, + menu: &Menu, + ) -> Result + where + Message: Clone, + { + let mut id = starting_id; + + for entry in menu.iter() { + match entry { + menu::Entry::Item { on_activation, .. } => { + if id == target { + return Ok(on_activation.clone()); + } + + id += 1; + } + menu::Entry::Dropdown { submenu, .. } => { + match find_message(target, id, submenu) { + Ok(message) => { + return Ok(message); + } + Err(n_children) => { + id += n_children; + } + } + } + menu::Entry::Separator => {} + } + } + + Err(id - starting_id) + } + + // TODO: Does `winit` really need to provide an `isize`? + if let Ok(id) = usize::try_from(id) { + find_message(id, 0, menu).ok() + } else { + None + } } /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. -- cgit From a2f49a74d08a0cff2e892932b484a88a4034f627 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 19 Jul 2021 21:01:24 +0700 Subject: Replace `content` with `title` in `menu` module --- winit/src/conversion.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index dc7f90ad..e61611aa 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -187,19 +187,17 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { for item in menu.iter() { match item { - menu::Entry::Item { - content, hotkey, .. - } => { - converted.add_item(id, content, hotkey.map(self::hotkey)); + menu::Entry::Item { title, hotkey, .. } => { + converted.add_item(id, title, hotkey.map(self::hotkey)); id += 1; } - menu::Entry::Dropdown { content, submenu } => { + menu::Entry::Dropdown { title, submenu } => { let mut converted_submenu = winit::window::Menu::new(); let n_children = menu_i(&mut converted_submenu, id, submenu); - converted.add_dropdown(content, converted_submenu); + converted.add_dropdown(title, converted_submenu); id += n_children; } -- cgit From 82db3c78b6cfa2cc55ece6ffa46811bfb5195f60 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 20 Jul 2021 21:34:20 +0700 Subject: Update `winit` and `glutin` dependencies ... and remove crates.io patch --- winit/src/conversion.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index e61611aa..5a8b9fd8 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -218,17 +218,15 @@ pub fn menu(menu: &Menu) -> winit::window::Menu { /// Given a [`Menu`] and an identifier of a [`menu::Entry`], it returns the /// `Message` that should be produced when that entry is activated. -pub fn menu_message(menu: &Menu, id: isize) -> Option +pub fn menu_message(menu: &Menu, id: u32) -> Option where Message: Clone, { - use std::convert::TryFrom; - fn find_message( - target: usize, - starting_id: usize, + target: u32, + starting_id: u32, menu: &Menu, - ) -> Result + ) -> Result where Message: Clone, { @@ -260,12 +258,7 @@ where Err(id - starting_id) } - // TODO: Does `winit` really need to provide an `isize`? - if let Ok(id) = usize::try_from(id) { - find_message(id, 0, menu).ok() - } else { - None - } + find_message(id, 0, menu).ok() } /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. -- cgit From 72b3bf95de3f11812b1541fe2ffea76a515aee79 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 21 Jul 2021 18:59:24 +0700 Subject: Improve `window::Position` API --- winit/src/conversion.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'winit/src/conversion.rs') diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b850a805..f785fce6 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -6,7 +6,7 @@ use crate::keyboard; use crate::mouse; use crate::touch; use crate::window; -use crate::{Event, Mode, Point}; +use crate::{Event, Mode, Point, Position}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -133,6 +133,49 @@ pub fn window_event( } } +/// Converts a [`Position`] to a [`winit`] logical position for a given monitor. +/// +/// [`winit`]: https://github.com/rust-windowing/winit +pub fn position( + monitor: Option<&winit::monitor::MonitorHandle>, + (width, height): (u32, u32), + position: Position, +) -> Option { + match position { + Position::Default => None, + Position::Specific(x, y) => { + Some(winit::dpi::Position::Logical(winit::dpi::LogicalPosition { + x: f64::from(x), + y: f64::from(y), + })) + } + Position::Centered => { + if let Some(monitor) = monitor { + let start = monitor.position(); + + let resolution: winit::dpi::LogicalSize = + monitor.size().to_logical(monitor.scale_factor()); + + let centered: winit::dpi::PhysicalPosition = + winit::dpi::LogicalPosition { + x: (resolution.width - f64::from(width)) / 2.0, + y: (resolution.height - f64::from(height)) / 2.0, + } + .to_physical(monitor.scale_factor()); + + Some(winit::dpi::Position::Physical( + winit::dpi::PhysicalPosition { + x: start.x + centered.x, + y: start.y + centered.y, + }, + )) + } else { + None + } + } + } +} + /// Converts a [`Mode`] to a [`winit`] fullscreen mode. /// /// [`winit`]: https://github.com/rust-windowing/winit -- cgit