use crate::{
handler::{
BoxedAction, BoxedActionOnce, Handler, HandlerOnce, boxed_action, boxed_action_once,
},
layout::Point,
metadata::MetadataKey,
};
use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum LifeCycle {
Appear,
Disappear,
}
pub struct LifeCycleHook {
lifecycle: LifeCycle,
handler: BoxedActionOnce<()>,
}
impl MetadataKey for LifeCycleHook {}
impl LifeCycleHook {
#[must_use]
pub fn new<Args>(lifecycle: LifeCycle, handler: impl HandlerOnce<Args, ()>) -> Self {
Self {
lifecycle,
handler: boxed_action_once(handler),
}
}
#[must_use]
pub const fn lifecycle(&self) -> LifeCycle {
self.lifecycle
}
#[must_use = "the hook is consumed, so dropping the handler discards the callback"]
pub fn into_handler(self) -> BoxedActionOnce<()> {
self.handler
}
pub fn handle(self, env: &crate::Environment) {
(self.handler)(env);
}
}
impl fmt::Debug for LifeCycleHook {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LifeCycleHook")
.field("lifecycle", &self.lifecycle)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HoverEvent {
pub location: Point,
}
impl HoverEvent {
#[must_use]
pub const fn new(location: Point) -> Self {
Self { location }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Event {
HoverEnter,
HoverMove,
HoverExit,
}
pub struct OnEvent {
event: Event,
handler: BoxedAction<()>,
}
impl MetadataKey for OnEvent {}
impl OnEvent {
#[must_use]
pub fn new<Args>(event: Event, handler: impl Handler<Args, ()>) -> Self {
Self {
event,
handler: boxed_action(handler),
}
}
#[must_use]
pub const fn event(&self) -> Event {
self.event
}
pub fn handle(&mut self, env: &crate::Environment) {
(self.handler)(env);
}
}
impl fmt::Debug for OnEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OnEvent")
.field("event", &self.event)
.finish_non_exhaustive()
}
}