Custom context menus in SelectableRegion on web (#121653)
contextMenuBuilder now works with SelectableRegion on web (when using BrowserContextMenu.disable).
This commit is contained in:
committed by
GitHub
parent
19d16bb00a
commit
71c70ebf36
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user