Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent toolbar from leaving editor #336

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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
42 changes: 42 additions & 0 deletions packages/fleather/test/widgets/editor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,48 @@ void main() {
tester.view.viewInsets = FakeViewPadding.zero;
});

testWidgets('Keep selectiontoolbar with editor bounds', (tester) async {
final delta = Delta();
for (int i = 0; i < 30; i++) {
delta.insert('Test\n');
}
final scrollController = ScrollController();
final controller =
FleatherController(document: ParchmentDocument.fromDelta(delta));
final editor = MaterialApp(
home: Scaffold(
body: Column(
children: [
const SizedBox(width: 300, height: 150),
Expanded(
child: FleatherEditor(
controller: controller,
scrollController: scrollController,
),
),
],
),
),
);
await tester.pumpWidget(editor);
// Double tap to show toolbar
await tester.tapAt(
tester.getTopLeft(find.byType(RawEditor)) + const Offset(1, 1));
await tester.tapAt(
tester.getTopLeft(find.byType(RawEditor)) + const Offset(1, 1));
await tester.pumpAndSettle();
expect(find.byType(TextSelectionToolbar), findsOneWidget);
// Scroll extent is > 500, toolbar position should be around -400 if not
// capped
scrollController.jumpTo(scrollController.position.maxScrollExtent);
await tester.pumpAndSettle();
final renderToolbarTextButton =
tester.renderObject(find.byType(TextSelectionToolbarTextButton).first)
as RenderBox;
final toolbarTop = renderToolbarTextButton.localToGlobal(Offset.zero);
expect(toolbarTop.dy, greaterThan(90));
});

testWidgets('allows merging attribute theme data', (tester) async {
var delta = Delta()
..insert(
Expand Down
Loading