use std::sync::Arc;
use super::{Widget, Children, PaintCtx};
pub struct Pressable<W: Widget> {
child: W,
on_press: Arc<dyn Fn() + Send + Sync>,
}
impl<W: Widget + Send + Sync + 'static> Pressable<W> {
pub fn new(child: W, on_press: impl Fn() + Send + Sync + 'static) -> Self {
Self { child, on_press: Arc::new(on_press) }
}
}
impl<W: Widget + Send + Sync + 'static> Widget for Pressable<W> {
fn children(&self) -> Children<'_> {
Children::One(&self.child)
}
fn paint(&self, ctx: &mut PaintCtx) {
let f = self.on_press.clone();
ctx.on_press(move || f());
let r = ctx.rect;
self.child.paint(&mut ctx.child(r));
}
}
pub struct LongPressable<W: Widget> {
child: W,
on_long_press: Arc<dyn Fn() + Send + Sync>,
}
impl<W: Widget + Send + Sync + 'static> LongPressable<W> {
pub fn new(child: W, f: impl Fn() + Send + Sync + 'static) -> Self {
Self { child, on_long_press: Arc::new(f) }
}
}
impl<W: Widget + Send + Sync + 'static> Widget for LongPressable<W> {
fn children(&self) -> Children<'_> { Children::One(&self.child) }
fn paint(&self, ctx: &mut PaintCtx) {
let f = self.on_long_press.clone();
ctx.on_long_press(move || f());
let r = ctx.rect;
self.child.paint(&mut ctx.child(r));
}
}
pub trait PressApi: Widget + Sized + Send + Sync + 'static {
fn on_press(self, f: impl Fn() + Send + Sync + 'static) -> Pressable<Self> {
Pressable::new(self, f)
}
fn on_long_press(self, f: impl Fn() + Send + Sync + 'static) -> LongPressable<Self> {
LongPressable::new(self, f)
}
}
impl<W: Widget + Send + Sync + 'static> PressApi for W {}