Refactor using platform helper

pull/602/head
X Code 3 years ago
parent 6b9c85f7cc
commit 53507416d0
  1. 21
      lib/src/utils/platform_helper.dart
  2. 15
      lib/src/widgets/cursor.dart
  3. 14
      lib/src/widgets/default_styles.dart
  4. 67
      lib/src/widgets/editor.dart
  5. 21
      lib/src/widgets/raw_editor.dart
  6. 20
      lib/src/widgets/text_line.dart

@ -5,8 +5,21 @@ bool isMobile([TargetPlatform? targetPlatform]) {
return {TargetPlatform.iOS, TargetPlatform.android}.contains(targetPlatform); return {TargetPlatform.iOS, TargetPlatform.android}.contains(targetPlatform);
} }
bool get isDesktop => { bool isDesktop([TargetPlatform? targetPlatform]) {
targetPlatform ??= defaultTargetPlatform;
return {TargetPlatform.macOS, TargetPlatform.linux, TargetPlatform.windows}
.contains(targetPlatform);
}
bool isKeyboardOS([TargetPlatform? targetPlatform]) {
targetPlatform ??= defaultTargetPlatform;
return isDesktop(targetPlatform) || targetPlatform == TargetPlatform.fuchsia;
}
bool isAppleOS([TargetPlatform? targetPlatform]) {
targetPlatform ??= defaultTargetPlatform;
return {
TargetPlatform.macOS, TargetPlatform.macOS,
TargetPlatform.linux, TargetPlatform.iOS,
TargetPlatform.windows }.contains(targetPlatform);
}.contains(defaultTargetPlatform); }

@ -1,8 +1,8 @@
import 'dart:async'; import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import '../utils/platform_helper.dart';
import 'box.dart'; import 'box.dart';
/// Style properties of editing cursor. /// Style properties of editing cursor.
@ -287,11 +287,7 @@ class CursorPainter {
final caretHeight = editable!.getFullHeightForCaret(position); final caretHeight = editable!.getFullHeightForCaret(position);
if (caretHeight != null) { if (caretHeight != null) {
switch (defaultTargetPlatform) { if (isKeyboardOS()) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
// Override the height to take the full height of the glyph at the // Override the height to take the full height of the glyph at the
// TextPosition when not on iOS. iOS has special handling that // TextPosition when not on iOS. iOS has special handling that
// creates a taller caret. // creates a taller caret.
@ -301,9 +297,7 @@ class CursorPainter {
caretRect.width, caretRect.width,
caretHeight, caretHeight,
); );
break; } else if (isAppleOS()) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
// Center the caret vertically along the text. // Center the caret vertically along the text.
caretRect = Rect.fromLTWH( caretRect = Rect.fromLTWH(
caretRect.left, caretRect.left,
@ -311,8 +305,7 @@ class CursorPainter {
caretRect.width, caretRect.width,
caretRect.height, caretRect.height,
); );
break; } else {
default:
throw UnimplementedError(); throw UnimplementedError();
} }
} }

@ -3,6 +3,7 @@ import 'package:tuple/tuple.dart';
import '../../flutter_quill.dart'; import '../../flutter_quill.dart';
import '../../models/documents/style.dart'; import '../../models/documents/style.dart';
import '../utils/platform_helper.dart';
class QuillStyles extends InheritedWidget { class QuillStyles extends InheritedWidget {
const QuillStyles({ const QuillStyles({
@ -192,18 +193,11 @@ class DefaultStyles {
); );
const baseSpacing = Tuple2<double, double>(6, 0); const baseSpacing = Tuple2<double, double>(6, 0);
String fontFamily; String fontFamily;
switch (themeData.platform) { if (isAppleOS(themeData.platform)) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
fontFamily = 'Menlo'; fontFamily = 'Menlo';
break; } else if (isKeyboardOS(themeData.platform)) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.windows:
case TargetPlatform.linux:
fontFamily = 'Roboto Mono'; fontFamily = 'Roboto Mono';
break; } else {
default:
throw UnimplementedError(); throw UnimplementedError();
} }

@ -398,33 +398,25 @@ class _QuillEditorState extends State<QuillEditor>
Color selectionColor; Color selectionColor;
Radius? cursorRadius; Radius? cursorRadius;
switch (theme.platform) { if (isKeyboardOS(theme.platform)) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
textSelectionControls = materialTextSelectionControls; textSelectionControls = materialTextSelectionControls;
paintCursorAboveText = false; paintCursorAboveText = false;
cursorOpacityAnimates = false; cursorOpacityAnimates = false;
cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary; cursorColor ??= selectionTheme.cursorColor ?? theme.colorScheme.primary;
selectionColor = selectionTheme.selectionColor ?? selectionColor = selectionTheme.selectionColor ??
theme.colorScheme.primary.withOpacity(0.40); theme.colorScheme.primary.withOpacity(0.40);
break; } else if (isAppleOS(theme.platform)) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
final cupertinoTheme = CupertinoTheme.of(context); final cupertinoTheme = CupertinoTheme.of(context);
textSelectionControls = cupertinoTextSelectionControls; textSelectionControls = cupertinoTextSelectionControls;
paintCursorAboveText = true; paintCursorAboveText = true;
cursorOpacityAnimates = true; cursorOpacityAnimates = true;
cursorColor ??= cursorColor ??= selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
selectionTheme.cursorColor ?? cupertinoTheme.primaryColor;
selectionColor = selectionTheme.selectionColor ?? selectionColor = selectionTheme.selectionColor ??
cupertinoTheme.primaryColor.withOpacity(0.40); cupertinoTheme.primaryColor.withOpacity(0.40);
cursorRadius ??= const Radius.circular(2); cursorRadius ??= const Radius.circular(2);
cursorOffset = Offset( cursorOffset = Offset(
iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0); iOSHorizontalOffset / MediaQuery.of(context).devicePixelRatio, 0);
break; } else {
default:
throw UnimplementedError(); throw UnimplementedError();
} }
@ -524,26 +516,21 @@ class _QuillEditorSelectionGestureDetectorBuilder
if (!delegate.selectionEnabled) { if (!delegate.selectionEnabled) {
return; return;
} }
switch (Theme.of(_state.context).platform) {
case TargetPlatform.iOS: final _platform = Theme.of(_state.context).platform;
case TargetPlatform.macOS: if (isAppleOS(_platform)) {
renderEditor!.selectPositionAt( renderEditor!.selectPositionAt(
from: details.globalPosition, from: details.globalPosition,
cause: SelectionChangedCause.longPress, cause: SelectionChangedCause.longPress,
); );
break; } else if (isKeyboardOS(_platform)) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectWordsInRange( renderEditor!.selectWordsInRange(
details.globalPosition - details.offsetFromOrigin, details.globalPosition - details.offsetFromOrigin,
details.globalPosition, details.globalPosition,
SelectionChangedCause.longPress, SelectionChangedCause.longPress,
); );
break; } else {
default: throw UnimplementedError();
throw 'Invalid platform';
} }
} }
@ -599,9 +586,8 @@ class _QuillEditorSelectionGestureDetectorBuilder
final positionSelected = _isPositionSelected(details); final positionSelected = _isPositionSelected(details);
if (delegate.selectionEnabled && !positionSelected) { if (delegate.selectionEnabled && !positionSelected) {
switch (Theme.of(_state.context).platform) { final _platform = Theme.of(_state.context).platform;
case TargetPlatform.iOS: if (isAppleOS(_platform)) {
case TargetPlatform.macOS:
switch (details.kind) { switch (details.kind) {
case PointerDeviceKind.mouse: case PointerDeviceKind.mouse:
case PointerDeviceKind.stylus: case PointerDeviceKind.stylus:
@ -625,26 +611,15 @@ class _QuillEditorSelectionGestureDetectorBuilder
case PointerDeviceKind.unknown: case PointerDeviceKind.unknown:
// On macOS/iOS/iPadOS a touch tap places the cursor at the edge // On macOS/iOS/iPadOS a touch tap places the cursor at the edge
// of the word. // of the word.
try {
renderEditor! renderEditor!
..selectWordEdge(SelectionChangedCause.tap) ..selectWordEdge(SelectionChangedCause.tap)
..onSelectionCompleted(); ..onSelectionCompleted();
} finally {
break; break;
} }
} } else if (isKeyboardOS(_platform)) {
break;
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
try {
renderEditor! renderEditor!
..selectPosition(cause: SelectionChangedCause.tap) ..selectPosition(cause: SelectionChangedCause.tap)
..onSelectionCompleted(); ..onSelectionCompleted();
} finally {
break;
}
} }
} }
_state._requestKeyboard(); _state._requestKeyboard();
@ -661,23 +636,17 @@ class _QuillEditorSelectionGestureDetectorBuilder
} }
if (delegate.selectionEnabled) { if (delegate.selectionEnabled) {
switch (Theme.of(_state.context).platform) { final _platform = Theme.of(_state.context).platform;
case TargetPlatform.iOS: if (isAppleOS(_platform)) {
case TargetPlatform.macOS:
renderEditor!.selectPositionAt( renderEditor!.selectPositionAt(
from: details.globalPosition, from: details.globalPosition,
cause: SelectionChangedCause.longPress, cause: SelectionChangedCause.longPress,
); );
break; } else if (isKeyboardOS(_platform)) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
renderEditor!.selectWord(SelectionChangedCause.longPress); renderEditor!.selectWord(SelectionChangedCause.longPress);
Feedback.forLongPress(_state.context); Feedback.forLongPress(_state.context);
break; } else {
default: throw UnimplementedError();
throw 'Invalid platform';
} }
} }
} }

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:io';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
@ -16,6 +17,7 @@ import '../models/documents/attribute.dart';
import '../models/documents/document.dart'; import '../models/documents/document.dart';
import '../models/documents/nodes/block.dart'; import '../models/documents/nodes/block.dart';
import '../models/documents/nodes/line.dart'; import '../models/documents/nodes/line.dart';
import '../utils/platform_helper.dart';
import 'controller.dart'; import 'controller.dart';
import 'cursor.dart'; import 'cursor.dart';
import 'default_styles.dart'; import 'default_styles.dart';
@ -524,10 +526,7 @@ class RawEditorState extends EditorState
_floatingCursorResetController = AnimationController(vsync: this); _floatingCursorResetController = AnimationController(vsync: this);
_floatingCursorResetController.addListener(onFloatingCursorResetTick); _floatingCursorResetController.addListener(onFloatingCursorResetTick);
if (defaultTargetPlatform == TargetPlatform.windows || if (isKeyboardOS()) {
defaultTargetPlatform == TargetPlatform.macOS ||
defaultTargetPlatform == TargetPlatform.linux ||
defaultTargetPlatform == TargetPlatform.fuchsia) {
_keyboardVisible = true; _keyboardVisible = true;
} else { } else {
_keyboardVisibilityController = KeyboardVisibilityController(); _keyboardVisibilityController = KeyboardVisibilityController();
@ -852,24 +851,16 @@ class RawEditorState extends EditorState
bringIntoView(textEditingValue.selection.extent); bringIntoView(textEditingValue.selection.extent);
hideToolbar(false); hideToolbar(false);
switch (defaultTargetPlatform) { if (isKeyboardOS() || Platform.isAndroid) {
case TargetPlatform.iOS:
break;
case TargetPlatform.macOS:
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
// Collapse the selection and hide the toolbar and handles. // Collapse the selection and hide the toolbar and handles.
userUpdateTextEditingValue( userUpdateTextEditingValue(
TextEditingValue( TextEditingValue(
text: textEditingValue.text, text: textEditingValue.text,
selection: TextSelection.collapsed( selection:
offset: textEditingValue.selection.end), TextSelection.collapsed(offset: textEditingValue.selection.end),
), ),
SelectionChangedCause.toolbar, SelectionChangedCause.toolbar,
); );
break;
} }
} }
} }

@ -1,7 +1,6 @@
import 'dart:collection'; import 'dart:collection';
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
@ -81,7 +80,7 @@ class _TextLineState extends State<TextLine> {
// Desktop platforms (macos, linux, windows): // Desktop platforms (macos, linux, windows):
// only allow Meta(Control)+Click combinations // only allow Meta(Control)+Click combinations
if (isDesktop) { if (isDesktop()) {
return _metaOrControlPressed; return _metaOrControlPressed;
} }
// Mobile platforms (ios, android): always allow but we install a // Mobile platforms (ios, android): always allow but we install a
@ -376,7 +375,7 @@ class _TextLineState extends State<TextLine> {
return _linkRecognizers[segment]!; return _linkRecognizers[segment]!;
} }
if (isDesktop || widget.readOnly) { if (isDesktop() || widget.readOnly) {
_linkRecognizers[segment] = TapGestureRecognizer() _linkRecognizers[segment] = TapGestureRecognizer()
..onTap = () => _tapNodeLink(segment); ..onTap = () => _tapNodeLink(segment);
} else { } else {
@ -854,19 +853,12 @@ class RenderEditableTextLine extends RenderEditableBox {
/// of the cursor for iOS is approximate and obtained through an eyeball /// of the cursor for iOS is approximate and obtained through an eyeball
/// comparison. /// comparison.
void _computeCaretPrototype() { void _computeCaretPrototype() {
switch (defaultTargetPlatform) { if (isAppleOS()) {
case TargetPlatform.iOS:
case TargetPlatform.macOS:
_caretPrototype = Rect.fromLTWH(0, 0, cursorWidth, cursorHeight + 2); _caretPrototype = Rect.fromLTWH(0, 0, cursorWidth, cursorHeight + 2);
break; } else if (isKeyboardOS()) {
case TargetPlatform.android:
case TargetPlatform.fuchsia:
case TargetPlatform.linux:
case TargetPlatform.windows:
_caretPrototype = Rect.fromLTWH(0, 2, cursorWidth, cursorHeight - 4.0); _caretPrototype = Rect.fromLTWH(0, 2, cursorWidth, cursorHeight - 4.0);
break; } else {
default: throw UnimplementedError();
throw 'Invalid platform';
} }
} }

Loading…
Cancel
Save