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

@ -56,7 +56,7 @@ use super::window::set_skip_taskbar;
use super::SelectedCursor;
use crate::application::ApplicationHandler;
use crate::dpi::{PhysicalPosition, PhysicalSize};
use crate::error::{EventLoopError, ExternalError, OsError};
use crate::error::{EventLoopError, RequestError};
use crate::event::{
Event, FingerId as RootFingerId, Force, Ime, RawKeyEvent, SurfaceSizeWriter, Touch, TouchPhase,
WindowEvent,
@ -521,14 +521,14 @@ impl RootActiveEventLoop for ActiveEventLoop {
fn create_window(
&self,
window_attributes: WindowAttributes,
) -> Result<Box<dyn CoreWindow>, OsError> {
) -> Result<Box<dyn CoreWindow>, RequestError> {
Ok(Box::new(Window::new(self, window_attributes)?))
}
fn create_custom_cursor(
&self,
source: CustomCursorSource,
) -> Result<RootCustomCursor, ExternalError> {
) -> Result<RootCustomCursor, RequestError> {
Ok(RootCustomCursor { inner: WinCursor::new(&source.inner.0)? })
}

View file

@ -17,7 +17,7 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{
use super::util;
use crate::cursor::CursorImage;
use crate::dpi::PhysicalSize;
use crate::error::ExternalError;
use crate::error::RequestError;
use crate::icon::*;
impl Pixel {
@ -179,7 +179,7 @@ impl Default for SelectedCursor {
pub struct WinCursor(pub(super) Arc<RaiiCursor>);
impl WinCursor {
pub(crate) fn new(image: &CursorImage) -> Result<Self, ExternalError> {
pub(crate) fn new(image: &CursorImage) -> Result<Self, RequestError> {
let mut bgra = image.rgba.clone();
bgra.chunks_exact_mut(4).for_each(|chunk| chunk.swap(0, 2));
@ -189,16 +189,16 @@ impl WinCursor {
unsafe {
let hdc_screen = GetDC(0);
if hdc_screen == 0 {
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
return Err(os_error!(io::Error::last_os_error()).into());
}
let hbm_color = CreateCompatibleBitmap(hdc_screen, w, h);
ReleaseDC(0, hdc_screen);
if hbm_color == 0 {
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
return Err(os_error!(io::Error::last_os_error()).into());
}
if SetBitmapBits(hbm_color, bgra.len() as u32, bgra.as_ptr() as *const c_void) == 0 {
DeleteObject(hbm_color);
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
return Err(os_error!(io::Error::last_os_error()).into());
};
// Mask created according to https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-createbitmap#parameters
@ -206,7 +206,7 @@ impl WinCursor {
let hbm_mask = CreateBitmap(w, h, 1, 1, mask_bits.as_ptr() as *const _);
if hbm_mask == 0 {
DeleteObject(hbm_color);
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
return Err(os_error!(io::Error::last_os_error()).into());
}
let icon_info = ICONINFO {
@ -221,7 +221,7 @@ impl WinCursor {
DeleteObject(hbm_color);
DeleteObject(hbm_mask);
if handle == 0 {
return Err(ExternalError::Os(os_error!(io::Error::last_os_error())));
return Err(os_error!(io::Error::last_os_error()).into());
}
Ok(Self(Arc::new(RaiiCursor { handle })))

View file

@ -103,8 +103,6 @@ fn wrap_device_id(id: u32) -> RootDeviceId {
RootDeviceId(DeviceId(id))
}
pub type OsError = std::io::Error;
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct KeyEventExtra {
pub text_with_all_modifiers: Option<SmolStr>,

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