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 6 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
139 changes: 40 additions & 99 deletions packages/fleather/lib/src/widgets/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,8 @@
@override
GlobalKey<EditorState> get editableTextKey => widget.editorKey ?? _editorKey!;

// TODO: Add support for forcePress on iOS.
@override
bool get forcePressEnabled => false;
bool get forcePressEnabled => true;

@override
bool get selectionEnabled => widget.enableInteractiveSelection;
Expand Down Expand Up @@ -426,106 +425,28 @@
void onForcePressStart(ForcePressDetails details) {
super.onForcePressStart(details);
if (delegate.selectionEnabled && shouldShowSelectionToolbar) {
editor!.showToolbar();
editor.showToolbar();

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L428 was not covered by tests
}
}

@override
void onForcePressEnd(ForcePressDetails details) {
// Not required.
}

@override
void onSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) {
if (delegate.selectionEnabled) {
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditor!.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectWordsInRange(
from: details.globalPosition - details.offsetFromOrigin,
to: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
}
}
}

bool isShiftClick(PointerDeviceKind deviceKind) {
final pressed = RawKeyboard.instance.keysPressed;
return deviceKind == PointerDeviceKind.mouse &&
(pressed.contains(LogicalKeyboardKey.shiftLeft) ||
pressed.contains(LogicalKeyboardKey.shiftRight));
}

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

if (delegate.selectionEnabled) {
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
switch (details.kind) {
case PointerDeviceKind.mouse:
case PointerDeviceKind.stylus:
case PointerDeviceKind.invertedStylus:
// 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,
cause: SelectionChangedCause.tap);
} else {
renderEditor!.selectPosition(cause: SelectionChangedCause.tap);
}
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);
break;
}
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectPosition(cause: SelectionChangedCause.tap);
break;
}
}
void onSingleTapUp(TapDragUpDetails details) {
super.onSingleTapUp(details);
_state._requestKeyboard();
// if (_state.widget.onTap != null)
// _state.widget.onTap();
}

@override
void onSingleLongTapStart(LongPressStartDetails details) {
super.onSingleLongTapStart(details);
if (delegate.selectionEnabled) {
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditor!.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectWord(cause: SelectionChangedCause.longPress);
Feedback.forLongPress(_state.context);
break;
}
Expand Down Expand Up @@ -770,6 +691,8 @@

bool showToolbar();

void toggleToolbar([bool hideHandles = true]);

void requestKeyboard();

FocusNode get effectiveFocusNode;
Expand Down Expand Up @@ -883,6 +806,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 +1073,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 +1100,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 +1549,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