From 528fc3e552530c4eb7b88c5cc59ea5f586a9e3cc Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Tue, 22 Sep 2026 18:54:47 +0000 Subject: [PATCH] Add enum CustomCursorError --- winit-android/src/event_loop.rs | 10 ++++--- winit-appkit/src/cursor.rs | 10 ++++--- winit-appkit/src/event_loop.rs | 6 ++-- winit-core/src/error.rs | 35 +++++++++++++++++++++++ winit-core/src/event_loop/mod.rs | 6 ++-- winit-orbital/src/event_loop.rs | 11 ++++--- winit-uikit/src/event_loop.rs | 8 +++--- winit-wayland/src/event_loop/mod.rs | 8 +++--- winit-web/src/event_loop/mod.rs | 4 +-- winit-web/src/event_loop/window_target.rs | 4 +-- winit-win32/src/event_loop.rs | 10 +++---- winit-win32/src/icon.rs | 4 +-- winit-x11/src/event_loop.rs | 6 ++-- winit-x11/src/util/cursor.rs | 6 ++-- winit/examples/application.rs | 4 +-- winit/src/event_loop.rs | 7 +++-- winit/src/platform_impl/linux/mod.rs | 4 +-- 17 files changed, 92 insertions(+), 51 deletions(-) diff --git a/winit-android/src/event_loop.rs b/winit-android/src/event_loop.rs index 6b3a0fd8..43552f0b 100644 --- a/winit-android/src/event_loop.rs +++ b/winit-android/src/event_loop.rs @@ -13,7 +13,9 @@ use dpi::{PhysicalInsets, PhysicalPosition, PhysicalSize, Position, Size}; use tracing::{debug, trace, warn}; use winit_core::application::ApplicationHandler; use winit_core::cursor::{Cursor, CustomCursor, CustomCursorSource}; -use winit_core::error::{CreateWindowError, EventLoopError, NotSupportedError, RequestError}; +use winit_core::error::{ + CreateWindowError, CustomCursorError, EventLoopError, NotSupportedError, RequestError, +}; use winit_core::event::{self, DeviceId, FingerId, Force, StartCause, SurfaceSizeWriter}; use winit_core::event_loop::pump_events::PumpStatus; use winit_core::event_loop::{ @@ -674,7 +676,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.window_target().create_custom_cursor(custom_cursor) } } @@ -733,8 +735,8 @@ impl RootActiveEventLoop for ActiveEventLoop { fn create_custom_cursor( &self, _source: CustomCursorSource, - ) -> Result { - Err(NotSupportedError::new("create_custom_cursor is not supported").into()) + ) -> Result { + Err(CustomCursorError::NotSupported) } fn available_monitors(&self) -> Box> { diff --git a/winit-appkit/src/cursor.rs b/winit-appkit/src/cursor.rs index c41c3e9b..20bf1adf 100644 --- a/winit-appkit/src/cursor.rs +++ b/winit-appkit/src/cursor.rs @@ -13,7 +13,7 @@ use objc2_foundation::{ NSData, NSDictionary, NSNumber, NSObject, NSPoint, NSSize, NSString, ns_string, }; use winit_core::cursor::{CursorIcon, CursorImage, CustomCursorProvider, CustomCursorSource}; -use winit_core::error::{NotSupportedError, RequestError}; +use winit_core::error::{CustomCursorError, NotSupportedError, RequestError}; use winit_core::icon::{Icon, RgbaIcon}; #[derive(Clone, Debug, PartialEq, Eq, Hash)] @@ -31,11 +31,11 @@ unsafe impl Send for CustomCursor {} unsafe impl Sync for CustomCursor {} impl CustomCursor { - pub(crate) fn new(cursor: CustomCursorSource) -> Result { + pub(crate) fn new(cursor: CustomCursorSource) -> Result { let cursor = match cursor { CustomCursorSource::Image(cursor_image) => cursor_image, _ => { - return Err(NotSupportedError::new("unsupported cursor kind").into()); + return Err(CustomCursorError::UnsupportedSource); }, }; @@ -77,7 +77,9 @@ pub(crate) fn image_from_icon(icon: &Icon) -> Result, RequestE Ok(image) } -pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Result, RequestError> { +pub(crate) fn cursor_from_image( + cursor: &CursorImage, +) -> Result, CustomCursorError> { let width = cursor.width(); let height = cursor.height(); diff --git a/winit-appkit/src/event_loop.rs b/winit-appkit/src/event_loop.rs index 4abb7e48..6990ea3a 100644 --- a/winit-appkit/src/event_loop.rs +++ b/winit-appkit/src/event_loop.rs @@ -22,7 +22,7 @@ use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource}; use winit_core::data_transfer::{ DataTransfer, DataTransferId, DataTransferSend, SendData, TransferType, TypeHint, }; -use winit_core::error::{CreateWindowError, EventLoopError, RequestError, TransferError}; +use winit_core::error::{CreateWindowError, CustomCursorError, EventLoopError, TransferError}; use winit_core::event::WindowEvent; use winit_core::event_loop::pump_events::PumpStatus; use winit_core::event_loop::{ @@ -82,7 +82,7 @@ impl RootActiveEventLoop for ActiveEventLoop { fn create_custom_cursor( &self, source: CustomCursorSource, - ) -> Result { + ) -> Result { Ok(CoreCustomCursor(Arc::new(CustomCursor::new(source)?))) } @@ -568,7 +568,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.window_target().create_custom_cursor(custom_cursor) } } diff --git a/winit-core/src/error.rs b/winit-core/src/error.rs index 2e6c0daa..503d266d 100644 --- a/winit-core/src/error.rs +++ b/winit-core/src/error.rs @@ -137,6 +137,41 @@ impl From for TransferError { } } +/// An error from `create_custom_cursor`. +#[derive(Debug)] +#[non_exhaustive] +pub enum CustomCursorError { + /// Custom cursors are not supported. + NotSupported, + /// The [`CustomCursorSource`] variant is not supported. + UnsupportedSource, + /// Got unspecified OS specific error during the request. + Os(OsError), +} + +impl Display for CustomCursorError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::NotSupported => write!(f, "create_custom_cursor is not supported"), + Self::UnsupportedSource => { + write!(f, "the provided CustomCursorSource is not supported") + }, + Self::Os(err) => err.fmt(f), + } + } +} +impl Error for CustomCursorError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + if let Self::Os(err) = self { err.source() } else { None } + } +} + +impl From for CustomCursorError { + fn from(value: OsError) -> Self { + Self::Os(value) + } +} + /// A general error that may occur during a request to the windowing system. #[derive(Debug)] #[non_exhaustive] diff --git a/winit-core/src/event_loop/mod.rs b/winit-core/src/event_loop/mod.rs index 00da6aca..9e54b194 100644 --- a/winit-core/src/event_loop/mod.rs +++ b/winit-core/src/event_loop/mod.rs @@ -16,7 +16,7 @@ use crate::application::ApplicationHandler; use crate::cursor::{CustomCursor, CustomCursorSource}; use crate::data_transfer::{DataTransfer, DataTransferId, DataTransferSend, TransferType}; use crate::error::{ - CreateWindowError, EventLoopError, NotSupportedError, RequestError, TransferError, + CreateWindowError, CustomCursorError, EventLoopError, NotSupportedError, TransferError, }; use crate::icon::Icon; use crate::monitor::MonitorHandle; @@ -108,7 +108,7 @@ pub trait EventLoopProvider: fmt::Debug { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result; + ) -> Result; } pub trait ActiveEventLoop: Any + fmt::Debug { @@ -137,7 +137,7 @@ pub trait ActiveEventLoop: Any + fmt::Debug { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result; + ) -> Result; /// Returns the list of all the monitors available on the system. /// diff --git a/winit-orbital/src/event_loop.rs b/winit-orbital/src/event_loop.rs index 6cb560da..b23d9379 100644 --- a/winit-orbital/src/event_loop.rs +++ b/winit-orbital/src/event_loop.rs @@ -16,7 +16,7 @@ use redox_event::{EventFlags, EventQueue, UserData}; use smol_str::SmolStr; use winit_core::application::ApplicationHandler; use winit_core::cursor::{CustomCursor, CustomCursorSource}; -use winit_core::error::{CreateWindowError, EventLoopError, NotSupportedError, RequestError}; +use winit_core::error::{CreateWindowError, CustomCursorError, EventLoopError}; use winit_core::event::{self, Modifiers, StartCause}; use winit_core::event_loop::pump_events::PumpStatus; use winit_core::event_loop::{ @@ -776,7 +776,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.window_target().create_custom_cursor(custom_cursor) } } @@ -829,8 +829,11 @@ impl RootActiveEventLoop for ActiveEventLoop { Ok(Box::new(Window::new(self, window_attributes)?)) } - fn create_custom_cursor(&self, _: CustomCursorSource) -> Result { - Err(NotSupportedError::new("create_custom_cursor is not supported").into()) + fn create_custom_cursor( + &self, + _: CustomCursorSource, + ) -> Result { + Err(CustomCursorError::NotSupported) } fn available_monitors(&self) -> Box> { diff --git a/winit-uikit/src/event_loop.rs b/winit-uikit/src/event_loop.rs index 17bac4a4..3a5a9671 100644 --- a/winit-uikit/src/event_loop.rs +++ b/winit-uikit/src/event_loop.rs @@ -17,7 +17,7 @@ use winit_common::core_foundation::{MainRunLoop, MainRunLoopObserver, tracing_ob use winit_common::foundation::create_observer; use winit_core::application::ApplicationHandler; use winit_core::cursor::{CustomCursor, CustomCursorSource}; -use winit_core::error::{CreateWindowError, EventLoopError, NotSupportedError, RequestError}; +use winit_core::error::{CreateWindowError, CustomCursorError, EventLoopError}; use winit_core::event_loop::{ ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopProvider, EventLoopProxy as CoreEventLoopProxy, OwnedDisplayHandle as CoreOwnedDisplayHandle, @@ -50,8 +50,8 @@ impl RootActiveEventLoop for ActiveEventLoop { fn create_custom_cursor( &self, _source: CustomCursorSource, - ) -> Result { - Err(NotSupportedError::new("create_custom_cursor is not supported").into()) + ) -> Result { + Err(CustomCursorError::NotSupported) } fn available_monitors(&self) -> Box> { @@ -358,7 +358,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.window_target().create_custom_cursor(custom_cursor) } } diff --git a/winit-wayland/src/event_loop/mod.rs b/winit-wayland/src/event_loop/mod.rs index be5283d6..6b9e3bff 100644 --- a/winit-wayland/src/event_loop/mod.rs +++ b/winit-wayland/src/event_loop/mod.rs @@ -28,7 +28,7 @@ use winit_core::application::ApplicationHandler; use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource}; use winit_core::data_transfer::{DataTransfer, DataTransferId, DataTransferSend, TransferType}; use winit_core::error::{ - CreateWindowError, EventLoopError, NotSupportedError, OsError, RequestError, TransferError, + CreateWindowError, CustomCursorError, EventLoopError, NotSupportedError, OsError, TransferError, }; use winit_core::event::{DeviceEvent, StartCause, SurfaceSizeWriter, WindowEvent}; use winit_core::event_loop::pump_events::PumpStatus; @@ -654,7 +654,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.active_event_loop.create_custom_cursor(custom_cursor) } } @@ -726,11 +726,11 @@ impl RootActiveEventLoop for ActiveEventLoop { fn create_custom_cursor( &self, cursor: CustomCursorSource, - ) -> Result { + ) -> Result { let cursor_image = match cursor { CustomCursorSource::Image(cursor_image) => cursor_image, _ => { - return Err(NotSupportedError::new("unsupported cursor kind").into()); + return Err(CustomCursorError::UnsupportedSource); }, }; diff --git a/winit-web/src/event_loop/mod.rs b/winit-web/src/event_loop/mod.rs index fb42a423..652de6d9 100644 --- a/winit-web/src/event_loop/mod.rs +++ b/winit-web/src/event_loop/mod.rs @@ -2,7 +2,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use winit_core::application::ApplicationHandler; use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource}; -use winit_core::error::{EventLoopError, NotSupportedError, RequestError}; +use winit_core::error::{CustomCursorError, EventLoopError, NotSupportedError}; use winit_core::event_loop::{ ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopProvider, EventLoopProxy, OwnedDisplayHandle, @@ -107,7 +107,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.window_target().create_custom_cursor(custom_cursor) } } diff --git a/winit-web/src/event_loop/window_target.rs b/winit-web/src/event_loop/window_target.rs index 29a015a2..19975ad6 100644 --- a/winit-web/src/event_loop/window_target.rs +++ b/winit-web/src/event_loop/window_target.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use web_sys::Element; use winit_core::application::ApplicationHandler; use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource}; -use winit_core::error::{CreateWindowError, NotSupportedError, RequestError}; +use winit_core::error::{CreateWindowError, CustomCursorError, NotSupportedError}; use winit_core::event::{ElementState, KeyEvent, TouchPhase, WindowEvent}; use winit_core::event_loop::{ ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, @@ -501,7 +501,7 @@ impl RootActiveEventLoop for ActiveEventLoop { fn create_custom_cursor( &self, source: CustomCursorSource, - ) -> Result { + ) -> Result { Ok(CoreCustomCursor(Arc::new(CustomCursor::new(self, source)))) } diff --git a/winit-win32/src/event_loop.rs b/winit-win32/src/event_loop.rs index 2ccddbd5..5fed2d07 100644 --- a/winit-win32/src/event_loop.rs +++ b/winit-win32/src/event_loop.rs @@ -68,7 +68,7 @@ use winit_core::data_transfer::{ DataTransfer, DataTransferId, DataTransferSend, TransferType, TypedData, }; use winit_core::error::{ - CreateWindowError, EventLoopError, NotSupportedError, RequestError, TransferError, + CreateWindowError, CustomCursorError, EventLoopError, NotSupportedError, TransferError, }; use winit_core::event::{ DeviceEvent, DeviceId, FingerId, Force, Ime, RawKeyEvent, SurfaceSizeWriter, TabletToolButton, @@ -425,7 +425,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.window_target().create_custom_cursor(custom_cursor) } } @@ -466,12 +466,10 @@ impl RootActiveEventLoop for ActiveEventLoop { fn create_custom_cursor( &self, source: CustomCursorSource, - ) -> Result { + ) -> Result { let cursor = match source { CustomCursorSource::Image(cursor) => cursor, - _ => { - return Err(NotSupportedError::new("unsupported cursor kind").into()); - }, + _ => return Err(CustomCursorError::UnsupportedSource), }; Ok(CustomCursor(Arc::new(WinCursor::new(&cursor)?))) diff --git a/winit-win32/src/icon.rs b/winit-win32/src/icon.rs index 88fa9588..40118d56 100644 --- a/winit-win32/src/icon.rs +++ b/winit-win32/src/icon.rs @@ -14,7 +14,7 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{ }; use windows_sys::core::PCWSTR; use winit_core::cursor::{CursorImage, CustomCursorProvider}; -use winit_core::error::RequestError; +use winit_core::error::CustomCursorError; use winit_core::icon::*; use super::util; @@ -174,7 +174,7 @@ impl CustomCursorProvider for WinCursor { } impl WinCursor { - pub(crate) fn new(image: &CursorImage) -> Result { + pub(crate) fn new(image: &CursorImage) -> Result { let mut bgra = Vec::from(image.buffer()); bgra.chunks_exact_mut(4).for_each(|chunk| chunk.swap(0, 2)); diff --git a/winit-x11/src/event_loop.rs b/winit-x11/src/event_loop.rs index 51edaeff..da350d07 100644 --- a/winit-x11/src/event_loop.rs +++ b/winit-x11/src/event_loop.rs @@ -21,7 +21,7 @@ use winit_core::application::ApplicationHandler; use winit_core::cursor::{CustomCursor as CoreCustomCursor, CustomCursorSource}; use winit_core::data_transfer::{DataTransfer, DataTransferId, TransferType}; use winit_core::error::{ - CreateWindowError, EventLoopError, NotSupportedError, RequestError, TransferError, + CreateWindowError, CustomCursorError, EventLoopError, NotSupportedError, TransferError, }; use winit_core::event::{DeviceId, StartCause, WindowEvent}; use winit_core::event_loop::pump_events::PumpStatus; @@ -695,7 +695,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.window_target().create_custom_cursor(custom_cursor) } } @@ -766,7 +766,7 @@ impl RootActiveEventLoop for ActiveEventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { Ok(CoreCustomCursor(Arc::new(CustomCursor::new(self, custom_cursor)?))) } diff --git a/winit-x11/src/util/cursor.rs b/winit-x11/src/util/cursor.rs index cf155345..c32f7fbb 100644 --- a/winit-x11/src/util/cursor.rs +++ b/winit-x11/src/util/cursor.rs @@ -4,7 +4,7 @@ use std::iter; use std::sync::Arc; use winit_core::cursor::{CursorIcon, CustomCursorProvider, CustomCursorSource}; -use winit_core::error::{NotSupportedError, RequestError}; +use winit_core::error::CustomCursorError; use x11rb::connection::Connection; use x11rb::protocol::render::{self, ConnectionExt as _}; use x11rb::protocol::xproto; @@ -196,11 +196,11 @@ impl CustomCursor { pub(crate) fn new( event_loop: &ActiveEventLoop, cursor: CustomCursorSource, - ) -> Result { + ) -> Result { let mut cursor = match cursor { CustomCursorSource::Image(cursor_image) => cursor_image, _ => { - return Err(NotSupportedError::new("unsupported cursor kind").into()); + return Err(CustomCursorError::UnsupportedSource); }, }; diff --git a/winit/examples/application.rs b/winit/examples/application.rs index 27fb8443..534bc325 100644 --- a/winit/examples/application.rs +++ b/winit/examples/application.rs @@ -22,7 +22,7 @@ use web_time::Instant; use winit::application::ApplicationHandler; use winit::cursor::{Cursor, CustomCursor, CustomCursorSource}; use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize}; -use winit::error::RequestError; +use winit::error::CustomCursorError; use winit::event::{DeviceEvent, DeviceId, MouseButton, MouseScrollDelta, WindowEvent}; use winit::event_loop::{ActiveEventLoop, EventLoop, OwnedDisplayHandle}; use winit::icon::{Icon, RgbaIcon}; @@ -83,7 +83,7 @@ struct Application { receiver: Receiver, sender: Sender, /// Custom cursors assets. - custom_cursors: Result, RequestError>, + custom_cursors: Result, CustomCursorError>, /// Application icon. icon: Icon, windows: HashMap, diff --git a/winit/src/event_loop.rs b/winit/src/event_loop.rs index ff9ee6cc..77929011 100644 --- a/winit/src/event_loop.rs +++ b/winit/src/event_loop.rs @@ -13,11 +13,12 @@ use std::marker::PhantomData; use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; use rwh_06::{DisplayHandle, HandleError, HasDisplayHandle}; +use winit_core::error::CustomCursorError; pub use winit_core::event_loop::*; use crate::application::ApplicationHandler; use crate::cursor::{CustomCursor, CustomCursorSource}; -use crate::error::{EventLoopError, RequestError}; +use crate::error::EventLoopError; use crate::platform_impl; /// Provides a way to retrieve events from the system and from the windows that were registered to @@ -207,7 +208,7 @@ impl EventLoop { pub fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.event_loop.window_target().create_custom_cursor(custom_cursor) } } @@ -236,7 +237,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.create_custom_cursor(custom_cursor) } } diff --git a/winit/src/platform_impl/linux/mod.rs b/winit/src/platform_impl/linux/mod.rs index 79a8a86d..52c0117d 100644 --- a/winit/src/platform_impl/linux/mod.rs +++ b/winit/src/platform_impl/linux/mod.rs @@ -10,7 +10,7 @@ use std::time::Duration; pub(crate) use winit_common::xkb::{physicalkey_to_scancode, scancode_to_physicalkey}; use winit_core::application::ApplicationHandler; use winit_core::cursor::{CustomCursor, CustomCursorSource}; -use winit_core::error::{EventLoopError, NotSupportedError, RequestError}; +use winit_core::error::{CustomCursorError, EventLoopError, NotSupportedError}; use winit_core::event_loop::pump_events::PumpStatus; use winit_core::event_loop::{ ActiveEventLoop, ControlFlow, DeviceEvents, EventLoopProvider, EventLoopProxy, @@ -201,7 +201,7 @@ impl EventLoopProvider for EventLoop { fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, - ) -> Result { + ) -> Result { self.window_target().create_custom_cursor(custom_cursor) } }