use crate::IntoFFI;
use crate::action::WuiAction;
use alloc::boxed::Box;
use waterui::gesture::{Gesture, GestureObserver};
#[repr(C)]
#[derive(Debug)]
pub enum WuiGesture {
Tap {
count: u32,
},
LongPress {
duration: u32,
},
Drag {
min_distance: f32,
},
Magnification {
initial_scale: f32,
},
Rotation {
initial_angle: f32,
},
Then {
first: *mut Self,
then: *mut Self,
},
Simultaneous {
first: *mut Self,
second: *mut Self,
},
Exclusive {
first: *mut Self,
second: *mut Self,
},
}
impl IntoFFI for Gesture {
type FFI = WuiGesture;
fn into_ffi(self) -> Self::FFI {
match self {
Self::Tap(tap) => WuiGesture::Tap { count: tap.count },
Self::LongPress(lp) => WuiGesture::LongPress {
duration: lp.duration,
},
Self::Drag(drag) => WuiGesture::Drag {
min_distance: drag.min_distance,
},
Self::Magnification(mag) => WuiGesture::Magnification {
initial_scale: mag.initial_scale,
},
Self::Rotation(rot) => WuiGesture::Rotation {
initial_angle: rot.initial_angle,
},
Self::Then(then) => {
let first = Box::into_raw(Box::new(then.first().clone().into_ffi()));
let then_gesture = Box::into_raw(Box::new(then.then().clone().into_ffi()));
WuiGesture::Then {
first,
then: then_gesture,
}
}
Self::Simultaneous(pair) => {
let first = Box::into_raw(Box::new(pair.first().clone().into_ffi()));
let second = Box::into_raw(Box::new(pair.second().clone().into_ffi()));
WuiGesture::Simultaneous { first, second }
}
Self::Exclusive(pair) => {
let first = Box::into_raw(Box::new(pair.first().clone().into_ffi()));
let second = Box::into_raw(Box::new(pair.second().clone().into_ffi()));
WuiGesture::Exclusive { first, second }
}
_ => panic!("Unsupported Gesture variant for FFI conversion"),
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn waterui_drop_gesture(gesture: *mut WuiGesture) {
unsafe {
let gesture = Box::from_raw(gesture);
match *gesture {
WuiGesture::Then { first, then } => {
waterui_drop_gesture(first);
waterui_drop_gesture(then);
}
WuiGesture::Simultaneous { first, second }
| WuiGesture::Exclusive { first, second } => {
waterui_drop_gesture(first);
waterui_drop_gesture(second);
}
WuiGesture::Tap { .. }
| WuiGesture::LongPress { .. }
| WuiGesture::Drag { .. }
| WuiGesture::Magnification { .. }
| WuiGesture::Rotation { .. } => {}
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct WuiGestureObserver {
pub gesture: WuiGesture,
pub action: *mut WuiAction,
}
impl IntoFFI for GestureObserver {
type FFI = WuiGestureObserver;
fn into_ffi(self) -> Self::FFI {
WuiGestureObserver {
gesture: self.gesture.into_ffi(),
action: self.action.into_ffi(),
}
}
}