Skip to content

Commit

Permalink
PDF Viewer: Refactor downloads code
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 18 changed files with 605 additions and 371 deletions.
6 changes: 6 additions & 0 deletions chrome/browser/pdf/pdf_extension_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,12 @@ IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, Elements) {
RunTestsInJsModule("material_elements_test.js", "test.pdf");
}

IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, DownloadControls) {
// Although this test file does not require a PDF to be loaded, loading the
// elements without loading a PDF is difficult.
RunTestsInJsModule("download_controls_test.js", "test.pdf");
}

IN_PROC_BROWSER_TEST_F(PDFExtensionJSTest, ToolbarManager) {
RunTestsInJsModule("toolbar_manager_test.js", "test.pdf");
}
Expand Down
2 changes: 2 additions & 0 deletions chrome/browser/resources/component_extension_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@
<include name="IDR_PDF_BROWSER_API_JS" file="pdf/browser_api.js" type="BINDATA" />
<include name="IDR_PDF_METRICS_JS" file="pdf/metrics.js" type="BINDATA" />

<include name="IDR_PDF_ELEMENTS_SHARED_CSS_JS" file="${root_gen_dir}/chrome/browser/resources/pdf/elements/shared-css.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_PDF_SHARED_VARS_JS" file="${root_gen_dir}/chrome/browser/resources/pdf/elements/shared-vars.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_PDF_ICONS_JS" file="${root_gen_dir}/chrome/browser/resources/pdf/elements/icons.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_PDF_VIEWER_BOOKMARK_JS" file="${root_gen_dir}/chrome/browser/resources/pdf/elements/viewer-bookmark.js" use_base_dir="false" type="BINDATA" />
<include name="IDR_PDF_VIEWER_DOWNLOADS_JS" file="${root_gen_dir}/chrome/browser/resources/pdf/elements/viewer-download-controls.js" use_base_dir="false" type="BINDATA"/>
<include name="IDR_PDF_VIEWER_ERROR_SCREEN_JS" file="${root_gen_dir}/chrome/browser/resources/pdf/elements/viewer-error-screen.js" use_base_dir="false" type="BINDATA" />
<if expr="chromeos">
<include name="IDR_PDF_VIEWER_INK_CONTROLLER_JS" file="pdf/ink_controller.js" type="BINDATA" />
Expand Down
13 changes: 12 additions & 1 deletion chrome/browser/resources/pdf/elements/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ js_type_check("closure_compile") {
is_polymer3 = true
deps = [
":viewer-bookmark",
":viewer-download-controls",
":viewer-error-screen",
":viewer-page-indicator",
":viewer-page-selector",
Expand All @@ -35,6 +36,14 @@ js_library("viewer-bookmark") {
]
}

js_library("viewer-download-controls") {
deps = [
"..:constants",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
"//ui/webui/resources/cr_elements/cr_action_menu:cr_action_menu.m",
]
}

js_library("viewer-error-screen") {
deps = [ "//ui/webui/resources/cr_elements/cr_dialog:cr_dialog.m" ]
}
Expand Down Expand Up @@ -76,11 +85,11 @@ js_library("viewer-password-screen") {
js_library("viewer-pdf-toolbar") {
deps = [
":viewer-bookmark",
":viewer-download-controls",
":viewer-page-selector",
":viewer-toolbar-dropdown",
"..:annotation_tool",
"..:constants",
"//ui/webui/resources/cr_elements/cr_action_menu:cr_action_menu.m",
"//ui/webui/resources/js:assert.m",
"//ui/webui/resources/js:load_time_data.m",
"//ui/webui/resources/js:promise_resolver.m",
Expand Down Expand Up @@ -117,7 +126,9 @@ html_to_js("web_components") {
js_files = [
"icons.js",
"shared-vars.js",
"shared-css.js",
"viewer-bookmark.js",
"viewer-download-controls.js",
"viewer-error-screen.js",
"viewer-page-indicator.js",
"viewer-page-selector.js",
Expand Down
13 changes: 13 additions & 0 deletions chrome/browser/resources/pdf/elements/shared-css.html
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>
11 changes: 11 additions & 0 deletions chrome/browser/resources/pdf/elements/shared-css.js
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));
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 chrome/browser/resources/pdf/elements/viewer-download-controls.js
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);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<style>
<style include="pdf-shared">
:host {
--pdf-toolbar-background-color: rgb(50, 54, 57);
--pdf-toolbar-text-color: rgb(241, 241, 241);
Expand All @@ -11,16 +11,6 @@
height: 48px;
}

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%;
}

#menuButton {
margin: 0 6px;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'chrome://resources/polymer/v3_0/paper-progress/paper-progress.js';

import './icons.js';
import './viewer-page-selector.js';
import './shared-css.js';

import {html, PolymerElement} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';

Expand Down
35 changes: 9 additions & 26 deletions chrome/browser/resources/pdf/elements/viewer-pdf-toolbar.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<style include="cr-hidden-style">
<style include="pdf-shared cr-hidden-style">
:host {
--pdf-toolbar-background-color: rgb(50, 54, 57);
--pdf-toolbar-text-color: rgb(241, 241, 241);
Expand Down Expand Up @@ -39,16 +39,6 @@
user-select: none;
}

cr-icon-button {
--cr-icon-button-fill-color: var(--pdf-toolbar-text-color);
margin: 6px;
}

cr-icon-button:hover {
background: rgba(255, 255, 255, 0.08);
border-radius: 50%;
}

paper-progress {
--paper-progress-active-color: var(--google-blue-300);
--paper-progress-container-color: transparent;
Expand Down Expand Up @@ -171,7 +161,7 @@

<div id="buttons" class="invisible">
<if expr="chromeos">
<template is="dom-if" if="[[pdfAnnotationsEnabled_]]">
<template is="dom-if" if="[[pdfAnnotationsEnabled]]">
<cr-icon-button id="annotate" iron-icon="pdf:create"
disabled="[[!annotationAvailable]]" on-click="toggleAnnotation"
aria-label$="$i18n{tooltipAnnotate}"
Expand All @@ -183,22 +173,15 @@
disabled="[[annotationMode]]" on-click="rotateRight"
aria-label$="$i18n{tooltipRotateCW}"
title="$i18n{tooltipRotateCW}"></cr-icon-button>

<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="downloadMenu">
<button class="dropdown-item" on-click="onDownloadEditedClick_">
$i18n{downloadEdited}
</button>
<button class="dropdown-item" on-click="onDownloadOriginalClick_">
$i18n{downloadOriginal}
</button>
</cr-action-menu>
<viewer-download-controls id="downloads"
has-edits="[[hasEdits]]"
has-entered-annotation-mode="[[hasEnteredAnnotationMode]]"
is-form-field-focused="[[isFormFieldFocused]]"
pdf-form-save-enabled="[[pdfFormSaveEnabled]]">
</viewer-download-controls>

<cr-icon-button id="print" iron-icon="cr:print" on-click="print"
hidden="[[!printingEnabled_]]" title="$i18n{tooltipPrint}"
hidden="[[!printingEnabled]]" title="$i18n{tooltipPrint}"
aria-label$="$i18n{tooltipPrint}"></cr-icon-button>

<viewer-toolbar-dropdown id="bookmarks"
Expand Down

0 comments on commit 1e29d75

Please sign in to comment.