updateWebImePosition method

void updateWebImePosition()

Implementation

void updateWebImePosition() {
  if (connection == null || !connection!.attached) return;
  if (document == null) return;
  final doc = document!;

  final fragId = doc.cursor.focusId.isNotEmpty
      ? doc.cursor.focusId
      : doc.cursor.anchorId;
  if (fragId.isEmpty) return;

  final containerId = doc.findLogicalContainerId(fragId);
  if (containerId == null) return;

  final render = doc.paragraphRegistry.renderFor(containerId);
  if (render == null || !render.attached || !render.hasSize) return;

  String fontFamily = 'DejaVu Sans';
  double fontSize = 14.0;
  FontWeight fontWeight = FontWeight.normal;
  final fragNode = doc.nodeById(fragId);
  if (fragNode is Fragment) {
    fontFamily = fragNode.fontFamily;
    fontSize = fragNode.fontSize;
    fontWeight = fragNode.isBold ? FontWeight.bold : FontWeight.normal;
  }

  final fragmentStartRect = render.getCaretScreenRect(fragId, 0);

  final Matrix4 transform;
  if (fragmentStartRect != null) {
    final renderBoxOrigin = render.localToGlobal(Offset.zero);
    final fragOffsetX = fragmentStartRect.left - renderBoxOrigin.dx;
    final fragOffsetY = fragmentStartRect.top - renderBoxOrigin.dy;
    transform = render
        .getTransformTo(null)
        .multiplied(Matrix4.translationValues(fragOffsetX, fragOffsetY, 0));
  } else {
    transform = render.getTransformTo(null);
  }

  connection!.setEditableSizeAndTransform(render.size, transform);

  final browserFontFamily = _webFontFallback(fontFamily);
  connection!.setStyle(
    fontFamily: browserFontFamily,
    fontSize: fontSize,
    fontWeight: fontWeight,
    textDirection: TextDirection.ltr,
    textAlign: TextAlign.left,
  );
}