api: unify error handling

Make error infrastructure more backend agnostic and let backends
just forward the os errors opaquely.
This commit is contained in:
Kirill Chibisov 2024-09-06 17:20:11 +03:00 committed by GitHub
parent 8db3e0e043
commit b674d20edf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
35 changed files with 507 additions and 630 deletions

View file

@ -47,7 +47,7 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{
use crate::cursor::Cursor;
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
use crate::error::{ExternalError, NotSupportedError, OsError as RootOsError};
use crate::error::{NotSupportedError, RequestError};
use crate::icon::Icon;
use crate::monitor::MonitorHandle as CoreMonitorHandle;
use crate::platform::windows::{BackdropType, Color, CornerPreference};
@ -89,7 +89,7 @@ impl Window {
pub(crate) fn new(
event_loop: &ActiveEventLoop,
w_attr: WindowAttributes,
) -> Result<Window, RootOsError> {
) -> Result<Window, RequestError> {
// We dispatch an `init` function because of code style.
// First person to remove the need for cloning here gets a cookie!
//
@ -411,7 +411,7 @@ impl CoreWindow for Window {
fn pre_present_notify(&self) {}
fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
fn outer_position(&self) -> Result<PhysicalPosition<i32>, RequestError> {
util::WindowArea::Outer
.get_rect(self.hwnd())
.map(|rect| Ok(PhysicalPosition::new(rect.left, rect.top)))
@ -421,7 +421,7 @@ impl CoreWindow for Window {
)
}
fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
fn inner_position(&self) -> Result<PhysicalPosition<i32>, RequestError> {
let mut position: POINT = unsafe { mem::zeroed() };
if unsafe { ClientToScreen(self.hwnd(), &mut position) } == false.into() {
panic!(
@ -588,12 +588,12 @@ impl CoreWindow for Window {
}
}
fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ExternalError> {
fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), RequestError> {
let confine = match mode {
CursorGrabMode::None => false,
CursorGrabMode::Confined => true,
CursorGrabMode::Locked => {
return Err(ExternalError::NotSupported(NotSupportedError::new()))
return Err(NotSupportedError::new("locked cursor is not supported").into())
},
};
@ -608,9 +608,10 @@ impl CoreWindow for Window {
.unwrap()
.mouse
.set_cursor_flags(window, |f| f.set(CursorFlags::GRABBED, confine))
.map_err(|e| ExternalError::Os(os_error!(e)));
.map_err(|err| os_error!(err).into());
let _ = tx.send(result);
});
rx.recv().unwrap()
}
@ -636,23 +637,23 @@ impl CoreWindow for Window {
self.window_state_lock().scale_factor
}
fn set_cursor_position(&self, position: Position) -> Result<(), ExternalError> {
fn set_cursor_position(&self, position: Position) -> Result<(), RequestError> {
let scale_factor = self.scale_factor();
let (x, y) = position.to_physical::<i32>(scale_factor).into();
let mut point = POINT { x, y };
unsafe {
if ClientToScreen(self.hwnd(), &mut point) == false.into() {
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
return Err(os_error!(io::Error::last_os_error()).into());
}
if SetCursorPos(point.x, point.y) == false.into() {
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
return Err(os_error!(io::Error::last_os_error()).into());
}
}
Ok(())
}
fn drag_window(&self) -> Result<(), ExternalError> {
fn drag_window(&self) -> Result<(), RequestError> {
unsafe {
self.handle_os_dragging(HTCAPTION as WPARAM);
}
@ -660,7 +661,7 @@ impl CoreWindow for Window {
Ok(())
}
fn drag_resize_window(&self, direction: ResizeDirection) -> Result<(), ExternalError> {
fn drag_resize_window(&self, direction: ResizeDirection) -> Result<(), RequestError> {
unsafe {
self.handle_os_dragging(match direction {
ResizeDirection::East => HTRIGHT,
@ -683,7 +684,7 @@ impl CoreWindow for Window {
}
}
fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
fn set_cursor_hittest(&self, hittest: bool) -> Result<(), RequestError> {
let window = self.window;
let window_state = Arc::clone(&self.window_state);
self.thread_executor.execute_in_thread(move || {
@ -1249,7 +1250,7 @@ impl<'a> InitData<'a> {
unsafe fn init(
attributes: WindowAttributes,
event_loop: &ActiveEventLoop,
) -> Result<Window, RootOsError> {
) -> Result<Window, RequestError> {
let title = util::encode_wide(&attributes.title);
let class_name = util::encode_wide(&attributes.platform_specific.class_name);
@ -1332,7 +1333,7 @@ unsafe fn init(
}
if handle == 0 {
return Err(os_error!(io::Error::last_os_error()));
return Err(os_error!(io::Error::last_os_error()).into());
}
// If the handle is non-null, then window creation must have succeeded, which means