From 87ed2371407e699a4fa70ba700b57eb8e8bd7c23 Mon Sep 17 00:00:00 2001 From: John Wells Date: Fri, 17 Nov 2023 11:11:37 -0500 Subject: [PATCH] Update winit to 0.29 --- CHANGELOG.markdown | 2 +- imgui-winit-support/Cargo.toml | 2 +- imgui-winit-support/src/lib.rs | 302 +++++++++++++++++---------------- 3 files changed, 156 insertions(+), 150 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 845d88b67..c2b16dcb3 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -2,7 +2,7 @@ ## [unreleased] -- Breaking: Updated winit to `0.28` for `imgui-winit-support`. +- Breaking: Updated winit to `0.29` for `imgui-winit-support`. ## [0.10.0] - 2023-01-16 diff --git a/imgui-winit-support/Cargo.toml b/imgui-winit-support/Cargo.toml index ac4ffb0df..dce260bae 100644 --- a/imgui-winit-support/Cargo.toml +++ b/imgui-winit-support/Cargo.toml @@ -11,4 +11,4 @@ categories = ["gui"] [dependencies] imgui = { version = "0.10.0", path = "../imgui" } -winit = { version = "0.28.2", default-features = false } +winit = { version = "0.29", default-features = false } diff --git a/imgui-winit-support/src/lib.rs b/imgui-winit-support/src/lib.rs index 47806ccee..4945f70c4 100644 --- a/imgui-winit-support/src/lib.rs +++ b/imgui-winit-support/src/lib.rs @@ -20,10 +20,10 @@ //! use imgui_winit_support::{HiDpiMode, WinitPlatform}; //! use std::time::Instant; //! use winit::event::{Event, WindowEvent}; -//! use winit::event_loop::{ControlFlow, EventLoop}; +//! use winit::event_loop::EventLoop; //! use winit::window::Window; //! -//! let mut event_loop = EventLoop::new(); +//! let mut event_loop = EventLoop::new().unwrap(); //! let mut window = Window::new(&event_loop).unwrap(); //! //! let mut imgui = Context::create(); @@ -34,7 +34,7 @@ //! //! let mut last_frame = Instant::now(); //! let mut run = true; -//! event_loop.run(move |event, _, control_flow| { +//! event_loop.run(move |event, window_target| { //! match event { //! Event::NewEvents(_) => { //! // other application-specific logic @@ -42,13 +42,13 @@ //! imgui.io_mut().update_delta_time(now - last_frame); //! last_frame = now; //! }, -//! Event::MainEventsCleared => { +//! Event::AboutToWait => { //! // other application-specific logic //! platform.prepare_frame(imgui.io_mut(), &window) // step 4 //! .expect("Failed to prepare frame"); //! window.request_redraw(); //! } -//! Event::RedrawRequested(_) => { +//! Event::WindowEvent { event: WindowEvent::RedrawRequested, .. } => { //! let ui = imgui.frame(); //! // application-specific rendering *under the UI* //! @@ -62,7 +62,7 @@ //! // application-specific rendering *over the UI* //! }, //! Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => { -//! *control_flow = ControlFlow::Exit; +//! window_target.exit(); //! } //! // other application-specific event handling //! event => { @@ -70,7 +70,7 @@ //! // other application-specific event handling //! } //! } -//! }) +//! }).unwrap(); //! ``` use imgui::{self, BackendFlags, ConfigFlags, Context, Io, Key, Ui}; @@ -83,9 +83,10 @@ use winit::dpi::{LogicalPosition, LogicalSize}; use winit::{ error::ExternalError, event::{ - DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase, - VirtualKeyCode, WindowEvent, + DeviceEvent, ElementState, Event, KeyEvent, MouseButton, MouseScrollDelta, RawKeyEvent, + TouchPhase, WindowEvent, }, + keyboard::{KeyCode, PhysicalKey}, window::{CursorIcon as MouseCursor, Window}, }; @@ -112,7 +113,7 @@ fn to_winit_cursor(cursor: imgui::MouseCursor) -> MouseCursor { imgui::MouseCursor::ResizeEW => MouseCursor::EwResize, imgui::MouseCursor::ResizeNESW => MouseCursor::NeswResize, imgui::MouseCursor::ResizeNWSE => MouseCursor::NwseResize, - imgui::MouseCursor::Hand => MouseCursor::Hand, + imgui::MouseCursor::Hand => MouseCursor::Pointer, imgui::MouseCursor::NotAllowed => MouseCursor::NotAllowed, } } @@ -180,127 +181,128 @@ fn to_imgui_mouse_button(button: MouseButton) -> Option { } } -fn to_imgui_key(keycode: VirtualKeyCode) -> Option { +fn to_imgui_key(keycode: KeyCode) -> Option { match keycode { - VirtualKeyCode::Tab => Some(Key::Tab), - VirtualKeyCode::Left => Some(Key::LeftArrow), - VirtualKeyCode::Right => Some(Key::RightArrow), - VirtualKeyCode::Up => Some(Key::UpArrow), - VirtualKeyCode::Down => Some(Key::DownArrow), - VirtualKeyCode::PageUp => Some(Key::PageUp), - VirtualKeyCode::PageDown => Some(Key::PageDown), - VirtualKeyCode::Home => Some(Key::Home), - VirtualKeyCode::End => Some(Key::End), - VirtualKeyCode::Insert => Some(Key::Insert), - VirtualKeyCode::Delete => Some(Key::Delete), - VirtualKeyCode::Back => Some(Key::Backspace), - VirtualKeyCode::Space => Some(Key::Space), - VirtualKeyCode::Return => Some(Key::Enter), - VirtualKeyCode::Escape => Some(Key::Escape), - VirtualKeyCode::LControl => Some(Key::LeftCtrl), - VirtualKeyCode::LShift => Some(Key::LeftShift), - VirtualKeyCode::LAlt => Some(Key::LeftAlt), - VirtualKeyCode::LWin => Some(Key::LeftSuper), - VirtualKeyCode::RControl => Some(Key::RightCtrl), - VirtualKeyCode::RShift => Some(Key::RightShift), - VirtualKeyCode::RAlt => Some(Key::RightAlt), - VirtualKeyCode::RWin => Some(Key::RightSuper), - //VirtualKeyCode::Menu => Some(Key::Menu), // TODO: find out if there is a Menu key in winit - VirtualKeyCode::Key0 => Some(Key::Alpha0), - VirtualKeyCode::Key1 => Some(Key::Alpha1), - VirtualKeyCode::Key2 => Some(Key::Alpha2), - VirtualKeyCode::Key3 => Some(Key::Alpha3), - VirtualKeyCode::Key4 => Some(Key::Alpha4), - VirtualKeyCode::Key5 => Some(Key::Alpha5), - VirtualKeyCode::Key6 => Some(Key::Alpha6), - VirtualKeyCode::Key7 => Some(Key::Alpha7), - VirtualKeyCode::Key8 => Some(Key::Alpha8), - VirtualKeyCode::Key9 => Some(Key::Alpha9), - VirtualKeyCode::A => Some(Key::A), - VirtualKeyCode::B => Some(Key::B), - VirtualKeyCode::C => Some(Key::C), - VirtualKeyCode::D => Some(Key::D), - VirtualKeyCode::E => Some(Key::E), - VirtualKeyCode::F => Some(Key::F), - VirtualKeyCode::G => Some(Key::G), - VirtualKeyCode::H => Some(Key::H), - VirtualKeyCode::I => Some(Key::I), - VirtualKeyCode::J => Some(Key::J), - VirtualKeyCode::K => Some(Key::K), - VirtualKeyCode::L => Some(Key::L), - VirtualKeyCode::M => Some(Key::M), - VirtualKeyCode::N => Some(Key::N), - VirtualKeyCode::O => Some(Key::O), - VirtualKeyCode::P => Some(Key::P), - VirtualKeyCode::Q => Some(Key::Q), - VirtualKeyCode::R => Some(Key::R), - VirtualKeyCode::S => Some(Key::S), - VirtualKeyCode::T => Some(Key::T), - VirtualKeyCode::U => Some(Key::U), - VirtualKeyCode::V => Some(Key::V), - VirtualKeyCode::W => Some(Key::W), - VirtualKeyCode::X => Some(Key::X), - VirtualKeyCode::Y => Some(Key::Y), - VirtualKeyCode::Z => Some(Key::Z), - VirtualKeyCode::F1 => Some(Key::F1), - VirtualKeyCode::F2 => Some(Key::F2), - VirtualKeyCode::F3 => Some(Key::F3), - VirtualKeyCode::F4 => Some(Key::F4), - VirtualKeyCode::F5 => Some(Key::F5), - VirtualKeyCode::F6 => Some(Key::F6), - VirtualKeyCode::F7 => Some(Key::F7), - VirtualKeyCode::F8 => Some(Key::F8), - VirtualKeyCode::F9 => Some(Key::F9), - VirtualKeyCode::F10 => Some(Key::F10), - VirtualKeyCode::F11 => Some(Key::F11), - VirtualKeyCode::F12 => Some(Key::F12), - VirtualKeyCode::Apostrophe => Some(Key::Apostrophe), - VirtualKeyCode::Comma => Some(Key::Comma), - VirtualKeyCode::Minus => Some(Key::Minus), - VirtualKeyCode::Period => Some(Key::Period), - VirtualKeyCode::Slash => Some(Key::Slash), - VirtualKeyCode::Semicolon => Some(Key::Semicolon), - VirtualKeyCode::Equals => Some(Key::Equal), - VirtualKeyCode::LBracket => Some(Key::LeftBracket), - VirtualKeyCode::Backslash => Some(Key::Backslash), - VirtualKeyCode::RBracket => Some(Key::RightBracket), - VirtualKeyCode::Grave => Some(Key::GraveAccent), - VirtualKeyCode::Capital => Some(Key::CapsLock), - VirtualKeyCode::Scroll => Some(Key::ScrollLock), - VirtualKeyCode::Numlock => Some(Key::NumLock), - VirtualKeyCode::Snapshot => Some(Key::PrintScreen), - VirtualKeyCode::Pause => Some(Key::Pause), - VirtualKeyCode::Numpad0 => Some(Key::Keypad0), - VirtualKeyCode::Numpad1 => Some(Key::Keypad1), - VirtualKeyCode::Numpad2 => Some(Key::Keypad2), - VirtualKeyCode::Numpad3 => Some(Key::Keypad3), - VirtualKeyCode::Numpad4 => Some(Key::Keypad4), - VirtualKeyCode::Numpad5 => Some(Key::Keypad5), - VirtualKeyCode::Numpad6 => Some(Key::Keypad6), - VirtualKeyCode::Numpad7 => Some(Key::Keypad7), - VirtualKeyCode::Numpad8 => Some(Key::Keypad8), - VirtualKeyCode::Numpad9 => Some(Key::Keypad9), - VirtualKeyCode::NumpadDecimal => Some(Key::KeypadDecimal), - VirtualKeyCode::NumpadDivide => Some(Key::KeypadDivide), - VirtualKeyCode::NumpadMultiply => Some(Key::KeypadMultiply), - VirtualKeyCode::NumpadSubtract => Some(Key::KeypadSubtract), - VirtualKeyCode::NumpadAdd => Some(Key::KeypadAdd), - VirtualKeyCode::NumpadEnter => Some(Key::KeypadEnter), - VirtualKeyCode::NumpadEquals => Some(Key::KeypadEqual), + KeyCode::Tab => Some(Key::Tab), + KeyCode::ArrowLeft => Some(Key::LeftArrow), + KeyCode::ArrowRight => Some(Key::RightArrow), + KeyCode::ArrowUp => Some(Key::UpArrow), + KeyCode::ArrowDown => Some(Key::DownArrow), + KeyCode::PageUp => Some(Key::PageUp), + KeyCode::PageDown => Some(Key::PageDown), + KeyCode::Home => Some(Key::Home), + KeyCode::End => Some(Key::End), + KeyCode::Insert => Some(Key::Insert), + KeyCode::Delete => Some(Key::Delete), + KeyCode::Backspace => Some(Key::Backspace), + KeyCode::Space => Some(Key::Space), + KeyCode::Enter => Some(Key::Enter), + KeyCode::Escape => Some(Key::Escape), + KeyCode::ControlLeft => Some(Key::LeftCtrl), + KeyCode::ShiftLeft => Some(Key::LeftShift), + KeyCode::AltLeft => Some(Key::LeftAlt), + KeyCode::SuperLeft => Some(Key::LeftSuper), + KeyCode::ControlRight => Some(Key::RightCtrl), + KeyCode::ShiftRight => Some(Key::RightShift), + KeyCode::AltRight => Some(Key::RightAlt), + KeyCode::SuperRight => Some(Key::RightSuper), + KeyCode::ContextMenu => Some(Key::Menu), + KeyCode::Digit0 => Some(Key::Alpha0), + KeyCode::Digit1 => Some(Key::Alpha1), + KeyCode::Digit2 => Some(Key::Alpha2), + KeyCode::Digit3 => Some(Key::Alpha3), + KeyCode::Digit4 => Some(Key::Alpha4), + KeyCode::Digit5 => Some(Key::Alpha5), + KeyCode::Digit6 => Some(Key::Alpha6), + KeyCode::Digit7 => Some(Key::Alpha7), + KeyCode::Digit8 => Some(Key::Alpha8), + KeyCode::Digit9 => Some(Key::Alpha9), + KeyCode::KeyA => Some(Key::A), + KeyCode::KeyB => Some(Key::B), + KeyCode::KeyC => Some(Key::C), + KeyCode::KeyD => Some(Key::D), + KeyCode::KeyE => Some(Key::E), + KeyCode::KeyF => Some(Key::F), + KeyCode::KeyG => Some(Key::G), + KeyCode::KeyH => Some(Key::H), + KeyCode::KeyI => Some(Key::I), + KeyCode::KeyJ => Some(Key::J), + KeyCode::KeyK => Some(Key::K), + KeyCode::KeyL => Some(Key::L), + KeyCode::KeyM => Some(Key::M), + KeyCode::KeyN => Some(Key::N), + KeyCode::KeyO => Some(Key::O), + KeyCode::KeyP => Some(Key::P), + KeyCode::KeyQ => Some(Key::Q), + KeyCode::KeyR => Some(Key::R), + KeyCode::KeyS => Some(Key::S), + KeyCode::KeyT => Some(Key::T), + KeyCode::KeyU => Some(Key::U), + KeyCode::KeyV => Some(Key::V), + KeyCode::KeyW => Some(Key::W), + KeyCode::KeyX => Some(Key::X), + KeyCode::KeyY => Some(Key::Y), + KeyCode::KeyZ => Some(Key::Z), + KeyCode::F1 => Some(Key::F1), + KeyCode::F2 => Some(Key::F2), + KeyCode::F3 => Some(Key::F3), + KeyCode::F4 => Some(Key::F4), + KeyCode::F5 => Some(Key::F5), + KeyCode::F6 => Some(Key::F6), + KeyCode::F7 => Some(Key::F7), + KeyCode::F8 => Some(Key::F8), + KeyCode::F9 => Some(Key::F9), + KeyCode::F10 => Some(Key::F10), + KeyCode::F11 => Some(Key::F11), + KeyCode::F12 => Some(Key::F12), + KeyCode::Quote => Some(Key::Apostrophe), + KeyCode::Comma => Some(Key::Comma), + KeyCode::Minus => Some(Key::Minus), + KeyCode::Period => Some(Key::Period), + KeyCode::Slash => Some(Key::Slash), + KeyCode::Semicolon => Some(Key::Semicolon), + KeyCode::Equal => Some(Key::Equal), + KeyCode::BracketLeft => Some(Key::LeftBracket), + KeyCode::Backslash => Some(Key::Backslash), + KeyCode::BracketRight => Some(Key::RightBracket), + KeyCode::Backquote => Some(Key::GraveAccent), + KeyCode::CapsLock => Some(Key::CapsLock), + KeyCode::ScrollLock => Some(Key::ScrollLock), + KeyCode::NumLock => Some(Key::NumLock), + KeyCode::PrintScreen => Some(Key::PrintScreen), + KeyCode::Pause => Some(Key::Pause), + KeyCode::Numpad0 => Some(Key::Keypad0), + KeyCode::Numpad1 => Some(Key::Keypad1), + KeyCode::Numpad2 => Some(Key::Keypad2), + KeyCode::Numpad3 => Some(Key::Keypad3), + KeyCode::Numpad4 => Some(Key::Keypad4), + KeyCode::Numpad5 => Some(Key::Keypad5), + KeyCode::Numpad6 => Some(Key::Keypad6), + KeyCode::Numpad7 => Some(Key::Keypad7), + KeyCode::Numpad8 => Some(Key::Keypad8), + KeyCode::Numpad9 => Some(Key::Keypad9), + KeyCode::NumpadDecimal => Some(Key::KeypadDecimal), + KeyCode::NumpadDivide => Some(Key::KeypadDivide), + KeyCode::NumpadMultiply => Some(Key::KeypadMultiply), + KeyCode::NumpadSubtract => Some(Key::KeypadSubtract), + KeyCode::NumpadAdd => Some(Key::KeypadAdd), + KeyCode::NumpadEnter => Some(Key::KeypadEnter), + KeyCode::NumpadEqual => Some(Key::KeypadEqual), _ => None, } } -fn handle_key_modifier(io: &mut Io, key: VirtualKeyCode, down: bool) { - if key == VirtualKeyCode::LShift || key == VirtualKeyCode::RShift { - io.add_key_event(imgui::Key::ModShift, down); - } else if key == VirtualKeyCode::LControl || key == VirtualKeyCode::RControl { - io.add_key_event(imgui::Key::ModCtrl, down); - } else if key == VirtualKeyCode::LAlt || key == VirtualKeyCode::RAlt { - io.add_key_event(imgui::Key::ModAlt, down); - } else if key == VirtualKeyCode::LWin || key == VirtualKeyCode::RWin { - io.add_key_event(imgui::Key::ModSuper, down); - } +fn handle_key_modifier(io: &mut Io, key: KeyCode, down: bool) { + io.add_key_event( + match key { + KeyCode::ShiftLeft | KeyCode::ShiftRight => imgui::Key::ModShift, + KeyCode::ControlLeft | KeyCode::ControlRight => imgui::Key::ModCtrl, + KeyCode::AltLeft | KeyCode::AltRight => imgui::Key::ModAlt, + KeyCode::SuperLeft | KeyCode::SuperRight => imgui::Key::ModSuper, + _ => return, + }, + down, + ); } impl WinitPlatform { @@ -413,9 +415,9 @@ impl WinitPlatform { // we might never see the release event if some other window gets focus. Event::DeviceEvent { event: - DeviceEvent::Key(KeyboardInput { + DeviceEvent::Key(RawKeyEvent { state: ElementState::Released, - virtual_keycode: Some(key), + physical_key: PhysicalKey::Code(key), .. }), .. @@ -428,13 +430,13 @@ impl WinitPlatform { } } fn handle_window_event(&mut self, io: &mut Io, window: &Window, event: &WindowEvent) { - match *event { + match event { WindowEvent::Resized(physical_size) => { let logical_size = physical_size.to_logical(window.scale_factor()); let logical_size = self.scale_size_from_winit(window, logical_size); io.display_size = [logical_size.width as f32, logical_size.height as f32]; } - WindowEvent::ScaleFactorChanged { scale_factor, .. } => { + &WindowEvent::ScaleFactorChanged { scale_factor, .. } => { let hidpi_factor = match self.hidpi_mode { ActiveHiDpiMode::Default => scale_factor, ActiveHiDpiMode::Rounded => scale_factor.round(), @@ -459,21 +461,22 @@ impl WinitPlatform { // We need to track modifiers separately because some system like macOS, will // not reliably send modifier states during certain events like ScreenCapture. // Gotta let the people show off their pretty imgui widgets! - io.add_key_event(Key::ModShift, modifiers.shift()); - io.add_key_event(Key::ModCtrl, modifiers.ctrl()); - io.add_key_event(Key::ModAlt, modifiers.alt()); - io.add_key_event(Key::ModSuper, modifiers.logo()); + io.add_key_event(Key::ModShift, modifiers.state().shift_key()); + io.add_key_event(Key::ModCtrl, modifiers.state().control_key()); + io.add_key_event(Key::ModAlt, modifiers.state().alt_key()); + io.add_key_event(Key::ModSuper, modifiers.state().super_key()); } WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(key), + event: + KeyEvent { + physical_key: PhysicalKey::Code(key), state, + text, .. }, .. } => { - let pressed = state == ElementState::Pressed; + let pressed = *state == ElementState::Pressed; // We map both left and right ctrl to `ModCtrl`, etc. // imgui is told both "left control is pressed" and @@ -481,18 +484,21 @@ impl WinitPlatform { // applications to use either general "ctrl" or a // specific key. Same applies to other modifiers. // https://github.com/ocornut/imgui/issues/5047 - handle_key_modifier(io, key, pressed); + handle_key_modifier(io, *key, pressed); // Add main key event - if let Some(key) = to_imgui_key(key) { + if let Some(key) = to_imgui_key(*key) { io.add_key_event(key, pressed); } - } - WindowEvent::ReceivedCharacter(ch) => { - // Exclude the backspace key ('\u{7f}'). Otherwise we will insert this char and then - // delete it. - if ch != '\u{7f}' { - io.add_input_character(ch) + + if let Some(text) = text { + for ch in text.chars() { + // Exclude the backspace key ('\u{7f}'). Otherwise we will insert this char + // and then delete it. + if ch != '\u{7f}' { + io.add_input_character(ch) + } + } } } WindowEvent::CursorMoved { position, .. } => { @@ -500,7 +506,7 @@ impl WinitPlatform { let position = self.scale_pos_from_winit(window, position); io.add_mouse_pos_event([position.x as f32, position.y as f32]); } - WindowEvent::MouseWheel { + &WindowEvent::MouseWheel { delta, phase: TouchPhase::Moved, .. @@ -524,7 +530,7 @@ impl WinitPlatform { }; io.add_mouse_wheel_event([h, v]); } - WindowEvent::MouseInput { state, button, .. } => { + &WindowEvent::MouseInput { state, button, .. } => { if let Some(mb) = to_imgui_mouse_button(button) { let pressed = state == ElementState::Pressed; io.add_mouse_button_event(mb, pressed);