use std::cell::RefCell;
use std::sync::Mutex;
use std::time::Instant;
use ratatui::{buffer::Buffer, layout::Rect, style::Modifier};
use crate::palette;
use crate::tui::hover_hit::{
HoverHit, HoverTargetKind, copy_affordance, hit_test, link_hover_style,
};
static POINTER: Mutex<Option<(u16, u16)>> = Mutex::new(None);
thread_local! {
static FRAME_TARGETS: RefCell<Vec<HoverHit>> = const { RefCell::new(Vec::new()) };
static FRAME_HOVER: RefCell<Option<HoverHit>> = const { RefCell::new(None) };
static FRAME_START: RefCell<Option<Instant>> = const { RefCell::new(None) };
}
pub fn begin_frame() {
FRAME_TARGETS.with(|t| t.borrow_mut().clear());
FRAME_HOVER.with(|h| *h.borrow_mut() = None);
FRAME_START.with(|s| *s.borrow_mut() = Some(Instant::now()));
}
pub fn register(hit: HoverHit) {
FRAME_TARGETS.with(|t| t.borrow_mut().push(hit));
}
pub fn register_rect(kind: HoverTargetKind, area: Rect, label: impl Into<String>, copyable: bool) {
if area.width == 0 || area.height == 0 {
return;
}
register(HoverHit {
kind,
area,
label: label.into(),
copyable,
});
}
pub fn set_pointer(column: u16, row: u16) {
if let Ok(mut guard) = POINTER.lock() {
*guard = Some((column, row));
}
}
#[cfg(test)]
pub fn clear_pointer() {
if let Ok(mut guard) = POINTER.lock() {
*guard = None;
}
}
pub fn resolve_hover() {
let pointer = POINTER.lock().ok().and_then(|g| *g);
let Some((col, row)) = pointer else {
FRAME_HOVER.with(|h| *h.borrow_mut() = None);
return;
};
FRAME_TARGETS.with(|t| {
let targets = t.borrow();
let hit = hit_test(col, row, &targets).cloned();
FRAME_HOVER.with(|h| *h.borrow_mut() = hit);
});
}
#[must_use]
pub fn current_hover() -> Option<HoverHit> {
FRAME_HOVER.with(|h| h.borrow().clone())
}
fn elapsed_ms() -> u128 {
FRAME_START
.with(|s| s.borrow().map(|t| t.elapsed().as_millis()))
.unwrap_or(0)
}
pub fn paint_link_glow(
buf: &mut Buffer,
area: Rect,
fg: ratatui::style::Color,
reduced_motion: bool,
) {
let ms = elapsed_ms();
let style = link_hover_style(fg, reduced_motion, ms);
for y in area.y..area.y.saturating_add(area.height) {
for x in area.x..area.x.saturating_add(area.width) {
if x >= buf.area.x.saturating_add(buf.area.width)
|| y >= buf.area.y.saturating_add(buf.area.height)
{
continue;
}
let cell = &mut buf[(x, y)];
if let Some(color) = style.fg {
cell.set_fg(color);
}
cell.modifier.insert(Modifier::UNDERLINED);
}
}
}
pub fn apply_resolved_effects(
buf: &mut Buffer,
reduced_motion: bool,
_accent: ratatui::style::Color,
) {
resolve_hover();
let Some(hit) = current_hover() else {
return;
};
paint_link_glow(buf, hit.area, palette::WHALE_ACTION, reduced_motion);
if hit.copyable && hit.area.width > 8 {
let chip = copy_affordance();
let chip_w = unicode_width::UnicodeWidthStr::width(chip) as u16;
if chip_w < hit.area.width {
let x = hit
.area
.x
.saturating_add(hit.area.width.saturating_sub(chip_w + 1));
let y = hit.area.y;
for (i, ch) in chip.chars().enumerate() {
let cx = x.saturating_add(i as u16);
if cx >= buf.area.x.saturating_add(buf.area.width) {
break;
}
let cell = &mut buf[(cx, y)];
cell.set_symbol(&ch.to_string());
cell.set_fg(palette::TEXT_HINT);
cell.modifier.insert(Modifier::DIM);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
static HOVER_TEST_LOCK: Mutex<()> = Mutex::new(());
#[test]
fn register_and_resolve_hit() {
let _guard = HOVER_TEST_LOCK.lock().unwrap();
clear_pointer();
begin_frame();
set_pointer(5, 2);
register_rect(
HoverTargetKind::Link,
Rect::new(0, 2, 20, 1),
"fn main",
true,
);
resolve_hover();
let hit = current_hover().expect("hover");
assert_eq!(hit.kind, HoverTargetKind::Link);
assert!(hit.copyable);
clear_pointer();
}
}