This commit is contained in:
Roland Fredenhagen 2023-09-02 17:29:56 +02:00
parent e7d01c02bb
commit 272d1c7462
No known key found for this signature in database
GPG key ID: 094AF99241035EB6
2 changed files with 53 additions and 58 deletions

View file

@ -11,11 +11,10 @@ pub use winit;
use raw_window_handle::HasRawWindowHandle;
pub use window::{RunningWindow, Window, WindowAttributes, WindowBehavior, WindowBuilder};
use winit::error::OsError;
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};
@ -82,58 +81,54 @@ where
/// Begins running the application. This function will never return.
///
/// Internally this runs the [`EventLoop`].
pub fn run(mut self) -> ! {
self.event_loop
.run(move |event, target, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { window_id, event } => {
let event = WindowEvent::from(event);
self.running
.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);
}
}
EventLoopMessage::WindowPanic(window_id) => {
if self.running.windows.close(window_id) {
*control_flow = ControlFlow::ExitWithCode(1);
}
}
EventLoopMessage::OpenWindow {
attrs,
sender,
open_sender,
} => {
let result = self.running.windows.open(target, attrs, sender);
let _result = open_sender.send(result);
}
EventLoopMessage::User {
message,
response_sender,
} => {
let _result = response_sender
.send((self.message_callback)(message, &self.running.windows));
}
},
Event::NewEvents(_)
| Event::DeviceEvent { .. }
| Event::Suspended
| Event::Resumed
| Event::LoopExiting
| Event::AboutToWait => {}
pub fn run(mut self) -> Result<(), EventLoopError> {
self.event_loop.run(move |event, target, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { window_id, event } => {
let event = WindowEvent::from(event);
self.running
.windows
.send(window_id, WindowMessage::Event(event));
}
})
.unwrap();
// TODO do we want to forward the change to a result
exit(0);
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);
}
}
EventLoopMessage::WindowPanic(window_id) => {
if self.running.windows.close(window_id) {
*control_flow = ControlFlow::ExitWithCode(1);
}
}
EventLoopMessage::OpenWindow {
attrs,
sender,
open_sender,
} => {
let result = self.running.windows.open(target, attrs, sender);
let _result = open_sender.send(result);
}
EventLoopMessage::User {
message,
response_sender,
} => {
let _result = response_sender
.send((self.message_callback)(message, &self.running.windows));
}
},
Event::NewEvents(_)
| Event::DeviceEvent { .. }
| Event::Suspended
| Event::Resumed
| Event::LoopExiting
| Event::AboutToWait => {}
}
})
}
}

View file

@ -7,7 +7,7 @@ use std::thread;
use std::time::{Duration, Instant};
use winit::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
use winit::error::OsError;
use winit::error::{EventLoopError, OsError};
use winit::event::{
AxisId, DeviceId, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, Touch,
TouchPhase,
@ -774,7 +774,7 @@ where
fn run_with_event_callback(
app_callback: impl FnMut(AppMessage, &Windows<AppMessage::Window>) -> AppMessage::Response
+ 'static,
) -> !
) -> Result<(), EventLoopError>
where
Self::Context: Default,
{
@ -795,7 +795,7 @@ where
context: Self::Context,
app_callback: impl FnMut(AppMessage, &Windows<AppMessage::Window>) -> AppMessage::Response
+ 'static,
) -> ! {
) -> Result<(), EventLoopError> {
let app = PendingApp::new_with_event_callback(app_callback);
Self::open_with(&app, context).expect("error opening initial window");
app.run()
@ -1020,7 +1020,7 @@ pub trait Run: WindowBehavior<()> {
///
/// This function is shorthand for creating a [`PendingApp`], opening this
/// window inside of it, and running the pending app.
fn run() -> !
fn run() -> Result<(), EventLoopError>
where
Self::Context: Default,
{
@ -1033,7 +1033,7 @@ pub trait Run: WindowBehavior<()> {
///
/// This function is shorthand for creating a [`PendingApp`], opening this
/// window inside of it, and running the pending app.
fn run_with(context: Self::Context) -> ! {
fn run_with(context: Self::Context) -> Result<(), EventLoopError> {
let app = PendingApp::new();
Self::open_with(&app, context).expect("error opening initial window");
app.run()