Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] Simplify API to implement zoom in custom viewers #18179

Merged
merged 2 commits into from
May 28, 2024

Conversation

nicolo-ribaudo
Copy link
Contributor

@nicolo-ribaudo nicolo-ribaudo commented May 28, 2024

Closes #18076

This PR extends the zoom/scale API to simplify the logic in custom viewer wrappers:

  • the first commit provides an updateScale function that does both increaseScale/decreaseScale. Ad you can see from the commit itself, it significantly reduces code duplication both in embedders (see the changes in web/app.js) and in web/pdf_viewer.js itself
  • the second commits moves the "preserve zoom origin when zooming" logic from the app to the viewer, since embedders that want to implement pinch-to-zoom or zoom-around-cursor would need to also implement it (and figuring out exactly what the logic needs to be can be challenging). Part of the "scroll after zoom" logic was already in the viewer, now all of it is roughly in the same place.

Note that this PR increases the net number of lines because it adds a test, but ignoring the new test it would be +56 -95.

@Snuffleupagus In #18098 (comment) you said that you probably do not want updateScale, but I still uploaded it since it de-duplicates a significant amount of code. What do you think about it? (the second commit could be done independently from the first one)


Commits:

Unify increaseScale/decreaseScale logic as updateScale

updateScale receives a drawingDelay, a scaleFactor and/or a number of steps.
If scaleFactor is a positive number different from 1 the current scale is multiplied by
that number. Otherwise, if steps if a positive integer the current scale is multiplied by
DEFAULT_SCALE_DELTA steps times. Finally, if steps is a negative integer, the
current scale is divided by DEFAULT_SCALE_DELTA abs(steps) times.

Add origin parameter to updateScale

This parameter allows defining which point should remain
fixed while scaling the document. It can be used, for example,
to implement "zoom around the cursor" or "zoom around
pinch center".

The logic was previously implemented in web/app.js, but
moving it to the viewer scaling utilities themselves makes it
easier to implement similar zooming functionalities in
other embedders.

@nicolo-ribaudo nicolo-ribaudo changed the title Simplify API to implement zoom in custom viewers [api-minor] Simplify API to implement zoom in custom viewers May 28, 2024
web/pdf_viewer.js Outdated Show resolved Hide resolved
@nicolo-ribaudo nicolo-ribaudo force-pushed the zooming-utilities branch 2 times, most recently from 3d6ce52 to b65ba0d Compare May 28, 2024 11:15
@Snuffleupagus
Copy link
Collaborator

In #18098 (comment) you said that you probably do not want updateScale, but I still uploaded it since it de-duplicates a significant amount of code. What do you think about it?

Looking at a working patch, written by an actual human, I'm happy to retract that statement since it's clear that it avoids a bunch of code duplication :-)

@Snuffleupagus
Copy link
Collaborator

/botio integrationtest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/41d5c1423b09441/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.193.163.58:8877/c713b44aab198d2/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/41d5c1423b09441/output.txt

Total script time: 7.85 mins

  • Integration Tests: Passed

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/c713b44aab198d2/output.txt

Total script time: 16.62 mins

  • Integration Tests: FAILED

@nicolo-ribaudo
Copy link
Contributor Author

nicolo-ribaudo commented May 28, 2024

This is the test failing on windows (line 727 is throwing):

it("must check that a resized stamp has its canvas at the right position", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await switchToStamp(page);
await copyImage(page, "../images/firefox_logo.png", 0);
await page.waitForSelector(getEditorSelector(0));
await waitForSerialized(page, 1);
const serializedRect = await getFirstSerialized(page, x => x.rect);
const rect = await getRect(page, ".resizer.bottomRight");
const centerX = rect.x + rect.width / 2;
const centerY = rect.y + rect.height / 2;
await page.mouse.move(centerX, centerY);
await page.mouse.down();
await page.mouse.move(centerX - 500, centerY - 500);
await page.mouse.up();

It is failing because the .move() on line 727 is moving to outside of the viewport. It seems unrelated, because that test doesn't change zoom in any way, however looking at recent CI bot I couldn't find any similar failures 🤔

web/pdf_viewer.js Outdated Show resolved Hide resolved
`updateScale` receives a `drawingDelay`, a `scaleFactor` and/or a number of `steps`.
If `scaleFactor` is a positive number different from `1` the current scale is multiplied by
that number. Otherwise, if `steps` if a positive integer the current scale is multiplied by
`DEFAULT_SCALE_DELTA` `steps` times. Finally, if `steps` is a negative integer, the
current scale is divided by `DEFAULT_SCALE_DELTA` `abs(steps)` times.
@Snuffleupagus
Copy link
Collaborator

/botio integrationtest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/1a80676073ff22b/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.193.163.58:8877/aa3933c8fd7da6b/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/1a80676073ff22b/output.txt

Total script time: 7.82 mins

  • Integration Tests: Passed

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Success

Full output at http://54.193.163.58:8877/aa3933c8fd7da6b/output.txt

Total script time: 19.04 mins

  • Integration Tests: Passed

@Snuffleupagus
Copy link
Collaborator

/botio-linux preview

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_preview from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/55892e089776b04/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/55892e089776b04/output.txt

Total script time: 1.17 mins

Published

@Snuffleupagus
Copy link
Collaborator

/botio integrationtest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/8b3e402acee8dcf/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.193.163.58:8877/1724685ff947a26/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/8b3e402acee8dcf/output.txt

Total script time: 7.75 mins

  • Integration Tests: Passed

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Success

Full output at http://54.193.163.58:8877/1724685ff947a26/output.txt

Total script time: 18.98 mins

  • Integration Tests: Passed

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me, with a couple of final comments; thank you!

web/pdf_viewer.js Outdated Show resolved Hide resolved
web/pdf_viewer.js Outdated Show resolved Hide resolved
This parameter allows defining which point should remain
fixed while scaling the document. It can be used, for example,
to implement "zoom around the cursor" or "zoom around
pinch center".

The logic was previously implemented in `web/app.js`, but
moving it to the viewer scaling utilities themselves makes it
easier to implement similar zooming functionalities in
other embedders.
@Snuffleupagus
Copy link
Collaborator

/botio-linux integrationtest

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_integrationtest from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.241.84.105:8877/d288c01ae5dfbfa/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/d288c01ae5dfbfa/output.txt

Total script time: 7.82 mins

  • Integration Tests: Passed

@Snuffleupagus Snuffleupagus merged commit 0cec644 into mozilla:master May 28, 2024
7 checks passed
@nicolo-ribaudo
Copy link
Contributor Author

Thanks for the quick review!

@nicolo-ribaudo nicolo-ribaudo deleted the zooming-utilities branch May 28, 2024 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Improve scaling API to simplify zoom implementation in embedders
4 participants