egui_glium: don't take control of the control_flow

Closes https://github.com/emilk/egui/issues/434
This commit is contained in:
Emil Ernerfeldt 2021-06-24 16:24:05 +02:00
parent 182eb32b95
commit 749c5cbdc8
4 changed files with 47 additions and 18 deletions

View file

@ -5,6 +5,7 @@ All notable changes to the `egui_glium` integration will be noted in this file.
## Unreleased ## Unreleased
* Add `EguiGlium::is_quit_event` to replace `control_flow` arguemnt to `EguiGlium::on_event`.
* [Fix modifier key for zoom with mouse wheel on Mac](https://github.com/emilk/egui/issues/401) * [Fix modifier key for zoom with mouse wheel on Mac](https://github.com/emilk/egui/issues/401)
## 0.12.0 - 2021-05-10 ## 0.12.0 - 2021-05-10

View file

@ -90,7 +90,12 @@ fn main() {
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(), glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
glutin::event::Event::WindowEvent { event, .. } => { glutin::event::Event::WindowEvent { event, .. } => {
egui.on_event(event, control_flow); if egui.is_quit_event(&event) {
*control_flow = glium::glutin::event_loop::ControlFlow::Exit;
}
egui.on_event(&event);
display.gl_window().window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead display.gl_window().window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
} }

View file

@ -326,11 +326,15 @@ pub fn run(mut app: Box<dyn epi::App>, nativve_options: epi::NativeOptions) -> !
glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(), glutin::event::Event::RedrawRequested(_) if !cfg!(windows) => redraw(),
glutin::event::Event::WindowEvent { event, .. } => { glutin::event::Event::WindowEvent { event, .. } => {
if egui.is_quit_event(&event) {
*control_flow = glium::glutin::event_loop::ControlFlow::Exit;
}
if let glutin::event::WindowEvent::Focused(new_focused) = event { if let glutin::event::WindowEvent::Focused(new_focused) = event {
is_focused = new_focused; is_focused = new_focused;
} }
egui.on_event(&event, control_flow); egui.on_event(&event);
display.gl_window().window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead display.gl_window().window().request_redraw(); // TODO: ask egui if the events warrants a repaint instead
} }

View file

@ -38,7 +38,6 @@ use {
glium::glutin::{ glium::glutin::{
self, self,
event::{Force, VirtualKeyCode}, event::{Force, VirtualKeyCode},
event_loop::ControlFlow,
}, },
std::hash::{Hash, Hasher}, std::hash::{Hash, Hasher},
}; };
@ -62,16 +61,43 @@ impl GliumInputState {
} }
} }
/// Helper: checks for Alt-F4 (windows/linux) or Cmd-Q (Mac)
pub fn is_quit_shortcut(
input_state: &GliumInputState,
input: &glium::glutin::event::KeyboardInput,
) -> bool {
if cfg!(target_os = "macos") {
input.state == glutin::event::ElementState::Pressed
&& input_state.raw.modifiers.mac_cmd
&& input.virtual_keycode == Some(VirtualKeyCode::Q)
} else {
input.state == glutin::event::ElementState::Pressed
&& input_state.raw.modifiers.alt
&& input.virtual_keycode == Some(VirtualKeyCode::F4)
}
}
/// Is this a close event or a Cmd-Q/Alt-F4 keyboard command?
pub fn is_quit_event(
input_state: &GliumInputState,
event: &glutin::event::WindowEvent<'_>,
) -> bool {
use glutin::event::WindowEvent;
match event {
WindowEvent::CloseRequested | WindowEvent::Destroyed => true,
WindowEvent::KeyboardInput { input, .. } => is_quit_shortcut(input_state, input),
_ => false,
}
}
pub fn input_to_egui( pub fn input_to_egui(
pixels_per_point: f32, pixels_per_point: f32,
event: &glutin::event::WindowEvent<'_>, event: &glutin::event::WindowEvent<'_>,
clipboard: Option<&mut ClipboardContext>, clipboard: Option<&mut ClipboardContext>,
input_state: &mut GliumInputState, input_state: &mut GliumInputState,
control_flow: &mut ControlFlow,
) { ) {
use glutin::event::WindowEvent; use glutin::event::WindowEvent;
match event { match event {
WindowEvent::CloseRequested | WindowEvent::Destroyed => *control_flow = ControlFlow::Exit,
WindowEvent::ScaleFactorChanged { scale_factor, .. } => { WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
input_state.raw.pixels_per_point = Some(*scale_factor as f32); input_state.raw.pixels_per_point = Some(*scale_factor as f32);
} }
@ -138,13 +164,6 @@ pub fn input_to_egui(
} }
if pressed { if pressed {
if cfg!(target_os = "macos")
&& input_state.raw.modifiers.mac_cmd
&& keycode == VirtualKeyCode::Q
{
*control_flow = ControlFlow::Exit;
}
// VirtualKeyCode::Paste etc in winit are broken/untrustworthy, // VirtualKeyCode::Paste etc in winit are broken/untrustworthy,
// so we detect these things manually: // so we detect these things manually:
if is_cut_command(input_state.raw.modifiers, keycode) { if is_cut_command(input_state.raw.modifiers, keycode) {
@ -482,20 +501,20 @@ impl EguiGlium {
(&self.egui_ctx, &mut self.painter) (&self.egui_ctx, &mut self.painter)
} }
pub fn on_event( pub fn on_event(&mut self, event: &glium::glutin::event::WindowEvent<'_>) {
&mut self,
event: &glium::glutin::event::WindowEvent<'_>,
control_flow: &mut glium::glutin::event_loop::ControlFlow,
) {
crate::input_to_egui( crate::input_to_egui(
self.egui_ctx.pixels_per_point(), self.egui_ctx.pixels_per_point(),
&event, &event,
self.clipboard.as_mut(), self.clipboard.as_mut(),
&mut self.input_state, &mut self.input_state,
control_flow,
); );
} }
/// Is this a close event or a Cmd-Q/Alt-F4 keyboard command?
pub fn is_quit_event(&self, event: &glutin::event::WindowEvent<'_>) -> bool {
crate::is_quit_event(&self.input_state, event)
}
pub fn begin_frame(&mut self, display: &glium::Display) { pub fn begin_frame(&mut self, display: &glium::Display) {
let pixels_per_point = self let pixels_per_point = self
.input_state .input_state