use std::marker::PhantomData;
use ori_macro::Build;
use crate::{
context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
event::{Event, Key, PointerButton},
layout::{Size, Space},
rebuild::Rebuild,
view::{Pod, PodState, View},
};
pub fn on_press<T, V, F>(content: V, on_press: F) -> Clickable<T, V, F>
where
V: View<T>,
F: FnMut(&mut EventCx, &mut T) + 'static,
{
Clickable::new(content, ClickEvent::Press, on_press)
}
pub fn on_release<T, V, F>(content: V, on_release: F) -> Clickable<T, V, F>
where
V: View<T>,
F: FnMut(&mut EventCx, &mut T) + 'static,
{
Clickable::new(content, ClickEvent::Release, on_release)
}
pub fn on_click<T, V, F>(content: V, on_click: F) -> Clickable<T, V, F>
where
V: View<T>,
F: FnMut(&mut EventCx, &mut T) + 'static,
{
Clickable::new(content, ClickEvent::Click, on_click)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ClickEvent {
Press,
Release,
Click,
}
#[derive(Build, Rebuild)]
pub struct Clickable<T, V, F>
where
V: View<T>,
F: FnMut(&mut EventCx, &mut T) + 'static,
{
pub content: Pod<V>,
pub descendants: bool,
pub button: Option<PointerButton>,
pub event: ClickEvent,
#[build(ignore)]
pub callback: F,
marker: PhantomData<fn() -> T>,
}
impl<T, V, F> Clickable<T, V, F>
where
V: View<T>,
F: FnMut(&mut EventCx, &mut T) + 'static,
{
pub fn new(content: V, event: ClickEvent, callback: F) -> Self {
Self {
content: Pod::new(content),
descendants: true,
button: None,
event,
callback,
marker: PhantomData,
}
}
fn is_button(&self, button: PointerButton) -> bool {
self.button.map_or(true, |b| b == button)
}
}
impl<T, V, F> View<T> for Clickable<T, V, F>
where
V: View<T>,
F: FnMut(&mut EventCx, &mut T) + 'static,
{
type State = PodState<T, V>;
fn build(&mut self, cx: &mut BuildCx, data: &mut T) -> Self::State {
self.content.build(cx, data)
}
fn rebuild(&mut self, content: &mut Self::State, cx: &mut RebuildCx, data: &mut T, old: &Self) {
Rebuild::rebuild(self, cx, old);
self.content.rebuild(content, cx, data, &old.content);
}
fn event(
&mut self,
content: &mut Self::State,
cx: &mut EventCx,
data: &mut T,
event: &Event,
) -> bool {
let is_hovered = content.is_hovered() || (content.has_hovered() && self.descendants);
let mut handled = false;
match event {
Event::PointerPressed(e) if is_hovered && self.is_button(e.button) => {
if self.event == ClickEvent::Press {
(self.callback)(cx, data);
handled = true;
}
content.set_active(true);
}
Event::PointerPressed(_) if !is_hovered => {
content.set_focused(false);
}
Event::PointerReleased(e) if content.is_active() && self.is_button(e.button) => {
if self.event == ClickEvent::Release {
(self.callback)(cx, data);
handled = true;
}
if e.clicked && self.event == ClickEvent::Click {
(self.callback)(cx, data);
handled = true;
}
content.set_active(false);
}
Event::KeyPressed(e) if content.is_focused() => {
if e.is_key(Key::Enter) || e.is_key(' ') {
if matches!(self.event, ClickEvent::Press | ClickEvent::Click) {
(self.callback)(cx, data);
handled = true;
}
content.set_active(true);
}
}
Event::KeyReleased(e) if content.is_active() => {
if e.is_key(Key::Enter) || e.is_key(' ') {
if self.event == ClickEvent::Release {
(self.callback)(cx, data);
handled = true;
}
content.set_active(false);
}
}
_ => {}
}
self.content.event_maybe(handled, content, cx, data, event)
}
fn layout(
&mut self,
content: &mut Self::State,
cx: &mut LayoutCx,
data: &mut T,
space: Space,
) -> Size {
self.content.layout(content, cx, data, space)
}
fn draw(&mut self, content: &mut Self::State, cx: &mut DrawCx, data: &mut T) {
self.content.draw(content, cx, data);
}
}