codewhale-tui 0.9.7

Terminal UI for open-source and open-weight coding models
Documentation
//! Frame-scoped hover registry for transcript / diff / tool surfaces.
//!
//! Collects hit targets during render, resolves the pointer once, and applies
//! restrained aura / copy / link glow. Reuses [`super::hover_hit`] primitives
//! and context-menu hover-follow patterns without growing `ui.rs`.

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,
};

/// Pointer position from the last mouse move (column, row).
static POINTER: Mutex<Option<(u16, u16)>> = Mutex::new(None);

// Targets registered for the current frame (thread-local for render path).
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) };
}

/// Clear targets at the start of a draw.
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()));
}

/// Record an interactive region for hit-testing this frame.
pub fn register(hit: HoverHit) {
    FRAME_TARGETS.with(|t| t.borrow_mut().push(hit));
}

/// Convenience: register a rectangular target.
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,
    });
}

/// Update the shared pointer from mouse motion (call from mouse_ui).
pub fn set_pointer(column: u16, row: u16) {
    if let Ok(mut guard) = POINTER.lock() {
        *guard = Some((column, row));
    }
}

/// Clear the process-wide pointer between tests.
#[cfg(test)]
pub fn clear_pointer() {
    if let Ok(mut guard) = POINTER.lock() {
        *guard = None;
    }
}

/// Resolve hover after targets are registered; call once near end of draw.
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);
    });
}

/// Current hover hit, if any.
#[must_use]
pub fn current_hover() -> Option<HoverHit> {
    FRAME_HOVER.with(|h| h.borrow().clone())
}

/// Elapsed ms since frame begin for pulse math.
fn elapsed_ms() -> u128 {
    FRAME_START
        .with(|s| s.borrow().map(|t| t.elapsed().as_millis()))
        .unwrap_or(0)
}

/// Paint OSC-8 / file-ref underline glow on a hovered link span row.
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);
        }
    }
}

/// Apply all hover effects for the resolved target onto `buf`.
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);
    // Hover-only copy chip on the trailing edge of copyable targets.
    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::*;

    // Global POINTER is process-wide; serialize tests that touch it.
    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();
    }
}