resolveCaretX method
Returns the global x coordinate (in logical pixels) of the caret
for the given stop. Iterates the renders until one recognizes the fragmentId.
Returns 0.0 if no render knows the fragment (e.g.: layout not yet happened — in that case preferredX will remain -1.0 and will be recalculated at the next vertical movement).
OPTIMISATION: visible renders are checked first because during arrow-key navigation the target fragment is almost always inside the current or a neighbouring visible paragraph.
Implementation
double resolveCaretX(CaretStop stop) {
// 1. Check visible paragraph renders first (O(visible), usually ~20)
for (final id in _visibleContainerIds) {
final render = _renders[id];
if (render != null) {
final x = render.getCaretX(stop.fragmentId, stop.offset);
if (x != null) return x;
}
}
// 2. HR renders — HR stops use the node id as fragmentId.
// Return the center X so vertical navigation lands in the middle.
final hrRender = _hrRenders[stop.fragmentId];
if (hrRender != null && hrRender.attached && hrRender.hasSize) {
final box = hrRender.localToGlobal(Offset.zero);
return box.dx + hrRender.size.width / 2;
}
// 3. Fall back to all paragraph renders (rare, e.g. after scroll)
for (final render in _renders.values) {
final x = render.getCaretX(stop.fragmentId, stop.offset);
if (x != null) return x;
}
return 0.0;
}