onHighlightRect method
Approximate Overlay.highlightRect behavior by hit-testing the rect center and applying element highlight. This keeps implementation minimal and non-invasive to the render pipeline.
Implementation
void onHighlightRect(int? id, Map<String, dynamic> params) {
_highlightElement?.debugHideHighlight();
final ctx = dbgContext;
if (ctx == null || document == null) {
sendToFrontend(id, null);
return;
}
// Accept either int or double values
double _toDouble(dynamic v) {
if (v is int) return v.toDouble();
if (v is double) return v;
return 0.0;
}
final double x = _toDouble(params['x'] ?? params['left']);
final double y = _toDouble(params['y'] ?? params['top']);
final double w = _toDouble(params['width']);
final double h = _toDouble(params['height']);
final double cx = x + (w > 0 ? w / 2 : 0);
final double cy = y + (h > 0 ? h / 2 : 0);
try {
final rootRenderObject = document!.viewport!;
final result = BoxHitTestResult();
rootRenderObject.hitTest(result, position: Offset(cx, cy));
var hitPath = result.path;
if (hitPath.isNotEmpty) {
// Skip non-element render targets
final firstTarget = hitPath.first.target;
if (firstTarget is WebFRenderImage ||
(firstTarget is RenderBoxModel && firstTarget.renderStyle.target.pointer == null)) {
hitPath = hitPath.skip(1);
}
if (hitPath.isNotEmpty && hitPath.first.target is RenderBoxModel) {
final ro = hitPath.first.target as RenderBoxModel;
final element = ro.renderStyle.target;
element.debugHighlight();
_highlightElement = element;
}
}
} catch (_) {}
sendToFrontend(id, null);
}