Skip to content

Commit

Permalink
Prevent toolbar from leaving editor
Browse files Browse the repository at this point in the history
  • Loading branch information
amantoux committed May 6, 2024
1 parent 428bf54 commit 30daa18
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/fleather/lib/src/rendering/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class RenderEditor extends RenderEditableContainerBox
markNeedsSemanticsUpdate();
}

Offset get _paintOffset => Offset(0.0, -(offset?.pixels ?? 0.0));
Offset get paintOffset => Offset(0.0, -(offset?.pixels ?? 0.0));

ViewportOffset? get offset => _offset;
ViewportOffset? _offset;
Expand Down Expand Up @@ -633,7 +633,7 @@ class RenderEditor extends RenderEditableContainerBox
_paintFloatingCursor(context, offset);
}
defaultPaint(context, offset);
_updateSelectionExtentsVisibility(offset + _paintOffset);
_updateSelectionExtentsVisibility(offset + paintOffset);
_paintHandleLayers(context, getEndpointsForSelection(selection));

if (hasFocus &&
Expand Down
28 changes: 20 additions & 8 deletions packages/fleather/lib/src/widgets/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2025,21 +2025,24 @@ class RawEditorState extends EditorState
final smallestLineHeight = math.min(baseLineHeight, extentLineHeight);

return _textSelectionToolbarAnchorsFromSelection(
renderEditor: renderEditor,
startGlyphHeight: smallestLineHeight,
endGlyphHeight: smallestLineHeight,
selectionEndpoints: endpoints);
}

TextSelectionToolbarAnchors _textSelectionToolbarAnchorsFromSelection({
required RenderEditor renderEditor,
required double startGlyphHeight,
required double endGlyphHeight,
required List<TextSelectionPoint> selectionEndpoints,
}) {
// If editor is scrollable, the editing region is only the viewport
// otherwise use editor as editing region
final viewport = RenderAbstractViewport.maybeOf(renderEditor);
final visualSizeRenderer = (viewport ?? renderEditor) as RenderBox;

Check warning on line 2041 in packages/fleather/lib/src/widgets/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L2041

Added line #L2041 was not covered by tests
final Rect editingRegion = Rect.fromPoints(
renderEditor.localToGlobal(Offset.zero),
renderEditor.localToGlobal(renderEditor.size.bottomRight(Offset.zero)),
visualSizeRenderer.localToGlobal(Offset.zero),
visualSizeRenderer
.localToGlobal(visualSizeRenderer.size.bottomRight(Offset.zero)),
);

if (editingRegion.left.isNaN ||
Expand All @@ -2056,12 +2059,21 @@ class RawEditorState extends EditorState
final Rect selectionRect = Rect.fromLTRB(
isMultiline
? editingRegion.left
: editingRegion.left + selectionEndpoints.first.point.dx,
editingRegion.top + selectionEndpoints.first.point.dy - startGlyphHeight,
: editingRegion.left +
selectionEndpoints.first.point.dx +
renderEditor.paintOffset.dx,
editingRegion.top +
selectionEndpoints.first.point.dy +
renderEditor.paintOffset.dy -
startGlyphHeight,
isMultiline
? editingRegion.right
: editingRegion.left + selectionEndpoints.last.point.dx,
editingRegion.top + selectionEndpoints.last.point.dy,
: editingRegion.left +
selectionEndpoints.last.point.dx +
renderEditor.paintOffset.dx,
editingRegion.top +
selectionEndpoints.last.point.dy +
renderEditor.paintOffset.dy,
);

return TextSelectionToolbarAnchors(
Expand Down

0 comments on commit 30daa18

Please sign in to comment.