From 842cd0700b8893c820ad16e30460c607066111e1 Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 1 Feb 2023 11:10:26 +1030 Subject: [PATCH 1/5] Move ModifiersChanged handler No longer needs to be handled outside of the main event match statement --- imgui-winit-support/src/lib.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/imgui-winit-support/src/lib.rs b/imgui-winit-support/src/lib.rs index 9bf3ef5dd..caba62900 100644 --- a/imgui-winit-support/src/lib.rs +++ b/imgui-winit-support/src/lib.rs @@ -395,16 +395,6 @@ impl WinitPlatform { window_id, ref event, } if window_id == window.id() => { - // 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! - if let WindowEvent::ModifiersChanged(modifiers) = event { - 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()); - } - self.handle_window_event(io, window, event); } // Track key release events outside our window. If we don't do this, @@ -453,6 +443,15 @@ impl WinitPlatform { 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::ModifiersChanged(modifiers) => { + // 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()); + } WindowEvent::KeyboardInput { input: KeyboardInput { From 0863512c43d552d6da40dbdb693f2aaa83ee59a1 Mon Sep 17 00:00:00 2001 From: dbr Date: Wed, 1 Feb 2023 13:23:46 +1030 Subject: [PATCH 2/5] Handle modifiers properly in winit-support Previously only the "LeftCtrl" was set, not ModCtrl (same for shift etc). Although winit also act weirdly, and once the window regains focus it starts starts triggering ModifierChanged which also sets ModCtrl, so this problem wasn't always noticeable --- imgui-winit-support/src/lib.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/imgui-winit-support/src/lib.rs b/imgui-winit-support/src/lib.rs index caba62900..47806ccee 100644 --- a/imgui-winit-support/src/lib.rs +++ b/imgui-winit-support/src/lib.rs @@ -291,6 +291,18 @@ fn to_imgui_key(keycode: VirtualKeyCode) -> Option { } } +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); + } +} + impl WinitPlatform { /// Initializes a winit platform instance and configures imgui. /// @@ -461,8 +473,18 @@ impl WinitPlatform { }, .. } => { + let pressed = state == ElementState::Pressed; + + // We map both left and right ctrl to `ModCtrl`, etc. + // imgui is told both "left control is pressed" and + // "consider the control key is pressed". Allows + // 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); + + // Add main key event if let Some(key) = to_imgui_key(key) { - let pressed = state == ElementState::Pressed; io.add_key_event(key, pressed); } } From b781572e3347b294b25999d65ea4c9d6e1607df4 Mon Sep 17 00:00:00 2001 From: dbr Date: Fri, 3 Mar 2023 18:56:52 +1030 Subject: [PATCH 3/5] Copy modified-handling code for viewport renderer --- .../src/lib.rs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/imgui-winit-glow-renderer-viewports/src/lib.rs b/imgui-winit-glow-renderer-viewports/src/lib.rs index 7b5209190..c71251e92 100644 --- a/imgui-winit-glow-renderer-viewports/src/lib.rs +++ b/imgui-winit-glow-renderer-viewports/src/lib.rs @@ -493,8 +493,19 @@ impl Renderer { }, .. } => { + let pressed = state == ElementState::Pressed; + + // We map both left and right ctrl to `ModCtrl`, etc. + // imgui is told both "left control is pressed" and + // "consider the control key is pressed". Allows + // 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); + + // Add main key event if let Some(key) = to_imgui_key(key) { - imgui.io_mut().add_key_event(key, true); + imgui.io_mut().add_key_event(key, pressed); } } winit::event::WindowEvent::ModifiersChanged(modifiers) => { @@ -891,6 +902,18 @@ struct PlatformBackend { event_queue: Rc>>, } +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); + } +} + impl imgui::PlatformViewportBackend for PlatformBackend { fn create_window(&mut self, viewport: &mut imgui::Viewport) { viewport.platform_user_data = Box::into_raw(Box::new(ViewportData { From 4e146f2ef05ae4bd6d932c2b80878fc0a24bef2c Mon Sep 17 00:00:00 2001 From: dbr Date: Fri, 3 Mar 2023 18:57:01 +1030 Subject: [PATCH 4/5] fmt --- imgui-glow-renderer/src/lib.rs | 26 ++++++++++++++----- .../src/lib.rs | 15 ++++++++--- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/imgui-glow-renderer/src/lib.rs b/imgui-glow-renderer/src/lib.rs index a5911f87e..c0131a39f 100644 --- a/imgui-glow-renderer/src/lib.rs +++ b/imgui-glow-renderer/src/lib.rs @@ -285,9 +285,10 @@ impl Renderer { #[cfg(feature = "bind_vertex_array_support")] if self.gl_version.bind_vertex_array_support() { unsafe { - self.vertex_array_object = Some(gl - .create_vertex_array() - .map_err(|err| format!("Error creating vertex array object: {}", err))?); + self.vertex_array_object = Some( + gl.create_vertex_array() + .map_err(|err| format!("Error creating vertex array object: {}", err))?, + ); gl.bind_vertex_array(self.vertex_array_object); } } @@ -572,7 +573,9 @@ impl TextureMap for SimpleTextureMap { #[inline(always)] fn gl_texture(&self, imgui_texture: imgui::TextureId) -> Option { #[allow(clippy::cast_possible_truncation)] - Some(glow::NativeTexture(NonZeroU32::new(imgui_texture.id() as _).unwrap())) + Some(glow::NativeTexture( + NonZeroU32::new(imgui_texture.id() as _).unwrap(), + )) } } @@ -646,7 +649,10 @@ impl GlStateBackup { fn post_init(&mut self, gl: &Context) { #[allow(clippy::cast_sign_loss)] unsafe { - gl.bind_texture(glow::TEXTURE_2D, to_native_gl(self.texture, glow::NativeTexture)); + gl.bind_texture( + glow::TEXTURE_2D, + to_native_gl(self.texture, glow::NativeTexture), + ); } } @@ -705,7 +711,10 @@ impl GlStateBackup { #![allow(clippy::cast_sign_loss)] unsafe { gl.use_program(to_native_gl(self.program, glow::NativeProgram)); - gl.bind_texture(glow::TEXTURE_2D, to_native_gl(self.texture, glow::NativeTexture)); + gl.bind_texture( + glow::TEXTURE_2D, + to_native_gl(self.texture, glow::NativeTexture), + ); #[cfg(feature = "bind_sampler_support")] if let Some(sampler) = self.sampler { gl.bind_sampler(0, to_native_gl(sampler, glow::NativeSampler)); @@ -715,7 +724,10 @@ impl GlStateBackup { if let Some(vao) = self.vertex_array_object { gl.bind_vertex_array(to_native_gl(vao, glow::NativeVertexArray)); } - gl.bind_buffer(glow::ARRAY_BUFFER, to_native_gl(self.array_buffer, glow::NativeBuffer)); + gl.bind_buffer( + glow::ARRAY_BUFFER, + to_native_gl(self.array_buffer, glow::NativeBuffer), + ); gl.blend_equation_separate( self.blend_equation_rgb as _, self.blend_equation_alpha as _, diff --git a/imgui-winit-glow-renderer-viewports/src/lib.rs b/imgui-winit-glow-renderer-viewports/src/lib.rs index c71251e92..5d3b13cd0 100644 --- a/imgui-winit-glow-renderer-viewports/src/lib.rs +++ b/imgui-winit-glow-renderer-viewports/src/lib.rs @@ -305,10 +305,19 @@ impl GlStateBackup { context.bind_vertex_array(to_native_gl(self.vao, glow::NativeVertexArray)); - context.bind_buffer(glow::ARRAY_BUFFER, to_native_gl(self.vbo, glow::NativeBuffer)); - context.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, to_native_gl(self.ibo, glow::NativeBuffer)); + context.bind_buffer( + glow::ARRAY_BUFFER, + to_native_gl(self.vbo, glow::NativeBuffer), + ); + context.bind_buffer( + glow::ELEMENT_ARRAY_BUFFER, + to_native_gl(self.ibo, glow::NativeBuffer), + ); - context.bind_texture(glow::TEXTURE_2D, to_native_gl(self.texture, glow::NativeTexture)); + context.bind_texture( + glow::TEXTURE_2D, + to_native_gl(self.texture, glow::NativeTexture), + ); context.active_texture(self.active_texture); context.use_program(to_native_gl(self.program, glow::NativeProgram)); From b49572871ab2efe72ae430b6a8497359a50b091f Mon Sep 17 00:00:00 2001 From: dbr Date: Fri, 3 Mar 2023 19:21:40 +1030 Subject: [PATCH 5/5] Fix copypaste-errors --- imgui-winit-glow-renderer-viewports/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/imgui-winit-glow-renderer-viewports/src/lib.rs b/imgui-winit-glow-renderer-viewports/src/lib.rs index 5d3b13cd0..a61f30d17 100644 --- a/imgui-winit-glow-renderer-viewports/src/lib.rs +++ b/imgui-winit-glow-renderer-viewports/src/lib.rs @@ -497,7 +497,7 @@ impl Renderer { input: KeyboardInput { virtual_keycode: Some(key), - state: ElementState::Pressed, + state, .. }, .. @@ -510,7 +510,7 @@ impl Renderer { // 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(imgui.io_mut(), key, pressed); // Add main key event if let Some(key) = to_imgui_key(key) { @@ -911,7 +911,7 @@ struct PlatformBackend { event_queue: Rc>>, } -fn handle_key_modifier(io: &mut Io, key: VirtualKeyCode, down: bool) { +fn handle_key_modifier(io: &mut imgui::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 {