-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move downloads button/action menu handling to its own child element. This is in preparation for re-using the new element in the new PDF toolbar. Bug: 1100427 Change-Id: I891ab8d198ba30a6ee362988a8768fe199079e74 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2276500 Commit-Queue: Rebekah Potter <rbpotter@chromium.org> Reviewed-by: dpapad <dpapad@chromium.org> Cr-Commit-Position: refs/heads/master@{#785560}
- Loading branch information
rbpotter
authored and
Commit Bot
committed
Jul 6, 2020
1 parent
5f083e7
commit 1e29d75
Showing
18 changed files
with
605 additions
and
371 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,13 @@ | ||
<template> | ||
<style> | ||
cr-icon-button { | ||
--cr-icon-button-fill-color: var(--pdf-toolbar-text-color); | ||
margin: 0; | ||
} | ||
|
||
cr-icon-button:hover { | ||
background: rgba(255, 255, 255, 0.08); | ||
border-radius: 50%; | ||
} | ||
</style> | ||
</template> |
This file contains 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,11 @@ | ||
// Copyright 2020 The Chromium 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 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; | ||
|
||
const template = document.createElement('template'); | ||
template.innerHTML = ` | ||
<dom-module id="pdf-shared">{__html_template__}</dom-module> | ||
`; | ||
document.body.appendChild(template.content.cloneNode(true)); |
13 changes: 13 additions & 0 deletions
13
chrome/browser/resources/pdf/elements/viewer-download-controls.html
This file contains 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,13 @@ | ||
<style include="pdf-shared"></style> | ||
<cr-icon-button id="download" iron-icon="cr:file-download" | ||
on-click="onDownloadClick_" aria-label$="$i18n{tooltipDownload}" | ||
aria-haspopup$="[[downloadHasPopup_]]" | ||
title="$i18n{tooltipDownload}"></cr-icon-button> | ||
<cr-action-menu id="menu"> | ||
<button class="dropdown-item" on-click="onDownloadEditedClick_"> | ||
$i18n{downloadEdited} | ||
</button> | ||
<button class="dropdown-item" on-click="onDownloadOriginalClick_"> | ||
$i18n{downloadOriginal} | ||
</button> | ||
</cr-action-menu> |
179 changes: 179 additions & 0 deletions
179
chrome/browser/resources/pdf/elements/viewer-download-controls.js
This file contains 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,179 @@ | ||
// Copyright 2020 The Chromium 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 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.m.js'; | ||
import 'chrome://resources/cr_elements/icons.m.js'; | ||
import './icons.js'; | ||
import './shared-css.js'; | ||
|
||
import {AnchorAlignment} from 'chrome://resources/cr_elements/cr_action_menu/cr_action_menu.m.js'; | ||
import {PromiseResolver} from 'chrome://resources/js/promise_resolver.m.js'; | ||
import {html, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; | ||
|
||
import {SaveRequestType} from '../constants.js'; | ||
|
||
export class ViewerDownloadControlsElement extends PolymerElement { | ||
static get is() { | ||
return 'viewer-download-controls'; | ||
} | ||
|
||
static get template() { | ||
return html`{__html_template__}`; | ||
} | ||
|
||
static get properties() { | ||
return { | ||
hasEdits: Boolean, | ||
|
||
hasEnteredAnnotationMode: Boolean, | ||
|
||
isFormFieldFocused: { | ||
type: Boolean, | ||
observer: 'onFormFieldFocusedChanged_', | ||
}, | ||
|
||
pdfFormSaveEnabled: Boolean, | ||
|
||
downloadHasPopup_: { | ||
type: String, | ||
computed: 'computeDownloadHasPopup_(' + | ||
'pdfFormSaveEnabled, hasEdits, hasEnteredAnnotationMode)', | ||
}, | ||
}; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
|
||
// Polymer properties | ||
/** @private {string} */ | ||
this.downloadHasPopup_; | ||
|
||
/** @type {boolean} */ | ||
this.hasEdits; | ||
|
||
/** @type {boolean} */ | ||
this.hasEnteredAnnotationMode; | ||
|
||
/** @type {boolean} */ | ||
this.isFormFieldFocused; | ||
|
||
/** @type {boolean} */ | ||
this.pdfFormSaveEnabled; | ||
|
||
// Non-Polymer properties | ||
/** @private {?PromiseResolver<boolean>} */ | ||
this.waitForFormFocusChange_ = null; | ||
} | ||
|
||
/** @return {boolean} */ | ||
isMenuOpen() { | ||
return this.getDownloadMenu_().open; | ||
} | ||
|
||
closeMenu() { | ||
this.getDownloadMenu_().close(); | ||
} | ||
|
||
/** | ||
* @return {boolean} | ||
* @private | ||
*/ | ||
hasEditsToSave_() { | ||
return this.hasEnteredAnnotationMode || | ||
(this.pdfFormSaveEnabled && this.hasEdits); | ||
} | ||
|
||
/** | ||
* @return {string} The value for the aria-haspopup attribute for the download | ||
* button. | ||
* @private | ||
*/ | ||
computeDownloadHasPopup_() { | ||
return this.hasEditsToSave_() ? 'menu' : 'false'; | ||
} | ||
|
||
/** | ||
* @return {!CrActionMenuElement} | ||
* @private | ||
*/ | ||
getDownloadMenu_() { | ||
return /** @type {!CrActionMenuElement} */ ( | ||
this.shadowRoot.querySelector('#menu')); | ||
} | ||
|
||
/** @private */ | ||
showDownloadMenu_() { | ||
this.getDownloadMenu_().showAt(this.$.download, { | ||
anchorAlignmentX: AnchorAlignment.CENTER, | ||
anchorAlignmentY: AnchorAlignment.AFTER_END, | ||
noOffset: true, | ||
}); | ||
// For tests | ||
this.dispatchEvent(new CustomEvent( | ||
'download-menu-shown-for-testing', {bubbles: true, composed: true})); | ||
} | ||
|
||
/** @private */ | ||
onDownloadClick_() { | ||
this.waitForEdits_().then(hasEdits => { | ||
if (hasEdits) { | ||
this.showDownloadMenu_(); | ||
} else { | ||
this.dispatchSaveEvent_(SaveRequestType.ORIGINAL); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @return {!Promise<boolean>} Promise that resolves with true if the PDF has | ||
* edits and/or annotations, and false otherwise. | ||
* @private | ||
*/ | ||
waitForEdits_() { | ||
if (this.hasEditsToSave_()) { | ||
return Promise.resolve(true); | ||
} | ||
if (!this.isFormFieldFocused || !this.pdfFormSaveEnabled) { | ||
return Promise.resolve(false); | ||
} | ||
this.waitForFormFocusChange_ = new PromiseResolver(); | ||
return this.waitForFormFocusChange_.promise; | ||
} | ||
|
||
/** @private */ | ||
onFormFieldFocusedChanged_() { | ||
if (!this.waitForFormFocusChange_) { | ||
return; | ||
} | ||
|
||
this.waitForFormFocusChange_.resolve(this.hasEdits); | ||
this.waitForFormFocusChange_ = null; | ||
} | ||
|
||
/** | ||
* @param {!SaveRequestType} type | ||
* @private | ||
*/ | ||
dispatchSaveEvent_(type) { | ||
this.dispatchEvent( | ||
new CustomEvent('save', {detail: type, bubbles: true, composed: true})); | ||
} | ||
|
||
/** @private */ | ||
onDownloadOriginalClick_() { | ||
this.dispatchSaveEvent_(SaveRequestType.ORIGINAL); | ||
this.getDownloadMenu_().close(); | ||
} | ||
|
||
/** @private */ | ||
onDownloadEditedClick_() { | ||
this.dispatchSaveEvent_( | ||
this.hasEnteredAnnotationMode ? SaveRequestType.ANNOTATION : | ||
SaveRequestType.EDITED); | ||
this.getDownloadMenu_().close(); | ||
} | ||
} | ||
customElements.define( | ||
ViewerDownloadControlsElement.is, ViewerDownloadControlsElement); |
This file contains 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 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 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
Oops, something went wrong.