use super::*;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ViewportProximitySnapshot {
pub in_proximity: bool,
pub bounds: kurbo::Rect,
pub viewport_intersection: Option<kurbo::Rect>,
}
impl ViewportProximitySnapshot {
pub fn is_in_viewport(&self) -> bool {
self.viewport_intersection.is_some()
}
pub fn intersection_ratio(&self) -> f64 {
let area = self.bounds.area();
if area.is_finite() && area > 0.0 {
self.viewport_intersection
.map_or(0.0, |r| (r.area() / area).clamp(0.0, 1.0))
} else {
0.0
}
}
}
#[derive(Clone, Debug)]
pub struct ViewportProximityEnter {
pub current: ViewportProximitySnapshot,
}
#[derive(Clone, Debug)]
pub struct ViewportProximityChange {
pub previous: Option<ViewportProximitySnapshot>,
pub current: ViewportProximitySnapshot,
}
#[derive(Clone, Debug)]
pub struct ViewportProximityExit {
pub previous: ViewportProximitySnapshot,
pub current: ViewportProximitySnapshot,
}
#[derive(Clone)]
pub struct Event<T> {
pub args: T,
cancelled: Rc<Cell<bool>>,
}
impl<T: Clone + 'static> ImplToFromPaxAny for Event<T> {}
impl<T> Event<T> {
pub fn new(args: T) -> Self {
Self {
args,
cancelled: Default::default(),
}
}
pub fn prevent_default(&self) {
self.cancelled.set(true);
}
pub fn cancelled(&self) -> bool {
self.cancelled.get()
}
}
impl<T> Deref for Event<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.args
}
}
#[derive(Clone)]
pub struct Scroll {
pub delta_x: f64,
pub delta_y: f64,
}
#[derive(Clone)]
pub struct Touch {
pub x: f64,
pub y: f64,
pub identifier: i64,
pub delta_x: f64,
pub delta_y: f64,
}
impl From<&TouchMessage> for Touch {
fn from(value: &TouchMessage) -> Self {
Touch {
x: value.x,
y: value.y,
identifier: value.identifier,
delta_x: value.delta_x,
delta_y: value.delta_y,
}
}
}
#[derive(Clone)]
pub struct TouchStart {
pub touches: Vec<Touch>,
}
#[derive(Clone)]
pub struct TouchMove {
pub touches: Vec<Touch>,
}
#[derive(Clone)]
pub struct TouchEnd {
pub touches: Vec<Touch>,
}
#[derive(Clone)]
pub struct TouchCancel {
pub touches: Vec<Touch>,
}
#[derive(Clone)]
pub struct KeyboardEventArgs {
pub key: String,
pub modifiers: Vec<ModifierKey>,
pub is_repeat: bool,
}
#[derive(Clone)]
pub struct KeyDown {
pub keyboard: KeyboardEventArgs,
}
#[derive(Clone)]
pub struct SelectStart {}
#[derive(Clone)]
pub struct KeyUp {
pub keyboard: KeyboardEventArgs,
}
#[derive(Clone)]
pub struct KeyPress {
pub keyboard: KeyboardEventArgs,
}
#[derive(Clone)]
pub struct Focus {}
#[derive(Clone)]
pub struct MouseEventArgs {
pub x: f64,
pub y: f64,
pub button: MouseButton,
pub modifiers: Vec<ModifierKey>,
}
#[derive(Clone, PartialEq)]
pub enum MouseButton {
Left,
Right,
Middle,
Unknown,
}
impl From<MouseButtonMessage> for MouseButton {
fn from(value: MouseButtonMessage) -> Self {
match value {
MouseButtonMessage::Left => MouseButton::Left,
MouseButtonMessage::Right => MouseButton::Right,
MouseButtonMessage::Middle => MouseButton::Middle,
MouseButtonMessage::Unknown => MouseButton::Unknown,
}
}
}
#[derive(Clone)]
pub enum ModifierKey {
Shift,
Control,
Alt,
Command,
}
impl From<&ModifierKeyMessage> for ModifierKey {
fn from(value: &ModifierKeyMessage) -> Self {
match value {
ModifierKeyMessage::Shift => ModifierKey::Shift,
ModifierKeyMessage::Control => ModifierKey::Control,
ModifierKeyMessage::Alt => ModifierKey::Alt,
ModifierKeyMessage::Command => ModifierKey::Command,
}
}
}
#[derive(Clone)]
pub struct Click {
pub mouse: MouseEventArgs,
}
#[derive(Clone)]
pub struct Drop {
pub x: f64,
pub y: f64,
pub name: String,
pub mime_type: String,
pub data: Vec<u8>,
}
#[derive(Clone)]
pub struct DoubleClick {
pub mouse: MouseEventArgs,
}
#[derive(Clone)]
pub struct MouseMove {
pub mouse: MouseEventArgs,
}
#[derive(Clone)]
pub struct Wheel {
pub x: f64,
pub y: f64,
pub delta_x: f64,
pub delta_y: f64,
pub modifiers: Vec<ModifierKey>,
}
#[derive(Clone)]
pub struct CheckboxChange {
pub checked: bool,
}
#[derive(Clone)]
pub struct SliderChange {
pub value: f64,
}
#[derive(Clone)]
pub struct TextInput {
pub text: String,
}
#[derive(Clone)]
pub struct TextboxChange {
pub text: String,
}
#[derive(Clone)]
pub struct TextboxInput {
pub text: String,
}
#[derive(Clone)]
pub struct ButtonClick {}
#[derive(Clone, PartialEq)]
pub enum PhotoPickerStatus {
Selected,
Cancelled,
PermissionDenied,
Unavailable,
SizeLimitExceeded,
Failed,
}
impl From<&str> for PhotoPickerStatus {
fn from(value: &str) -> Self {
match value {
"selected" => Self::Selected,
"cancelled" => Self::Cancelled,
"permission_denied" => Self::PermissionDenied,
"unavailable" => Self::Unavailable,
"size_limit_exceeded" => Self::SizeLimitExceeded,
_ => Self::Failed,
}
}
}
#[derive(Clone, PartialEq)]
pub enum PhotoPickerSourceKind {
Library,
File,
Camera,
Other(String),
}
impl From<&str> for PhotoPickerSourceKind {
fn from(value: &str) -> Self {
match value {
"library" => Self::Library,
"file" => Self::File,
"camera" => Self::Camera,
other => Self::Other(other.to_string()),
}
}
}
#[derive(Clone)]
pub struct PhotoPickerPhoto {
pub temp_id: String,
pub file_name: Option<String>,
pub mime_type: String,
pub byte_size: u64,
pub width: Option<u32>,
pub height: Option<u32>,
pub source_kind: PhotoPickerSourceKind,
pub handle: Option<String>,
pub data: Option<Vec<u8>>,
}
impl From<&PhotoPickerAssetArgs> for PhotoPickerPhoto {
fn from(value: &PhotoPickerAssetArgs) -> Self {
Self {
temp_id: value.temp_id.clone(),
file_name: value.file_name.clone(),
mime_type: value.mime_type.clone(),
byte_size: value.byte_size,
width: value.width,
height: value.height,
source_kind: PhotoPickerSourceKind::from(value.source_kind.as_str()),
handle: value.handle.clone(),
data: if value.data.is_empty() {
None
} else {
Some(value.data.clone())
},
}
}
}
#[derive(Clone)]
pub struct PhotoPickerChange {
pub id: u32,
pub request_id: u64,
pub status: PhotoPickerStatus,
pub message: Option<String>,
pub photos: Vec<PhotoPickerPhoto>,
}
impl From<&PhotoPickerInterruptArgs> for PhotoPickerChange {
fn from(value: &PhotoPickerInterruptArgs) -> Self {
Self {
id: value.id,
request_id: value.request_id,
status: PhotoPickerStatus::from(value.status.as_str()),
message: value.message.clone(),
photos: value.photos.iter().map(PhotoPickerPhoto::from).collect(),
}
}
}
#[derive(Clone)]
pub struct MouseDown {
pub mouse: MouseEventArgs,
}
#[derive(Clone)]
pub struct MouseUp {
pub mouse: MouseEventArgs,
}
#[derive(Clone)]
pub struct MouseOver {}
#[derive(Clone)]
pub struct MouseOut {}
#[derive(Clone)]
pub struct ContextMenu {
pub mouse: MouseEventArgs,
}