Unverified Commit 71c70ebf authored by Justin McCandless's avatar Justin McCandless Committed by GitHub

Custom context menus in SelectableRegion on web (#121653)

contextMenuBuilder now works with SelectableRegion on web (when using BrowserContextMenu.disable).
parent 19d16bb0
......@@ -738,10 +738,11 @@ class SelectableRegionState extends State<SelectableRegion> with TextSelectionDe
}
// Web is using native dom elements to enable clipboard functionality of the
// toolbar: copy, paste, select, cut. It might also provide additional
// functionality depending on the browser (such as translate). Due to this
// we should not show a Flutter toolbar for the editable text elements.
if (kIsWeb) {
// context menu: copy, paste, select, cut. It might also provide additional
// functionality depending on the browser (such as translate). Due to this,
// we should not show a Flutter toolbar for the editable text elements
// unless the browser's context menu is explicitly disabled.
if (kIsWeb && BrowserContextMenu.enabled) {
return false;
}
......
......@@ -1676,7 +1676,7 @@ void main() {
SystemChannels.contextMenu.setMockMethodCallHandler(null);
});
testWidgets('web can show toolbar when the browser context menu is disabled', (WidgetTester tester) async {
testWidgets('web can show flutter context menu when the browser context menu is disabled', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: EditableText(
......
......@@ -1781,6 +1781,51 @@ void main() {
expect(content, isNotNull);
expect(content!.plainText, 'How');
});
group('BrowserContextMenu', () {
setUp(() async {
SystemChannels.contextMenu.setMockMethodCallHandler((MethodCall call) {
// Just complete successfully, so that BrowserContextMenu thinks that
// the engine successfully received its call.
return Future<void>.value();
});
await BrowserContextMenu.disableContextMenu();
});
tearDown(() async {
await BrowserContextMenu.enableContextMenu();
SystemChannels.contextMenu.setMockMethodCallHandler(null);
});
testWidgets('web can show flutter context menu when the browser context menu is disabled', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: SelectableRegion(
onSelectionChanged: (SelectedContent? selectedContent) {},
focusNode: FocusNode(),
selectionControls: materialTextSelectionControls,
child: const Center(
child: Text('How are you'),
),
),
),
);
final SelectableRegionState state =
tester.state<SelectableRegionState>(find.byType(SelectableRegion));
expect(find.text('Copy'), findsNothing);
state.selectAll(SelectionChangedCause.toolbar);
await tester.pumpAndSettle();
expect(find.text('Copy'), findsOneWidget);
state.hideToolbar();
await tester.pumpAndSettle();
expect(find.text('Copy'), findsNothing);
},
skip: !kIsWeb, // [intended]
);
});
}
class SelectionSpy extends LeafRenderObjectWidget {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment