Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(716)

Unified Diff: ui/webui/resources/js/cr/ui/menu.js

Issue 1358893003: Ignore mouseup shortly after showing cr.ui.Menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix context menus Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/webui/resources/js/cr/ui/context_menu_handler.js ('k') | ui/webui/resources/js/cr/ui/menu_button.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/webui/resources/js/cr/ui/menu.js
diff --git a/ui/webui/resources/js/cr/ui/menu.js b/ui/webui/resources/js/cr/ui/menu.js
index d54f5430f855a9239caaa9ba9af714054acae0db..abcb79c71906f6a6fb1f83f7617f183d54699281 100644
--- a/ui/webui/resources/js/cr/ui/menu.js
+++ b/ui/webui/resources/js/cr/ui/menu.js
@@ -32,6 +32,7 @@ cr.define('cr.ui', function() {
decorate: function() {
this.addEventListener('mouseover', this.handleMouseOver_);
this.addEventListener('mouseout', this.handleMouseOut_);
+ this.addEventListener('mouseup', this.handleMouseUp_, true);
this.classList.add('decorated');
this.setAttribute('role', 'menu');
@@ -113,6 +114,37 @@ cr.define('cr.ui', function() {
this.selectedItem = null;
},
+ /**
+ * If there's a mouseup that happens quickly in about the same position,
+ * stop it from propagating to items. This is to prevent accidentally
+ * selecting a menu item that's created under the mouse cursor.
+ * @param {Event} e A mouseup event on the menu (in capturing phase).
+ * @private
+ */
+ handleMouseUp_: function(e) {
+ assert(this.contains(/** @type {Element} */(e.target)));
+
+ if (!this.trustEvent_(e) || Date.now() - this.shown_.time > 200)
+ return;
+
+ var pos = this.shown_.mouseDownPos;
+ if (!pos || Math.abs(pos.x - e.screenX) + Math.abs(pos.y - e.screenY) > 4)
+ return;
+
+ e.preventDefault();
+ e.stopPropagation();
+ },
+
+ /**
+ * @param {!Event} e
+ * @return {boolean} Whether |e| can be trusted.
+ * @private
+ * @suppress {checkTypes}
+ */
+ trustEvent_: function(e) {
+ return e.isTrusted || e.isTrustedForTesting;
+ },
+
get menuItems() {
return this.querySelectorAll(this.menuItemSelector || '*');
},
@@ -238,6 +270,17 @@ cr.define('cr.ui', function() {
return false;
},
+ hide: function() {
+ this.hidden = true;
+ delete this.shown_;
+ },
+
+ /** @param {{x: number, y: number}=} opt_mouseDownPos */
+ show: function(opt_mouseDownPos) {
+ this.shown_ = {mouseDownPos: opt_mouseDownPos, time: Date.now()};
+ this.hidden = false;
+ },
+
/**
* Updates menu items command according to context.
* @param {Node=} node Node for which to actuate commands state.
« no previous file with comments | « ui/webui/resources/js/cr/ui/context_menu_handler.js ('k') | ui/webui/resources/js/cr/ui/menu_button.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698