use crate::{
App, Bounds, CursorStyle, EntityId, Hitbox, HitboxBehavior, Pixels, TextLayout, Window,
WrapBoundary, WrappedLineLayout, point, size,
};
use std::ops::Range;
#[derive(Default)]
pub(crate) struct ClickableHitboxes(Vec<Hitbox>);
pub(crate) fn insert_clickable_hitboxes(
hitboxes: &mut ClickableHitboxes,
layout: &TextLayout,
ranges: &[Range<usize>],
window: &mut Window,
) {
hitboxes.0.clear();
let ranges = in_order(ranges);
if ranges.is_empty() {
return;
}
let inner = layout.0.borrow();
let Some(inner) = inner.as_ref() else {
return;
};
let Some(bounds) = inner.bounds else {
return;
};
let line_height = inner.line_height;
let mut line_origin = bounds.origin;
let mut line_start_ix = 0;
for line in &inner.lines {
let line_end_ix = line_start_ix + line.len();
if touches(&ranges, line_start_ix..line_end_ix) {
clickable_stretches(
line,
line_height,
|ix| contains(&ranges, line_start_ix + ix),
|stretch| {
let stretch =
Bounds::new(line_origin + stretch.origin, stretch.size).intersect(&bounds);
if !stretch.is_empty() {
hitboxes
.0
.push(window.insert_hitbox(stretch, HitboxBehavior::Normal));
}
},
);
}
line_origin.y += line.size(line_height).height;
line_start_ix = line_end_ix + 1;
}
}
pub(crate) fn set_clickable_cursor(hitboxes: &ClickableHitboxes, window: &mut Window) {
for hitbox in &hitboxes.0 {
window.set_cursor_style(CursorStyle::PointingHand, hitbox);
}
}
pub(crate) fn notify_hovered_index(has_hover_listener: bool, view: EntityId, cx: &mut App) {
if has_hover_listener {
cx.notify(view);
}
}
pub(crate) fn clickable_stretches(
line: &WrappedLineLayout,
line_height: Pixels,
clickable: impl Fn(usize) -> bool,
mut stretch: impl FnMut(Bounds<Pixels>),
) {
let layout = &line.unwrapped_layout;
let steps = index_steps(line);
let boundary_x = |boundary: &WrapBoundary| {
layout.runs[boundary.run_ix].glyphs[boundary.glyph_ix]
.position
.x
};
for row in 0..=line.wrap_boundaries.len() {
let start_x = match row {
0 => Pixels::ZERO,
row => boundary_x(&line.wrap_boundaries[row - 1]),
};
let end_x = line
.wrap_boundaries
.get(row)
.map_or(layout.width, boundary_x)
.min(layout.width);
if start_x >= end_x {
continue;
}
let top = line_height * row as f32;
let mut emit = |from: Pixels, to: Pixels| {
stretch(Bounds::new(
point(from - start_x, top),
size(to - from, line_height),
))
};
let first = steps.partition_point(|&(x, _)| x <= start_x);
let mut index = first.checked_sub(1).map_or(0, |step| steps[step].1);
let mut from = start_x;
let mut open = None;
for &(x, next_index) in steps[first..].iter().take_while(|&&(x, _)| x < end_x) {
note_piece(&clickable, index, from, &mut open, &mut emit);
from = x;
index = next_index;
}
note_piece(&clickable, index, from, &mut open, &mut emit);
if let Some(open) = open {
emit(open, end_x);
}
}
}
fn note_piece(
clickable: &impl Fn(usize) -> bool,
index: usize,
from: Pixels,
open: &mut Option<Pixels>,
emit: &mut impl FnMut(Pixels, Pixels),
) {
if clickable(index) {
open.get_or_insert(from);
} else if let Some(open) = open.take() {
emit(open, from);
}
}
fn index_steps(line: &WrappedLineLayout) -> Vec<(Pixels, usize)> {
let mut glyphs: Vec<_> = line
.unwrapped_layout
.runs
.iter()
.flat_map(|run| &run.glyphs)
.enumerate()
.map(|(order, glyph)| (glyph.position.x, order, glyph.index))
.collect();
glyphs.sort_by(|a, b| a.0.0.total_cmp(&b.0.0));
let mut steps: Vec<(Pixels, usize)> = Vec::with_capacity(glyphs.len());
let mut latest: Option<usize> = None;
let mut latest_index = 0;
for (x, order, index) in glyphs {
if latest.is_none_or(|latest| order > latest) {
latest = Some(order);
latest_index = index;
}
match steps.last_mut() {
Some(step) if step.0 == x => step.1 = latest_index,
_ => steps.push((x, latest_index)),
}
}
steps
}
fn in_order(ranges: &[Range<usize>]) -> Vec<Range<usize>> {
let mut sorted: Vec<_> = ranges
.iter()
.filter(|range| range.start < range.end)
.cloned()
.collect();
sorted.sort_unstable_by_key(|range| range.start);
let mut merged: Vec<Range<usize>> = Vec::with_capacity(sorted.len());
for range in sorted {
match merged.last_mut() {
Some(last) if range.start <= last.end => last.end = last.end.max(range.end),
_ => merged.push(range),
}
}
merged
}
fn first_ending_after(ranges: &[Range<usize>], ix: usize) -> Option<&Range<usize>> {
ranges.get(ranges.partition_point(|range| range.end <= ix))
}
fn contains(ranges: &[Range<usize>], ix: usize) -> bool {
first_ending_after(ranges, ix).is_some_and(|range| range.start <= ix)
}
fn touches(ranges: &[Range<usize>], span: Range<usize>) -> bool {
first_ending_after(ranges, span.start).is_some_and(|range| range.start < span.end)
}