8
8
import 'package:flutter/material.dart' ;
9
9
import 'package:flutter/services.dart' ;
10
10
11
+ import 'raw_editor_state.dart' ;
12
+
11
13
/// Displays the system context menu on top of the Flutter view.
12
14
///
13
15
/// Currently, only supports iOS 16.0 and above and displays nothing on other
@@ -45,10 +47,10 @@ import 'package:flutter/services.dart';
45
47
///
46
48
/// * [SystemContextMenuController] , which directly controls the hiding and
47
49
/// showing of the system context menu.
48
- class SystemContextMenu extends StatefulWidget {
50
+ class QuillSystemContextMenu extends StatefulWidget {
49
51
/// Creates an instance of [SystemContextMenu] that points to the given
50
52
/// [anchor] .
51
- const SystemContextMenu ._({
53
+ const QuillSystemContextMenu ._({
52
54
super .key,
53
55
required this .anchor,
54
56
required this .items,
@@ -57,15 +59,15 @@ class SystemContextMenu extends StatefulWidget {
57
59
58
60
/// Creates an instance of [SystemContextMenu] for the field indicated by the
59
61
/// given [EditableTextState] .
60
- factory SystemContextMenu .editableText ({
62
+ factory QuillSystemContextMenu .editableText ({
61
63
Key ? key,
62
64
required EditableTextState editableTextState,
63
65
List <IOSSystemContextMenuItem >? items,
64
66
}) {
65
67
final (startGlyphHeight: double startGlyphHeight, endGlyphHeight: double endGlyphHeight) =
66
68
editableTextState.getGlyphHeights ();
67
69
68
- return SystemContextMenu ._(
70
+ return QuillSystemContextMenu ._(
69
71
key: key,
70
72
anchor: TextSelectionToolbarAnchors .getSelectionRect (
71
73
editableTextState.renderEditable,
@@ -80,6 +82,42 @@ class SystemContextMenu extends StatefulWidget {
80
82
);
81
83
}
82
84
85
+
86
+ /// Creates an instance of [QuillSystemContextMenu] for the field indicated by the
87
+ /// given [QuillRawEditorState] .
88
+ factory QuillSystemContextMenu .quillEditor ({
89
+ Key ? key,
90
+ required QuillRawEditorState quillEditorState,
91
+ List <IOSSystemContextMenuItem >? items,
92
+ }) {
93
+ final selection = quillEditorState.textEditingValue.selection;
94
+ final points = quillEditorState.renderEditor.getEndpointsForSelection (selection);
95
+
96
+ // Calculate glyph heights manually since _getGlyphHeights is private
97
+ double startGlyphHeight, endGlyphHeight;
98
+ if (selection.isValid && ! selection.isCollapsed) {
99
+ final startCharacterRect = quillEditorState.renderEditor.getLocalRectForCaret (selection.base );
100
+ final endCharacterRect = quillEditorState.renderEditor.getLocalRectForCaret (selection.extent);
101
+ startGlyphHeight = startCharacterRect.height;
102
+ endGlyphHeight = endCharacterRect.height;
103
+ } else {
104
+ startGlyphHeight = quillEditorState.renderEditor.preferredLineHeight (selection.base );
105
+ endGlyphHeight = startGlyphHeight;
106
+ }
107
+
108
+ return QuillSystemContextMenu ._(
109
+ key: key,
110
+ anchor: TextSelectionToolbarAnchors .getSelectionRect (
111
+ quillEditorState.renderEditor,
112
+ startGlyphHeight,
113
+ endGlyphHeight,
114
+ points,
115
+ ),
116
+ items: items ?? getDefaultItemsForQuill (quillEditorState),
117
+ onSystemHide: quillEditorState.hideToolbar,
118
+ );
119
+ }
120
+
83
121
/// The [Rect] that the context menu should point to.
84
122
final Rect anchor;
85
123
@@ -130,11 +168,23 @@ class SystemContextMenu extends StatefulWidget {
130
168
];
131
169
}
132
170
171
+ /// Returns the default context menu items for the given [QuillRawEditorState] .
172
+ static List <IOSSystemContextMenuItem > getDefaultItemsForQuill (QuillRawEditorState quillEditorState) {
173
+ return < IOSSystemContextMenuItem > [
174
+ if (quillEditorState.copyEnabled) const IOSSystemContextMenuItemCopy (),
175
+ if (quillEditorState.cutEnabled) const IOSSystemContextMenuItemCut (),
176
+ if (quillEditorState.pasteEnabled) const IOSSystemContextMenuItemPaste (),
177
+ if (quillEditorState.selectAllEnabled) const IOSSystemContextMenuItemSelectAll (),
178
+ if (quillEditorState.lookUpEnabled) const IOSSystemContextMenuItemLookUp (),
179
+ if (quillEditorState.searchWebEnabled) const IOSSystemContextMenuItemSearchWeb (),
180
+ ];
181
+ }
182
+
133
183
@override
134
- State <SystemContextMenu > createState () => _SystemContextMenuState ();
184
+ State <QuillSystemContextMenu > createState () => _SystemContextMenuState ();
135
185
}
136
186
137
- class _SystemContextMenuState extends State <SystemContextMenu > {
187
+ class _SystemContextMenuState extends State <QuillSystemContextMenu > {
138
188
late final SystemContextMenuController _systemContextMenuController;
139
189
140
190
@override
0 commit comments