mirror of
https://github.com/danbulant/appit
synced 2026-05-19 04:08:34 +00:00
Updating winit
Discovered the rwh feature flags
This commit is contained in:
parent
bde351f2b4
commit
125e9f7c46
5 changed files with 394 additions and 298 deletions
598
Cargo.lock
generated
598
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -12,5 +12,4 @@ wayland = ["winit/wayland"]
|
|||
wayland-dlopen = ["winit/wayland-dlopen"]
|
||||
|
||||
[dependencies]
|
||||
winit = { version = "=0.29.1-beta", default-features = false }
|
||||
raw-window-handle = "0.5.1"
|
||||
winit = { version = "0.29.3", default-features = false, features = ["rwh_05"] }
|
||||
|
|
|
|||
51
src/lib.rs
51
src/lib.rs
|
|
@ -7,18 +7,18 @@
|
|||
mod private;
|
||||
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::process::exit;
|
||||
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};
|
||||
|
||||
|
|
@ -50,7 +50,7 @@ impl PendingApp<()> {
|
|||
/// app is run, the app will immediately close.
|
||||
#[must_use]
|
||||
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
|
||||
/// [`EventLoop::run`] for more information.
|
||||
pub fn run(mut self) -> Result<(), EventLoopError> {
|
||||
self.event_loop.run(move |event, target, control_flow| {
|
||||
*control_flow = ControlFlow::Wait;
|
||||
self.event_loop.run(move |event, target| {
|
||||
target.set_control_flow(ControlFlow::Wait);
|
||||
match event {
|
||||
Event::WindowEvent { window_id, event } => {
|
||||
let event = WindowEvent::from(event);
|
||||
|
|
@ -97,18 +97,15 @@ where
|
|||
.windows
|
||||
.send(window_id, WindowMessage::Event(event));
|
||||
}
|
||||
Event::RedrawRequested(window_id) => {
|
||||
self.running.windows.send(window_id, WindowMessage::Redraw);
|
||||
}
|
||||
Event::UserEvent(message) => match message {
|
||||
EventLoopMessage::CloseWindow(window_id) => {
|
||||
if self.running.windows.close(window_id) {
|
||||
*control_flow = ControlFlow::ExitWithCode(0);
|
||||
exit(0)
|
||||
}
|
||||
}
|
||||
EventLoopMessage::WindowPanic(window_id) => {
|
||||
if self.running.windows.close(window_id) {
|
||||
*control_flow = ControlFlow::ExitWithCode(1);
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
EventLoopMessage::OpenWindow {
|
||||
|
|
@ -128,6 +125,7 @@ where
|
|||
}
|
||||
},
|
||||
Event::NewEvents(_)
|
||||
| Event::MemoryWarning
|
||||
| Event::DeviceEvent { .. }
|
||||
| Event::Suspended
|
||||
| Event::Resumed
|
||||
|
|
@ -208,7 +206,7 @@ where
|
|||
{
|
||||
fn open(
|
||||
&self,
|
||||
window: WindowAttributes<AppMessage::Window>,
|
||||
window: WindowAttributes,
|
||||
sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>,
|
||||
) -> Result<Option<Arc<winit::window::Window>>, OsError> {
|
||||
self.running
|
||||
|
|
@ -251,7 +249,7 @@ impl<Message> Windows<Message> {
|
|||
fn open<AppMessage>(
|
||||
&self,
|
||||
target: &EventLoopWindowTarget<EventLoopMessage<AppMessage>>,
|
||||
attrs: WindowAttributes<Message>,
|
||||
attrs: WindowAttributes,
|
||||
sender: mpsc::SyncSender<WindowMessage<Message>>,
|
||||
) -> Result<Arc<winit::window::Window>, OsError>
|
||||
where
|
||||
|
|
@ -304,19 +302,8 @@ impl<Message> Windows<Message> {
|
|||
if let Some(resize_increments) = attrs.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 mut windows = self.data.lock().map_or_else(PoisonError::into_inner, |g| g);
|
||||
windows.insert(
|
||||
winit.id(),
|
||||
OpenWindow {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ where
|
|||
{
|
||||
fn open(
|
||||
&self,
|
||||
window: WindowAttributes<AppMessage::Window>,
|
||||
window: WindowAttributes,
|
||||
sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>,
|
||||
) -> Result<Option<Arc<winit::window::Window>>, OsError>;
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ where
|
|||
AppMessage: Message,
|
||||
{
|
||||
OpenWindow {
|
||||
attrs: WindowAttributes<AppMessage::Window>,
|
||||
attrs: WindowAttributes,
|
||||
sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>,
|
||||
open_sender: mpsc::SyncSender<Result<Arc<winit::window::Window>, OsError>>,
|
||||
},
|
||||
|
|
@ -43,13 +43,14 @@ where
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum WindowMessage<User> {
|
||||
Redraw,
|
||||
User(User),
|
||||
Event(WindowEvent),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum WindowEvent {
|
||||
RedrawRequested,
|
||||
|
||||
/// The size of the window has changed. Contains the client area's new dimensions.
|
||||
Resized(PhysicalSize<u32>),
|
||||
|
||||
|
|
@ -244,6 +245,7 @@ impl From<winit::event::WindowEvent> for WindowEvent {
|
|||
#[allow(clippy::too_many_lines)] // it's a match statement
|
||||
fn from(event: winit::event::WindowEvent) -> Self {
|
||||
match event {
|
||||
winit::event::WindowEvent::RedrawRequested => Self::RedrawRequested,
|
||||
winit::event::WindowEvent::Resized(size) => Self::Resized(size),
|
||||
winit::event::WindowEvent::Moved(pos) => Self::Moved(pos),
|
||||
winit::event::WindowEvent::CloseRequested => Self::CloseRequested,
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ use winit::event::{
|
|||
AxisId, DeviceId, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, Touch,
|
||||
TouchPhase,
|
||||
};
|
||||
use winit::keyboard::KeyCode;
|
||||
use winit::keyboard::PhysicalKey;
|
||||
use winit::window::{Fullscreen, Icon, Theme, WindowButtons, WindowId, WindowLevel};
|
||||
|
||||
use crate::private::{self, WindowEvent};
|
||||
|
|
@ -64,7 +64,7 @@ where
|
|||
{
|
||||
owner: &'a Application,
|
||||
context: Behavior::Context,
|
||||
attributes: WindowAttributes<AppMessage::Window>,
|
||||
attributes: WindowAttributes,
|
||||
}
|
||||
impl<'a, Behavior, Application, AppMessage> Deref
|
||||
for WindowBuilder<'a, Behavior, Application, AppMessage>
|
||||
|
|
@ -72,7 +72,7 @@ where
|
|||
Behavior: self::WindowBehavior<AppMessage>,
|
||||
AppMessage: Message,
|
||||
{
|
||||
type Target = WindowAttributes<AppMessage::Window>;
|
||||
type Target = WindowAttributes;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.attributes
|
||||
|
|
@ -96,7 +96,7 @@ where
|
|||
/// that `parent_window` accepts a [`Window`] rather than relying on raw window
|
||||
/// handle.
|
||||
#[allow(clippy::struct_excessive_bools)]
|
||||
pub struct WindowAttributes<ParentWindowEvent> {
|
||||
pub struct WindowAttributes {
|
||||
/// The inner size of the window.
|
||||
pub inner_size: Option<Size>,
|
||||
/// The minimum inner size of the window.
|
||||
|
|
@ -133,8 +133,6 @@ pub struct WindowAttributes<ParentWindowEvent> {
|
|||
pub content_protected: bool,
|
||||
/// The level of the window.
|
||||
pub window_level: WindowLevel,
|
||||
/// The parent window of this window.
|
||||
pub parent_window: Option<Window<ParentWindowEvent>>,
|
||||
/// Whether the window is active or not.
|
||||
pub active: bool,
|
||||
/// Name of the application
|
||||
|
|
@ -146,9 +144,10 @@ pub struct WindowAttributes<ParentWindowEvent> {
|
|||
pub app_name: Option<String>,
|
||||
}
|
||||
|
||||
impl<User> Default for WindowAttributes<User> {
|
||||
impl Default for WindowAttributes {
|
||||
fn default() -> Self {
|
||||
let defaults = winit::window::WindowAttributes::default();
|
||||
let fullscreen = defaults.fullscreen().cloned();
|
||||
Self {
|
||||
inner_size: defaults.inner_size,
|
||||
min_inner_size: defaults.min_inner_size,
|
||||
|
|
@ -157,7 +156,7 @@ impl<User> Default for WindowAttributes<User> {
|
|||
resizable: defaults.resizable,
|
||||
enabled_buttons: defaults.enabled_buttons,
|
||||
title: defaults.title,
|
||||
fullscreen: defaults.fullscreen,
|
||||
fullscreen,
|
||||
maximized: defaults.maximized,
|
||||
visible: defaults.visible,
|
||||
transparent: defaults.transparent,
|
||||
|
|
@ -168,7 +167,6 @@ impl<User> Default for WindowAttributes<User> {
|
|||
content_protected: defaults.content_protected,
|
||||
window_level: defaults.window_level,
|
||||
active: defaults.active,
|
||||
parent_window: None,
|
||||
app_name: None,
|
||||
}
|
||||
}
|
||||
|
|
@ -252,7 +250,7 @@ where
|
|||
position: PhysicalPosition<i32>,
|
||||
cursor_position: Option<PhysicalPosition<f64>>,
|
||||
mouse_buttons: HashSet<MouseButton>,
|
||||
keys: HashSet<KeyCode>,
|
||||
keys: HashSet<PhysicalKey>,
|
||||
scale: f64,
|
||||
close: bool,
|
||||
occluded: bool,
|
||||
|
|
@ -474,11 +472,11 @@ where
|
|||
Behavior: self::WindowBehavior<AppMessage>,
|
||||
{
|
||||
match message {
|
||||
WindowMessage::Redraw => {
|
||||
self.set_needs_redraw();
|
||||
}
|
||||
WindowMessage::User(user) => behavior.event(self, user),
|
||||
WindowMessage::Event(evt) => match evt {
|
||||
WindowEvent::RedrawRequested => {
|
||||
self.set_needs_redraw();
|
||||
}
|
||||
WindowEvent::CloseRequested => {
|
||||
if behavior.close_requested(self) {
|
||||
self.close();
|
||||
|
|
@ -640,14 +638,14 @@ where
|
|||
/// Returns an iterator of the currently pressed keys.
|
||||
///
|
||||
/// 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()
|
||||
}
|
||||
|
||||
/// Returns true if the given key code is currently pressed.
|
||||
#[must_use]
|
||||
pub fn key_pressed(&self, keycode: &KeyCode) -> bool {
|
||||
self.keys.contains(keycode)
|
||||
pub fn key_pressed(&self, key: &PhysicalKey) -> bool {
|
||||
self.keys.contains(key)
|
||||
}
|
||||
|
||||
/// Returns an iterator of the currently pressed mouse buttons.
|
||||
|
|
@ -690,7 +688,7 @@ where
|
|||
{
|
||||
fn open(
|
||||
&self,
|
||||
attrs: WindowAttributes<AppMessage::Window>,
|
||||
attrs: WindowAttributes,
|
||||
sender: mpsc::SyncSender<WindowMessage<AppMessage::Window>>,
|
||||
) -> Result<Option<Arc<winit::window::Window>>, OsError> {
|
||||
let (open_sender, open_receiver) = mpsc::sync_channel(1);
|
||||
|
|
|
|||
Loading…
Reference in a new issue