fix(web): prevent browser context menu over task menus (#1627)

This commit is contained in:
HouKangle authored and GitHub committed 2026-10-30 18:02:02 +08:00
1 parent c0b70d319d
commit 8719166dbf
9 files changed
+420 -1

No files matched your search

@@ -0,0 +1 @@
export 'context_menu_event_stub.dart' if (dart.library.js_interop) 'context_menu_event_web.dart';
@@ -0,0 +1,5 @@
import 'dart:ui';
bool dispatchContextMenu(Offset position, int viewId) {
throw UnsupportedError('DOM contextmenu events require a browser');
}
@@ -0,0 +1,25 @@
import 'dart:ui';
import 'dart:ui_web' as ui_web;
import 'package:web/web.dart' as web;
/// Exercises DOM cancellation, independently of Flutter's global menu flag.
/// Synthetic events cannot prove whether a browser's native menu is visible.
bool dispatchContextMenu(Offset position, int viewId) {
final view = PlatformDispatcher.instance.views.singleWhere((view) => view.viewId == viewId);
final host = ui_web.views.getHostElement(viewId) as web.HTMLElement;
final bounds = host.getBoundingClientRect();
final logicalSize = view.physicalSize / view.devicePixelRatio;
final event = web.MouseEvent(
'contextmenu',
web.MouseEventInit(
bubbles: true,
cancelable: true,
button: 2,
clientX: (bounds.left + position.dx * bounds.width / logicalSize.width).round(),
clientY: (bounds.top + position.dy * bounds.height / logicalSize.height).round(),
),
);
host.dispatchEvent(event);
return event.defaultPrevented;
}