Skip to content

Commit

Permalink
fix(events): properly fire WindowEvent::Destroyed on Linux (#349)
Browse files Browse the repository at this point in the history
* fix(events): properly fire `WindowEvent::Destroyed` on Linux

* remove unused variable

* fix docs
  • Loading branch information
lucasfernog committed Mar 26, 2022
1 parent 84c677f commit cdd4ac3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-gtk-destroyed-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Properly fire `WindowEvent::Destroyed` on Linux when the `Window` is dropped.
31 changes: 22 additions & 9 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use tao::{

#[allow(clippy::single_match)]
fn main() {
env_logger::init();
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_title("A fantastic window!")
.with_inner_size(tao::dpi::LogicalSize::new(128.0, 128.0))
.build(&event_loop)
.unwrap();
let mut window = Some(
WindowBuilder::new()
.with_title("A fantastic window!")
.with_inner_size(tao::dpi::LogicalSize::new(128.0, 128.0))
.build(&event_loop)
.unwrap(),
);

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand All @@ -25,11 +26,23 @@ fn main() {
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
window_id: _,
..
} if window_id == window.id() => *control_flow = ControlFlow::Exit,
} => {
// drop the window to fire the `Destroyed` event
window = None;
}
Event::WindowEvent {
event: WindowEvent::Destroyed,
window_id: _,
..
} => {
*control_flow = ControlFlow::Exit;
}
Event::MainEventsCleared => {
window.request_redraw();
if let Some(w) = &window {
w.request_redraw();
}
}
_ => (),
}
Expand Down
5 changes: 5 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ pub enum WindowEvent<'a> {
CloseRequested,

/// The window has been destroyed.
///
/// ## Platform-specific
///
/// - **Windows / Linux:** Only fired if the [`crate::window::Window`] is dropped.
/// - **macOS:** Fired if the [`crate::window::Window`] is dropped or the dock `Quit` item is clicked.
Destroyed,

/// A file has been dropped into the window.
Expand Down
3 changes: 1 addition & 2 deletions src/platform_impl/linux/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ impl<T: 'static> EventLoop<T> {
});

let tx_clone = event_tx.clone();
window.connect_destroy_event(move |_, _| {
window.connect_destroy(move |_| {
if let Err(e) = tx_clone.send(Event::WindowEvent {
window_id: RootWindowId(id),
event: WindowEvent::Destroyed,
Expand All @@ -461,7 +461,6 @@ impl<T: 'static> EventLoop<T> {
e
);
}
Inhibit(false)
});

let tx_clone = event_tx.clone();
Expand Down

0 comments on commit cdd4ac3

Please sign in to comment.