paragraphAtGlobalY method
Finds the rendered paragraph whose global bounds vertically contain
globalY; if none contains it, returns the vertically nearest one.
Iterates only the currently rendered paragraphs (bounded by the viewport
- ListView cache), so it is O(visible) — used by drag selection to do precise hit testing without an O(n) scan over the whole document.
Implementation
({String id, RenderFluentParagraph render})? paragraphAtGlobalY(double globalY) {
RenderFluentParagraph? best;
String? bestId;
double bestDist = double.infinity;
for (final entry in _renders.entries) {
final render = entry.value;
if (!render.attached || !render.hasSize) continue;
final top = render.localToGlobal(Offset.zero).dy;
final bottom = top + render.size.height;
final double dist;
if (globalY >= top && globalY <= bottom) {
dist = 0.0;
} else if (globalY < top) {
dist = top - globalY;
} else {
dist = globalY - bottom;
}
if (dist < bestDist) {
bestDist = dist;
best = render;
bestId = entry.key;
if (dist == 0.0) break; // exact vertical hit, cannot do better
}
}
if (best == null || bestId == null) return null;
return (id: bestId, render: best);
}