Commit 1b3e4010 authored by dpapad's avatar dpapad Committed by Commit Bot

PDF Viewer update: Adjust zoom level when PDF area resizes

In the new UI layout, the PDF area can be resized in two ways

1) expanding/collapsing the sidenav
2) resizing the entire window

Because of #1, the previous approach of listening for the Window's
'resize' event is no longer sufficient. Therefore a ResizeObserver
is used which works for both #1 and #2.

Bug: 1121351
Change-Id: I6fd895a55e4630f9562c8d8ea38dc7d4fc6ebb75
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2391335Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Commit-Queue: dpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808469}
parent 9c43aeef
...@@ -107,8 +107,17 @@ ...@@ -107,8 +107,17 @@
z-index: initial; z-index: initial;
} }
/* #main and #scroller are intentionally different elements so that a
* ResizeObserver can be used on #main without triggering
* "ResizeObserver loop limit exceeded".
*/
#main { #main {
flex: 1; flex: 1;
overflow: hidden;
}
#scroller {
height: 100%;
overflow: auto; overflow: auto;
position: relative; position: relative;
} }
...@@ -181,10 +190,12 @@ ...@@ -181,10 +190,12 @@
on-navigate="onNavigate_"> on-navigate="onNavigate_">
</viewer-pdf-sidenav> </viewer-pdf-sidenav>
</div> </div>
<div id="main" on-scroll="onScroll_"> <div id="main">
<div id="scroller" on-scroll="onScroll_">
<div id="sizer"></div> <div id="sizer"></div>
<div id="content"></div> <div id="content"></div>
</div> </div>
<div>
</div> </div>
</template> </template>
......
...@@ -188,19 +188,26 @@ export class Viewport { ...@@ -188,19 +188,26 @@ export class Viewport {
// Set to a default zoom manager - used in tests. // Set to a default zoom manager - used in tests.
this.setZoomManager(new InactiveZoomManager(this.getZoom.bind(this), 1)); this.setZoomManager(new InactiveZoomManager(this.getZoom.bind(this), 1));
if (this.window_ === document.documentElement) { // Case where |chrome_pdf::features::kPDFViewerUpdate| is disabled.
if (this.window_ === document.documentElement ||
// Necessary check since during testing a fake DOM element is used.
!(this.window_ instanceof HTMLElement)) {
window.addEventListener('scroll', this.updateViewport_.bind(this)); window.addEventListener('scroll', this.updateViewport_.bind(this));
// The following line is only used in tests, since they expect // The following line is only used in tests, since they expect
// |scrollCallback| to be called on the mock |window_| object (legacy). // |scrollCallback| to be called on the mock |window_| object (legacy).
this.window_.scrollCallback = this.updateViewport_.bind(this); this.window_.scrollCallback = this.updateViewport_.bind(this);
} else {
this.window_.addEventListener('scroll', this.updateViewport_.bind(this));
}
window.addEventListener('resize', this.resizeWrapper_.bind(this)); window.addEventListener('resize', this.resizeWrapper_.bind(this));
// The following line is only used in tests, since they expect // The following line is only used in tests, since they expect
// |resizeCallback| to be called on the mock |window_| object (legacy). // |resizeCallback| to be called on the mock |window_| object (legacy).
this.window_.resizeCallback = this.resizeWrapper_.bind(this); this.window_.resizeCallback = this.resizeWrapper_.bind(this);
} else {
// Case where |chrome_pdf::features::kPDFViewerUpdate| is enabled.
this.window_.addEventListener('scroll', this.updateViewport_.bind(this));
const resizeObserver = new ResizeObserver(_ => this.resizeWrapper_());
const target = this.window_.parentElement;
assert(target.id === 'main');
resizeObserver.observe(target);
}
document.body.addEventListener( document.body.addEventListener(
'change-zoom', e => this.setZoom(e.detail.zoom)); 'change-zoom', e => this.setZoom(e.detail.zoom));
......
...@@ -62,7 +62,7 @@ chrome.test.runTests([ ...@@ -62,7 +62,7 @@ chrome.test.runTests([
const updateEnabled = const updateEnabled =
document.documentElement.hasAttribute('pdf-viewer-update-enabled'); document.documentElement.hasAttribute('pdf-viewer-update-enabled');
const scrollingContainer = const scrollingContainer =
updateEnabled ? viewer.shadowRoot.querySelector('#main') : window; updateEnabled ? viewer.shadowRoot.querySelector('#scroller') : window;
scrollingContainer.scrollTo(100, 100); scrollingContainer.scrollTo(100, 100);
await animationFrame(); await animationFrame();
......
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