#![allow(dead_code)]
use std::cell::RefCell;
use std::sync::Mutex;
use std::time::Instant;
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Modifier, Style},
text::{Line, Span},
};
use crate::palette;
use crate::tui::hover_hit::{
HoverHit, HoverTargetKind, copy_affordance, cursor_shape_for, hit_test, hover_aura_style,
link_hover_style, tooltip_line,
};
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));
}
}
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())
}
#[must_use]
pub fn active_cursor_shape() -> Option<&'static str> {
current_hover().map(|h| cursor_shape_for(h.kind))
}
fn elapsed_ms() -> u128 {
FRAME_START
.with(|s| s.borrow().map(|t| t.elapsed().as_millis()))
.unwrap_or(0)
}
pub fn paint_aura(
buf: &mut Buffer,
area: Rect,
accent: ratatui::style::Color,
reduced_motion: bool,
) {
if area.width == 0 || area.height == 0 {
return;
}
let ms = elapsed_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)];
let base = cell.bg;
let style = hover_aura_style(base, accent, reduced_motion, ms);
if let Some(bg) = style.bg {
cell.set_bg(bg);
}
}
}
}
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);
}
}
}
#[must_use]
pub fn copy_chip_line(max_width: u16) -> Line<'static> {
let text = copy_affordance();
let budget = usize::from(max_width.max(1));
let label = if unicode_width::UnicodeWidthStr::width(text) > budget {
"⧉".to_string()
} else {
text.to_string()
};
Line::from(Span::styled(
label,
Style::default()
.fg(palette::TEXT_HINT)
.add_modifier(Modifier::DIM),
))
}
#[must_use]
pub fn hover_tooltip(max_width: usize) -> Option<String> {
let hit = current_hover()?;
if hit.label.trim().is_empty() {
return None;
}
match hit.kind {
HoverTargetKind::FileRef | HoverTargetKind::ToolCard | HoverTargetKind::Link => {
Some(tooltip_line(&hit.label, max_width.max(8)))
}
_ => None,
}
}
pub fn apply_resolved_effects(
buf: &mut Buffer,
reduced_motion: bool,
accent: ratatui::style::Color,
) {
resolve_hover();
let Some(hit) = current_hover() else {
return;
};
match hit.kind {
HoverTargetKind::Link | HoverTargetKind::FileRef => {
paint_link_glow(buf, hit.area, palette::WHALE_ACTION, reduced_motion);
}
HoverTargetKind::Code
| HoverTargetKind::Diff
| HoverTargetKind::ToolCard
| HoverTargetKind::DiffAction
| HoverTargetKind::Plain
| HoverTargetKind::MenuRow => {
paint_aura(buf, hit.area, accent, 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::Code,
Rect::new(0, 2, 20, 1),
"fn main",
true,
);
resolve_hover();
let hit = current_hover().expect("hover");
assert_eq!(hit.kind, HoverTargetKind::Code);
assert!(hit.copyable);
clear_pointer();
}
#[test]
fn tooltip_only_for_file_and_tool() {
let _guard = HOVER_TEST_LOCK.lock().unwrap();
clear_pointer();
begin_frame();
set_pointer(1, 1);
register_rect(
HoverTargetKind::FileRef,
Rect::new(0, 1, 10, 1),
"src/lib.rs",
false,
);
resolve_hover();
let tip = hover_tooltip(40).expect("tooltip");
assert!(tip.contains("lib.rs"), "{tip}");
clear_pointer();
}
}