Skip to content

Commit

Permalink
Copy modified-handling code for viewport renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
dbr committed Mar 3, 2023
1 parent 0863512 commit b781572
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion imgui-winit-glow-renderer-viewports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -891,6 +902,18 @@ struct PlatformBackend {
event_queue: Rc<RefCell<VecDeque<ViewportEvent>>>,
}

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 {
Expand Down

0 comments on commit b781572

Please sign in to comment.