88import 'package:flutter/material.dart' ;
99import 'package:flutter/services.dart' ;
1010
11+ import 'raw_editor_state.dart' ;
12+
1113/// Displays the system context menu on top of the Flutter view.
1214///
1315/// Currently, only supports iOS 16.0 and above and displays nothing on other
@@ -45,10 +47,10 @@ import 'package:flutter/services.dart';
4547///
4648/// * [SystemContextMenuController] , which directly controls the hiding and
4749/// showing of the system context menu.
48- class SystemContextMenu extends StatefulWidget {
50+ class QuillSystemContextMenu extends StatefulWidget {
4951 /// Creates an instance of [SystemContextMenu] that points to the given
5052 /// [anchor] .
51- const SystemContextMenu ._({
53+ const QuillSystemContextMenu ._({
5254 super .key,
5355 required this .anchor,
5456 required this .items,
@@ -57,15 +59,15 @@ class SystemContextMenu extends StatefulWidget {
5759
5860 /// Creates an instance of [SystemContextMenu] for the field indicated by the
5961 /// given [EditableTextState] .
60- factory SystemContextMenu .editableText ({
62+ factory QuillSystemContextMenu .editableText ({
6163 Key ? key,
6264 required EditableTextState editableTextState,
6365 List <IOSSystemContextMenuItem >? items,
6466 }) {
6567 final (startGlyphHeight: double startGlyphHeight, endGlyphHeight: double endGlyphHeight) =
6668 editableTextState.getGlyphHeights ();
6769
68- return SystemContextMenu ._(
70+ return QuillSystemContextMenu ._(
6971 key: key,
7072 anchor: TextSelectionToolbarAnchors .getSelectionRect (
7173 editableTextState.renderEditable,
@@ -80,6 +82,42 @@ class SystemContextMenu extends StatefulWidget {
8082 );
8183 }
8284
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+
83121 /// The [Rect] that the context menu should point to.
84122 final Rect anchor;
85123
@@ -130,11 +168,23 @@ class SystemContextMenu extends StatefulWidget {
130168 ];
131169 }
132170
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+
133183 @override
134- State <SystemContextMenu > createState () => _SystemContextMenuState ();
184+ State <QuillSystemContextMenu > createState () => _SystemContextMenuState ();
135185}
136186
137- class _SystemContextMenuState extends State <SystemContextMenu > {
187+ class _SystemContextMenuState extends State <QuillSystemContextMenu > {
138188 late final SystemContextMenuController _systemContextMenuController;
139189
140190 @override
0 commit comments