From 272d1c7462dfbaba8a3400fabf23bd65d1f7775a Mon Sep 17 00:00:00 2001 From: Roland Fredenhagen Date: Sat, 2 Sep 2023 17:29:56 +0200 Subject: [PATCH] wip --- src/lib.rs | 101 ++++++++++++++++++++++++-------------------------- src/window.rs | 10 ++--- 2 files changed, 53 insertions(+), 58 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cd72ce8..4b22627 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 => {} + } + }) } } diff --git a/src/window.rs b/src/window.rs index cdcb24b..620a437 100644 --- a/src/window.rs +++ b/src/window.rs @@ -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::Response + 'static, - ) -> ! + ) -> Result<(), EventLoopError> where Self::Context: Default, { @@ -795,7 +795,7 @@ where context: Self::Context, app_callback: impl FnMut(AppMessage, &Windows) -> 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()