Update EditorTextSelectionGestureDetectorBuilderDelegate

pull/587/head
X Code 3 years ago
parent 8bd15fe785
commit 1505fceca1
  1. 47
      lib/src/widgets/delegate.dart
  2. 22
      lib/src/widgets/editor.dart

@ -10,12 +10,32 @@ typedef EmbedBuilder = Widget Function(
typedef CustomStyleBuilder = TextStyle Function(Attribute attribute); typedef CustomStyleBuilder = TextStyle Function(Attribute attribute);
/// Delegate interface for the [EditorTextSelectionGestureDetectorBuilder].
///
/// The interface is usually implemented by textfield implementations wrapping
/// [EditableText], that use a [EditorTextSelectionGestureDetectorBuilder]
/// to build a [EditorTextSelectionGestureDetector] for their [EditableText].
/// The delegate provides the builder with information about the current state
/// of the textfield.
/// Based on these information, the builder adds the correct gesture handlers
/// to the gesture detector.
///
/// See also:
///
/// * [TextField], which implements this delegate for the Material textfield.
/// * [CupertinoTextField], which implements this delegate for the Cupertino
/// textfield.
abstract class EditorTextSelectionGestureDetectorBuilderDelegate { abstract class EditorTextSelectionGestureDetectorBuilderDelegate {
GlobalKey<EditorState> getEditableTextKey(); /// [GlobalKey] to the [EditableText] for which the
/// [EditorTextSelectionGestureDetectorBuilder] will build
/// a [EditorTextSelectionGestureDetector].
GlobalKey<EditorState> get editableTextKey;
bool getForcePressEnabled(); /// Whether the textfield should respond to force presses.
bool get forcePressEnabled;
bool getSelectionEnabled(); /// Whether the user may select text in the textfield.
bool get selectionEnabled;
} }
/// Builds a [EditorTextSelectionGestureDetector] to wrap an [EditableText]. /// Builds a [EditorTextSelectionGestureDetector] to wrap an [EditableText].
@ -60,7 +80,7 @@ class EditorTextSelectionGestureDetectorBuilder {
/// The [State] of the [EditableText] for which the builder will provide a /// The [State] of the [EditableText] for which the builder will provide a
/// [EditorTextSelectionGestureDetector]. /// [EditorTextSelectionGestureDetector].
@protected @protected
EditorState? get editor => delegate.getEditableTextKey().currentState; EditorState? get editor => delegate.editableTextKey.currentState;
/// The [RenderObject] of the [EditableText] for which the builder will /// The [RenderObject] of the [EditableText] for which the builder will
/// provide a [EditorTextSelectionGestureDetector]. /// provide a [EditorTextSelectionGestureDetector].
@ -103,9 +123,9 @@ class EditorTextSelectionGestureDetectorBuilder {
/// which triggers this callback. /// which triggers this callback.
@protected @protected
void onForcePressStart(ForcePressDetails details) { void onForcePressStart(ForcePressDetails details) {
assert(delegate.getForcePressEnabled()); assert(delegate.forcePressEnabled);
shouldShowSelectionToolbar = true; shouldShowSelectionToolbar = true;
if (delegate.getSelectionEnabled()) { if (delegate.selectionEnabled) {
renderEditor!.selectWordsInRange( renderEditor!.selectWordsInRange(
details.globalPosition, details.globalPosition,
null, null,
@ -127,7 +147,7 @@ class EditorTextSelectionGestureDetectorBuilder {
/// which triggers this callback. /// which triggers this callback.
@protected @protected
void onForcePressEnd(ForcePressDetails details) { void onForcePressEnd(ForcePressDetails details) {
assert(delegate.getForcePressEnabled()); assert(delegate.forcePressEnabled);
renderEditor!.selectWordsInRange( renderEditor!.selectWordsInRange(
details.globalPosition, details.globalPosition,
null, null,
@ -148,7 +168,7 @@ class EditorTextSelectionGestureDetectorBuilder {
/// this callback. /// this callback.
@protected @protected
void onSingleTapUp(TapUpDetails details) { void onSingleTapUp(TapUpDetails details) {
if (delegate.getSelectionEnabled()) { if (delegate.selectionEnabled) {
renderEditor!.selectWordEdge(SelectionChangedCause.tap); renderEditor!.selectWordEdge(SelectionChangedCause.tap);
} }
} }
@ -177,7 +197,7 @@ class EditorTextSelectionGestureDetectorBuilder {
/// which triggers this callback. /// which triggers this callback.
@protected @protected
void onSingleLongTapStart(LongPressStartDetails details) { void onSingleLongTapStart(LongPressStartDetails details) {
if (delegate.getSelectionEnabled()) { if (delegate.selectionEnabled) {
renderEditor!.selectPositionAt( renderEditor!.selectPositionAt(
from: details.globalPosition, from: details.globalPosition,
cause: SelectionChangedCause.longPress, cause: SelectionChangedCause.longPress,
@ -196,7 +216,7 @@ class EditorTextSelectionGestureDetectorBuilder {
/// triggers this callback. /// triggers this callback.
@protected @protected
void onSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) { void onSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) {
if (delegate.getSelectionEnabled()) { if (delegate.selectionEnabled) {
renderEditor!.selectPositionAt( renderEditor!.selectPositionAt(
from: details.globalPosition, from: details.globalPosition,
cause: SelectionChangedCause.longPress, cause: SelectionChangedCause.longPress,
@ -230,7 +250,7 @@ class EditorTextSelectionGestureDetectorBuilder {
/// which triggers this callback. /// which triggers this callback.
@protected @protected
void onDoubleTapDown(TapDownDetails details) { void onDoubleTapDown(TapDownDetails details) {
if (delegate.getSelectionEnabled()) { if (delegate.selectionEnabled) {
renderEditor!.selectWord(SelectionChangedCause.tap); renderEditor!.selectWord(SelectionChangedCause.tap);
if (shouldShowSelectionToolbar) { if (shouldShowSelectionToolbar) {
editor!.showToolbar(); editor!.showToolbar();
@ -289,9 +309,8 @@ class EditorTextSelectionGestureDetectorBuilder {
return EditorTextSelectionGestureDetector( return EditorTextSelectionGestureDetector(
key: key, key: key,
onTapDown: onTapDown, onTapDown: onTapDown,
onForcePressStart: onForcePressStart: delegate.forcePressEnabled ? onForcePressStart : null,
delegate.getForcePressEnabled() ? onForcePressStart : null, onForcePressEnd: delegate.forcePressEnabled ? onForcePressEnd : null,
onForcePressEnd: delegate.getForcePressEnabled() ? onForcePressEnd : null,
onSingleTapUp: onSingleTapUp, onSingleTapUp: onSingleTapUp,
onSingleTapCancel: onSingleTapCancel, onSingleTapCancel: onSingleTapCancel,
onSingleLongTapStart: onSingleLongTapStart, onSingleLongTapStart: onSingleLongTapStart,

@ -569,19 +569,13 @@ class _QuillEditorState extends State<QuillEditor>
} }
@override @override
GlobalKey<EditorState> getEditableTextKey() { GlobalKey<EditorState> get editableTextKey => _editorKey;
return _editorKey;
}
@override @override
bool getForcePressEnabled() { bool get forcePressEnabled => false;
return false;
}
@override @override
bool getSelectionEnabled() { bool get selectionEnabled => widget.enableInteractiveSelection;
return widget.enableInteractiveSelection;
}
void _requestKeyboard() { void _requestKeyboard() {
_editorKey.currentState!.requestKeyboard(); _editorKey.currentState!.requestKeyboard();
@ -598,7 +592,7 @@ class _QuillEditorSelectionGestureDetectorBuilder
@override @override
void onForcePressStart(ForcePressDetails details) { void onForcePressStart(ForcePressDetails details) {
super.onForcePressStart(details); super.onForcePressStart(details);
if (delegate.getSelectionEnabled() && shouldShowSelectionToolbar) { if (delegate.selectionEnabled && shouldShowSelectionToolbar) {
editor!.showToolbar(); editor!.showToolbar();
} }
} }
@ -615,7 +609,7 @@ class _QuillEditorSelectionGestureDetectorBuilder
return; return;
} }
} }
if (!delegate.getSelectionEnabled()) { if (!delegate.selectionEnabled) {
return; return;
} }
switch (Theme.of(_state.context).platform) { switch (Theme.of(_state.context).platform) {
@ -692,7 +686,7 @@ class _QuillEditorSelectionGestureDetectorBuilder
final positionSelected = _isPositionSelected(details); final positionSelected = _isPositionSelected(details);
if (delegate.getSelectionEnabled() && !positionSelected) { if (delegate.selectionEnabled && !positionSelected) {
switch (Theme.of(_state.context).platform) { switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS: case TargetPlatform.iOS:
case TargetPlatform.macOS: case TargetPlatform.macOS:
@ -754,7 +748,7 @@ class _QuillEditorSelectionGestureDetectorBuilder
} }
} }
if (delegate.getSelectionEnabled()) { if (delegate.selectionEnabled) {
switch (Theme.of(_state.context).platform) { switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS: case TargetPlatform.iOS:
case TargetPlatform.macOS: case TargetPlatform.macOS:
@ -785,7 +779,7 @@ class _QuillEditorSelectionGestureDetectorBuilder
return; return;
} }
if (delegate.getSelectionEnabled()) { if (delegate.selectionEnabled) {
renderEditor!.onSelectionCompleted(); renderEditor!.onSelectionCompleted();
} }
} }

Loading…
Cancel
Save