Updating winit

Discovered the rwh feature flags
This commit is contained in:
Jonathan Johnson 2023-11-20 06:20:20 -08:00
parent bde351f2b4
commit 125e9f7c46
No known key found for this signature in database
GPG key ID: A66D6A34D6620579
5 changed files with 394 additions and 298 deletions

598
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -12,5 +12,4 @@ wayland = ["winit/wayland"]
wayland-dlopen = ["winit/wayland-dlopen"] wayland-dlopen = ["winit/wayland-dlopen"]
[dependencies] [dependencies]
winit = { version = "=0.29.1-beta", default-features = false } winit = { version = "0.29.3", default-features = false, features = ["rwh_05"] }
raw-window-handle = "0.5.1"

View file

@ -7,18 +7,18 @@
mod private; mod private;
mod window; mod window;
pub use winit;
use raw_window_handle::HasRawWindowHandle;
pub use window::{RunningWindow, Window, WindowAttributes, WindowBehavior, WindowBuilder};
use winit::error::{EventLoopError, OsError};
use winit::window::WindowId;
use std::collections::HashMap; use std::collections::HashMap;
use std::process::exit;
use std::sync::{mpsc, Arc, Mutex, PoisonError}; use std::sync::{mpsc, Arc, Mutex, PoisonError};
use winit::event_loop::{ControlFlow, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget};
use winit::{event::Event, event_loop::EventLoop}; pub use window::{RunningWindow, Window, WindowAttributes, WindowBehavior, WindowBuilder};
pub use winit;
use winit::error::{EventLoopError, OsError};
use winit::event::Event;
use winit::event_loop::{
ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget,
};
use winit::window::WindowId;
use crate::private::{EventLoopMessage, WindowEvent, WindowMessage}; use crate::private::{EventLoopMessage, WindowEvent, WindowMessage};
@ -50,7 +50,7 @@ impl PendingApp<()> {
/// app is run, the app will immediately close. /// app is run, the app will immediately close.
#[must_use] #[must_use]
pub fn new() -> Self { pub fn new() -> Self {
Self::new_with_event_callback(|_, _| {}) Self::new_with_event_callback(|(), _| {})
} }
} }
@ -88,8 +88,8 @@ where
/// Returns an [`EventLoopError`] upon the loop exiting due to an error. See /// Returns an [`EventLoopError`] upon the loop exiting due to an error. See
/// [`EventLoop::run`] for more information. /// [`EventLoop::run`] for more information.
pub fn run(mut self) -> Result<(), EventLoopError> { pub fn run(mut self) -> Result<(), EventLoopError> {
self.event_loop.run(move |event, target, control_flow| { self.event_loop.run(move |event, target| {
*control_flow = ControlFlow::Wait; target.set_control_flow(ControlFlow::Wait);
match event { match event {
Event::WindowEvent { window_id, event } => { Event::WindowEvent { window_id, event } => {
let event = WindowEvent::from(event); let event = WindowEvent::from(event);
@ -97,18 +97,15 @@ where
.windows .windows
.send(window_id, WindowMessage::Event(event)); .send(window_id, WindowMessage::Event(event));
} }
Event::RedrawRequested(window_id) => {
self.running.windows.send(window_id, WindowMessage::Redraw);
}
Event::UserEvent(message) => match message { Event::UserEvent(message) => match message {
EventLoopMessage::CloseWindow(window_id) => { EventLoopMessage::CloseWindow(window_id) => {
if self.running.windows.close(window_id) { if self.running.windows.close(window_id) {
*control_flow = ControlFlow::ExitWithCode(0); exit(0)
} }
} }
EventLoopMessage::WindowPanic(window_id) => { EventLoopMessage::WindowPanic(window_id) => {
if self.running.windows.close(window_id) { if self.running.windows.close(window_id) {
*control_flow = ControlFlow::ExitWithCode(1); exit(1)
} }
} }
EventLoopMessage::OpenWindow { EventLoopMessage::OpenWindow {
@ -128,6 +125,7 @@ where
} }
}, },
Event::NewEvents(_) Event::NewEvents(_)
| Event::MemoryWarning
| Event::DeviceEvent { .. } | Event::DeviceEvent { .. }
| Event::Suspended | Event::Suspended
| Event::Resumed | Event::Resumed
@ -208,7 +206,7 @@ where
{ {
fn open( fn open(
&self, &self,
window: WindowAttributes<AppMessage::Window>, window: WindowAttributes,
sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>, sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>,
) -> Result<Option<Arc<winit::window::Window>>, OsError> { ) -> Result<Option<Arc<winit::window::Window>>, OsError> {
self.running self.running
@ -251,7 +249,7 @@ impl<Message> Windows<Message> {
fn open<AppMessage>( fn open<AppMessage>(
&self, &self,
target: &EventLoopWindowTarget<EventLoopMessage<AppMessage>>, target: &EventLoopWindowTarget<EventLoopMessage<AppMessage>>,
attrs: WindowAttributes<Message>, attrs: WindowAttributes,
sender: mpsc::SyncSender<WindowMessage<Message>>, sender: mpsc::SyncSender<WindowMessage<Message>>,
) -> Result<Arc<winit::window::Window>, OsError> ) -> Result<Arc<winit::window::Window>, OsError>
where where
@ -304,19 +302,8 @@ impl<Message> Windows<Message> {
if let Some(resize_increments) = attrs.resize_increments { if let Some(resize_increments) = attrs.resize_increments {
builder = builder.with_resize_increments(resize_increments); builder = builder.with_resize_increments(resize_increments);
} }
let mut windows = self.data.lock().map_or_else(PoisonError::into_inner, |g| g);
if let Some(parent_window) = attrs.parent_window {
let parent_window = windows
.get(&parent_window.id())
.expect("invalid parent window");
// SAFETY: The only way for us to resolve to a winit Window is for
// the window to still be in our list of open windows. This
// guarantees that the window handle is still valid.
unsafe {
builder = builder.with_parent_window(Some(parent_window.winit.raw_window_handle()));
}
}
let winit = Arc::new(builder.build(target)?); let winit = Arc::new(builder.build(target)?);
let mut windows = self.data.lock().map_or_else(PoisonError::into_inner, |g| g);
windows.insert( windows.insert(
winit.id(), winit.id(),
OpenWindow { OpenWindow {

View file

@ -19,7 +19,7 @@ where
{ {
fn open( fn open(
&self, &self,
window: WindowAttributes<AppMessage::Window>, window: WindowAttributes,
sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>, sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>,
) -> Result<Option<Arc<winit::window::Window>>, OsError>; ) -> Result<Option<Arc<winit::window::Window>>, OsError>;
} }
@ -29,7 +29,7 @@ where
AppMessage: Message, AppMessage: Message,
{ {
OpenWindow { OpenWindow {
attrs: WindowAttributes<AppMessage::Window>, attrs: WindowAttributes,
sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>, sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>,
open_sender: mpsc::SyncSender<Result<Arc<winit::window::Window>, OsError>>, open_sender: mpsc::SyncSender<Result<Arc<winit::window::Window>, OsError>>,
}, },
@ -43,13 +43,14 @@ where
#[derive(Debug)] #[derive(Debug)]
pub enum WindowMessage<User> { pub enum WindowMessage<User> {
Redraw,
User(User), User(User),
Event(WindowEvent), Event(WindowEvent),
} }
#[derive(Debug)] #[derive(Debug)]
pub enum WindowEvent { pub enum WindowEvent {
RedrawRequested,
/// The size of the window has changed. Contains the client area's new dimensions. /// The size of the window has changed. Contains the client area's new dimensions.
Resized(PhysicalSize<u32>), Resized(PhysicalSize<u32>),
@ -244,6 +245,7 @@ impl From<winit::event::WindowEvent> for WindowEvent {
#[allow(clippy::too_many_lines)] // it's a match statement #[allow(clippy::too_many_lines)] // it's a match statement
fn from(event: winit::event::WindowEvent) -> Self { fn from(event: winit::event::WindowEvent) -> Self {
match event { match event {
winit::event::WindowEvent::RedrawRequested => Self::RedrawRequested,
winit::event::WindowEvent::Resized(size) => Self::Resized(size), winit::event::WindowEvent::Resized(size) => Self::Resized(size),
winit::event::WindowEvent::Moved(pos) => Self::Moved(pos), winit::event::WindowEvent::Moved(pos) => Self::Moved(pos),
winit::event::WindowEvent::CloseRequested => Self::CloseRequested, winit::event::WindowEvent::CloseRequested => Self::CloseRequested,

View file

@ -12,7 +12,7 @@ use winit::event::{
AxisId, DeviceId, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, Touch, AxisId, DeviceId, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, Touch,
TouchPhase, TouchPhase,
}; };
use winit::keyboard::KeyCode; use winit::keyboard::PhysicalKey;
use winit::window::{Fullscreen, Icon, Theme, WindowButtons, WindowId, WindowLevel}; use winit::window::{Fullscreen, Icon, Theme, WindowButtons, WindowId, WindowLevel};
use crate::private::{self, WindowEvent}; use crate::private::{self, WindowEvent};
@ -64,7 +64,7 @@ where
{ {
owner: &'a Application, owner: &'a Application,
context: Behavior::Context, context: Behavior::Context,
attributes: WindowAttributes<AppMessage::Window>, attributes: WindowAttributes,
} }
impl<'a, Behavior, Application, AppMessage> Deref impl<'a, Behavior, Application, AppMessage> Deref
for WindowBuilder<'a, Behavior, Application, AppMessage> for WindowBuilder<'a, Behavior, Application, AppMessage>
@ -72,7 +72,7 @@ where
Behavior: self::WindowBehavior<AppMessage>, Behavior: self::WindowBehavior<AppMessage>,
AppMessage: Message, AppMessage: Message,
{ {
type Target = WindowAttributes<AppMessage::Window>; type Target = WindowAttributes;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.attributes &self.attributes
@ -96,7 +96,7 @@ where
/// that `parent_window` accepts a [`Window`] rather than relying on raw window /// that `parent_window` accepts a [`Window`] rather than relying on raw window
/// handle. /// handle.
#[allow(clippy::struct_excessive_bools)] #[allow(clippy::struct_excessive_bools)]
pub struct WindowAttributes<ParentWindowEvent> { pub struct WindowAttributes {
/// The inner size of the window. /// The inner size of the window.
pub inner_size: Option<Size>, pub inner_size: Option<Size>,
/// The minimum inner size of the window. /// The minimum inner size of the window.
@ -133,8 +133,6 @@ pub struct WindowAttributes<ParentWindowEvent> {
pub content_protected: bool, pub content_protected: bool,
/// The level of the window. /// The level of the window.
pub window_level: WindowLevel, pub window_level: WindowLevel,
/// The parent window of this window.
pub parent_window: Option<Window<ParentWindowEvent>>,
/// Whether the window is active or not. /// Whether the window is active or not.
pub active: bool, pub active: bool,
/// Name of the application /// Name of the application
@ -146,9 +144,10 @@ pub struct WindowAttributes<ParentWindowEvent> {
pub app_name: Option<String>, pub app_name: Option<String>,
} }
impl<User> Default for WindowAttributes<User> { impl Default for WindowAttributes {
fn default() -> Self { fn default() -> Self {
let defaults = winit::window::WindowAttributes::default(); let defaults = winit::window::WindowAttributes::default();
let fullscreen = defaults.fullscreen().cloned();
Self { Self {
inner_size: defaults.inner_size, inner_size: defaults.inner_size,
min_inner_size: defaults.min_inner_size, min_inner_size: defaults.min_inner_size,
@ -157,7 +156,7 @@ impl<User> Default for WindowAttributes<User> {
resizable: defaults.resizable, resizable: defaults.resizable,
enabled_buttons: defaults.enabled_buttons, enabled_buttons: defaults.enabled_buttons,
title: defaults.title, title: defaults.title,
fullscreen: defaults.fullscreen, fullscreen,
maximized: defaults.maximized, maximized: defaults.maximized,
visible: defaults.visible, visible: defaults.visible,
transparent: defaults.transparent, transparent: defaults.transparent,
@ -168,7 +167,6 @@ impl<User> Default for WindowAttributes<User> {
content_protected: defaults.content_protected, content_protected: defaults.content_protected,
window_level: defaults.window_level, window_level: defaults.window_level,
active: defaults.active, active: defaults.active,
parent_window: None,
app_name: None, app_name: None,
} }
} }
@ -252,7 +250,7 @@ where
position: PhysicalPosition<i32>, position: PhysicalPosition<i32>,
cursor_position: Option<PhysicalPosition<f64>>, cursor_position: Option<PhysicalPosition<f64>>,
mouse_buttons: HashSet<MouseButton>, mouse_buttons: HashSet<MouseButton>,
keys: HashSet<KeyCode>, keys: HashSet<PhysicalKey>,
scale: f64, scale: f64,
close: bool, close: bool,
occluded: bool, occluded: bool,
@ -474,11 +472,11 @@ where
Behavior: self::WindowBehavior<AppMessage>, Behavior: self::WindowBehavior<AppMessage>,
{ {
match message { match message {
WindowMessage::Redraw => {
self.set_needs_redraw();
}
WindowMessage::User(user) => behavior.event(self, user), WindowMessage::User(user) => behavior.event(self, user),
WindowMessage::Event(evt) => match evt { WindowMessage::Event(evt) => match evt {
WindowEvent::RedrawRequested => {
self.set_needs_redraw();
}
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
if behavior.close_requested(self) { if behavior.close_requested(self) {
self.close(); self.close();
@ -640,14 +638,14 @@ where
/// Returns an iterator of the currently pressed keys. /// Returns an iterator of the currently pressed keys.
/// ///
/// This iterator does not guarantee any specific order. /// This iterator does not guarantee any specific order.
pub fn pressed_keys(&self) -> impl Iterator<Item = KeyCode> + '_ { pub fn pressed_keys(&self) -> impl Iterator<Item = PhysicalKey> + '_ {
self.keys.iter().copied() self.keys.iter().copied()
} }
/// Returns true if the given key code is currently pressed. /// Returns true if the given key code is currently pressed.
#[must_use] #[must_use]
pub fn key_pressed(&self, keycode: &KeyCode) -> bool { pub fn key_pressed(&self, key: &PhysicalKey) -> bool {
self.keys.contains(keycode) self.keys.contains(key)
} }
/// Returns an iterator of the currently pressed mouse buttons. /// Returns an iterator of the currently pressed mouse buttons.
@ -690,7 +688,7 @@ where
{ {
fn open( fn open(
&self, &self,
attrs: WindowAttributes<AppMessage::Window>, attrs: WindowAttributes,
sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>, sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>,
) -> Result<Option<Arc<winit::window::Window>>, OsError> { ) -> Result<Option<Arc<winit::window::Window>>, OsError> {
let (open_sender, open_receiver) = mpsc::sync_channel(1); let (open_sender, open_receiver) = mpsc::sync_channel(1);