fix cursor when line contains image

pull/400/head
li3317 4 years ago
parent 5b230a718b
commit 2b572ca409
  1. 18
      lib/src/widgets/cursor.dart
  2. 8
      lib/src/widgets/text_line.dart

@ -245,9 +245,23 @@ class CursorPainter {
/// Paints cursor on [canvas] at specified [position]. /// Paints cursor on [canvas] at specified [position].
/// [offset] is global top left (x, y) of text line /// [offset] is global top left (x, y) of text line
/// [position] is relative (x) in text line /// [position] is relative (x) in text line
void paint(Canvas canvas, Offset offset, TextPosition position) { void paint(Canvas canvas, Offset offset, TextPosition position, bool lineHasEmbed) {
final caretOffset = var caretOffset =
editable!.getOffsetForCaret(position, prototype) + offset; editable!.getOffsetForCaret(position, prototype) + offset;
if (lineHasEmbed) {
// relative (x, y) to global offset
var relativeCaretOffset = editable!.getOffsetForCaret(position, prototype);
if (relativeCaretOffset == Offset.zero) {
relativeCaretOffset = editable!.getOffsetForCaret(
TextPosition(
offset: position.offset - 1, affinity: position.affinity),
prototype);
// Hardcoded 6 as estimate of the width of a character
relativeCaretOffset =
Offset(relativeCaretOffset.dx + 6, relativeCaretOffset.dy);
}
caretOffset = relativeCaretOffset + offset;
}
var caretRect = prototype.shift(caretOffset); var caretRect = prototype.shift(caretOffset);
if (style.offset != null) { if (style.offset != null) {

@ -825,7 +825,7 @@ class RenderEditableTextLine extends RenderEditableBox {
cursorCont.show.value && cursorCont.show.value &&
containsCursor() && containsCursor() &&
!cursorCont.style.paintAboveText) { !cursorCont.style.paintAboveText) {
_paintCursor(context, effectiveOffset); _paintCursor(context, effectiveOffset, line.hasEmbed);
} }
context.paintChild(_body!, effectiveOffset); context.paintChild(_body!, effectiveOffset);
@ -834,7 +834,7 @@ class RenderEditableTextLine extends RenderEditableBox {
cursorCont.show.value && cursorCont.show.value &&
containsCursor() && containsCursor() &&
cursorCont.style.paintAboveText) { cursorCont.style.paintAboveText) {
_paintCursor(context, effectiveOffset); _paintCursor(context, effectiveOffset, line.hasEmbed);
} }
} }
} }
@ -847,12 +847,12 @@ class RenderEditableTextLine extends RenderEditableBox {
} }
} }
void _paintCursor(PaintingContext context, Offset effectiveOffset) { void _paintCursor(PaintingContext context, Offset effectiveOffset, bool lineHasEmbed) {
final position = TextPosition( final position = TextPosition(
offset: textSelection.extentOffset - line.documentOffset, offset: textSelection.extentOffset - line.documentOffset,
affinity: textSelection.base.affinity, affinity: textSelection.base.affinity,
); );
_cursorPainter.paint(context.canvas, effectiveOffset, position); _cursorPainter.paint(context.canvas, effectiveOffset, position, lineHasEmbed);
} }
@override @override

Loading…
Cancel
Save