Propagate error from EventLoop creation

Inner panics could make it hard to trouble shoot the issues and for some
users it's not desirable.

The inner panics were left only when they are used to `assert!` during
development.

This reverts commit 9f91bc413fe20618bd7090829832bb074aab15c3 which
reverted the original patch which was merged without a proper review.

Fixes: #500.
This commit is contained in:
Kirill Chibisov
2023-08-13 23:20:09 +04:00
parent e3fbfd6792
commit 08ad3f19e2
59 changed files with 353 additions and 297 deletions

View File

@@ -12,7 +12,7 @@ use orbclient::{
use raw_window_handle::{OrbitalDisplayHandle, RawDisplayHandle};
use crate::{
error::RunLoopError,
error::EventLoopError,
event::{self, Ime, Modifiers, StartCause},
event_loop::{self, ControlFlow},
keyboard::{
@@ -22,8 +22,8 @@ use crate::{
};
use super::{
DeviceId, KeyEventExtra, MonitorHandle, PlatformSpecificEventLoopAttributes, RedoxSocket,
TimeSocket, WindowId, WindowProperties,
DeviceId, KeyEventExtra, MonitorHandle, OsError, PlatformSpecificEventLoopAttributes,
RedoxSocket, TimeSocket, WindowId, WindowProperties,
};
fn convert_scancode(scancode: u8) -> KeyCode {
@@ -270,12 +270,20 @@ pub struct EventLoop<T: 'static> {
}
impl<T: 'static> EventLoop<T> {
pub(crate) fn new(_: &PlatformSpecificEventLoopAttributes) -> Self {
pub(crate) fn new(_: &PlatformSpecificEventLoopAttributes) -> Result<Self, EventLoopError> {
let (user_events_sender, user_events_receiver) = mpsc::channel();
let event_socket = Arc::new(RedoxSocket::event().unwrap());
let event_socket = Arc::new(
RedoxSocket::event()
.map_err(OsError::new)
.map_err(|error| EventLoopError::Os(os_error!(error)))?,
);
let wake_socket = Arc::new(TimeSocket::open().unwrap());
let wake_socket = Arc::new(
TimeSocket::open()
.map_err(OsError::new)
.map_err(|error| EventLoopError::Os(os_error!(error)))?,
);
event_socket
.write(&syscall::Event {
@@ -283,9 +291,10 @@ impl<T: 'static> EventLoop<T> {
flags: syscall::EventFlags::EVENT_READ,
data: wake_socket.0.fd,
})
.unwrap();
.map_err(OsError::new)
.map_err(|error| EventLoopError::Os(os_error!(error)))?;
Self {
Ok(Self {
windows: Vec::new(),
window_target: event_loop::EventLoopWindowTarget {
p: EventLoopWindowTarget {
@@ -299,7 +308,7 @@ impl<T: 'static> EventLoop<T> {
},
_marker: std::marker::PhantomData,
},
}
})
}
fn process_event<F>(
@@ -443,7 +452,7 @@ impl<T: 'static> EventLoop<T> {
}
}
pub fn run<F>(mut self, mut event_handler_inner: F) -> Result<(), RunLoopError>
pub fn run<F>(mut self, mut event_handler_inner: F) -> Result<(), EventLoopError>
where
F: FnMut(event::Event<T>, &event_loop::EventLoopWindowTarget<T>, &mut ControlFlow),
{
@@ -685,7 +694,7 @@ impl<T: 'static> EventLoop<T> {
if code == 0 {
Ok(())
} else {
Err(RunLoopError::ExitFailure(code))
Err(EventLoopError::ExitFailure(code))
}
}