1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 14:50:03 -04:00

Now a WindowEvent::CloseRequested is received will be sent to egui_winit::State in RawInput

Now when we press the close button from a native window if the window has a open variabile connected the window will be closed
This commit is contained in:
Konkitoman
2023-07-24 16:15:34 +03:00
parent 4d883b8217
commit f4a196cddc
6 changed files with 106 additions and 16 deletions

View File

@@ -449,13 +449,14 @@ impl EpiIntegration {
event: &winit::event::WindowEvent<'_>, event: &winit::event::WindowEvent<'_>,
window_id: &winit::window::WindowId, window_id: &winit::window::WindowId,
egui_winit: &mut egui_winit::State, egui_winit: &mut egui_winit::State,
viewport_id: u64,
) -> EventResponse { ) -> EventResponse {
use winit::event::{ElementState, MouseButton, WindowEvent}; use winit::event::{ElementState, MouseButton, WindowEvent};
match event { match event {
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
log::debug!("Received WindowEvent::CloseRequested"); log::debug!("Received WindowEvent::CloseRequested");
self.close = app.on_close_event(); self.close = app.on_close_event() && viewport_id == 0;
log::debug!("App::on_close_event returned {}", self.close); log::debug!("App::on_close_event returned {}", self.close);
} }
WindowEvent::Destroyed => { WindowEvent::Destroyed => {

View File

@@ -75,7 +75,8 @@ trait WinitApp {
fn window(&self, window_id: winit::window::WindowId) -> Option<&winit::window::Window>; fn window(&self, window_id: winit::window::WindowId) -> Option<&winit::window::Window>;
fn get_window_id(&self, id: u64) -> Option<winit::window::WindowId>; fn get_window_winit_id(&self, id: u64) -> Option<winit::window::WindowId>;
fn get_window_id(&self, id: &winit::window::WindowId) -> Option<u64>;
fn save_and_destroy(&mut self); fn save_and_destroy(&mut self);
@@ -167,7 +168,7 @@ fn run_and_return(
}) => { }) => {
if winit_app.frame_nr() == *frame_nr { if winit_app.frame_nr() == *frame_nr {
log::trace!("UserEvent::RequestRepaint scheduling repaint at {when:?}"); log::trace!("UserEvent::RequestRepaint scheduling repaint at {when:?}");
if let Some(window_id) = winit_app.get_window_id(*window_id) { if let Some(window_id) = winit_app.get_window_winit_id(*window_id) {
vec![EventResult::RepaintAt(window_id, *when)] vec![EventResult::RepaintAt(window_id, *when)]
} else { } else {
vec![EventResult::Wait] vec![EventResult::Wait]
@@ -320,7 +321,7 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp +
window_id, window_id,
}) => { }) => {
if winit_app.frame_nr() == frame_nr { if winit_app.frame_nr() == frame_nr {
if let Some(window_id) = winit_app.get_window_id(window_id) { if let Some(window_id) = winit_app.get_window_winit_id(window_id) {
vec![EventResult::RepaintAt(window_id, when)] vec![EventResult::RepaintAt(window_id, when)]
} else { } else {
vec![EventResult::Wait] vec![EventResult::Wait]
@@ -960,7 +961,7 @@ mod glow_integration {
.flatten() .flatten()
} }
fn get_window_id(&self, id: u64) -> Option<winit::window::WindowId> { fn get_window_winit_id(&self, id: u64) -> Option<winit::window::WindowId> {
self.running self.running
.as_ref() .as_ref()
.map(|r| { .map(|r| {
@@ -974,6 +975,22 @@ mod glow_integration {
.flatten() .flatten()
} }
fn get_window_id(&self, id: &winit::window::WindowId) -> Option<u64> {
self.running
.as_ref()
.map(|r| {
for window in r.gl_window.windows.iter() {
if let Some(win) = &window.window {
if win.id() == *id {
return Some(window.window_id);
}
}
}
None
})
.flatten()
}
fn save_and_destroy(&mut self) { fn save_and_destroy(&mut self) {
if let Some(mut running) = self.running.take() { if let Some(mut running) = self.running.take() {
running running
@@ -1319,7 +1336,25 @@ mod glow_integration {
} }
} }
winit::event::WindowEvent::CloseRequested winit::event::WindowEvent::CloseRequested
if running.integration.should_close() => if running
.gl_window
.windows
.iter()
.flat_map(|window| {
if let Some(win) = &window.window {
if win.id() == *window_id {
Some(window.window_id)
} else {
None
}
} else {
None
}
})
.flat_map(|id| if id == 0 { Some(()) } else { None })
.count()
== 1
&& running.integration.should_close() =>
{ {
log::debug!("Received WindowEvent::CloseRequested"); log::debug!("Received WindowEvent::CloseRequested");
return Ok(EventResult::Exit); return Ok(EventResult::Exit);
@@ -1335,6 +1370,7 @@ mod glow_integration {
event, event,
window_id, window_id,
window.egui_winit.as_mut().unwrap(), window.egui_winit.as_mut().unwrap(),
window.window_id,
); );
} }
} }
@@ -1617,7 +1653,7 @@ mod wgpu_integration {
self.window.as_ref() self.window.as_ref()
} }
fn get_window_id(&self, id: u64) -> Option<winit::window::WindowId> { fn get_window_winit_id(&self, id: u64) -> Option<winit::window::WindowId> {
if id == 0 { if id == 0 {
return self.window.as_ref().map(|w| w.id()); return self.window.as_ref().map(|w| w.id());
} }
@@ -1849,6 +1885,10 @@ mod wgpu_integration {
// }) // })
// //
} }
fn get_window_id(&self, id: &winit::window::WindowId) -> Option<u64> {
todo!()
}
} }
pub fn run_wgpu( pub fn run_wgpu(

View File

@@ -372,8 +372,16 @@ impl State {
} }
// Things that may require repaint: // Things that may require repaint:
WindowEvent::CloseRequested WindowEvent::CloseRequested => {
| WindowEvent::CursorEntered { .. } self.egui_input
.events
.push(egui::Event::WindowEvent(egui::WindowEvent::CloseRequested));
EventResponse {
consumed: true,
repaint: true,
}
}
WindowEvent::CursorEntered { .. }
| WindowEvent::Destroyed | WindowEvent::Destroyed
| WindowEvent::Occluded(_) | WindowEvent::Occluded(_)
| WindowEvent::Resized(_) | WindowEvent::Resized(_)

View File

@@ -432,7 +432,7 @@ impl<'open> Window<'open> {
) { ) {
let Window { let Window {
title, title,
open, mut open,
area, area,
frame, frame,
resize, resize,
@@ -444,8 +444,21 @@ impl<'open> Window<'open> {
mut window_builder, mut window_builder,
} = self; } = self;
let is_explicitly_closed = matches!(open, Some(false)); let is_open = if let Some(open) = &mut open {
let is_open = !is_explicitly_closed || ctx.memory(|mem| mem.everything_is_visible()); if let Some(tmp_open) = ctx.data_mut(|data| {
let tmp = data.get_persisted::<bool>(area.id.with("_open"));
data.remove::<bool>(area.id.with("_open"));
tmp
}) {
**open = tmp_open;
}
**open
} else {
true
};
// let is_explicitly_closed = matches!(open, Some(false));
let is_open = is_open || ctx.memory(|mem| mem.everything_is_visible());
if !embedded { if !embedded {
if !is_open { if !is_open {
return; return;
@@ -456,10 +469,29 @@ impl<'open> Window<'open> {
window_builder.with_inner_size((size.x as u32 + 1, size.y as u32 + 1)); window_builder.with_inner_size((size.x as u32 + 1, size.y as u32 + 1));
} }
let area_id = area.id;
ctx.create_viewport( ctx.create_viewport(
window_builder, window_builder,
move |ctx, viewport_id, parent_viewport_id| { move |ctx, viewport_id, parent_viewport_id| {
let mut frame = frame.unwrap_or(Frame::window(&ctx.style())).rounding(0.0); let mut frame = frame.unwrap_or(Frame::window(&ctx.style())).rounding(0.0);
let count = ctx.input(|input| {
input
.events
.iter()
.filter(|event| {
if let Event::WindowEvent(WindowEvent::CloseRequested) = **event {
true
} else {
false
}
})
.count()
});
if count > 0 {
ctx.data_mut(|data| data.insert_persisted(area_id.with("_open"), false));
ctx.request_repaint_viewport(parent_viewport_id);
}
CentralPanel::default().frame(frame).show(ctx, |ui| { CentralPanel::default().frame(frame).show(ctx, |ui| {
Some(add_contents(ui, viewport_id, parent_viewport_id)) Some(add_contents(ui, viewport_id, parent_viewport_id))
}); });

View File

@@ -302,6 +302,14 @@ pub enum Event {
/// An assistive technology (e.g. screen reader) requested an action. /// An assistive technology (e.g. screen reader) requested an action.
#[cfg(feature = "accesskit")] #[cfg(feature = "accesskit")]
AccessKitActionRequest(accesskit::ActionRequest), AccessKitActionRequest(accesskit::ActionRequest),
WindowEvent(WindowEvent),
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum WindowEvent {
CloseRequested,
} }
/// Mouse button (or similar for touch input) /// Mouse button (or similar for touch input)

View File

@@ -39,16 +39,17 @@ fn main() -> Result<(), eframe::Error> {
let clone = window1_embedded.clone(); let clone = window1_embedded.clone();
let embedded = *window1_embedded.read().unwrap(); let embedded = *window1_embedded.read().unwrap();
egui::CollapsingHeader::new("Show Test1").show(ui, |ui| { egui::CollapsingHeader::new("Show Test1").show(ui, |ui| {
egui::Window::new("Test1") egui::Window::new("Test1").embedded(embedded).show(
.embedded(embedded) ctx,
.show(ctx, move |ui, _, _| { move |ui, id, parent_id| {
ui.checkbox(&mut *clone.write().unwrap(), "Should embedd?"); ui.checkbox(&mut *clone.write().unwrap(), "Should embedd?");
let ctx = ui.ctx().clone(); let ctx = ui.ctx().clone();
ui.label(format!( ui.label(format!(
"Current rendering window: {}", "Current rendering window: {}",
ctx.current_rendering_viewport() ctx.current_rendering_viewport()
)); ));
}); },
);
}); });
let clone = window2_embedded.clone(); let clone = window2_embedded.clone();
let embedded = *window2_embedded.read().unwrap(); let embedded = *window2_embedded.read().unwrap();