Skip to main content

winio_ui_apple_common/
lib.rs

1//! Apple common methods for winio.
2
3#![allow(unused_features)]
4#![cfg(target_vendor = "apple")]
5
6use objc2::{exception::Exception, rc::Retained};
7use objc2_foundation::NSError;
8
9/// Error type for Apple backends.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13    #[error("IO error: {0}")]
14    Io(#[from] std::io::Error),
15    #[error("Objective-C exception: {0:?}")]
16    ObjC(Option<Retained<Exception>>),
17    #[error("NSError: {0:?}")]
18    NS(Option<Retained<NSError>>),
19    #[error("Channel recv error: {0}")]
20    ChannelRecv(#[from] local_sync::oneshot::error::RecvError),
21    #[cfg(feature = "webview")]
22    #[error("Time component range error: {0}")]
23    TimeRange(#[from] time::error::ComponentRange),
24    #[error("Null pointer returned")]
25    NullPointer,
26    #[error("Called from non-main thread")]
27    NotMainThread,
28    #[error("Feature not supported")]
29    NotSupported,
30}
31
32unsafe impl Send for Error {}
33unsafe impl Sync for Error {}
34
35impl From<Retained<Exception>> for Error {
36    fn from(exc: Retained<Exception>) -> Self {
37        Error::ObjC(Some(exc))
38    }
39}
40
41impl From<Option<Retained<Exception>>> for Error {
42    fn from(exc: Option<Retained<Exception>>) -> Self {
43        Error::ObjC(exc)
44    }
45}
46
47impl From<Retained<NSError>> for Error {
48    fn from(err: Retained<NSError>) -> Self {
49        Error::NS(Some(err))
50    }
51}
52
53impl From<Option<Retained<NSError>>> for Error {
54    fn from(err: Option<Retained<NSError>>) -> Self {
55        Error::NS(err)
56    }
57}
58
59pub type Result<T, E = Error> = std::result::Result<T, E>;
60
61pub fn catch<F, R>(f: F) -> Result<R>
62where
63    F: FnOnce() -> R,
64{
65    match objc2::exception::catch(std::panic::AssertUnwindSafe(f)) {
66        Ok(v) => Ok(v),
67        Err(exc) => {
68            compio_log::error!("Caught Objective-C exception: {exc:?}");
69            Err(Error::from(exc))
70        }
71    }
72}
73
74mod drawing;
75pub use drawing::*;
76
77mod string;
78pub use string::*;
79
80#[cfg(feature = "webview")]
81mod webview;
82#[cfg(feature = "webview")]
83pub use webview::*;