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

Show context menu on desktop #179

Merged
merged 7 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 17 additions & 2 deletions packages/fleather/lib/src/rendering/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
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 @@
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,14 +427,20 @@
// by dragging
TextSelection? _extendSelectionOrigin;

@override
void handleSecondaryTapDown(TapDownDetails details) {
_lastTapDownPosition = details.globalPosition;
_lastSecondaryTapDownPosition = details.globalPosition;
}

@override
void handleTapDown(TapDownDetails details) {
_lastTapDownPosition = details.globalPosition;
}

bool _isDragging = false;

void handleDragStart(DragStartDetails details) {
void handleDragStart(TapDragStartDetails details) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L443 was not covered by tests
Amir-P marked this conversation as resolved.
Show resolved Hide resolved
_isDragging = true;

final newSelection = selectPositionAt(
Expand All @@ -438,7 +453,7 @@
_extendSelectionOrigin = newSelection;
}

void handleDragEnd(DragEndDetails details) {
void handleDragEnd(TapDragEndDetails details) {

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L456 was not covered by tests
Amir-P marked this conversation as resolved.
Show resolved Hide resolved
_isDragging = false;
}

Expand Down
72 changes: 46 additions & 26 deletions packages/fleather/lib/src/widgets/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
void onForcePressStart(ForcePressDetails details) {
super.onForcePressStart(details);
if (delegate.selectionEnabled && shouldShowSelectionToolbar) {
editor!.showToolbar();
editor.showToolbar();

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L429 was not covered by tests
}
}

Expand All @@ -441,7 +441,7 @@
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditor!.selectPositionAt(
renderEditor.selectPositionAt(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L444 was not covered by tests
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
Expand All @@ -450,7 +450,7 @@
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectWordsInRange(
renderEditor.selectWordsInRange(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L453 was not covered by tests
from: details.globalPosition - details.offsetFromOrigin,
to: details.globalPosition,
cause: SelectionChangedCause.longPress,
Expand All @@ -468,8 +468,8 @@
}

@override
void onSingleTapUp(TapUpDetails details) {
editor!.hideToolbar();
void onSingleTapUp(TapDragUpDetails details) {
editor.hideToolbar();

if (delegate.selectionEnabled) {
switch (Theme.of(_state.context).platform) {
Expand All @@ -482,26 +482,26 @@
// Precise devices should place the cursor at a precise position.
// If `Shift` key is pressed then extend current selection instead.
if (isShiftClick(details.kind)) {
renderEditor!.extendSelection(details.globalPosition,
renderEditor.extendSelection(details.globalPosition,

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L485 was not covered by tests
cause: SelectionChangedCause.tap);
} else {
renderEditor!.selectPosition(cause: SelectionChangedCause.tap);
renderEditor.selectPosition(cause: SelectionChangedCause.tap);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L488 was not covered by tests
}
break;
case PointerDeviceKind.touch:
case PointerDeviceKind.trackpad:
case PointerDeviceKind.unknown:
// On macOS/iOS/iPadOS a touch tap places the cursor at the edge
// of the word.
renderEditor!.selectWordEdge(cause: SelectionChangedCause.tap);
renderEditor.selectWordEdge(cause: SelectionChangedCause.tap);
break;
}
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectPosition(cause: SelectionChangedCause.tap);
renderEditor.selectPosition(cause: SelectionChangedCause.tap);
break;
}
}
Expand All @@ -516,7 +516,7 @@
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditor!.selectPositionAt(
renderEditor.selectPositionAt(

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L519 was not covered by tests
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
Expand All @@ -525,7 +525,7 @@
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectWord(cause: SelectionChangedCause.longPress);
renderEditor.selectWord(cause: SelectionChangedCause.longPress);
Feedback.forLongPress(_state.context);
break;
}
Expand Down Expand Up @@ -770,6 +770,8 @@

bool showToolbar();

void toggleToolbar([bool hideHandles = true]);

void requestKeyboard();

FocusNode get effectiveFocusNode;
Expand Down Expand Up @@ -883,6 +885,17 @@
return true;
}

@override
void toggleToolbar([bool hideHandles = true]) {
final selectionOverlay = _selectionOverlay ??= _createSelectionOverlay();

if (selectionOverlay.toolbarIsVisible) {
hideToolbar(hideHandles);
} else {
showToolbar();
}
}

/// Copy current selection to [Clipboard].
@override
void copySelection(SelectionChangedCause cause) {
Expand Down Expand Up @@ -1139,21 +1152,7 @@
_selectionOverlay = null;
} else {
if (_selectionOverlay == null) {
_selectionOverlay = EditorTextSelectionOverlay(
clipboardStatus: clipboardStatus,
context: context,
value: textEditingValue,
debugRequiredFor: widget,
toolbarLayerLink: _toolbarLayerLink,
startHandleLayerLink: _startHandleLayerLink,
endHandleLayerLink: _endHandleLayerLink,
renderObject: renderEditor,
selectionControls: widget.selectionControls,
selectionDelegate: this,
dragStartBehavior: DragStartBehavior.start,
contextMenuBuilder: (context) =>
widget.contextMenuBuilder(context, this),
);
_selectionOverlay = _createSelectionOverlay();
} else {
_selectionOverlay!.update(textEditingValue);
}
Expand All @@ -1180,6 +1179,23 @@
}
}

EditorTextSelectionOverlay _createSelectionOverlay() {
return EditorTextSelectionOverlay(
clipboardStatus: clipboardStatus,
context: context,
value: textEditingValue,
debugRequiredFor: widget,
toolbarLayerLink: _toolbarLayerLink,
startHandleLayerLink: _startHandleLayerLink,
endHandleLayerLink: _endHandleLayerLink,
renderObject: renderEditor,
selectionControls: widget.selectionControls,
selectionDelegate: this,
dragStartBehavior: DragStartBehavior.start,
contextMenuBuilder: (context) => widget.contextMenuBuilder(context, this),
);
}

void _handleFocusChanged() {
openOrCloseConnection();
_cursorController.startOrStopCursorTimerIfNeeded(
Expand Down Expand Up @@ -1612,6 +1628,10 @@
/// Returns the anchor points for the default context menu.
@override
TextSelectionToolbarAnchors get contextMenuAnchors {
if (renderEditor.lastSecondaryTapDownPosition != null) {
return TextSelectionToolbarAnchors(
primaryAnchor: renderEditor.lastSecondaryTapDownPosition!);
}
final selection = textEditingValue.selection;
// Find the horizontal midpoint, just above the selected text.
final List<TextSelectionPoint> endpoints =
Expand Down
Loading