Add enum CustomCursorError

This commit is contained in:
Diggory Hardy authored and Olivier Goffart committed 2026-09-24 10:08:56 +02:00
1 parent c8f71cf346
commit 528fc3e552
17 files changed
+92 -51

No files matched your search

+6 -4
View File
@@ -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<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
self.window_target().create_custom_cursor(custom_cursor)
}
}
@@ -733,8 +735,8 @@ impl RootActiveEventLoop for ActiveEventLoop {
fn create_custom_cursor(
&self,
_source: CustomCursorSource,
) -> Result<CustomCursor, RequestError> {
Err(NotSupportedError::new("create_custom_cursor is not supported").into())
) -> Result<CustomCursor, CustomCursorError> {
Err(CustomCursorError::NotSupported)
}
fn available_monitors(&self) -> Box<dyn Iterator<Item = CoreMonitorHandle>> {
+6 -4
View File
@@ -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<CustomCursor, RequestError> {
pub(crate) fn new(cursor: CustomCursorSource) -> Result<CustomCursor, CustomCursorError> {
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<Retained<NSImage>, RequestE
Ok(image)
}
pub(crate) fn cursor_from_image(cursor: &CursorImage) -> Result<Retained<NSCursor>, RequestError> {
pub(crate) fn cursor_from_image(
cursor: &CursorImage,
) -> Result<Retained<NSCursor>, CustomCursorError> {
let width = cursor.width();
let height = cursor.height();
+3 -3
View File
@@ -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<CoreCustomCursor, RequestError> {
) -> Result<CoreCustomCursor, CustomCursorError> {
Ok(CoreCustomCursor(Arc::new(CustomCursor::new(source)?)))
}
@@ -568,7 +568,7 @@ impl EventLoopProvider for EventLoop {
fn create_custom_cursor(
&self,
custom_cursor: CustomCursorSource,
) -> Result<CoreCustomCursor, RequestError> {
) -> Result<CoreCustomCursor, CustomCursorError> {
self.window_target().create_custom_cursor(custom_cursor)
}
}
+35
View File
@@ -137,6 +137,41 @@ impl From<OsError> 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<OsError> 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]
+3 -3
View File
@@ -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<CustomCursor, RequestError>;
) -> Result<CustomCursor, CustomCursorError>;
}
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<CustomCursor, RequestError>;
) -> Result<CustomCursor, CustomCursorError>;
/// Returns the list of all the monitors available on the system.
///
+7 -4
View File
@@ -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<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
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<CustomCursor, RequestError> {
Err(NotSupportedError::new("create_custom_cursor is not supported").into())
fn create_custom_cursor(
&self,
_: CustomCursorSource,
) -> Result<CustomCursor, CustomCursorError> {
Err(CustomCursorError::NotSupported)
}
fn available_monitors(&self) -> Box<dyn Iterator<Item = winit_core::monitor::MonitorHandle>> {
+4 -4
View File
@@ -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<CustomCursor, RequestError> {
Err(NotSupportedError::new("create_custom_cursor is not supported").into())
) -> Result<CustomCursor, CustomCursorError> {
Err(CustomCursorError::NotSupported)
}
fn available_monitors(&self) -> Box<dyn Iterator<Item = CoreMonitorHandle>> {
@@ -358,7 +358,7 @@ impl EventLoopProvider for EventLoop {
fn create_custom_cursor(
&self,
custom_cursor: CustomCursorSource,
) -> Result<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
self.window_target().create_custom_cursor(custom_cursor)
}
}
+4 -4
View File
@@ -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<CoreCustomCursor, RequestError> {
) -> Result<CoreCustomCursor, CustomCursorError> {
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<CoreCustomCursor, RequestError> {
) -> Result<CoreCustomCursor, CustomCursorError> {
let cursor_image = match cursor {
CustomCursorSource::Image(cursor_image) => cursor_image,
_ => {
return Err(NotSupportedError::new("unsupported cursor kind").into());
return Err(CustomCursorError::UnsupportedSource);
},
};
+2 -2
View File
@@ -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<CoreCustomCursor, RequestError> {
) -> Result<CoreCustomCursor, CustomCursorError> {
self.window_target().create_custom_cursor(custom_cursor)
}
}
+2 -2
View File
@@ -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<CoreCustomCursor, RequestError> {
) -> Result<CoreCustomCursor, CustomCursorError> {
Ok(CoreCustomCursor(Arc::new(CustomCursor::new(self, source))))
}
+4 -6
View File
@@ -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<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
self.window_target().create_custom_cursor(custom_cursor)
}
}
@@ -466,12 +466,10 @@ impl RootActiveEventLoop for ActiveEventLoop {
fn create_custom_cursor(
&self,
source: CustomCursorSource,
) -> Result<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
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)?)))
+2 -2
View File
@@ -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<Self, RequestError> {
pub(crate) fn new(image: &CursorImage) -> Result<Self, CustomCursorError> {
let mut bgra = Vec::from(image.buffer());
bgra.chunks_exact_mut(4).for_each(|chunk| chunk.swap(0, 2));
+3 -3
View File
@@ -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<CoreCustomCursor, RequestError> {
) -> Result<CoreCustomCursor, CustomCursorError> {
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<CoreCustomCursor, RequestError> {
) -> Result<CoreCustomCursor, CustomCursorError> {
Ok(CoreCustomCursor(Arc::new(CustomCursor::new(self, custom_cursor)?)))
}
+3 -3
View File
@@ -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<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
let mut cursor = match cursor {
CustomCursorSource::Image(cursor_image) => cursor_image,
_ => {
return Err(NotSupportedError::new("unsupported cursor kind").into());
return Err(CustomCursorError::UnsupportedSource);
},
};
+2 -2
View File
@@ -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<Action>,
sender: Sender<Action>,
/// Custom cursors assets.
custom_cursors: Result<Vec<CustomCursor>, RequestError>,
custom_cursors: Result<Vec<CustomCursor>, CustomCursorError>,
/// Application icon.
icon: Icon,
windows: HashMap<WindowId, WindowState>,
+4 -3
View File
@@ -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<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
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<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
self.create_custom_cursor(custom_cursor)
}
}
+2 -2
View File
@@ -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<CustomCursor, RequestError> {
) -> Result<CustomCursor, CustomCursorError> {
self.window_target().create_custom_cursor(custom_cursor)
}
}