Skip to content

Commit

Permalink
Refactor window errors
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 30, 2023
1 parent 687e2f6 commit c79d885
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 254 deletions.
69 changes: 0 additions & 69 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@ use std::{error, fmt};

use crate::platform_impl;

// TODO: Rename
/// An error that may be generated when requesting Winit state
#[derive(Debug)]
pub enum ExternalError {
/// The operation is not supported by the backend.
NotSupported(NotSupportedError),
/// The OS cannot perform the operation.
Os(OsError),
}

/// The error type for when the requested operation is not supported by the backend.
#[derive(Clone)]
pub struct NotSupportedError {
_marker: (),
}

/// The error type for when the OS cannot perform the requested operation.
#[derive(Debug)]
pub struct OsError {
Expand All @@ -26,14 +10,6 @@ pub struct OsError {
error: platform_impl::OsError,
}

impl NotSupportedError {
#[inline]
#[allow(dead_code)]
pub(crate) fn new() -> NotSupportedError {
NotSupportedError { _marker: () }
}
}

impl OsError {
#[allow(dead_code)]
pub(crate) fn new(line: u32, file: &'static str, error: platform_impl::OsError) -> OsError {
Expand All @@ -57,49 +33,4 @@ impl fmt::Display for OsError {
}
}

impl fmt::Display for ExternalError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
match self {
ExternalError::NotSupported(e) => e.fmt(f),
ExternalError::Os(e) => e.fmt(f),
}
}
}

impl fmt::Debug for NotSupportedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.debug_struct("NotSupportedError").finish()
}
}

impl fmt::Display for NotSupportedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
f.pad("the requested operation is not supported by Winit")
}
}

impl error::Error for OsError {}
impl error::Error for ExternalError {}
impl error::Error for NotSupportedError {}

#[cfg(test)]
mod tests {
#![allow(clippy::redundant_clone)]

use super::*;

// Eat attributes for testing
#[test]
fn ensure_fmt_does_not_panic() {
let _ = format!(
"{:?}, {}",
NotSupportedError::new(),
NotSupportedError::new().clone()
);
let _ = format!(
"{:?}, {}",
ExternalError::NotSupported(NotSupportedError::new()),
ExternalError::NotSupported(NotSupportedError::new())
);
}
}
29 changes: 26 additions & 3 deletions src/platform/startup_notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
//! [`here`]: https://specifications.freedesktop.org/startup-notification-spec/startup-notification-latest.txt
use std::env;
use std::error::Error;
use std::fmt;

use crate::error::NotSupportedError;
use crate::event_loop::{AsyncRequestSerial, EventLoopWindowTarget};
use crate::window::{ActivationToken, Window, WindowBuilder};

Expand All @@ -33,6 +34,28 @@ const X11_VAR: &str = "DESKTOP_STARTUP_ID";
/// The variable which is used mostly on Wayland.
const WAYLAND_VAR: &str = "XDG_ACTIVATION_TOKEN";

/// The error type for when the requested operation is not supported by the backend.
#[derive(Debug)]
pub struct ActivationTokenNotFound {
_marker: (),
}

impl fmt::Display for ActivationTokenNotFound {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "the requested operation is not supported by Winit")
}
}

impl ActivationTokenNotFound {
#[inline]
#[allow(dead_code)]
pub(crate) fn new() -> ActivationTokenNotFound {
ActivationTokenNotFound { _marker: () }
}
}

impl Error for ActivationTokenNotFound {}

pub trait EventLoopExtStartupNotify {
/// Read the token from the environment.
///
Expand All @@ -44,7 +67,7 @@ pub trait WindowExtStartupNotify {
/// Request a new activation token.
///
/// The token will be delivered inside
fn request_activation_token(&self) -> Result<AsyncRequestSerial, NotSupportedError>;
fn request_activation_token(&self) -> Result<AsyncRequestSerial, ActivationTokenNotFound>;
}

pub trait WindowBuilderExtStartupNotify {
Expand All @@ -69,7 +92,7 @@ impl<T> EventLoopExtStartupNotify for EventLoopWindowTarget<T> {
}

impl WindowExtStartupNotify for Window {
fn request_activation_token(&self) -> Result<AsyncRequestSerial, NotSupportedError> {
fn request_activation_token(&self) -> Result<AsyncRequestSerial, ActivationTokenNotFound> {
self.window.request_activation_token()
}
}
Expand Down
48 changes: 18 additions & 30 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ use raw_window_handle::{
use crate::platform_impl::Fullscreen;
use crate::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
error,
error::OsError,
event::{self, InnerSizeWriter, StartCause},
event_loop::{
self, ControlFlow, EventLoopCreationError, EventLoopRunError,
EventLoopWindowTarget as RootELW,
},
platform::pump_events::PumpStatus,
window::{
self, CursorGrabMode, ImePurpose, ResizeDirection, Theme, WindowButtons, WindowLevel,
self, CursorGrabMode, ImePurpose, NotSupportedError, ResizeDirection, Theme, WindowButtons,
WindowError, WindowLevel,
},
};

Expand Down Expand Up @@ -794,7 +795,7 @@ impl Window {
el: &EventLoopWindowTarget<T>,
_window_attrs: window::WindowAttributes,
_: PlatformSpecificWindowBuilderAttributes,
) -> Result<Self, error::OsError> {
) -> Result<Self, OsError> {
// FIXME this ignores requested window attributes

Ok(Self {
Expand Down Expand Up @@ -839,12 +840,12 @@ impl Window {

pub fn pre_present_notify(&self) {}

pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, error::NotSupportedError> {
Err(error::NotSupportedError::new())
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
Err(NotSupportedError::new())
}

pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, error::NotSupportedError> {
Err(error::NotSupportedError::new())
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
Err(NotSupportedError::new())
}

pub fn set_outer_position(&self, _position: Position) {
Expand Down Expand Up @@ -937,39 +938,26 @@ impl Window {

pub fn set_cursor_icon(&self, _: window::CursorIcon) {}

pub fn set_cursor_position(&self, _: Position) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
pub fn set_cursor_position(&self, _: Position) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn set_cursor_grab(&self, _: CursorGrabMode) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
pub fn set_cursor_grab(&self, _: CursorGrabMode) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn set_cursor_visible(&self, _: bool) {}

pub fn drag_window(&self) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
pub fn drag_window(&self) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn drag_resize_window(
&self,
_direction: ResizeDirection,
) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
pub fn drag_resize_window(&self, _direction: ResizeDirection) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn set_cursor_hittest(&self, _hittest: bool) -> Result<(), error::ExternalError> {
Err(error::ExternalError::NotSupported(
error::NotSupportedError::new(),
))
pub fn set_cursor_hittest(&self, _hittest: bool) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn raw_window_handle(&self) -> RawWindowHandle {
Expand Down
27 changes: 14 additions & 13 deletions src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ use super::uikit::{UIApplication, UIScreen, UIScreenOverscanCompensation};
use super::view::{WinitUIWindow, WinitView, WinitViewController};
use crate::{
dpi::{self, LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
error::{ExternalError, NotSupportedError, OsError as RootOsError},
error::OsError as RootOsError,
event::{Event, WindowEvent},
icon::Icon,
platform::ios::{ScreenEdge, ValidOrientations},
platform_impl::platform::{
app_state, monitor, EventLoopWindowTarget, Fullscreen, MonitorHandle,
},
window::{
CursorGrabMode, CursorIcon, ImePurpose, ResizeDirection, Theme, UserAttentionType,
WindowAttributes, WindowButtons, WindowId as RootWindowId, WindowLevel,
CursorGrabMode, CursorIcon, ImePurpose, NotSupportedError, ResizeDirection, Theme,
UserAttentionType, WindowAttributes, WindowButtons, WindowError, WindowId as RootWindowId,
WindowLevel,
},
};

Expand Down Expand Up @@ -174,28 +175,28 @@ impl Inner {
debug!("`Window::set_cursor_icon` ignored on iOS")
}

pub fn set_cursor_position(&self, _position: Position) -> Result<(), ExternalError> {
Err(ExternalError::NotSupported(NotSupportedError::new()))
pub fn set_cursor_position(&self, _position: Position) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn set_cursor_grab(&self, _: CursorGrabMode) -> Result<(), ExternalError> {
Err(ExternalError::NotSupported(NotSupportedError::new()))
pub fn set_cursor_grab(&self, _: CursorGrabMode) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn set_cursor_visible(&self, _visible: bool) {
debug!("`Window::set_cursor_visible` is ignored on iOS")
}

pub fn drag_window(&self) -> Result<(), ExternalError> {
Err(ExternalError::NotSupported(NotSupportedError::new()))
pub fn drag_window(&self) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn drag_resize_window(&self, _direction: ResizeDirection) -> Result<(), ExternalError> {
Err(ExternalError::NotSupported(NotSupportedError::new()))
pub fn drag_resize_window(&self, _direction: ResizeDirection) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn set_cursor_hittest(&self, _hittest: bool) -> Result<(), ExternalError> {
Err(ExternalError::NotSupported(NotSupportedError::new()))
pub fn set_cursor_hittest(&self, _hittest: bool) -> Result<(), WindowError> {
Err(WindowError::NotSupported(NotSupportedError::new()))
}

pub fn set_minimized(&self, _minimized: bool) {
Expand Down
22 changes: 13 additions & 9 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ use once_cell::sync::Lazy;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use smol_str::SmolStr;

use crate::platform::startup_notify::ActivationTokenNotFound;
#[cfg(x11_platform)]
use crate::platform::x11::XlibErrorHook;
use crate::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
error::{ExternalError, NotSupportedError, OsError as RootOsError},
error::OsError as RootOsError,
event::{Event, KeyEvent},
event_loop::{
AsyncRequestSerial, ControlFlow, DeviceEvents, EventLoopClosed, EventLoopCreationError,
Expand All @@ -31,8 +32,9 @@ use crate::{
scancode::KeyCodeExtScancode,
},
window::{
ActivationToken, CursorGrabMode, CursorIcon, ImePurpose, ResizeDirection, Theme,
UserAttentionType, WindowAttributes, WindowButtons, WindowLevel,
ActivationToken, CursorGrabMode, CursorIcon, ImePurpose, NotSupportedError,
ResizeDirection, Theme, UserAttentionType, WindowAttributes, WindowButtons, WindowError,
WindowLevel,
},
};
#[cfg(x11_platform)]
Expand Down Expand Up @@ -375,7 +377,9 @@ impl Window {
}

#[inline]
pub(crate) fn request_activation_token(&self) -> Result<AsyncRequestSerial, NotSupportedError> {
pub(crate) fn request_activation_token(
&self,
) -> Result<AsyncRequestSerial, ActivationTokenNotFound> {
x11_or_wayland!(match self; Window(w) => w.request_activation_token())
}

Expand Down Expand Up @@ -425,7 +429,7 @@ impl Window {
}

#[inline]
pub fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ExternalError> {
pub fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), WindowError> {
x11_or_wayland!(match self; Window(window) => window.set_cursor_grab(mode))
}

Expand All @@ -435,17 +439,17 @@ impl Window {
}

#[inline]
pub fn drag_window(&self) -> Result<(), ExternalError> {
pub fn drag_window(&self) -> Result<(), WindowError> {
x11_or_wayland!(match self; Window(window) => window.drag_window())
}

#[inline]
pub fn drag_resize_window(&self, direction: ResizeDirection) -> Result<(), ExternalError> {
pub fn drag_resize_window(&self, direction: ResizeDirection) -> Result<(), WindowError> {
x11_or_wayland!(match self; Window(window) => window.drag_resize_window(direction))
}

#[inline]
pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), WindowError> {
x11_or_wayland!(match self; Window(w) => w.set_cursor_hittest(hittest))
}

Expand All @@ -455,7 +459,7 @@ impl Window {
}

#[inline]
pub fn set_cursor_position(&self, position: Position) -> Result<(), ExternalError> {
pub fn set_cursor_position(&self, position: Position) -> Result<(), WindowError> {
x11_or_wayland!(match self; Window(w) => w.set_cursor_position(position))
}

Expand Down
Loading

0 comments on commit c79d885

Please sign in to comment.