Skip to content

Commit

Permalink
Split enum Key into Key, Action
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Oct 15, 2023
1 parent 44832ea commit 8610913
Show file tree
Hide file tree
Showing 18 changed files with 1,101 additions and 1,121 deletions.
4 changes: 2 additions & 2 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::Key,
keyboard::{Action, Key},
window::WindowBuilder,
};

Expand Down Expand Up @@ -88,7 +88,7 @@ fn main() -> Result<(), impl std::error::Error> {
request_redraw = !request_redraw;
println!("\nrequest_redraw: {request_redraw}\n");
}
Key::Escape => {
Key::Action(Action::Escape) => {
close_requested = true;
}
_ => (),
Expand Down
4 changes: 2 additions & 2 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use simple_logger::SimpleLogger;
use winit::{
event::{DeviceEvent, ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
keyboard::{Key, ModifiersState},
keyboard::{Action, Key, ModifiersState},
window::{CursorGrabMode, WindowBuilder},
};

Expand Down Expand Up @@ -35,7 +35,7 @@ fn main() -> Result<(), impl std::error::Error> {
..
} => {
let result = match key {
Key::Escape => {
Key::Action(Action::Escape) => {
elwt.exit();
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use simple_logger::SimpleLogger;
use winit::dpi::PhysicalSize;
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
use winit::event_loop::EventLoop;
use winit::keyboard::Key;
use winit::keyboard::{Action, Key};
use winit::window::{Fullscreen, WindowBuilder};

#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -65,7 +65,7 @@ fn main() -> Result<(), impl std::error::Error> {
},
..
} => match key {
Key::Escape => elwt.exit(),
Key::Action(Action::Escape) => elwt.exit(),
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
Key::Character(ch) => match ch.to_lowercase().as_str() {
Expand Down
6 changes: 3 additions & 3 deletions examples/ime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event::{ElementState, Event, Ime, WindowEvent},
event_loop::EventLoop,
keyboard::Key,
keyboard::Action,
window::{ImePurpose, WindowBuilder},
};

Expand Down Expand Up @@ -69,12 +69,12 @@ fn main() -> Result<(), impl std::error::Error> {
WindowEvent::KeyboardInput { event, .. } => {
println!("key: {event:?}");

if event.state == ElementState::Pressed && event.logical_key == Key::F2 {
if event.state == ElementState::Pressed && event.logical_key == Action::F2 {
ime_allowed = !ime_allowed;
window.set_ime_allowed(ime_allowed);
println!("\nIME allowed: {ime_allowed}\n");
}
if event.state == ElementState::Pressed && event.logical_key == Key::F3 {
if event.state == ElementState::Pressed && event.logical_key == Action::F3 {
ime_purpose = match ime_purpose {
ImePurpose::Normal => ImePurpose::Password,
ImePurpose::Password => ImePurpose::Terminal,
Expand Down
18 changes: 9 additions & 9 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() -> Result<(), impl std::error::Error> {
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
keyboard::{Key, ModifiersState},
keyboard::{Action, Key, ModifiersState},
window::{CursorGrabMode, CursorIcon, Fullscreen, WindowBuilder, WindowLevel},
};

Expand Down Expand Up @@ -65,17 +65,17 @@ fn main() -> Result<(), impl std::error::Error> {
},
..
} => {
use Key::{ArrowLeft, ArrowRight};
use Action::{ArrowLeft, ArrowRight};
window.set_title(&format!("{key:?}"));
let state = !modifiers.shift_key();
match key {
// Cycle through video modes
Key::ArrowRight | Key::ArrowLeft => {
video_mode_id = match key {
ArrowLeft => video_mode_id.saturating_sub(1),
ArrowRight => (video_modes.len() - 1).min(video_mode_id + 1),
_ => unreachable!(),
};
Key::Action(ArrowRight) | Key::Action(ArrowLeft) => {
if key == ArrowLeft {
video_mode_id = video_mode_id.saturating_sub(1);
} else if key == ArrowRight {
video_mode_id = (video_modes.len() - 1).min(video_mode_id + 1);
}
println!("Picking video mode: {}", video_modes[video_mode_id]);
}
// WARNING: Consider using `key_without_modifers()` if available on your platform.
Expand Down Expand Up @@ -185,7 +185,7 @@ fn main() -> Result<(), impl std::error::Error> {
event:
KeyEvent {
state: ElementState::Released,
logical_key: Key::Escape,
logical_key: Key::Action(Action::Escape),
..
},
..
Expand Down
4 changes: 2 additions & 2 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, WindowEvent},
event_loop::EventLoop,
keyboard::Key,
keyboard::{Action, Key},
window::Window,
};

Expand Down Expand Up @@ -44,7 +44,7 @@ fn main() -> Result<(), impl std::error::Error> {
is_synthetic: false,
..
} if event.state == ElementState::Pressed => match event.logical_key {
Key::Escape => elwt.exit(),
Key::Action(Action::Escape) => elwt.exit(),
Key::Character(c) if c == "n" || c == "N" => {
let window = Window::new(elwt).unwrap();
println!("Opened a new window: {:?}", window.id());
Expand Down
16 changes: 5 additions & 11 deletions examples/window_resize_increments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use log::debug;
use simple_logger::SimpleLogger;
use winit::{
dpi::LogicalSize,
event::{ElementState, Event, KeyEvent, WindowEvent},
event::{ElementState, Event, WindowEvent},
event_loop::EventLoop,
keyboard::Key,
keyboard::Action,
window::WindowBuilder,
};

Expand All @@ -27,15 +27,9 @@ fn main() -> Result<(), impl std::error::Error> {
event_loop.run(move |event, elwt| match event {
Event::WindowEvent { event, window_id } if window_id == window.id() => match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Space,
state: ElementState::Released,
..
},
..
} => {
WindowEvent::KeyboardInput { event, .. }
if event.logical_key == Action::Space && event.state == ElementState::Released =>
{
has_increments = !has_increments;

let new_increments = match window.resize_increments() {
Expand Down
6 changes: 3 additions & 3 deletions examples/window_tabbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::EventLoop,
keyboard::Key,
keyboard::{Action, Key},
platform::macos::{WindowBuilderExtMacOS, WindowExtMacOS},
window::{Window, WindowBuilder},
};
Expand Down Expand Up @@ -70,10 +70,10 @@ fn main() -> Result<(), impl std::error::Error> {
Key::Character("w") => {
let _ = windows.remove(&window_id);
}
Key::ArrowRight => {
Key::Action(Action::ArrowRight) => {
windows.get(&window_id).unwrap().select_next_tab();
}
Key::ArrowLeft => {
Key::Action(Action::ArrowLeft) => {
windows.get(&window_id).unwrap().select_previous_tab();
}
Key::Character(ch) => {
Expand Down
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ pub struct KeyEvent {
/// An additional difference from `logical_key` is that
/// this field stores the text representation of any key
/// that has such a representation. For example when
/// `logical_key` is `Key::Enter`, this field is `Some("\r")`.
/// `logical_key` is `Key::Action(Action::Enter)`, this field is `Some("\r")`.
///
/// This is `None` if the current keypress cannot
/// be interpreted as text.
Expand Down
Loading

0 comments on commit 8610913

Please sign in to comment.