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

Copy TapAndPanGestureRecognizer from TextField #2128

Merged
merged 5 commits into from
Aug 20, 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
6 changes: 4 additions & 2 deletions lib/src/editor/config/editor_configurations.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart' show Brightness, Uint8List, immutable;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'
show TextCapitalization, TextInputAction, TextSelectionThemeData;
import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -258,11 +259,12 @@ class QuillEditorConfigurations extends Equatable {

// Returns whether gesture is handled
final bool Function(
TapDownDetails details, TextPosition Function(Offset offset))? onTapDown;
TapDragDownDetails details, TextPosition Function(Offset offset))?
onTapDown;

// Returns whether gesture is handled
final bool Function(
TapUpDetails details, TextPosition Function(Offset offset))? onTapUp;
TapDragUpDetails details, TextPosition Function(Offset offset))? onTapUp;

// Returns whether gesture is handled
final bool Function(
Expand Down
27 changes: 21 additions & 6 deletions lib/src/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import 'package:flutter/cupertino.dart'
show CupertinoTheme, cupertinoTextSelectionControls;
import 'package:flutter/foundation.dart'
show ValueListenable, defaultTargetPlatform;
import 'package:flutter/gestures.dart' show PointerDeviceKind;
import 'package:flutter/gestures.dart'
show
PointerDeviceKind,
TapDragDownDetails,
TapDragEndDetails,
TapDragStartDetails,
TapDragUpDetails;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -488,7 +494,7 @@ class _QuillEditorSelectionGestureDetectorBuilder
editor?.updateMagnifier(details.globalPosition);
}

bool _isPositionSelected(TapUpDetails details) {
bool _isPositionSelected(TapDragUpDetails details) {
if (_state.controller.document.isEmpty()) {
return false;
}
Expand All @@ -511,7 +517,7 @@ class _QuillEditorSelectionGestureDetectorBuilder
}

@override
void onTapDown(TapDownDetails details) {
void onTapDown(TapDragDownDetails details) {
if (_state.configurations.onTapDown != null) {
if (renderEditor != null &&
_state.configurations.onTapDown!(
Expand All @@ -532,7 +538,7 @@ class _QuillEditorSelectionGestureDetectorBuilder
}

@override
void onSingleTapUp(TapUpDetails details) {
void onSingleTapUp(TapDragUpDetails details) {
if (_state.configurations.onTapUp != null &&
renderEditor != null &&
_state.configurations.onTapUp!(
Expand Down Expand Up @@ -738,6 +744,7 @@ class RenderEditor extends RenderEditableContainerBox
Document document;
TextSelection selection;
bool _hasFocus = false;
bool get hasFocus => _hasFocus;
LayerLink _startHandleLayerLink;
LayerLink _endHandleLayerLink;

Expand Down Expand Up @@ -944,20 +951,28 @@ class RenderEditor extends RenderEditableContainerBox
}

Offset? _lastTapDownPosition;
Offset? _lastSecondaryTapDownPosition;

Offset? get lastSecondaryTapDownPosition => _lastSecondaryTapDownPosition;

// Used on Desktop (mouse and keyboard enabled platforms) as base offset
// for extending selection, either with combination of `Shift` + Click or
// by dragging
TextSelection? _extendSelectionOrigin;

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) {
_isDragging = true;

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

void handleDragEnd(DragEndDetails details) {
void handleDragEnd(TapDragEndDetails details) {
_isDragging = false;
onSelectionCompleted();
}
Expand Down
2 changes: 2 additions & 0 deletions lib/src/editor/raw_editor/raw_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ abstract class EditorState extends State<QuillRawEditor>
void updateMagnifier(Offset positionToShow);

void hideMagnifier();

void toggleToolbar([bool hideHandles = true]);
}
32 changes: 23 additions & 9 deletions lib/src/editor/raw_editor/raw_editor_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ class QuillRawEditorState extends EditorState
_selectionOverlay?.handlesVisible = _shouldShowSelectionHandles();
_selectionOverlay?.showHandles();

if (!_keyboardVisible) {
if (!_hasFocus) {
// This will show the keyboard for all selection changes on the
// editor, not just changes triggered by user gestures.
requestKeyboard();
Expand Down Expand Up @@ -1419,11 +1419,11 @@ class QuillRawEditorState extends EditorState

void _updateOrDisposeSelectionOverlayIfNeeded() {
if (_selectionOverlay != null) {
if (!_hasFocus || textEditingValue.selection.isCollapsed) {
_selectionOverlay?.dispose();
_selectionOverlay = null;
} else if (_hasFocus) {
if (_hasFocus) {
_selectionOverlay!.update(textEditingValue);
} else {
_selectionOverlay!.dispose();
_selectionOverlay = null;
}
} else if (_hasFocus) {
_selectionOverlay = _createSelectionOverlay();
Expand Down Expand Up @@ -1601,6 +1601,16 @@ class QuillRawEditorState extends EditorState
return true;
}

@override
void toggleToolbar([bool hideHandles = true]) {
final selectionOverlay = _selectionOverlay ??= _createSelectionOverlay();
if (selectionOverlay.handlesVisible) {
hideToolbar(hideHandles);
} else {
showToolbar();
}
}

void _replaceText(ReplaceTextIntent intent) {
userUpdateTextEditingValue(
intent.currentTextEditingValue
Expand Down Expand Up @@ -1835,15 +1845,19 @@ class QuillRawEditorState extends EditorState

@override
void showMagnifier(ui.Offset positionToShow) {
if (_hasFocus == false) return;
if (_selectionOverlay == null) return;
final position = renderEditor.getPositionForOffset(positionToShow);
_selectionOverlay?.showMagnifier(position, positionToShow, renderEditor);
if (_selectionOverlay!.magnifierIsVisible) {
_selectionOverlay!
.updateMagnifier(position, positionToShow, renderEditor);
} else {
_selectionOverlay!.showMagnifier(position, positionToShow, renderEditor);
}
}

@override
void updateMagnifier(ui.Offset positionToShow) {
_updateOrDisposeSelectionOverlayIfNeeded();
final position = renderEditor.getPositionForOffset(positionToShow);
_selectionOverlay?.updateMagnifier(position, positionToShow, renderEditor);
showMagnifier(positionToShow);
}
}
Loading