This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Move context menu handling to its own class #46042
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import '../dom.dart'; | ||
| import 'prevent_default.dart'; | ||
|
|
||
| /// Controls the browser's context menu in the given [element]. | ||
| class ContextMenu { | ||
| ContextMenu(this.element); | ||
|
|
||
| final DomElement element; | ||
|
|
||
| /// False when the context menu has been disabled, otherwise true. | ||
| bool _enabled = true; | ||
|
|
||
| /// Disables the browser's context menu for this [element]. | ||
| /// | ||
| /// By default, when a Flutter web app starts, the context menu is enabled. | ||
| /// | ||
| /// Can be re-enabled by calling [enable]. | ||
| void disable() { | ||
| if (!_enabled) { | ||
| return; | ||
| } | ||
| _enabled = false; | ||
| element.addEventListener('contextmenu', preventDefaultListener); | ||
| } | ||
|
|
||
| /// Enables the browser's context menu for this [element]. | ||
| /// | ||
| /// By default, when a Flutter web app starts, the context menu is already | ||
| /// enabled. Typically, this method would be used after calling | ||
| /// [disable] to first disable it. | ||
| void enable() { | ||
| if (_enabled) { | ||
| return; | ||
| } | ||
| _enabled = true; | ||
| element.removeEventListener('contextmenu', preventDefaultListener); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import '../dom.dart'; | ||
|
|
||
| /// Listener for DOM events that prevents the default browser behavior. | ||
| final DomEventListener preventDefaultListener = createDomEventListener((DomEvent event) { | ||
| event.preventDefault(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ import 'full_page_embedding_strategy.dart'; | |
| /// * [CustomElementEmbeddingStrategy] - Flutter is rendered inside a custom host | ||
| /// element, provided by the web app programmer through the engine | ||
| /// initialization. | ||
| abstract class EmbeddingStrategy with _ContextMenu { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| abstract class EmbeddingStrategy { | ||
| EmbeddingStrategy() { | ||
| // Initialize code to handle hot-restart (debug only). | ||
| assert(() { | ||
|
|
@@ -56,71 +56,3 @@ abstract class EmbeddingStrategy with _ContextMenu { | |
| _hotRestartCache?.registerElement(element); | ||
| } | ||
| } | ||
|
|
||
| /// Provides functionality to disable and enable the browser's context menu. | ||
| mixin _ContextMenu { | ||
| /// False when the context menu has been disabled, otherwise true. | ||
| bool _contextMenuEnabled = true; | ||
|
|
||
| /// Listener for contextmenu events that prevents the browser's context menu | ||
| /// from being shown. | ||
| final DomEventListener _disablingContextMenuListener = createDomEventListener((DomEvent event) { | ||
| event.preventDefault(); | ||
| }); | ||
|
|
||
| /// Disables the browser's context menu for this part of the DOM. | ||
| /// | ||
| /// By default, when a Flutter web app starts, the context menu is enabled. | ||
| /// | ||
| /// Can be re-enabled by calling [enableContextMenu]. | ||
| /// | ||
| /// See also: | ||
| /// | ||
| /// * [disableContextMenuOn], which is like this but takes the relevant | ||
| /// [DomElement] as a parameter. | ||
| void disableContextMenu(); | ||
|
|
||
| /// Disables the browser's context menu for the given [DomElement]. | ||
| /// | ||
| /// See also: | ||
| /// | ||
| /// * [disableContextMenu], which is like this but is not passed a | ||
| /// [DomElement]. | ||
| @protected | ||
| void disableContextMenuOn(DomEventTarget element) { | ||
| if (!_contextMenuEnabled) { | ||
| return; | ||
| } | ||
|
|
||
| element.addEventListener('contextmenu', _disablingContextMenuListener); | ||
| _contextMenuEnabled = false; | ||
| } | ||
|
|
||
| /// Enables the browser's context menu for this part of the DOM. | ||
| /// | ||
| /// By default, when a Flutter web app starts, the context menu is already | ||
| /// enabled. Typically, this method would be used after calling | ||
| /// [disableContextMenu] to first disable it. | ||
| /// | ||
| /// See also: | ||
| /// | ||
| /// * [enableContextMenuOn], which is like this but takes the relevant | ||
| /// [DomElement] as a parameter. | ||
| void enableContextMenu(); | ||
|
|
||
| /// Enables the browser's context menu for the given [DomElement]. | ||
| /// | ||
| /// See also: | ||
| /// | ||
| /// * [enableContextMenu], which is like this but is not passed a | ||
| /// [DomElement]. | ||
| @protected | ||
| void enableContextMenuOn(DomEventTarget element) { | ||
| if (_contextMenuEnabled) { | ||
| return; | ||
| } | ||
|
|
||
| element.removeEventListener('contextmenu', _disablingContextMenuListener); | ||
| _contextMenuEnabled = true; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:test/bootstrap/browser.dart'; | ||
| import 'package:test/test.dart'; | ||
| import 'package:ui/src/engine.dart'; | ||
|
|
||
| void main() { | ||
| internalBootstrapBrowserTest(() => testMain); | ||
| } | ||
|
|
||
| void testMain() { | ||
| group('$ContextMenu', () { | ||
| test('can disable context menu', () { | ||
| final DomElement rootViewElement = createDomElement('div'); | ||
| final ContextMenu contextMenu = ContextMenu(rootViewElement); | ||
|
|
||
| // When the app starts, contextmenu events are not prevented. | ||
| DomEvent event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isFalse); | ||
|
|
||
| // Disabling the context menu causes contextmenu events to be prevented. | ||
| contextMenu.disable(); | ||
| event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isTrue); | ||
|
|
||
| // Disabling again has no effect. | ||
| contextMenu.disable(); | ||
| event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isTrue); | ||
| }); | ||
|
|
||
| test('does not disable context menu outside root view element', () { | ||
| final DomElement rootViewElement = createDomElement('div'); | ||
| final ContextMenu contextMenu = ContextMenu(rootViewElement); | ||
|
|
||
| contextMenu.disable(); | ||
|
|
||
| // Dispatching on a DOM element outside of target's subtree has no effect. | ||
| final DomEvent event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| domDocument.body!.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isFalse); | ||
| }); | ||
|
|
||
| test('can enable context menu after disabling', () { | ||
| final DomElement rootViewElement = createDomElement('div'); | ||
| final ContextMenu contextMenu = ContextMenu(rootViewElement); | ||
|
|
||
| // When the app starts, contextmenu events are not prevented. | ||
| DomEvent event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isFalse); | ||
|
|
||
| // Disabling the context menu causes contextmenu events to be prevented. | ||
| contextMenu.disable(); | ||
| event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isTrue); | ||
|
|
||
| // Enabling the context menu means that contextmenu events are back to not | ||
| // being prevented. | ||
| contextMenu.enable(); | ||
| event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isFalse); | ||
|
|
||
| // Enabling again has no effect. | ||
| contextMenu.enable(); | ||
| event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isFalse); | ||
| }); | ||
|
|
||
| test('enabling before disabling has no effect', () { | ||
| final DomElement rootViewElement = createDomElement('div'); | ||
| final ContextMenu contextMenu = ContextMenu(rootViewElement); | ||
|
|
||
| // When the app starts, contextmenu events are not prevented. | ||
| DomEvent event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isFalse); | ||
|
|
||
| // Enabling has no effect. | ||
| contextMenu.enable(); | ||
| event = createDomEvent('Event', 'contextmenu'); | ||
| expect(event.defaultPrevented, isFalse); | ||
| rootViewElement.dispatchEvent(event); | ||
| expect(event.defaultPrevented, isFalse); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In all these I'd do
implicitView?.contextMenu... because we really don't mind if theimplicitViewexists or not, or if this call succeeds or fails :)Also, I find it weird that this
contextMenumessages don't have a target viewId.