Commit b6039241 authored by Demetrios Papadopoulos's avatar Demetrios Papadopoulos Committed by Commit Bot

Downloads WebUI: Replace cr.ui.Command usage with a keydown handler.

This is in preparation of migrating to Polymer3.

Bug: 1022215
Change-Id: Iee652ed3477175346f99c7a490d1637e754644ba
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1907413Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714640}
parent ebce9c80
...@@ -131,8 +131,6 @@ js_library("manager") { ...@@ -131,8 +131,6 @@ js_library("manager") {
"//ui/webui/resources/js:find_shortcut_behavior", "//ui/webui/resources/js:find_shortcut_behavior",
"//ui/webui/resources/js:load_time_data", "//ui/webui/resources/js:load_time_data",
"//ui/webui/resources/js:util", "//ui/webui/resources/js:util",
"//ui/webui/resources/js/cr:ui",
"//ui/webui/resources/js/cr/ui:command",
] ]
externs_list = [ "$externs_path/chrome_send.js" ] externs_list = [ "$externs_path/chrome_send.js" ]
} }
......
...@@ -37,14 +37,6 @@ ...@@ -37,14 +37,6 @@
</head> </head>
<body> <body>
<downloads-manager></downloads-manager> <downloads-manager></downloads-manager>
<if expr="is_macosx">
<command id="clear-all-command" shortcut="Alt|c Alt|ç">
<command id="undo-command" shortcut="Meta|z">
</if>
<if expr="not is_macosx">
<command id="clear-all-command" shortcut="Alt|c">
<command id="undo-command" shortcut="Ctrl|z">
</if>
<link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css"> <link rel="stylesheet" href="chrome://resources/css/text_defaults_md.css">
<link rel="import" href="chrome://resources/html/cr.html"> <link rel="import" href="chrome://resources/html/cr.html">
<link rel="import" href="chrome://resources/html/polymer.html"> <link rel="import" href="chrome://resources/html/polymer.html">
......
...@@ -68,6 +68,7 @@ ...@@ -68,6 +68,7 @@
type="chrome_html" /> type="chrome_html" />
<structure name="IDR_DOWNLOADS_MANAGER_JS" <structure name="IDR_DOWNLOADS_MANAGER_JS"
file="manager.js" file="manager.js"
preprocess="true"
type="chrome_html" /> type="chrome_html" />
<structure name="IDR_DOWNLOADS_SEARCH_SERVICE_HTML" <structure name="IDR_DOWNLOADS_SEARCH_SERVICE_HTML"
file="search_service.html" file="search_service.html"
......
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
<link rel="import" href="chrome://resources/cr_elements/shared_style_css.html"> <link rel="import" href="chrome://resources/cr_elements/shared_style_css.html">
<link rel="import" href="chrome://resources/cr_elements/shared_vars_css.html"> <link rel="import" href="chrome://resources/cr_elements/shared_vars_css.html">
<link rel="import" href="chrome://resources/html/cr.html"> <link rel="import" href="chrome://resources/html/cr.html">
<link rel="import" href="chrome://resources/html/cr/ui.html">
<link rel="import" href="chrome://resources/html/cr/ui/command.html">
<link rel="import" href="chrome://resources/html/find_shortcut_behavior.html"> <link rel="import" href="chrome://resources/html/find_shortcut_behavior.html">
<link rel="import" href="chrome://resources/html/util.html"> <link rel="import" href="chrome://resources/html/util.html">
<link rel="import" href="chrome://resources/polymer/v1_0/iron-a11y-announcer/iron-a11y-announcer.html"> <link rel="import" href="chrome://resources/polymer/v1_0/iron-a11y-announcer/iron-a11y-announcer.html">
......
...@@ -75,6 +75,9 @@ cr.define('downloads', function() { ...@@ -75,6 +75,9 @@ cr.define('downloads', function() {
/** @private {Array<number>} */ /** @private {Array<number>} */
listenerIds_: null, listenerIds_: null,
/** @private {?Function} */
boundOnKeyDown_: null,
/** @override */ /** @override */
created: function() { created: function() {
const browserProxy = downloads.BrowserProxy.getInstance(); const browserProxy = downloads.BrowserProxy.getInstance();
...@@ -103,12 +106,18 @@ cr.define('downloads', function() { ...@@ -103,12 +106,18 @@ cr.define('downloads', function() {
this.mojoEventTarget_.updateItem.addListener( this.mojoEventTarget_.updateItem.addListener(
this.updateItem_.bind(this)), this.updateItem_.bind(this)),
]; ];
this.boundOnKeyDown_ = e => this.onKeyDown_(e);
document.addEventListener('keydown', this.boundOnKeyDown_);
}, },
/** @override */ /** @override */
detached: function() { detached: function() {
this.listenerIds_.forEach( this.listenerIds_.forEach(
id => assert(this.mojoEventTarget_.removeListener(id))); id => assert(this.mojoEventTarget_.removeListener(id)));
document.removeEventListener('keydown', this.boundOnKeyDown_);
this.boundOnKeyDown_ = null;
}, },
/** @private */ /** @private */
...@@ -186,34 +195,53 @@ cr.define('downloads', function() { ...@@ -186,34 +195,53 @@ cr.define('downloads', function() {
}, },
/** /**
* @param {Event} e * @param {!KeyboardEvent} e
* @private * @private
*/ */
onCanExecute_: function(e) { onKeyDown_: function(e) {
e = /** @type {cr.ui.CanExecuteEvent} */ (e); let clearAllKey = 'c';
switch (e.command.id) { // <if expr="is_macosx">
case 'undo-command': // On Mac, pressing alt+c produces 'ç' as |event.key|.
e.canExecute = this.$.toolbar.canUndo(); clearAllKey = 'ç';
break; // </if>
case 'clear-all-command': if (e.key === clearAllKey && e.altKey && !e.ctrlKey && !e.shiftKey &&
e.canExecute = this.$.toolbar.canClearAll(); !e.metaKey) {
break; this.onClearAllCommand_();
e.preventDefault();
return;
}
if (e.key === 'z' && !e.altKey && !e.shiftKey) {
let hasTriggerModifier = e.ctrlKey && !e.metaKey;
// <if expr="is_macosx">
hasTriggerModifier = !e.ctrlKey && e.metaKey;
// </if>
if (hasTriggerModifier) {
this.onUndoCommand_();
e.preventDefault();
}
} }
}, },
/** /** @private */
* @param {Event} e onClearAllCommand_() {
* @private if (!this.$.toolbar.canClearAll()) {
*/ return;
onCommand_: function(e) {
if (e.command.id == 'clear-all-command') {
cr.toastManager.getInstance().show(
loadTimeData.getString('toastClearedAll'), true);
this.mojoHandler_.clearAll();
} else if (e.command.id == 'undo-command') {
cr.toastManager.getInstance().hide();
this.mojoHandler_.undo();
} }
this.mojoHandler_.clearAll();
cr.toastManager.getInstance().show(
loadTimeData.getString('toastClearedAll'), true);
},
/** @private */
onUndoCommand_() {
if (!this.$.toolbar.canUndo()) {
return;
}
cr.toastManager.getInstance().hide();
this.mojoHandler_.undo();
}, },
/** @private */ /** @private */
...@@ -233,10 +261,6 @@ cr.define('downloads', function() { ...@@ -233,10 +261,6 @@ cr.define('downloads', function() {
* @private * @private
*/ */
onLoad_: function() { onLoad_: function() {
cr.ui.decorate('command', cr.ui.Command);
document.addEventListener('canExecute', this.onCanExecute_.bind(this));
document.addEventListener('command', this.onCommand_.bind(this));
this.searchService_.loadMore(); this.searchService_.loadMore();
return this.loaded_.promise; return this.loaded_.promise;
}, },
......
...@@ -136,12 +136,21 @@ suite('manager tests', function() { ...@@ -136,12 +136,21 @@ suite('manager tests', function() {
loadTimeData.getString('browserManagedByOrg'); loadTimeData.getString('browserManagedByOrg');
}); });
test('toast is shown when clear-all-command is fired', () => { test('toast is shown when clear-all-command is fired', async () => {
// Add a download entry so that clear-all-command is applicable.
callbackRouterRemote.insertItems(0, [createDownload({
fileName: 'file name',
state: downloads.States.COMPLETE,
sinceString: 'Today',
url: 'a'.repeat(1000),
})]);
await callbackRouterRemote.$.flushForTesting();
const toastManager = cr.toastManager.getInstance(); const toastManager = cr.toastManager.getInstance();
assertFalse(toastManager.isToastOpen); assertFalse(toastManager.isToastOpen);
const event = new Event('command', {bubbles: true});
event.command = {id: 'clear-all-command'}; // Simulate 'alt+c' key combo.
manager.dispatchEvent(event); MockInteractions.keyDownOn(document, null, 'alt', cr.isMac ? 'ç' : 'c');
assertTrue(toastManager.isToastOpen); assertTrue(toastManager.isToastOpen);
assertFalse(toastManager.isUndoButtonHidden); assertFalse(toastManager.isUndoButtonHidden);
}); });
...@@ -149,10 +158,10 @@ suite('manager tests', function() { ...@@ -149,10 +158,10 @@ suite('manager tests', function() {
test('toast is hidden when undo-command is fired', () => { test('toast is hidden when undo-command is fired', () => {
const toastManager = cr.toastManager.getInstance(); const toastManager = cr.toastManager.getInstance();
toastManager.show(''); toastManager.show('');
const event = new Event('command', {bubbles: true});
event.command = {id: 'undo-command'};
assertTrue(toastManager.isToastOpen); assertTrue(toastManager.isToastOpen);
manager.dispatchEvent(event);
// Simulate 'ctrl+z' key combo (or meta+z for Mac).
MockInteractions.keyDownOn(document, null, cr.isMac ? 'meta' : 'ctrl', 'z');
assertFalse(toastManager.isToastOpen); assertFalse(toastManager.isToastOpen);
}); });
......
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