Skip to content

Commit

Permalink
Show context menu on desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Dec 1, 2023
1 parent 97fbd27 commit bd3e219
Show file tree
Hide file tree
Showing 3 changed files with 962 additions and 487 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 @@ -425,7 +425,7 @@ class RenderEditor extends RenderEditableContainerBox

bool _isDragging = false;

void handleDragStart(DragStartDetails details) {
void handleDragStart(TapDragStartDetails details) {
_isDragging = true;

final newSelection = selectPositionAt(
Expand All @@ -438,7 +438,7 @@ class RenderEditor extends RenderEditableContainerBox
_extendSelectionOrigin = newSelection;
}

void handleDragEnd(DragEndDetails details) {
void handleDragEnd(TapDragEndDetails details) {
_isDragging = false;
}

Expand Down
68 changes: 42 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 @@ class _FleatherEditorSelectionGestureDetectorBuilder
void onForcePressStart(ForcePressDetails details) {
super.onForcePressStart(details);
if (delegate.selectionEnabled && shouldShowSelectionToolbar) {
editor!.showToolbar();
editor.showToolbar();
}
}

Expand All @@ -441,7 +441,7 @@ class _FleatherEditorSelectionGestureDetectorBuilder
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditor!.selectPositionAt(
renderEditor.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
Expand All @@ -450,7 +450,7 @@ class _FleatherEditorSelectionGestureDetectorBuilder
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectWordsInRange(
renderEditor.selectWordsInRange(
from: details.globalPosition - details.offsetFromOrigin,
to: details.globalPosition,
cause: SelectionChangedCause.longPress,
Expand All @@ -468,8 +468,8 @@ class _FleatherEditorSelectionGestureDetectorBuilder
}

@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 @@ class _FleatherEditorSelectionGestureDetectorBuilder
// 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,
cause: SelectionChangedCause.tap);
} else {
renderEditor!.selectPosition(cause: SelectionChangedCause.tap);
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);
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 @@ class _FleatherEditorSelectionGestureDetectorBuilder
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
renderEditor!.selectPositionAt(
renderEditor.selectPositionAt(
from: details.globalPosition,
cause: SelectionChangedCause.longPress,
);
Expand All @@ -525,7 +525,7 @@ class _FleatherEditorSelectionGestureDetectorBuilder
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 @@ abstract class EditorState extends State<RawEditor>

bool showToolbar();

void toggleToolbar([bool hideHandles = true]);

void requestKeyboard();

FocusNode get effectiveFocusNode;
Expand Down Expand Up @@ -883,6 +885,17 @@ class RawEditorState extends EditorState
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 @@ class RawEditorState extends EditorState
_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 @@ class RawEditorState extends EditorState
}
}

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
Loading

0 comments on commit bd3e219

Please sign in to comment.