From 1505fceca1fdb97fe76dfd9312825837920d9776 Mon Sep 17 00:00:00 2001 From: X Code Date: Wed, 5 Jan 2022 23:13:08 -0800 Subject: [PATCH] Update EditorTextSelectionGestureDetectorBuilderDelegate --- lib/src/widgets/delegate.dart | 47 ++++++++++++++++++++++++----------- lib/src/widgets/editor.dart | 22 ++++++---------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/lib/src/widgets/delegate.dart b/lib/src/widgets/delegate.dart index 60da483f..17a5c723 100644 --- a/lib/src/widgets/delegate.dart +++ b/lib/src/widgets/delegate.dart @@ -10,12 +10,32 @@ typedef EmbedBuilder = Widget Function( 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 { - GlobalKey getEditableTextKey(); + /// [GlobalKey] to the [EditableText] for which the + /// [EditorTextSelectionGestureDetectorBuilder] will build + /// a [EditorTextSelectionGestureDetector]. + GlobalKey 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]. @@ -60,7 +80,7 @@ class EditorTextSelectionGestureDetectorBuilder { /// The [State] of the [EditableText] for which the builder will provide a /// [EditorTextSelectionGestureDetector]. @protected - EditorState? get editor => delegate.getEditableTextKey().currentState; + EditorState? get editor => delegate.editableTextKey.currentState; /// The [RenderObject] of the [EditableText] for which the builder will /// provide a [EditorTextSelectionGestureDetector]. @@ -103,9 +123,9 @@ class EditorTextSelectionGestureDetectorBuilder { /// which triggers this callback. @protected void onForcePressStart(ForcePressDetails details) { - assert(delegate.getForcePressEnabled()); + assert(delegate.forcePressEnabled); shouldShowSelectionToolbar = true; - if (delegate.getSelectionEnabled()) { + if (delegate.selectionEnabled) { renderEditor!.selectWordsInRange( details.globalPosition, null, @@ -127,7 +147,7 @@ class EditorTextSelectionGestureDetectorBuilder { /// which triggers this callback. @protected void onForcePressEnd(ForcePressDetails details) { - assert(delegate.getForcePressEnabled()); + assert(delegate.forcePressEnabled); renderEditor!.selectWordsInRange( details.globalPosition, null, @@ -148,7 +168,7 @@ class EditorTextSelectionGestureDetectorBuilder { /// this callback. @protected void onSingleTapUp(TapUpDetails details) { - if (delegate.getSelectionEnabled()) { + if (delegate.selectionEnabled) { renderEditor!.selectWordEdge(SelectionChangedCause.tap); } } @@ -177,7 +197,7 @@ class EditorTextSelectionGestureDetectorBuilder { /// which triggers this callback. @protected void onSingleLongTapStart(LongPressStartDetails details) { - if (delegate.getSelectionEnabled()) { + if (delegate.selectionEnabled) { renderEditor!.selectPositionAt( from: details.globalPosition, cause: SelectionChangedCause.longPress, @@ -196,7 +216,7 @@ class EditorTextSelectionGestureDetectorBuilder { /// triggers this callback. @protected void onSingleLongTapMoveUpdate(LongPressMoveUpdateDetails details) { - if (delegate.getSelectionEnabled()) { + if (delegate.selectionEnabled) { renderEditor!.selectPositionAt( from: details.globalPosition, cause: SelectionChangedCause.longPress, @@ -230,7 +250,7 @@ class EditorTextSelectionGestureDetectorBuilder { /// which triggers this callback. @protected void onDoubleTapDown(TapDownDetails details) { - if (delegate.getSelectionEnabled()) { + if (delegate.selectionEnabled) { renderEditor!.selectWord(SelectionChangedCause.tap); if (shouldShowSelectionToolbar) { editor!.showToolbar(); @@ -289,9 +309,8 @@ class EditorTextSelectionGestureDetectorBuilder { return EditorTextSelectionGestureDetector( key: key, onTapDown: onTapDown, - onForcePressStart: - delegate.getForcePressEnabled() ? onForcePressStart : null, - onForcePressEnd: delegate.getForcePressEnabled() ? onForcePressEnd : null, + onForcePressStart: delegate.forcePressEnabled ? onForcePressStart : null, + onForcePressEnd: delegate.forcePressEnabled ? onForcePressEnd : null, onSingleTapUp: onSingleTapUp, onSingleTapCancel: onSingleTapCancel, onSingleLongTapStart: onSingleLongTapStart, diff --git a/lib/src/widgets/editor.dart b/lib/src/widgets/editor.dart index 7893fff9..71c5a8b4 100644 --- a/lib/src/widgets/editor.dart +++ b/lib/src/widgets/editor.dart @@ -569,19 +569,13 @@ class _QuillEditorState extends State } @override - GlobalKey getEditableTextKey() { - return _editorKey; - } + GlobalKey get editableTextKey => _editorKey; @override - bool getForcePressEnabled() { - return false; - } + bool get forcePressEnabled => false; @override - bool getSelectionEnabled() { - return widget.enableInteractiveSelection; - } + bool get selectionEnabled => widget.enableInteractiveSelection; void _requestKeyboard() { _editorKey.currentState!.requestKeyboard(); @@ -598,7 +592,7 @@ class _QuillEditorSelectionGestureDetectorBuilder @override void onForcePressStart(ForcePressDetails details) { super.onForcePressStart(details); - if (delegate.getSelectionEnabled() && shouldShowSelectionToolbar) { + if (delegate.selectionEnabled && shouldShowSelectionToolbar) { editor!.showToolbar(); } } @@ -615,7 +609,7 @@ class _QuillEditorSelectionGestureDetectorBuilder return; } } - if (!delegate.getSelectionEnabled()) { + if (!delegate.selectionEnabled) { return; } switch (Theme.of(_state.context).platform) { @@ -692,7 +686,7 @@ class _QuillEditorSelectionGestureDetectorBuilder final positionSelected = _isPositionSelected(details); - if (delegate.getSelectionEnabled() && !positionSelected) { + if (delegate.selectionEnabled && !positionSelected) { switch (Theme.of(_state.context).platform) { case TargetPlatform.iOS: case TargetPlatform.macOS: @@ -754,7 +748,7 @@ class _QuillEditorSelectionGestureDetectorBuilder } } - if (delegate.getSelectionEnabled()) { + if (delegate.selectionEnabled) { switch (Theme.of(_state.context).platform) { case TargetPlatform.iOS: case TargetPlatform.macOS: @@ -785,7 +779,7 @@ class _QuillEditorSelectionGestureDetectorBuilder return; } - if (delegate.getSelectionEnabled()) { + if (delegate.selectionEnabled) { renderEditor!.onSelectionCompleted(); } }