use core::fmt::Debug;
use core::{marker::PhantomData, ops::ControlFlow};
use alloc::vec::Vec;
use crate::el::ElId;
#[derive(Clone, Debug)]
pub enum Capture {
Captured,
}
impl<E: Event> Into<EventResponse<E>> for Capture {
#[inline]
fn into(self) -> EventResponse<E> {
EventResponse::Break(self)
}
}
#[derive(Clone, Debug)]
pub enum Propagate<E: Event> {
Ignored,
BubbleUp(ElId, E),
}
impl<E: Event> Into<EventResponse<E>> for Propagate<E> {
#[inline]
fn into(self) -> EventResponse<E> {
EventResponse::Continue(self)
}
}
pub type EventResponse<E> = ControlFlow<Capture, Propagate<E>>;
#[derive(Clone, Copy, Debug)]
pub enum CommonEvent {
FocusMove(i32),
FocusClickDown,
FocusClickUp,
}
pub trait Event: Clone + From<CommonEvent> + Debug {
fn as_common(&self) -> Option<CommonEvent>;
fn as_select_shift(&self) -> Option<i32>;
fn as_slider_shift(&self) -> Option<i32>;
fn as_knob_rotation(&self) -> Option<i32>;
fn as_input_letter_scroll(&self) -> Option<i32>;
}
#[derive(Clone, Debug)]
pub struct EventStub;
impl Event for EventStub {
fn as_common(&self) -> Option<CommonEvent> {
None
}
fn as_select_shift(&self) -> Option<i32> {
None
}
fn as_slider_shift(&self) -> Option<i32> {
None
}
fn as_knob_rotation(&self) -> Option<i32> {
None
}
fn as_input_letter_scroll(&self) -> Option<i32> {
None
}
}
impl From<CommonEvent> for EventStub {
fn from(_: CommonEvent) -> Self {
Self
}
}
pub trait Controls<E: Event> {
fn events(&mut self) -> Vec<E>;
}
impl<F, E: Event> Controls<E> for F
where
F: FnMut() -> Vec<E>,
{
fn events(&mut self) -> Vec<E> {
self()
}
}
pub struct NullControls<E: Event> {
marker: PhantomData<E>,
}
impl<E: Event> Controls<E> for NullControls<E> {
fn events(&mut self) -> Vec<E> {
vec![]
}
}
impl<E: Event> Default for NullControls<E> {
fn default() -> Self {
Self { marker: PhantomData }
}
}