Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: Source/web/WebViewImpl.cpp

Issue 196133011: Refine autozoom scaling constants and logic (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Cleanup Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 175
176 static const double multipleTargetsZoomAnimationDurationInSeconds = 0.25; 176 static const double multipleTargetsZoomAnimationDurationInSeconds = 0.25;
177 static const double findInPageAnimationDurationInSeconds = 0; 177 static const double findInPageAnimationDurationInSeconds = 0;
178 178
179 // Constants for viewport anchoring on resize. 179 // Constants for viewport anchoring on resize.
180 static const float viewportAnchorXCoord = 0.5f; 180 static const float viewportAnchorXCoord = 0.5f;
181 static const float viewportAnchorYCoord = 0; 181 static const float viewportAnchorYCoord = 0;
182 182
183 // Constants for zooming in on a focused text field. 183 // Constants for zooming in on a focused text field.
184 static const double scrollAndScaleAnimationDurationInSeconds = 0.2; 184 static const double scrollAndScaleAnimationDurationInSeconds = 0.2;
185 static const int minReadableCaretHeight = 18; 185 static const int minReadableCaretHeight = 16;
186 static const float minScaleChangeToTriggerZoom = 1.05f; 186 static const float minScaleChangeToTriggerZoom = 1.35f;
187 static const float minScaleChangeFromBlockScaleToTriggerZoom = 2.25f;
187 static const float leftBoxRatio = 0.3f; 188 static const float leftBoxRatio = 0.3f;
188 static const int caretPadding = 10; 189 static const int caretPadding = 10;
189 190
190 namespace blink { 191 namespace blink {
191 192
192 // Change the text zoom level by kTextSizeMultiplierRatio each time the user 193 // Change the text zoom level by kTextSizeMultiplierRatio each time the user
193 // zooms text in or out (ie., change by 20%). The min and max values limit 194 // zooms text in or out (ie., change by 20%). The min and max values limit
194 // text zoom to half and 3x the original text size. These three values match 195 // text zoom to half and 3x the original text size. These three values match
195 // those in Apple's port in WebKit/WebKit/WebView/WebView.mm 196 // those in Apple's port in WebKit/WebKit/WebView/WebView.mm
196 const double WebView::textSizeMultiplierRatio = 1.2; 197 const double WebView::textSizeMultiplierRatio = 1.2;
(...skipping 2326 matching lines...) Expand 10 before | Expand all | Expand 10 after
2523 WebRect caret, unusedEnd; 2524 WebRect caret, unusedEnd;
2524 selectionBounds(caret, unusedEnd); 2525 selectionBounds(caret, unusedEnd);
2525 IntRect unscaledCaret = caret; 2526 IntRect unscaledCaret = caret;
2526 unscaledCaret.scale(1 / pageScaleFactor()); 2527 unscaledCaret.scale(1 / pageScaleFactor());
2527 caret = unscaledCaret; 2528 caret = unscaledCaret;
2528 2529
2529 // Pick a scale which is reasonably readable. This is the scale at which 2530 // Pick a scale which is reasonably readable. This is the scale at which
2530 // the caret height will become minReadableCaretHeight (adjusted for dpi 2531 // the caret height will become minReadableCaretHeight (adjusted for dpi
2531 // and font scale factor). 2532 // and font scale factor).
2532 newScale = clampPageScaleFactorToLimits(legibleScale() * minReadableCaretHei ght / caret.height); 2533 newScale = clampPageScaleFactorToLimits(legibleScale() * minReadableCaretHei ght / caret.height);
2533 const float deltaScale = newScale / pageScaleFactor(); 2534
2535 // Avoiding zooming in further if already zoomed sufficiently within the foc used block.
2536 if (newScale > pageScaleFactor()) {
2537 WebRect blockBounds = computeBlockBounds(textboxRect, false);
2538
2539 float blockScale;
2540 WebPoint blockScroll;
2541 computeScaleAndScrollForBlockRect(textboxRect.location(), blockBounds, t ouchPointPadding, minimumPageScaleFactor() * doubleTapZoomAlreadyLegibleRatio, b lockScale, blockScroll);
aelias_OOO_until_Jul13 2014/03/14 07:16:10 Hmm, this invokes more code than is necessary, par
2542 const float deltaScaleFromBlockScale = newScale / blockScale;
2543 if (pageScaleFactor() > blockScale && deltaScaleFromBlockScale < minScal eChangeFromBlockScaleToTriggerZoom)
2544 newScale = pageScaleFactor();
2545 }
2546
2547 needAnimation = false;
2548 // If we're sufficiently far from the target level, zoom in/out as necessary .
2549 if (newScale / pageScaleFactor() > minScaleChangeToTriggerZoom
2550 || pageScaleFactor() / newScale > minScaleChangeToTriggerZoom) {
2551 needAnimation = true;
2552 } else {
2553 // Avoid changing the scale if it's already sufficiently close to the ta rget.
2554 newScale = pageScaleFactor();
2555 }
2534 2556
2535 // Convert the rects to absolute space in the new scale. 2557 // Convert the rects to absolute space in the new scale.
2536 IntRect textboxRectInDocumentCoordinates = textboxRect; 2558 IntRect textboxRectInDocumentCoordinates = textboxRect;
2537 textboxRectInDocumentCoordinates.move(mainFrame()->scrollOffset()); 2559 textboxRectInDocumentCoordinates.move(mainFrame()->scrollOffset());
2538 IntRect caretInDocumentCoordinates = caret; 2560 IntRect caretInDocumentCoordinates = caret;
2539 caretInDocumentCoordinates.move(mainFrame()->scrollOffset()); 2561 caretInDocumentCoordinates.move(mainFrame()->scrollOffset());
2540 2562
2541 int viewWidth = m_size.width / newScale; 2563 int viewWidth = m_size.width / newScale;
2542 int viewHeight = m_size.height / newScale; 2564 int viewHeight = m_size.height / newScale;
2543 2565
2566 // If the caret is offscreen, then animate.
2567 IntRect sizeRect(0, 0, viewWidth, viewHeight);
2568 if (!sizeRect.contains(caret))
2569 needAnimation = true;
2570
2571 // If the box is partially offscreen and it's possible to bring it fully
2572 // onscreen, then animate.
2573 if (sizeRect.contains(textboxRectInDocumentCoordinates.width(), textboxRectI nDocumentCoordinates.height()) && !sizeRect.contains(textboxRect))
2574 needAnimation = true;
2575
2576 if (!needAnimation)
2577 return;
2578
2544 if (textboxRectInDocumentCoordinates.width() <= viewWidth) { 2579 if (textboxRectInDocumentCoordinates.width() <= viewWidth) {
2545 // Field is narrower than screen. Try to leave padding on left so field' s 2580 // Field is narrower than screen. Try to leave padding on left so field' s
2546 // label is visible, but it's more important to ensure entire field is 2581 // label is visible, but it's more important to ensure entire field is
2547 // onscreen. 2582 // onscreen.
2548 int idealLeftPadding = viewWidth * leftBoxRatio; 2583 int idealLeftPadding = viewWidth * leftBoxRatio;
2549 int maxLeftPaddingKeepingBoxOnscreen = viewWidth - textboxRectInDocument Coordinates.width(); 2584 int maxLeftPaddingKeepingBoxOnscreen = viewWidth - textboxRectInDocument Coordinates.width();
2550 newScroll.setX(textboxRectInDocumentCoordinates.x() - min<int>(idealLeft Padding, maxLeftPaddingKeepingBoxOnscreen)); 2585 newScroll.setX(textboxRectInDocumentCoordinates.x() - min<int>(idealLeft Padding, maxLeftPaddingKeepingBoxOnscreen));
2551 } else { 2586 } else {
2552 // Field is wider than screen. Try to left-align field, unless caret wou ld 2587 // Field is wider than screen. Try to left-align field, unless caret wou ld
2553 // be offscreen, in which case right-align the caret. 2588 // be offscreen, in which case right-align the caret.
2554 newScroll.setX(max<int>(textboxRectInDocumentCoordinates.x(), caretInDoc umentCoordinates.x() + caretInDocumentCoordinates.width() + caretPadding - viewW idth)); 2589 newScroll.setX(max<int>(textboxRectInDocumentCoordinates.x(), caretInDoc umentCoordinates.x() + caretInDocumentCoordinates.width() + caretPadding - viewW idth));
2555 } 2590 }
2556 if (textboxRectInDocumentCoordinates.height() <= viewHeight) { 2591 if (textboxRectInDocumentCoordinates.height() <= viewHeight) {
2557 // Field is shorter than screen. Vertically center it. 2592 // Field is shorter than screen. Vertically center it.
2558 newScroll.setY(textboxRectInDocumentCoordinates.y() - (viewHeight - text boxRectInDocumentCoordinates.height()) / 2); 2593 newScroll.setY(textboxRectInDocumentCoordinates.y() - (viewHeight - text boxRectInDocumentCoordinates.height()) / 2);
2559 } else { 2594 } else {
2560 // Field is taller than screen. Try to top align field, unless caret wou ld 2595 // Field is taller than screen. Try to top align field, unless caret wou ld
2561 // be offscreen, in which case bottom-align the caret. 2596 // be offscreen, in which case bottom-align the caret.
2562 newScroll.setY(max<int>(textboxRectInDocumentCoordinates.y(), caretInDoc umentCoordinates.y() + caretInDocumentCoordinates.height() + caretPadding - view Height)); 2597 newScroll.setY(max<int>(textboxRectInDocumentCoordinates.y(), caretInDoc umentCoordinates.y() + caretInDocumentCoordinates.height() + caretPadding - view Height));
2563 } 2598 }
2564
2565 needAnimation = false;
2566 // If we are at less than the target zoom level, zoom in.
2567 if (deltaScale > minScaleChangeToTriggerZoom)
2568 needAnimation = true;
2569 // If the caret is offscreen, then animate.
2570 IntRect sizeRect(0, 0, viewWidth, viewHeight);
2571 if (!sizeRect.contains(caret))
2572 needAnimation = true;
2573 // If the box is partially offscreen and it's possible to bring it fully
2574 // onscreen, then animate.
2575 if (sizeRect.contains(textboxRectInDocumentCoordinates.width(), textboxRectI nDocumentCoordinates.height()) && !sizeRect.contains(textboxRect))
2576 needAnimation = true;
2577 } 2599 }
2578 2600
2579 void WebViewImpl::advanceFocus(bool reverse) 2601 void WebViewImpl::advanceFocus(bool reverse)
2580 { 2602 {
2581 page()->focusController().advanceFocus(reverse ? FocusTypeBackward : FocusTy peForward); 2603 page()->focusController().advanceFocus(reverse ? FocusTypeBackward : FocusTy peForward);
2582 } 2604 }
2583 2605
2584 double WebViewImpl::zoomLevel() 2606 double WebViewImpl::zoomLevel()
2585 { 2607 {
2586 return m_zoomLevel; 2608 return m_zoomLevel;
(...skipping 1381 matching lines...) Expand 10 before | Expand all | Expand 10 after
3968 const PageScaleConstraints& constraints = m_pageScaleConstraintsSet.pageDefi nedConstraints(); 3990 const PageScaleConstraints& constraints = m_pageScaleConstraintsSet.pageDefi nedConstraints();
3969 3991
3970 if (!mainFrameImpl() || !mainFrameImpl()->frameView()) 3992 if (!mainFrameImpl() || !mainFrameImpl()->frameView())
3971 return false; 3993 return false;
3972 3994
3973 return mainFrameImpl()->frameView()->layoutSize().width() == m_size.width 3995 return mainFrameImpl()->frameView()->layoutSize().width() == m_size.width
3974 || (constraints.minimumScale == constraints.maximumScale && constraints. minimumScale != -1); 3996 || (constraints.minimumScale == constraints.maximumScale && constraints. minimumScale != -1);
3975 } 3997 }
3976 3998
3977 } // namespace blink 3999 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698