mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 13:51:30 +11:00
918430979f
Overhaul the keyboard API in winit to mimic the W3C specification to achieve better crossplatform parity. The `KeyboardInput` event is now uses `KeyEvent` which consists of: - `physical_key` - a cross platform way to refer to scancodes; - `logical_key` - keysym value, which shows your key respecting the layout; - `text` - the text produced by this keypress; - `location` - the location of the key on the keyboard; - `repeat` - whether the key was produced by the repeat. And also a `platform_specific` field which encapsulates extra information on desktop platforms, like key without modifiers and text with all modifiers. The `Modifiers` were also slightly reworked as in, the information whether the left or right modifier is pressed is now also exposed on platforms where it could be queried reliably. The support was also added for the web and orbital platforms finishing the API change. This change made the `OptionAsAlt` API on macOS redundant thus it was removed all together. Co-authored-by: Artúr Kovács <kovacs.artur.barnabas@gmail.com> Co-authored-by: Kirill Chibisov <contact@kchibisov.com> Co-authored-by: daxpedda <daxpedda@gmail.com> Fixes: #2631. Fixes: #2055. Fixes: #2032. Fixes: #1904. Fixes: #1810. Fixes: #1700. Fixes: #1443. Fixes: #1343. Fixes: #1208. Fixes: #1151. Fixes: #812. Fixes: #600. Fixes: #361. Fixes: #343.
81 lines
3.1 KiB
Rust
81 lines
3.1 KiB
Rust
#[cfg(any(x11_platform, macos_platform, windows_platform))]
|
|
fn main() {
|
|
use std::collections::HashMap;
|
|
|
|
use raw_window_handle::HasRawWindowHandle;
|
|
use winit::{
|
|
dpi::{LogicalPosition, LogicalSize, Position},
|
|
event::{ElementState, Event, KeyEvent, WindowEvent},
|
|
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
|
|
window::{Window, WindowBuilder, WindowId},
|
|
};
|
|
|
|
fn spawn_child_window(
|
|
parent: &Window,
|
|
event_loop: &EventLoopWindowTarget<()>,
|
|
windows: &mut HashMap<WindowId, Window>,
|
|
) {
|
|
let parent = parent.raw_window_handle();
|
|
let mut builder = WindowBuilder::new()
|
|
.with_title("child window")
|
|
.with_inner_size(LogicalSize::new(200.0f32, 200.0f32))
|
|
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
|
|
.with_visible(true);
|
|
// `with_parent_window` is unsafe. Parent window must be a valid window.
|
|
builder = unsafe { builder.with_parent_window(Some(parent)) };
|
|
let child_window = builder.build(event_loop).unwrap();
|
|
|
|
let id = child_window.id();
|
|
windows.insert(id, child_window);
|
|
println!("child window created with id: {id:?}");
|
|
}
|
|
|
|
let mut windows = HashMap::new();
|
|
|
|
let event_loop: EventLoop<()> = EventLoop::new();
|
|
let parent_window = WindowBuilder::new()
|
|
.with_title("parent window")
|
|
.with_position(Position::Logical(LogicalPosition::new(0.0, 0.0)))
|
|
.with_inner_size(LogicalSize::new(640.0f32, 480.0f32))
|
|
.build(&event_loop)
|
|
.unwrap();
|
|
|
|
println!("parent window: {parent_window:?})");
|
|
|
|
event_loop.run(move |event: Event<'_, ()>, event_loop, control_flow| {
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
if let Event::WindowEvent { event, window_id } = event {
|
|
match event {
|
|
WindowEvent::CloseRequested => {
|
|
windows.clear();
|
|
*control_flow = ControlFlow::Exit;
|
|
}
|
|
WindowEvent::CursorEntered { device_id: _ } => {
|
|
// On x11, println when the cursor entered in a window even if the child window is created
|
|
// by some key inputs.
|
|
// the child windows are always placed at (0, 0) with size (200, 200) in the parent window,
|
|
// so we also can see this log when we move the cursor arround (200, 200) in parent window.
|
|
println!("cursor entered in the window {window_id:?}");
|
|
}
|
|
WindowEvent::KeyboardInput {
|
|
event:
|
|
KeyEvent {
|
|
state: ElementState::Pressed,
|
|
..
|
|
},
|
|
..
|
|
} => {
|
|
spawn_child_window(&parent_window, event_loop, &mut windows);
|
|
}
|
|
_ => (),
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
#[cfg(not(any(x11_platform, macos_platform, windows_platform)))]
|
|
fn main() {
|
|
panic!("This example is supported only on x11, macOS, and Windows.");
|
|
}
|