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") {
"//ui/webui/resources/js:find_shortcut_behavior",
"//ui/webui/resources/js:load_time_data",
"//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" ]
}
......
......@@ -37,14 +37,6 @@
</head>
<body>
<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="import" href="chrome://resources/html/cr.html">
<link rel="import" href="chrome://resources/html/polymer.html">
......
......@@ -68,6 +68,7 @@
type="chrome_html" />
<structure name="IDR_DOWNLOADS_MANAGER_JS"
file="manager.js"
preprocess="true"
type="chrome_html" />
<structure name="IDR_DOWNLOADS_SEARCH_SERVICE_HTML"
file="search_service.html"
......
......@@ -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_vars_css.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/util.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() {
/** @private {Array<number>} */
listenerIds_: null,
/** @private {?Function} */
boundOnKeyDown_: null,
/** @override */
created: function() {
const browserProxy = downloads.BrowserProxy.getInstance();
......@@ -103,12 +106,18 @@ cr.define('downloads', function() {
this.mojoEventTarget_.updateItem.addListener(
this.updateItem_.bind(this)),
];
this.boundOnKeyDown_ = e => this.onKeyDown_(e);
document.addEventListener('keydown', this.boundOnKeyDown_);
},
/** @override */
detached: function() {
this.listenerIds_.forEach(
id => assert(this.mojoEventTarget_.removeListener(id)));
document.removeEventListener('keydown', this.boundOnKeyDown_);
this.boundOnKeyDown_ = null;
},
/** @private */
......@@ -186,34 +195,53 @@ cr.define('downloads', function() {
},
/**
* @param {Event} e
* @param {!KeyboardEvent} e
* @private
*/
onCanExecute_: function(e) {
e = /** @type {cr.ui.CanExecuteEvent} */ (e);
switch (e.command.id) {
case 'undo-command':
e.canExecute = this.$.toolbar.canUndo();
break;
case 'clear-all-command':
e.canExecute = this.$.toolbar.canClearAll();
break;
onKeyDown_: function(e) {
let clearAllKey = 'c';
// <if expr="is_macosx">
// On Mac, pressing alt+c produces 'ç' as |event.key|.
clearAllKey = 'ç';
// </if>
if (e.key === clearAllKey && e.altKey && !e.ctrlKey && !e.shiftKey &&
!e.metaKey) {
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();
}
}
},
/**
* @param {Event} e
* @private
*/
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();
/** @private */
onClearAllCommand_() {
if (!this.$.toolbar.canClearAll()) {
return;
}
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 */
......@@ -233,10 +261,6 @@ cr.define('downloads', function() {
* @private
*/
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();
return this.loaded_.promise;
},
......
......@@ -136,12 +136,21 @@ suite('manager tests', function() {
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();
assertFalse(toastManager.isToastOpen);
const event = new Event('command', {bubbles: true});
event.command = {id: 'clear-all-command'};
manager.dispatchEvent(event);
// Simulate 'alt+c' key combo.
MockInteractions.keyDownOn(document, null, 'alt', cr.isMac ? 'ç' : 'c');
assertTrue(toastManager.isToastOpen);
assertFalse(toastManager.isUndoButtonHidden);
});
......@@ -149,10 +158,10 @@ suite('manager tests', function() {
test('toast is hidden when undo-command is fired', () => {
const toastManager = cr.toastManager.getInstance();
toastManager.show('');
const event = new Event('command', {bubbles: true});
event.command = {id: 'undo-command'};
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);
});
......
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