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

Experiment with an improved event design #1374

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1d9c2a4
Draft improved event API
Osspial Jan 6, 2020
46176f3
Modify press flags
Osspial Jan 7, 2020
896f042
Unify KeyPress and PointerPress flags
Osspial Jan 7, 2020
761e456
Add AppEvent and ScrollStarted/Ended events
Osspial Jan 8, 2020
986be4e
Rename Key to LogicalKey
Osspial Jan 8, 2020
0afc10c
Implement opaque press events
Osspial May 15, 2020
68761e6
Address @Ralith's feedback
Osspial May 16, 2020
6024560
Redefine PointerButton
Osspial May 16, 2020
63dae5d
Make this compile on Windows
Osspial May 16, 2020
5f7cf2a
Reorder events.rs
Osspial May 16, 2020
6adde34
Make examples, docs, and tests work
Osspial May 16, 2020
0c3bc38
format all the things
Osspial May 16, 2020
d06f58f
Make serde work
Osspial May 16, 2020
510f005
Make serde tests work
Osspial May 17, 2020
bb5dd6b
Rename KeyPress to Key
Osspial May 20, 2020
852b292
Rename PointerPress to PointerButton
Osspial May 20, 2020
9f3fc7b
Format
Osspial May 20, 2020
f031607
Add constructors and setters to event structs
Osspial May 20, 2020
304cbf3
Reorder some things
Osspial May 20, 2020
8ef7b1e
Add PointerButton serialization styles support
Osspial May 21, 2020
b9e152a
Add pen events to the public API
Osspial May 22, 2020
b5bf39a
Fully implement new pointer API on Windows
Osspial May 22, 2020
851a7ea
Distinguish between touch and pen IDs
Osspial May 22, 2020
61f2934
Implement new pen API on Windows
Osspial May 25, 2020
eea84e3
Remove redundant pointermoved from wm_touch handler
Osspial May 25, 2020
4db1d6e
Fix Pointer(Created|Entered) events being sent after PointerDestroyed
Osspial May 25, 2020
201de6c
Include scroll events in example
Osspial May 25, 2020
253df57
Remove Pointer from Scroll events
Osspial May 25, 2020
23ce444
Use WM_POINTER for mouse events
Osspial May 25, 2020
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
25 changes: 8 additions & 17 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{thread, time};

use winit::{
event::{Event, KeyboardInput, WindowEvent},
event::{Event, LogicalKey, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
Expand Down Expand Up @@ -37,7 +37,6 @@ fn main() {
let mut close_requested = false;

event_loop.run(move |event, _, control_flow| {
use winit::event::{ElementState, StartCause, VirtualKeyCode};
println!("{:?}", event);
match event {
Event::NewEvents(start_cause) => {
Expand All @@ -46,36 +45,28 @@ fn main() {
_ => false,
}
}
Event::WindowEvent { event, .. } => match event {
Event::WindowEvent(_, event) => match event {
WindowEvent::CloseRequested => {
close_requested = true;
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(virtual_code),
state: ElementState::Pressed,
..
},
..
} => match virtual_code {
VirtualKeyCode::Key1 => {
WindowEvent::KeyPress(e) if e.is_down() => match e.logical_key() {
Some(LogicalKey::Key1) => {
mode = Mode::Wait;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::Key2 => {
Some(LogicalKey::Key2) => {
mode = Mode::WaitUntil;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::Key3 => {
Some(LogicalKey::Key3) => {
mode = Mode::Poll;
println!("\nmode: {:?}\n", mode);
}
VirtualKeyCode::R => {
Some(LogicalKey::R) => {
request_redraw = !request_redraw;
println!("\nrequest_redraw: {}\n", request_redraw);
}
VirtualKeyCode::Escape => {
Some(LogicalKey::Escape) => {
close_requested = true;
}
_ => (),
Expand Down
20 changes: 3 additions & 17 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{CursorIcon, WindowBuilder},
};
Expand All @@ -17,18 +17,7 @@ fn main() {
*control_flow = ControlFlow::Wait;

match event {
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
..
},
..
},
..
} => {
Event::WindowEvent(_, WindowEvent::KeyPress(e)) if e.is_down() => {
println!("Setting cursor to \"{:?}\"", CURSORS[cursor_idx]);
window.set_cursor_icon(CURSORS[cursor_idx]);
if cursor_idx < CURSORS.len() - 1 {
Expand All @@ -37,10 +26,7 @@ fn main() {
cursor_idx = 0;
}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
Event::WindowEvent(_, WindowEvent::CloseRequested) => {
*control_flow = ControlFlow::Exit;
return;
}
Expand Down
44 changes: 19 additions & 25 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use winit::{
event::{DeviceEvent, ElementState, Event, KeyboardInput, ModifiersState, WindowEvent},
event::{Event, LogicalKey, ModifiersState, RawPointerEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
Expand All @@ -19,34 +19,28 @@ fn main() {
*control_flow = ControlFlow::Wait;

match event {
Event::WindowEvent { event, .. } => match event {
Event::WindowEvent(_, event) => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Released,
virtual_keycode: Some(key),
..
},
..
} => {
use winit::event::VirtualKeyCode::*;
match key {
Escape => *control_flow = ControlFlow::Exit,
G => window.set_cursor_grab(!modifiers.shift()).unwrap(),
H => window.set_cursor_visible(modifiers.shift()),
_ => (),
}
}
WindowEvent::KeyPress(e) if e.is_down() => match e.logical_key() {
Some(LogicalKey::Escape) => *control_flow = ControlFlow::Exit,
Some(LogicalKey::G) => window.set_cursor_grab(!modifiers.shift()).unwrap(),
Some(LogicalKey::H) => window.set_cursor_visible(modifiers.shift()),
_ => (),
},
WindowEvent::ModifiersChanged(m) => modifiers = m,
_ => (),
},
Event::DeviceEvent { event, .. } => match event {
DeviceEvent::MouseMotion { delta } => println!("mouse moved: {:?}", delta),
DeviceEvent::Button { button, state } => match state {
ElementState::Pressed => println!("mouse button {} pressed", button),
ElementState::Released => println!("mouse button {} released", button),
},
Event::RawPointerEvent(_, event) => match event {
RawPointerEvent::MovedRelative(delta) => println!("pointer moved: {:?}", delta),
RawPointerEvent::MovedAbsolute(position) => {
println!("pointer moved to: {:?}", position)
}
RawPointerEvent::Press(e) if e.is_down() => {
println!("pointer button {:?} pressed", e.button())
}
RawPointerEvent::Press(e) if e.is_up() => {
println!("pointer button {:?} released", e.button())
}
_ => (),
},
_ => (),
Expand Down
5 changes: 1 addition & 4 deletions examples/custom_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ fn main() {

match event {
Event::UserEvent(event) => println!("user event: {:?}", event),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::WindowEvent(_, WindowEvent::CloseRequested) => *control_flow = ControlFlow::Exit,
_ => (),
}
});
Expand Down
24 changes: 8 additions & 16 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io::{stdin, stdout, Write};
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event::{Event, LogicalKey, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::monitor::{MonitorHandle, VideoMode};
use winit::window::{Fullscreen, WindowBuilder};
Expand Down Expand Up @@ -34,33 +34,25 @@ fn main() {
*control_flow = ControlFlow::Wait;

match event {
Event::WindowEvent { event, .. } => match event {
Event::WindowEvent(_, event) => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(virtual_code),
state,
..
},
..
} => match (virtual_code, state) {
(VirtualKeyCode::Escape, _) => *control_flow = ControlFlow::Exit,
(VirtualKeyCode::F, ElementState::Pressed) => {
WindowEvent::KeyPress(e) if e.is_down() => match e.logical_key() {
Some(LogicalKey::Escape) => *control_flow = ControlFlow::Exit,
Some(LogicalKey::F) => {
if window.fullscreen().is_some() {
window.set_fullscreen(None);
} else {
window.set_fullscreen(fullscreen.clone());
}
}
(VirtualKeyCode::S, ElementState::Pressed) => {
Some(LogicalKey::S) => {
println!("window.fullscreen {:?}", window.fullscreen());
}
(VirtualKeyCode::M, ElementState::Pressed) => {
Some(LogicalKey::M) => {
is_maximized = !is_maximized;
window.set_maximized(is_maximized);
}
(VirtualKeyCode::D, ElementState::Pressed) => {
Some(LogicalKey::D) => {
decorations = !decorations;
window.set_decorations(decorations);
}
Expand Down
90 changes: 37 additions & 53 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use winit::{
event::{Event, KeyboardInput, WindowEvent},
event::{Event, LogicalKey, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
Expand All @@ -16,67 +16,51 @@ fn main() {
let mut close_requested = false;

event_loop.run(move |event, _, control_flow| {
use winit::event::{
ElementState::Released,
VirtualKeyCode::{N, Y},
};
*control_flow = ControlFlow::Wait;

match event {
Event::WindowEvent { event, .. } => {
match event {
WindowEvent::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.
Event::WindowEvent(_, event) => match event {
WindowEvent::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;
// 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.
}
WindowEvent::KeyboardInput {
input:
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!");
// 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.
}
WindowEvent::KeyPress(e) if e.is_up() => match e.logical_key() {
Some(LogicalKey::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.
*control_flow = ControlFlow::Exit;
}
}
N => {
if close_requested {
println!("Your window will continue to stay by your side.");
close_requested = false;
}
}
_ => (),
// 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.
*control_flow = ControlFlow::Exit;
}
}
Some(LogicalKey::N) => {
if close_requested {
println!("Your window will continue to stay by your side.");
close_requested = false;
}
}
_ => (),
}
}
},
_ => (),
},
_ => (),
}
});
Expand Down
5 changes: 1 addition & 4 deletions examples/min_max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ fn main() {
println!("{:?}", event);

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::WindowEvent(_, WindowEvent::CloseRequested) => *control_flow = ControlFlow::Exit,
_ => (),
}
});
Expand Down
21 changes: 6 additions & 15 deletions examples/minimize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate winit;

use winit::event::{Event, VirtualKeyCode, WindowEvent};
use winit::event::{Event, LogicalKey, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;

Expand All @@ -17,22 +17,13 @@ fn main() {
*control_flow = ControlFlow::Wait;

match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::WindowEvent(_, WindowEvent::CloseRequested) => *control_flow = ControlFlow::Exit,

// Keyboard input event to handle minimize via a hotkey
Event::WindowEvent {
event: WindowEvent::KeyboardInput { input, .. },
window_id,
} => {
if window_id == window.id() {
// Pressing the 'M' key will minimize the window
if input.virtual_keycode == Some(VirtualKeyCode::M) {
window.set_minimized(true);
}
}
Event::WindowEvent(window_id, WindowEvent::KeyPress(e))
if e.is_down() && e.logical_key_is(LogicalKey::M) && window_id == window.id() =>
{
window.set_minimized(true)
}
_ => (),
}
Expand Down
Loading