Commit 48e1c5c5 authored by rbpotter's avatar rbpotter Committed by Commit Bot

PDF Viewer: Make zoom field respond to input

Bug: 1105701
Change-Id: I426c7fb28e5bf599879668ecb8cbf5865769bbfa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2309453
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatardpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791152}
parent 363c59a5
...@@ -124,7 +124,9 @@ ...@@ -124,7 +124,9 @@
<cr-icon-button iron-icon="pdf:remove" title="$i18n{tooltipZoomOut}" <cr-icon-button iron-icon="pdf:remove" title="$i18n{tooltipZoomOut}"
on-click="onZoomOutClick_"> on-click="onZoomOutClick_">
</cr-icon-button> </cr-icon-button>
<input type="text" value="100%"></input> <input type="text" value="100%" on-input="onZoomInput_"
on-blur="onZoomInputBlur_">
</input>
<cr-icon-button iron-icon="pdf:add" title="$i18n{tooltipZoomIn}" <cr-icon-button iron-icon="pdf:add" title="$i18n{tooltipZoomIn}"
on-click="onZoomInClick_"> on-click="onZoomInClick_">
</cr-icon-button> </cr-icon-button>
......
...@@ -98,6 +98,9 @@ export class ViewerPdfToolbarNewElement extends PolymerElement { ...@@ -98,6 +98,9 @@ export class ViewerPdfToolbarNewElement extends PolymerElement {
/** @private {boolean} */ /** @private {boolean} */
this.loading_ = true; this.loading_ = true;
/** @private {?number} */
this.zoomTimeout_ = null;
} }
/** /**
...@@ -128,7 +131,7 @@ export class ViewerPdfToolbarNewElement extends PolymerElement { ...@@ -128,7 +131,7 @@ export class ViewerPdfToolbarNewElement extends PolymerElement {
/** @private */ /** @private */
viewportZoomChanged_() { viewportZoomChanged_() {
const zoom = Math.round(this.viewportZoom * 100); const zoom = Math.round(this.viewportZoom * 100);
this.shadowRoot.querySelector('#zoom-controls input').value = `${zoom}%`; this.getZoomInput_().value = `${zoom}%`;
} }
// <if expr="chromeos"> // <if expr="chromeos">
...@@ -184,6 +187,52 @@ export class ViewerPdfToolbarNewElement extends PolymerElement { ...@@ -184,6 +187,52 @@ export class ViewerPdfToolbarNewElement extends PolymerElement {
this.fitToggle(); this.fitToggle();
} }
/**
* @return {!HTMLInputElement}
* @private
*/
getZoomInput_() {
return /** @type {!HTMLInputElement} */ (
this.shadowRoot.querySelector('#zoom-controls input'));
}
/** @private */
onZoomInput_() {
if (this.zoomTimeout_) {
clearTimeout(this.zoomTimeout_);
}
this.zoomTimeout_ = setTimeout(() => this.sendZoomChanged_(), 250);
}
/**
* @return {boolean} Whether the zoom-changed event was sent.
* @private
*/
sendZoomChanged_() {
this.zoomTimeout_ = null;
const value = Number.parseInt(this.getZoomInput_().value, 10);
if (Number.isNaN(value)) {
return false;
}
this.dispatchEvent(new CustomEvent('zoom-changed', {detail: value}));
return true;
}
/** @private */
onZoomInputBlur_() {
if (this.zoomTimeout_) {
clearTimeout(this.zoomTimeout_);
}
if (this.sendZoomChanged_()) {
return;
}
const zoom = Math.round(this.viewportZoom * 100);
const zoomString = `${zoom}%`;
this.getZoomInput_().value = zoomString;
}
/** @private */ /** @private */
onMoreClick_() { onMoreClick_() {
const menu = this.shadowRoot.querySelector('cr-action-menu'); const menu = this.shadowRoot.querySelector('cr-action-menu');
......
...@@ -62,6 +62,7 @@ ...@@ -62,6 +62,7 @@
on-change-page="onChangePage_" on-change-page="onChangePage_"
on-dropdown-opened="onDropdownOpened_" on-dropdown-opened="onDropdownOpened_"
on-fit-to-changed="onFitToChanged" on-zoom-in="onZoomIn" on-fit-to-changed="onFitToChanged" on-zoom-in="onZoomIn"
on-zoom-changed="onZoomChanged"
on-zoom-out="onZoomOut" on-rotate-left="rotateCounterclockwise" on-zoom-out="onZoomOut" on-rotate-left="rotateCounterclockwise"
<if expr="chromeos"> <if expr="chromeos">
on-annotation-mode-toggled="onAnnotationModeToggled_" on-annotation-mode-toggled="onAnnotationModeToggled_"
......
...@@ -604,6 +604,14 @@ export class PDFViewerBaseElement extends PolymerElement { ...@@ -604,6 +604,14 @@ export class PDFViewerBaseElement extends PolymerElement {
PDFMetrics.recordZoomAction(/*isZoomIn=*/ true); PDFMetrics.recordZoomAction(/*isZoomIn=*/ true);
} }
/**
* @param {!CustomEvent<number>} e
* @protected
*/
onZoomChanged(e) {
this.viewport_.setZoom(e.detail / 100);
}
/** @protected */ /** @protected */
onZoomOut() { onZoomOut() {
this.viewport_.zoomOut(); this.viewport_.zoomOut();
......
...@@ -125,6 +125,27 @@ const tests = [ ...@@ -125,6 +125,27 @@ const tests = [
toolbar.addEventListener('rotate-left', e => chrome.test.succeed()); toolbar.addEventListener('rotate-left', e => chrome.test.succeed());
rotateButton.click(); rotateButton.click();
}, },
function testZoomField() {
const toolbar = createToolbar();
toolbar.viewportZoom = .8;
const zoomField = toolbar.shadowRoot.querySelector('#zoom-controls input');
chrome.test.assertEq('80%', zoomField.value);
// Value is set based on viewport zoom.
toolbar.viewportZoom = .533;
chrome.test.assertEq('53%', zoomField.value);
// Setting a new value sends the value in a zoom-changed event.
let sentValue = -1;
toolbar.addEventListener('zoom-changed', e => {
sentValue = e.detail;
chrome.test.assertEq(110, sentValue);
chrome.test.succeed();
});
zoomField.value = '110%';
zoomField.dispatchEvent(new CustomEvent('input'));
}
]; ];
chrome.test.runTests(tests); chrome.test.runTests(tests);
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