use pyo3::{prelude::*, types::PyString};
use crate::utils::non_exhaustive_panic;
pub use tauri::EventId;
#[pyclass(frozen)]
#[non_exhaustive]
pub struct Event {
#[pyo3(get)]
pub id: EventId, #[pyo3(get)]
pub payload: Py<PyString>,
}
#[pyclass(frozen)]
#[non_exhaustive]
pub enum EventTarget {
Any(),
AnyLabel { label: Py<PyString> },
App(),
Window { label: Py<PyString> },
Webview { label: Py<PyString> },
WebviewWindow { label: Py<PyString> },
_NonExhaustive(),
}
impl EventTarget {
pub(crate) fn from_tauri(py: Python<'_>, value: &tauri::EventTarget) -> Self {
match value {
tauri::EventTarget::Any => Self::Any(),
tauri::EventTarget::AnyLabel { label } => Self::AnyLabel {
label: PyString::new(py, label).unbind(),
},
tauri::EventTarget::App => Self::App(),
tauri::EventTarget::Window { label } => Self::Window {
label: PyString::new(py, label).unbind(),
},
tauri::EventTarget::Webview { label } => Self::Webview {
label: PyString::new(py, label).unbind(),
},
tauri::EventTarget::WebviewWindow { label } => Self::WebviewWindow {
label: PyString::new(py, label).unbind(),
},
_ => Self::_NonExhaustive(),
}
}
pub(crate) fn to_tauri(&self, py: Python<'_>) -> PyResult<tauri::EventTarget> {
let value = match self {
Self::Any() => tauri::EventTarget::Any,
Self::AnyLabel { label } => tauri::EventTarget::AnyLabel {
label: label.bind(py).to_cow()?.into_owned(),
},
Self::App() => tauri::EventTarget::App,
Self::Window { label } => tauri::EventTarget::Window {
label: label.bind(py).to_cow()?.into_owned(),
},
Self::Webview { label } => tauri::EventTarget::Webview {
label: label.bind(py).to_cow()?.into_owned(),
},
Self::WebviewWindow { label } => tauri::EventTarget::WebviewWindow {
label: label.bind(py).to_cow()?.into_owned(),
},
Self::_NonExhaustive() => non_exhaustive_panic(),
};
Ok(value)
}
}