Skip to content

Commit

Permalink
Fix context menu anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Dec 3, 2023
1 parent bd3e219 commit 9863a64
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
15 changes: 15 additions & 0 deletions packages/fleather/lib/src/rendering/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ abstract class RenderAbstractEditor implements TextLayoutMetrics {
Offset lastBoundedOffset, TextPosition lastTextPosition,
{double? resetLerpValue});

/// Tracks the position of a secondary tap event.
///
/// Should be called before attempting to change the selection based on the
/// position of a secondary tap.
void handleSecondaryTapDown(TapDownDetails details);

/// If [ignorePointer] is false (the default) then this method is called by
/// the internal gesture recognizer's [TapGestureRecognizer.onTapDown]
/// callback.
Expand Down Expand Up @@ -187,6 +193,9 @@ class RenderEditor extends RenderEditableContainerBox
markNeedsLayout();
}

Offset? _lastSecondaryTapDownPosition;
Offset? get lastSecondaryTapDownPosition => _lastSecondaryTapDownPosition;

/// The region of text that is selected, if any.
///
/// The caret position is represented by a collapsed selection.
Expand Down Expand Up @@ -418,6 +427,12 @@ class RenderEditor extends RenderEditableContainerBox
// by dragging
TextSelection? _extendSelectionOrigin;

@override

Check warning on line 430 in packages/fleather/lib/src/rendering/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editor.dart#L430

Added line #L430 was not covered by tests
void handleSecondaryTapDown(TapDownDetails details) {
_lastTapDownPosition = details.globalPosition;
_lastSecondaryTapDownPosition = details.globalPosition;

Check warning on line 433 in packages/fleather/lib/src/rendering/editor.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/rendering/editor.dart#L432-L433

Added lines #L432 - L433 were not covered by tests
}

@override
void handleTapDown(TapDownDetails details) {
_lastTapDownPosition = details.globalPosition;
Expand Down
4 changes: 4 additions & 0 deletions packages/fleather/lib/src/widgets/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,10 @@ class RawEditorState extends EditorState
/// Returns the anchor points for the default context menu.
@override
TextSelectionToolbarAnchors get contextMenuAnchors {
if (renderEditor.lastSecondaryTapDownPosition != null) {
return TextSelectionToolbarAnchors(
primaryAnchor: renderEditor.lastSecondaryTapDownPosition!);

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

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/editor.dart#L1632-L1633

Added lines #L1632 - L1633 were not covered by tests
}
final selection = textEditingValue.selection;
// Find the horizontal midpoint, just above the selected text.
final List<TextSelectionPoint> endpoints =
Expand Down
12 changes: 5 additions & 7 deletions packages/fleather/lib/src/widgets/text_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


import 'dart:math' as math;

import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -599,14 +598,12 @@ class EditorTextSelectionGestureDetectorBuilder {
@protected
final EditorTextSelectionGestureDetectorBuilderDelegate delegate;

Offset? _lastSecondaryTapDownPosition;

/// Returns true if lastSecondaryTapDownPosition was on selection.
bool get _lastSecondaryTapWasOnSelection {
assert(_lastSecondaryTapDownPosition != null);
assert(renderEditor.lastSecondaryTapDownPosition != null);

Check warning on line 603 in packages/fleather/lib/src/widgets/text_selection.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/text_selection.dart#L602-L603

Added lines #L602 - L603 were not covered by tests

final TextPosition textPosition =
renderEditor.getPositionForOffset(_lastSecondaryTapDownPosition!);
final TextPosition textPosition = renderEditor
.getPositionForOffset(renderEditor.lastSecondaryTapDownPosition!);

Check warning on line 606 in packages/fleather/lib/src/widgets/text_selection.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/text_selection.dart#L605-L606

Added lines #L605 - L606 were not covered by tests

return renderEditor.selection.start <= textPosition.offset &&
renderEditor.selection.end >= textPosition.offset;

Check warning on line 609 in packages/fleather/lib/src/widgets/text_selection.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/text_selection.dart#L608-L609

Added lines #L608 - L609 were not covered by tests
Expand Down Expand Up @@ -1159,7 +1156,8 @@ class EditorTextSelectionGestureDetectorBuilder {
// vs [TapGestureRecognizer.onSecondaryTapUp] instead of having to track state in
// renderEditor. When this migration is complete we should remove this hack.
// See https://github.com/flutter/flutter/issues/115130.
_lastSecondaryTapDownPosition = details.globalPosition;
renderEditor.handleSecondaryTapDown(
TapDownDetails(globalPosition: details.globalPosition));
_shouldShowSelectionToolbar = true;

Check warning on line 1161 in packages/fleather/lib/src/widgets/text_selection.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/text_selection.dart#L1159-L1161

Added lines #L1159 - L1161 were not covered by tests
}

Expand Down

0 comments on commit 9863a64

Please sign in to comment.