use crate::WidgetId;
use crate::World;
use std::collections::HashMap;
use std::sync::Arc;
pub use crossterm::event::MouseEventKind;
pub type PointerFn = Arc<dyn Fn(&mut World, Area, u16, u16) + Send + Sync>;
#[derive(Debug, Clone, Copy)]
pub struct Area {
pub x: u16,
pub y: u16,
pub width: u16,
pub height: u16,
}
impl Area {
#[must_use]
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
Self {
x,
y,
width,
height,
}
}
#[must_use]
pub fn contains(&self, x: u16, y: u16) -> bool {
x >= self.x && x < self.x + self.width && y >= self.y && y < self.y + self.height
}
}
impl From<(u16, u16)> for Area {
fn from((x, y): (u16, u16)) -> Self {
Self::new(x, y, 1, 1)
}
}
#[cfg(feature = "ratatui")]
impl From<ratatui::layout::Rect> for Area {
fn from(rect: ratatui::layout::Rect) -> Self {
Self::new(rect.x, rect.y, rect.width, rect.height)
}
}
#[derive(Default)]
struct Handlers {
down: Option<PointerFn>,
drag: Option<PointerFn>,
up: Option<PointerFn>,
}
pub struct Pointer {
order: Vec<WidgetId>,
areas: HashMap<WidgetId, Area>,
handlers: HashMap<WidgetId, Handlers>,
active: Option<WidgetId>,
}
impl Default for Pointer {
fn default() -> Self {
Self::new()
}
}
impl Pointer {
#[must_use]
pub fn new() -> Self {
Self {
order: Vec::new(),
areas: HashMap::new(),
handlers: HashMap::new(),
active: None,
}
}
pub fn set<W: Into<Area>>(&mut self, id: WidgetId, area: W) {
if !self.areas.contains_key(&id) {
self.order.push(id);
}
self.areas.insert(id, area.into());
}
#[must_use]
pub fn get(&self, id: WidgetId) -> Option<&Area> {
self.areas.get(&id)
}
pub fn on_click<F>(&mut self, id: WidgetId, handler: F)
where
F: Fn(&mut World, Area, u16, u16) + Send + Sync + 'static,
{
self.handlers.entry(id).or_default().down = Some(Arc::new(handler));
}
pub fn on_down<F>(&mut self, id: WidgetId, handler: F)
where
F: Fn(&mut World, Area, u16, u16) + Send + Sync + 'static,
{
self.on_click(id, handler);
}
pub fn on_drag<F>(&mut self, id: WidgetId, handler: F)
where
F: Fn(&mut World, Area, u16, u16) + Send + Sync + 'static,
{
self.handlers.entry(id).or_default().drag = Some(Arc::new(handler));
}
pub fn on_up<F>(&mut self, id: WidgetId, handler: F)
where
F: Fn(&mut World, Area, u16, u16) + Send + Sync + 'static,
{
self.handlers.entry(id).or_default().up = Some(Arc::new(handler));
}
#[must_use]
pub fn get_handler(&self, id: WidgetId) -> Option<PointerFn> {
self.handlers.get(&id).and_then(|h| h.down.clone())
}
pub fn remove(&mut self, id: WidgetId) {
self.areas.remove(&id);
self.handlers.remove(&id);
self.order.retain(|&i| i != id);
if self.active == Some(id) {
self.active = None;
}
}
#[must_use]
pub fn hit_test(&self, x: u16, y: u16) -> Option<WidgetId> {
for &id in self.order.iter().rev() {
if let Some(area) = self.areas.get(&id)
&& area.contains(x, y)
{
return Some(id);
}
}
None
}
#[must_use]
pub fn hit_test_handler(&self, x: u16, y: u16) -> Option<(WidgetId, PointerFn)> {
self.hit_test(x, y).and_then(|id| {
self.handlers
.get(&id)
.and_then(|h| h.down.clone())
.map(|h| (id, h))
})
}
pub fn set_active(&mut self, id: Option<WidgetId>) {
self.active = id;
}
#[must_use]
pub fn active(&self) -> Option<WidgetId> {
self.active
}
#[must_use]
pub fn get_handler_for(&self, id: WidgetId, kind: MouseEventKind) -> Option<PointerFn> {
let handlers = self.handlers.get(&id)?;
match kind {
MouseEventKind::Down(_) => handlers.down.clone(),
MouseEventKind::Drag(_) => handlers.drag.clone(),
MouseEventKind::Up(_) => handlers.up.clone(),
_ => None,
}
}
}