Add enum TransferError

This commit is contained in:
Diggory Hardy authored and Olivier Goffart committed 2026-09-24 10:08:56 +02:00
1 parent 5766910754
commit c8f71cf346
7 files changed
+115 -125

No files matched your search

+12 -26
View File
@@ -1,4 +1,3 @@
use std::fmt;
use std::rc::Rc;
use std::sync::Arc;
use std::time::{Duration, Instant};
@@ -23,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};
use winit_core::error::{CreateWindowError, EventLoopError, RequestError, TransferError};
use winit_core::event::WindowEvent;
use winit_core::event_loop::pump_events::PumpStatus;
use winit_core::event_loop::{
@@ -141,12 +140,12 @@ impl RootActiveEventLoop for ActiveEventLoop {
&self,
id: DataTransferId,
type_: &dyn TransferType,
) -> Result<AsyncRequestSerial, RequestError> {
) -> Result<AsyncRequestSerial, TransferError> {
let Some(pb) = self.app_state.pasteboards().get(id) else {
return Err(RequestError::Ignored);
return Err(TransferError::UnknownTransfer(id));
};
let Some(window_id) = self.app_state.pasteboards().window_id(id) else {
return Err(RequestError::Ignored);
return Err(TransferError::Failed);
};
let serial = AsyncRequestSerial::get();
@@ -168,9 +167,9 @@ impl RootActiveEventLoop for ActiveEventLoop {
Ok(serial)
}
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, RequestError> {
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, TransferError> {
let Some(pb) = self.app_state.pasteboards().get(id) else {
return Err(RequestError::Ignored);
return Err(TransferError::UnknownTransfer(id));
};
Ok(Box::new(pb))
@@ -180,14 +179,14 @@ impl RootActiveEventLoop for ActiveEventLoop {
&self,
id: DataTransferId,
actions: &[DndAction],
) -> Result<(), RequestError> {
) -> Result<(), TransferError> {
let mut state = self.app_state.drag_state().borrow_mut();
let Some(drag_state) = &mut *state else {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
};
if drag_state.id != id {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
}
drag_state.valid_actions.clear();
@@ -202,7 +201,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
send_data: Box<dyn DataTransferSend>,
actions: &[DndAction],
icon: Option<DragIcon>,
) -> Result<DataTransferId, RequestError> {
) -> Result<DataTransferId, TransferError> {
let drag_operation = dnd_actions_to_ns_drag_operation(actions);
self.app_state
@@ -212,7 +211,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
let drag_image = icon.and_then(|icon| image_from_icon(&icon.icon).ok());
let Some(event) = delegate.window().currentEvent() else {
return Err(RequestError::Ignored);
return Err(TransferError::Failed);
};
let dragging_rect_size = drag_image
@@ -297,23 +296,10 @@ impl RootActiveEventLoop for ActiveEventLoop {
Ok(id)
})
.ok_or(RequestError::Ignored)?
.ok_or(TransferError::Failed)?
}
}
/// An operation was attempted on a data transfer ID, but that ID was invalid.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct UnknownDataTransfer(pub DataTransferId);
impl fmt::Display for UnknownDataTransfer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let id = self.0.into_raw();
write!(f, "Unknown data transfer with ID {id}")
}
}
impl std::error::Error for UnknownDataTransfer {}
impl rwh_06::HasDisplayHandle for ActiveEventLoop {
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
let raw = rwh_06::RawDisplayHandle::AppKit(rwh_06::AppKitDisplayHandle::new());
+3 -2
View File
@@ -1701,8 +1701,9 @@ impl WindowDelegate {
#[inline]
pub fn drag_window(&self) -> Result<(), RequestError> {
let mtm = MainThreadMarker::from(self);
let event =
NSApplication::sharedApplication(mtm).currentEvent().ok_or(RequestError::Ignored)?;
let event = NSApplication::sharedApplication(mtm)
.currentEvent()
.ok_or(RequestError::TransientFailed)?;
self.window().performWindowDragWithEvent(&event);
Ok(())
}
+46 -1
View File
@@ -1,6 +1,8 @@
use std::error::Error;
use std::fmt::{self, Display};
use crate::data_transfer::DataTransferId;
/// A general error that may occur while running or creating
/// the event loop.
#[derive(Debug)]
@@ -92,6 +94,49 @@ impl From<OsError> for CreateWindowError {
}
}
/// A data transfer / drag-and-drop error.
#[derive(Debug)]
#[non_exhaustive]
pub enum TransferError {
/// The request is not supported.
NotSupported(NotSupportedError),
/// Unknown [`DataTransferId`]
UnknownTransfer(DataTransferId),
/// The transfer (request) failed.
Failed,
/// Got unspecified OS specific error during the request.
Os(OsError),
}
impl Display for TransferError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotSupported(err) => err.fmt(f),
Self::UnknownTransfer(id) => {
write!(f, "Unknown data transfer with ID {}", id.into_raw())
},
Self::Failed => write!(f, "Transfer request failed"),
Self::Os(err) => err.fmt(f),
}
}
}
impl Error for TransferError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
if let Self::Os(err) = self { err.source() } else { None }
}
}
impl From<NotSupportedError> for TransferError {
fn from(value: NotSupportedError) -> Self {
Self::NotSupported(value)
}
}
impl From<OsError> for TransferError {
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]
@@ -159,7 +204,7 @@ pub struct NotSupportedError {
}
impl NotSupportedError {
pub fn new(reason: &'static str) -> Self {
pub const fn new(reason: &'static str) -> Self {
Self { reason }
}
}
+16 -21
View File
@@ -15,7 +15,9 @@ use crate::Instant;
use crate::application::ApplicationHandler;
use crate::cursor::{CustomCursor, CustomCursorSource};
use crate::data_transfer::{DataTransfer, DataTransferId, DataTransferSend, TransferType};
use crate::error::{CreateWindowError, EventLoopError, NotSupportedError, RequestError};
use crate::error::{
CreateWindowError, EventLoopError, NotSupportedError, RequestError, TransferError,
};
use crate::icon::Icon;
use crate::monitor::MonitorHandle;
use crate::window::{Theme, Window, WindowAttributes, WindowId};
@@ -222,23 +224,19 @@ pub trait ActiveEventLoop: Any + fmt::Debug {
&self,
id: DataTransferId,
type_: &dyn TransferType,
) -> Result<AsyncRequestSerial, RequestError> {
) -> Result<AsyncRequestSerial, TransferError> {
let _ = id;
let _ = type_;
Err(RequestError::NotSupported(NotSupportedError::new(
DATA_TRANSFER_UNSUPPORTED_ERROR_MESSAGE,
)))
Err(DATA_TRANSFER_NOT_SUPPORTED)
}
/// Get a [data transfer](DataTransfer) by its ID.
///
/// If the ID is invalid (e.g. if the lifetime of the data transfer has expired), this will
/// return an error.
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, RequestError> {
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, TransferError> {
let _ = id;
Err(RequestError::NotSupported(NotSupportedError::new(
DATA_TRANSFER_UNSUPPORTED_ERROR_MESSAGE,
)))
Err(DATA_TRANSFER_NOT_SUPPORTED)
}
/// Set a given set of `DndAction`s as the valid actions for the given [`DataTransferId`],
@@ -264,12 +262,10 @@ pub trait ActiveEventLoop: Any + fmt::Debug {
&self,
id: DataTransferId,
actions: &[DndAction],
) -> Result<(), RequestError> {
) -> Result<(), TransferError> {
let _ = id;
let _ = actions;
Err(RequestError::NotSupported(NotSupportedError::new(
DATA_TRANSFER_UNSUPPORTED_ERROR_MESSAGE,
)))
Err(DATA_TRANSFER_NOT_SUPPORTED)
}
/// Initiate a new drag-and-drop operation.
@@ -320,21 +316,20 @@ pub trait ActiveEventLoop: Any + fmt::Debug {
send_data: Box<dyn DataTransferSend>,
actions: &[DndAction],
icon: Option<DragIcon>,
) -> Result<DataTransferId, RequestError> {
) -> Result<DataTransferId, TransferError> {
let _ = source;
let _ = send_data;
let _ = actions;
let _ = icon;
Err(RequestError::NotSupported(NotSupportedError::new(
DATA_TRANSFER_UNSUPPORTED_ERROR_MESSAGE,
)))
Err(DATA_TRANSFER_NOT_SUPPORTED)
}
}
const DATA_TRANSFER_UNSUPPORTED_ERROR_MESSAGE: &str = {
"Cross-application data transfer (e.g. drag-and-drop, clipboard) is unsupported on this \
platform"
};
const DATA_TRANSFER_NOT_SUPPORTED: TransferError =
TransferError::NotSupported(NotSupportedError::new(
"Cross-application data transfer (e.g. drag-and-drop, clipboard) is unsupported on this \
platform",
));
impl HasDisplayHandle for dyn ActiveEventLoop + '_ {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
+13 -26
View File
@@ -2,6 +2,7 @@
use std::cell::{Cell, RefCell};
use std::io::{self, Read, Result as IOResult};
use std::mem;
use std::ops::BitOr;
use std::os::fd::OwnedFd;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
@@ -9,7 +10,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
use std::{fmt, mem};
use calloop::PostAction;
use calloop::ping::Ping;
@@ -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,
CreateWindowError, EventLoopError, NotSupportedError, OsError, RequestError, TransferError,
};
use winit_core::event::{DeviceEvent, StartCause, SurfaceSizeWriter, WindowEvent};
use winit_core::event_loop::pump_events::PumpStatus;
@@ -787,18 +787,18 @@ impl RootActiveEventLoop for ActiveEventLoop {
&self,
id: DataTransferId,
type_: &dyn TransferType,
) -> Result<AsyncRequestSerial, RequestError> {
) -> Result<AsyncRequestSerial, TransferError> {
let state = self.state.borrow_mut();
let Some(current_drag) = state.dnd_state.receive_drag() else {
return Err(RequestError::Ignored);
return Err(TransferError::Failed);
};
if current_drag.transfer_id() != id {
return Err(RequestError::Ignored);
return Err(TransferError::UnknownTransfer(id));
}
let Some(mime_type) = current_drag.find_type_dyn(type_) else {
return Err(RequestError::Ignored);
return Err(TransferError::Failed);
};
let mime_type_str = mime_type.to_string();
@@ -848,14 +848,14 @@ impl RootActiveEventLoop for ActiveEventLoop {
Ok(async_request_serial)
}
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, RequestError> {
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, TransferError> {
let state = self.state.borrow();
let Some(state) = state.dnd_state.receive_drag() else {
return Err(RequestError::Ignored);
return Err(TransferError::Failed);
};
if state.transfer_id() != id {
return Err(RequestError::Ignored);
return Err(TransferError::UnknownTransfer(id));
}
Ok(Box::new(state.clone()))
@@ -865,14 +865,14 @@ impl RootActiveEventLoop for ActiveEventLoop {
&self,
id: DataTransferId,
actions: &[DndAction],
) -> Result<(), RequestError> {
) -> Result<(), TransferError> {
let state = self.state.borrow();
let Some(state) = state.dnd_state.receive_drag() else {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
};
if state.transfer_id() != id {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
}
let any_actions = state.set_actions(actions);
@@ -894,7 +894,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
send_data: Box<dyn DataTransferSend>,
action_mask: &[DndAction],
icon: Option<DragIcon>,
) -> Result<DataTransferId, RequestError> {
) -> Result<DataTransferId, TransferError> {
const NO_POINTER_CAP_ERROR_MSG: &str =
"Tried to initiate drag, but source window does not have the pointer capability";
@@ -997,19 +997,6 @@ impl RootActiveEventLoop for ActiveEventLoop {
}
}
/// An operation was attempted on a data transfer ID, but that ID was invalid.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct UnknownDataTransfer(pub DataTransferId);
impl fmt::Display for UnknownDataTransfer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let id = self.0.into_raw();
write!(f, "Unknown data transfer with ID {id}")
}
}
impl std::error::Error for UnknownDataTransfer {}
impl ActiveEventLoop {
fn clear_exit(&self) {
self.exit.set(None)
+12 -23
View File
@@ -67,7 +67,9 @@ use winit_core::cursor::{CustomCursor, CustomCursorSource};
use winit_core::data_transfer::{
DataTransfer, DataTransferId, DataTransferSend, TransferType, TypedData,
};
use winit_core::error::{CreateWindowError, EventLoopError, NotSupportedError, RequestError};
use winit_core::error::{
CreateWindowError, EventLoopError, NotSupportedError, RequestError, TransferError,
};
use winit_core::event::{
DeviceEvent, DeviceId, FingerId, Force, Ime, RawKeyEvent, SurfaceSizeWriter, TabletToolButton,
TabletToolData, TabletToolKind, TabletToolTilt, TouchPhase, WindowEvent,
@@ -523,14 +525,14 @@ impl RootActiveEventLoop for ActiveEventLoop {
&self,
id: DataTransferId,
type_: &dyn TransferType,
) -> Result<AsyncRequestSerial, RequestError> {
) -> Result<AsyncRequestSerial, TransferError> {
let Some(state) = self.0.drag_state(id) else {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
};
let hint = type_.hint().ok_or(RequestError::Ignored)?;
let hint = type_.hint().ok_or(TransferError::Failed)?;
let typed_data = WinTypedData::new(state.data.clone(), hint)
.map(|value| Arc::new(value) as Arc<dyn TypedData>)
.ok_or(RequestError::Ignored)?;
.ok_or(TransferError::Failed)?;
let serial = AsyncRequestSerial::get();
@@ -542,9 +544,9 @@ impl RootActiveEventLoop for ActiveEventLoop {
Ok(serial)
}
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, RequestError> {
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, TransferError> {
let Some(state) = self.0.drag_state(id) else {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
};
Ok(Box::new(WinDataTransfer::new(state.data.clone())))
@@ -554,10 +556,10 @@ impl RootActiveEventLoop for ActiveEventLoop {
&self,
id: DataTransferId,
actions: &[DndAction],
) -> Result<(), RequestError> {
) -> Result<(), TransferError> {
let mut state = self.0.drag_state.borrow_mut();
let Some(state) = state.as_mut().filter(|s| s.id == id) else {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
};
state.actions = actions.to_vec();
Ok(())
@@ -569,7 +571,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
send_data: Box<dyn DataTransferSend>,
allowed_actions: &[DndAction],
icon: Option<DragIcon>,
) -> Result<DataTransferId, RequestError> {
) -> Result<DataTransferId, TransferError> {
let allowed_effects = crate::dnd::dnd_actions_to_dropeffect_mask(allowed_actions);
// Win32 would happily run a modal `DoDragDrop` with `allowed_effects == 0`, but every
// target would see "no action allowed" and the drag would end in a guaranteed cancel
@@ -626,19 +628,6 @@ impl rwh_06::HasDisplayHandle for ActiveEventLoop {
}
}
/// An operation was attempted on a data transfer ID, but that ID was invalid.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct UnknownDataTransfer(pub DataTransferId);
impl fmt::Display for UnknownDataTransfer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let id = self.0.into_raw();
write!(f, "Unknown data transfer with ID {id}")
}
}
impl std::error::Error for UnknownDataTransfer {}
#[derive(Clone)]
pub(crate) struct OwnedDisplayHandle;
+13 -26
View File
@@ -20,7 +20,9 @@ use winit_common::xkb::Context;
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};
use winit_core::error::{
CreateWindowError, EventLoopError, NotSupportedError, RequestError, TransferError,
};
use winit_core::event::{DeviceId, StartCause, WindowEvent};
use winit_core::event_loop::pump_events::PumpStatus;
use winit_core::event_loop::{
@@ -814,15 +816,15 @@ impl RootActiveEventLoop for ActiveEventLoop {
self
}
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, RequestError> {
fn data_transfer(&self, id: DataTransferId) -> Result<Box<dyn DataTransfer>, TransferError> {
let dnd = self.dnd.borrow();
if dnd.state().is_none_or(|state| state.transfer_id != id) {
return Err(RequestError::Ignored);
return Err(TransferError::UnknownTransfer(id));
}
let Some(state) = dnd.state() else {
return Err(RequestError::Ignored);
return Err(TransferError::Failed);
};
Ok(Box::new(Selection::new(state.types.clone())))
@@ -832,7 +834,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
&self,
id: DataTransferId,
type_: &dyn TransferType,
) -> Result<AsyncRequestSerial, RequestError> {
) -> Result<AsyncRequestSerial, TransferError> {
let mut dnd = self.dnd.borrow_mut();
let serial = AsyncRequestSerial::get();
@@ -841,17 +843,15 @@ impl RootActiveEventLoop for ActiveEventLoop {
.cast_ref::<SelectionType>()
.or_else(|| dnd.find_type_by_hint(type_.hint()?))
.cloned()
.ok_or(RequestError::NotSupported(NotSupportedError::new("Unknown type hint")))?;
.ok_or(TransferError::Failed)?;
let new_convert_selection = {
let Some(state) = dnd.state_mut() else {
return Err(RequestError::Ignored);
return Err(TransferError::Failed);
};
if state.transfer_id != id {
return Err(RequestError::NotSupported(NotSupportedError::new(
"Unknown data transfer",
)));
return Err(TransferError::UnknownTransfer(id));
}
// If it's non-empty, assume that we're still waiting on some other fetch operation.
@@ -882,15 +882,15 @@ impl RootActiveEventLoop for ActiveEventLoop {
&self,
id: DataTransferId,
actions: &[DndAction],
) -> Result<(), RequestError> {
) -> Result<(), TransferError> {
let mut dnd = self.dnd.borrow_mut();
let Some(state) = dnd.state_mut() else {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
};
if state.transfer_id != id {
return Err(os_error!(UnknownDataTransfer(id)).into());
return Err(TransferError::UnknownTransfer(id));
}
state.accepted = !actions.is_empty();
@@ -905,19 +905,6 @@ impl rwh_06::HasDisplayHandle for ActiveEventLoop {
}
}
/// An operation was attempted on a data transfer ID, but that ID was invalid.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct UnknownDataTransfer(pub DataTransferId);
impl fmt::Display for UnknownDataTransfer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let id = self.0.into_raw();
write!(f, "Unknown data transfer with ID {id}")
}
}
impl std::error::Error for UnknownDataTransfer {}
pub(crate) struct DeviceInfo<'a> {
xconn: &'a XConnection,
info: *const ffi::XIDeviceInfo,