Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Closed event with CloseRequested and Destroyed #476

Merged
merged 3 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Corrected `get_position` on macOS to return outer frame position, not content area position.
- Corrected `set_position` on macOS to set outer frame position, not content area position.
- Added `get_inner_position` method to `Window`, which gets the position of the window's client area. This is implemented on all applicable platforms (all desktop platforms other than Wayland, where this isn't possible).
- **Breaking:** the `Closed` event has been replaced by `CloseRequested` and `Destroyed`. To migrate, you typically just need to replace all usages of `Closed` with `CloseRequested`; see example programs for more info. The exception is iOS, where `Closed` must be replaced by `Destroyed`.

# Version 0.12.0 (2018-04-06)

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ fn main() {

events_loop.run_forever(|event| {
match event {
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => {
winit::ControlFlow::Break
},
winit::Event::WindowEvent {
event: winit::WindowEvent::CloseRequested,
..
} => winit::ControlFlow::Break,
_ => winit::ControlFlow::Continue,
}
});
Expand Down
2 changes: 1 addition & 1 deletion examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
cursor_idx = 0;
}
},
Event::WindowEvent { event: WindowEvent::Closed, .. } => {
Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
return ControlFlow::Break;
},
_ => ()
Expand Down
4 changes: 2 additions & 2 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn main() {

match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::Closed => return ControlFlow::Break,
WindowEvent::CloseRequested => return ControlFlow::Break,
WindowEvent::KeyboardInput {
input:
winit::KeyboardInput {
Expand All @@ -57,7 +57,7 @@ fn main() {
window.set_fullscreen(None);
} else {
window.set_fullscreen(Some(window.get_current_monitor()));
}
}
}
(winit::VirtualKeyCode::M, winit::ElementState::Pressed) => {
is_maximized = !is_maximized;
Expand Down
2 changes: 1 addition & 1 deletion examples/grabbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
}
},

WindowEvent::Closed => return ControlFlow::Break,
WindowEvent::CloseRequested => return ControlFlow::Break,

a @ WindowEvent::CursorMoved { .. } => {
println!("{:?}", a);
Expand Down
74 changes: 74 additions & 0 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
extern crate winit;

fn main() {
let mut events_loop = winit::EventsLoop::new();

let _window = winit::WindowBuilder::new()
.with_title("Your faithful window")
.build(&events_loop)
.unwrap();

let mut close_requested = false;

events_loop.run_forever(|event| {
use winit::WindowEvent::*;
use winit::ElementState::Released;
use winit::VirtualKeyCode::{N, Y};

match event {
winit::Event::WindowEvent { event, .. } => match event {
CloseRequested => {
// `CloseRequested` is sent when the close button on the window is pressed (or
// through whatever other mechanisms the window manager provides for closing a
// window). If you don't handle this event, the close button won't actually do
// anything.

// A common thing to do here is prompt the user if they have unsaved work.
// Creating a proper dialog box for that is far beyond the scope of this
// example, so here we'll just respond to the Y and N keys.
println!("Are you ready to bid your window farewell? [Y/N]");
close_requested = true;

// In applications where you can safely close the window without further
// action from the user, this is generally where you'd handle cleanup before
// closing the window. How to close the window is detailed in the handler for
// the Y key.
}
KeyboardInput {
input:
winit::KeyboardInput {
virtual_keycode: Some(virtual_code),
state: Released,
..
},
..
} => match virtual_code {
Y => {
if close_requested {
// This is where you'll want to do any cleanup you need.
println!("Buh-bye!");

// For a single-window application like this, you'd normally just
// break out of the event loop here. If you wanted to keep running the
// event loop (i.e. if it's a multi-window application), you need to
// drop the window. That closes it, and results in `Destroyed` being
// sent.
return winit::ControlFlow::Break;
}
}
N => {
if close_requested {
println!("Your window will continue to stay by your side.");
close_requested = false;
}
}
_ => (),
},
_ => (),
},
_ => (),
}

winit::ControlFlow::Continue
});
}
2 changes: 1 addition & 1 deletion examples/min_max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
println!("{:?}", event);

match event {
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => winit::ControlFlow::Break,
winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. } => winit::ControlFlow::Break,
_ => winit::ControlFlow::Continue,
}
});
Expand Down
35 changes: 17 additions & 18 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
extern crate winit;

use std::collections::HashMap;

fn main() {
let mut events_loop = winit::EventsLoop::new();

let window1 = winit::Window::new(&events_loop).unwrap();
let window2 = winit::Window::new(&events_loop).unwrap();
let window3 = winit::Window::new(&events_loop).unwrap();

let mut num_windows = 3;
let mut windows = HashMap::new();
for _ in 0..3 {
let window = winit::Window::new(&events_loop).unwrap();
windows.insert(window.id(), window);
}

events_loop.run_forever(|event| {
match event {
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, window_id } => {
if window_id == window1.id() {
println!("Window 1 has been closed")
} else if window_id == window2.id() {
println!("Window 2 has been closed")
} else if window_id == window3.id() {
println!("Window 3 has been closed");
} else {
unreachable!()
}
winit::Event::WindowEvent {
event: winit::WindowEvent::CloseRequested,
window_id,
} => {
println!("Window {:?} has received the signal to close", window_id);

// This drops the window, causing it to close.
windows.remove(&window_id);

num_windows -= 1;
if num_windows == 0 {
if windows.is_empty() {
return winit::ControlFlow::Break;
}
},
}
_ => (),
}
winit::ControlFlow::Continue
Expand Down
2 changes: 1 addition & 1 deletion examples/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
events_loop.run_forever(|event| {
println!("{:?}", event);
match event {
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } =>
winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. } =>
winit::ControlFlow::Break,
_ => winit::ControlFlow::Continue,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() {
println!("{:?}", event);

match event {
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => winit::ControlFlow::Break,
winit::Event::WindowEvent { event: winit::WindowEvent::CloseRequested, .. } => winit::ControlFlow::Break,
_ => winit::ControlFlow::Continue,
}
});
Expand Down
7 changes: 4 additions & 3 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ fn main() {
println!("{:?}", event);

match event {
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => {
winit::ControlFlow::Break
},
winit::Event::WindowEvent {
event: winit::WindowEvent::CloseRequested,
..
} => winit::ControlFlow::Break,
_ => winit::ControlFlow::Continue,
}
});
Expand Down
9 changes: 6 additions & 3 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ pub enum WindowEvent {
/// The position of the window has changed.
Moved(i32, i32),

/// The window has been closed.
Closed,
/// The window has been requested to close.
CloseRequested,

/// The window has been destroyed.
Destroyed,

/// A file has been dropped into the window.
DroppedFile(PathBuf),
Expand Down Expand Up @@ -75,7 +78,7 @@ pub enum WindowEvent {

/// An mouse button press has been received.
MouseInput { device_id: DeviceId, state: ElementState, button: MouseButton, modifiers: ModifiersState },


/// Touchpad pressure event.
///
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
//!
//! events_loop.run_forever(|event| {
//! match event {
//! Event::WindowEvent { event: WindowEvent::Closed, .. } => {
//! println!("The window was closed ; stopping");
//! Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
//! println!("The close button was pressed; stopping");
//! ControlFlow::Break
//! },
//! _ => ControlFlow::Continue,
Expand Down Expand Up @@ -127,7 +127,7 @@ pub mod os;
///
/// events_loop.run_forever(|event| {
/// match event {
/// Event::WindowEvent { event: WindowEvent::Closed, .. } => {
/// Event::WindowEvent { event: WindowEvent::CloseRequested, .. } => {
/// ControlFlow::Break
/// },
/// _ => ControlFlow::Continue,
Expand Down
8 changes: 4 additions & 4 deletions src/platform/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@
//! - applicationWillResignActive is Focused(false)
//! - applicationDidEnterBackground is Suspended(true)
//! - applicationWillEnterForeground is Suspended(false)
//! - applicationWillTerminate is Closed
//! - applicationWillTerminate is Destroyed
//!
//! Keep in mind that after Closed event is received every attempt to draw with
//! Keep in mind that after Destroyed event is received every attempt to draw with
//! opengl will result in segfault.
//!
//! Also note that app will not receive Closed event if suspended, it will be SIGKILL'ed
//! Also note that app will not receive Destroyed event if suspended, it will be SIGKILL'ed

#![cfg(target_os = "ios")]

Expand Down Expand Up @@ -459,7 +459,7 @@ fn create_delegate_class() {
// immidiatly after jump
state.events_queue.push_front(Event::WindowEvent {
window_id: RootEventId(WindowId),
event: WindowEvent::Closed,
event: WindowEvent::Destroyed,
});
longjmp(mem::transmute(&mut jmpbuf),1);
}
Expand Down
7 changes: 5 additions & 2 deletions src/platform/linux/wayland/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,11 @@ impl EventsLoop {
{
let mut cleanup_needed = self.cleanup_needed.lock().unwrap();
if *cleanup_needed {
evq.state().get_mut(&self.store).cleanup();
let pruned = evq.state().get_mut(&self.store).cleanup();
*cleanup_needed = false;
for wid in pruned {
sink.send_event(::WindowEvent::Destroyed, wid);
}
}
}
// process pending resize/refresh
Expand All @@ -355,7 +358,7 @@ impl EventsLoop {
sink.send_event(::WindowEvent::Refresh, wid);
}
if closed {
sink.send_event(::WindowEvent::Closed, wid);
sink.send_event(::WindowEvent::CloseRequested, wid);
}
}
)
Expand Down
5 changes: 4 additions & 1 deletion src/platform/linux/wayland/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,19 @@ impl WindowStore {
None
}

pub fn cleanup(&mut self) {
pub fn cleanup(&mut self) -> Vec<WindowId> {
let mut pruned = Vec::new();
self.windows.retain(|w| {
if *w.kill_switch.lock().unwrap() {
// window is dead, cleanup
pruned.push(make_wid(&w.surface));
w.surface.destroy();
false
} else {
true
}
});
pruned
}

pub fn for_each<F>(&mut self, mut f: F)
Expand Down
Loading