use teksilo_canvas::Point;
use teksilo_text::text_document::{MoveMode, TextDocument};
use teksilo_text::{HitRegion, HitTestResult, RichTextEngine};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContextTarget {
Plain,
InSelection,
Link { href: String },
Image { name: String },
TableCell {
table_id: usize,
row: usize,
col: usize,
},
}
pub fn hit_test_at(
engine: &RichTextEngine,
screen: Point,
_scroll_x: f32,
_scroll_y: f32,
) -> Option<HitTestResult> {
engine.hit_test(screen.x, screen.y)
}
pub fn classify(
hit: &HitTestResult,
selection: Option<(usize, usize)>,
document: &TextDocument,
) -> ContextTarget {
if let Some((anchor, caret)) = selection {
let (lo, hi) = (anchor.min(caret), anchor.max(caret));
if lo != hi && hit.position >= lo && hit.position <= hi {
return ContextTarget::InSelection;
}
}
match &hit.region {
HitRegion::Link { href } => ContextTarget::Link { href: href.clone() },
HitRegion::Image { name } => ContextTarget::Image { name: name.clone() },
_ => {
if let Some(table_id) = hit.table_id {
let probe = document.cursor();
probe.set_position(hit.position, MoveMode::MoveAnchor);
if let Some(cell_ref) = probe.current_table_cell() {
return ContextTarget::TableCell {
table_id,
row: cell_ref.row,
col: cell_ref.column,
};
}
return ContextTarget::TableCell {
table_id,
row: 0,
col: 0,
};
}
ContextTarget::Plain
}
}
}