Title: [175710] trunk/Source
Revision
175710
Author
bda...@apple.com
Date
2014-11-06 13:03:36 -0800 (Thu, 06 Nov 2014)

Log Message

Preview views often misplaced inside popover
https://bugs.webkit.org/show_bug.cgi?id=138472

Reviewed by Tim Horton.

Source/WebCore:

* WebCore.exp.in:

Source/WebKit2:

This patch makes the popover maintain aspect ratio, and it uses the final scale of 
that popover to scale the preview view as well.

* UIProcess/mac/WKActionMenuController.mm:
(-[WKPagePreviewViewController initWithPageURL:mainViewSize:popoverToViewScale:]):
(-[WKPagePreviewViewController loadView]):
(-[WKActionMenuController _createPreviewPopoverForURL:originRect:]):
(-[WKActionMenuController _targetSize:fitsInAvailableSpace:]):
(-[WKActionMenuController _preferredSizeForPopoverPresentedFromOriginRect:]):
(-[WKPagePreviewViewController initWithPageURL:]): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (175709 => 175710)


--- trunk/Source/WebCore/ChangeLog	2014-11-06 20:37:04 UTC (rev 175709)
+++ trunk/Source/WebCore/ChangeLog	2014-11-06 21:03:36 UTC (rev 175710)
@@ -1,3 +1,12 @@
+2014-11-06  Beth Dakin  <bda...@apple.com>
+
+        Preview views often misplaced inside popover
+        https://bugs.webkit.org/show_bug.cgi?id=138472
+
+        Reviewed by Tim Horton.
+
+        * WebCore.exp.in:
+
 2014-11-06  Michael Saboff  <msab...@apple.com>
 
         REGRESSION (r174985-174986): Site display disappears 

Modified: trunk/Source/WebCore/WebCore.exp.in (175709 => 175710)


--- trunk/Source/WebCore/WebCore.exp.in	2014-11-06 20:37:04 UTC (rev 175709)
+++ trunk/Source/WebCore/WebCore.exp.in	2014-11-06 21:03:36 UTC (rev 175710)
@@ -1088,6 +1088,7 @@
 __ZN7WebCore31equalIgnoringFragmentIdentifierERKNS_3URLES2_
 __ZN7WebCore33deleteAllCookiesModifiedAfterDateERKNS_21NetworkStorageSessionEd
 __ZN7WebCore33stripLeadingAndTrailingHTMLSpacesERKN3WTF6StringE
+__ZN7WebCore36largestRectWithAspectRatioInsideRectEfRKNS_9FloatRectE
 __ZN7WebCore36standardUserAgentWithApplicationNameERKN3WTF6StringES3_
 __ZN7WebCore37WidgetHierarchyUpdatesSuspensionScope11moveWidgetsEv
 __ZN7WebCore37WidgetHierarchyUpdatesSuspensionScope35s_widgetHierarchyUpdateSuspendCountE

Modified: trunk/Source/WebKit2/ChangeLog (175709 => 175710)


--- trunk/Source/WebKit2/ChangeLog	2014-11-06 20:37:04 UTC (rev 175709)
+++ trunk/Source/WebKit2/ChangeLog	2014-11-06 21:03:36 UTC (rev 175710)
@@ -1,3 +1,21 @@
+2014-11-06  Beth Dakin  <bda...@apple.com>
+
+        Preview views often misplaced inside popover
+        https://bugs.webkit.org/show_bug.cgi?id=138472
+
+        Reviewed by Tim Horton.
+
+        This patch makes the popover maintain aspect ratio, and it uses the final scale of 
+        that popover to scale the preview view as well.
+
+        * UIProcess/mac/WKActionMenuController.mm:
+        (-[WKPagePreviewViewController initWithPageURL:mainViewSize:popoverToViewScale:]):
+        (-[WKPagePreviewViewController loadView]):
+        (-[WKActionMenuController _createPreviewPopoverForURL:originRect:]):
+        (-[WKActionMenuController _targetSize:fitsInAvailableSpace:]):
+        (-[WKActionMenuController _preferredSizeForPopoverPresentedFromOriginRect:]):
+        (-[WKPagePreviewViewController initWithPageURL:]): Deleted.
+
 2014-11-06  Tim Horton  <timothy_hor...@apple.com>
 
         TextIndicatorWindow's bounce animation is lopsided when indicating two short lines of text

Modified: trunk/Source/WebKit2/UIProcess/mac/WKActionMenuController.mm (175709 => 175710)


--- trunk/Source/WebKit2/UIProcess/mac/WKActionMenuController.mm	2014-11-06 20:37:04 UTC (rev 175709)
+++ trunk/Source/WebKit2/UIProcess/mac/WKActionMenuController.mm	2014-11-06 21:03:36 UTC (rev 175710)
@@ -43,6 +43,7 @@
 #import <ImageIO/ImageIO.h>
 #import <ImageKit/ImageKit.h>
 #import <WebCore/DataDetectorsSPI.h>
+#import <WebCore/GeometryUtilities.h>
 #import <WebCore/NSSharingServiceSPI.h>
 #import <WebCore/NSSharingServicePickerSPI.h>
 #import <WebCore/NSViewSPI.h>
@@ -73,8 +74,6 @@
 
 #if WK_API_ENABLED
 
-static const CGFloat popoverToViewScale = 0.75;
-
 @class WKPagePreviewViewController;
 
 @protocol WKPagePreviewViewControllerDelegate <NSObject>
@@ -86,21 +85,23 @@
     NSSize _mainViewSize;
     RetainPtr<NSURL> _url;
     id <WKPagePreviewViewControllerDelegate> _delegate;
+    CGFloat _popoverToViewScale;
 }
 
-- (instancetype)initWithPageURL:(NSURL *)URL;
+- (instancetype)initWithPageURL:(NSURL *)URL mainViewSize:(NSSize)size popoverToViewScale:(CGFloat)scale;
 
 @end
 
 @implementation WKPagePreviewViewController
 
-- (instancetype)initWithPageURL:(NSURL *)URL
+- (instancetype)initWithPageURL:(NSURL *)URL mainViewSize:(NSSize)size popoverToViewScale:(CGFloat)scale
 {
     if (!(self = [super init]))
         return nil;
 
     _url = URL;
-    _mainViewSize = NSMakeSize(320, 568);
+    _mainViewSize = size;
+    _popoverToViewScale = scale;
 
     return self;
 }
@@ -115,7 +116,7 @@
     }
 
     // Setting the webView bounds will scale it to 75% of the _mainViewSize. 
-    [webView setBounds:NSMakeRect(0, 0, _mainViewSize.width / popoverToViewScale, _mainViewSize.height / popoverToViewScale)];
+    [webView setBounds:NSMakeRect(0, 0, _mainViewSize.width / _popoverToViewScale, _mainViewSize.height / _popoverToViewScale)];
 
     RetainPtr<NSClickGestureRecognizer> clickRecognizer = adoptNS([[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(_clickRecognized:)]);
     [webView addGestureRecognizer:clickRecognizer.get()];
@@ -309,38 +310,60 @@
 
 - (void)_createPreviewPopoverForURL:(NSURL *)url originRect:(NSRect)originRect
 {
-    RetainPtr<WKPagePreviewViewController> previewViewController = adoptNS([[WKPagePreviewViewController alloc] initWithPageURL:url]);
-    previewViewController->_mainViewSize = _wkView.bounds.size;
+    NSSize popoverSize = [self _preferredSizeForPopoverPresentedFromOriginRect:originRect];
+    CGFloat actualPopoverToViewScale = popoverSize.width / NSWidth(_wkView.bounds);
+    RetainPtr<WKPagePreviewViewController> previewViewController = adoptNS([[WKPagePreviewViewController alloc] initWithPageURL:url mainViewSize:_wkView.bounds.size popoverToViewScale:actualPopoverToViewScale]);
     previewViewController->_delegate = self;
 
     _previewPopover = adoptNS([[NSPopover alloc] init]);
     [_previewPopover setBehavior:NSPopoverBehaviorTransient];
-    [_previewPopover setContentSize:[self _preferredSizeForPopoverPresentedFromOriginRect:originRect]];
+    [_previewPopover setContentSize:popoverSize];
     [_previewPopover setContentViewController:previewViewController.get()];
     [_previewPopover setDelegate:self];
 }
 
+static bool targetSizeFitsInAvailableSpace(NSSize targetSize, NSSize availableSpace)
+{
+    return targetSize.width <= availableSpace.width && targetSize.height <= availableSpace.height;
+}
+
 - (NSSize)_preferredSizeForPopoverPresentedFromOriginRect:(NSRect)originRect
 {
+    static const CGFloat preferredPopoverToViewScale = 0.75;
     static const CGFloat screenPadding = 40;
 
     NSWindow *window = _wkView.window;
     NSRect originScreenRect = [window convertRectToScreen:[_wkView convertRect:originRect toView:nil]];
     NSRect screenFrame = window.screen.visibleFrame;
 
+    NSRect wkViewBounds = _wkView.bounds;
+    NSSize targetSize = NSMakeSize(NSWidth(wkViewBounds) * preferredPopoverToViewScale, NSHeight(wkViewBounds) * preferredPopoverToViewScale);
+
     CGFloat availableSpaceAbove = NSMaxY(screenFrame) - NSMaxY(originScreenRect);
     CGFloat availableSpaceBelow = NSMinY(originScreenRect) - NSMinY(screenFrame);
     CGFloat maxAvailableVerticalSpace = fmax(availableSpaceAbove, availableSpaceBelow) - screenPadding;
+    NSSize maxSpaceAvailableOnYEdge = NSMakeSize(screenFrame.size.width - screenPadding, maxAvailableVerticalSpace);
+    if (targetSizeFitsInAvailableSpace(targetSize, maxSpaceAvailableOnYEdge))
+        return targetSize;
 
     CGFloat availableSpaceAtLeft = NSMinX(originScreenRect) - NSMinX(screenFrame);
     CGFloat availableSpaceAtRight = NSMaxX(screenFrame) - NSMaxX(originScreenRect);
     CGFloat maxAvailableHorizontalSpace = fmax(availableSpaceAtLeft, availableSpaceAtRight) - screenPadding;
+    NSSize maxSpaceAvailableOnXEdge = NSMakeSize(maxAvailableHorizontalSpace, screenFrame.size.height - screenPadding);
+    if (targetSizeFitsInAvailableSpace(targetSize, maxSpaceAvailableOnXEdge))
+        return targetSize;
 
-    NSRect wkViewBounds = _wkView.bounds;
-    NSSize preferredSize = NSMakeSize(NSWidth(wkViewBounds) * popoverToViewScale, NSHeight(wkViewBounds) * popoverToViewScale);
-    preferredSize.width = fmin(preferredSize.width, maxAvailableHorizontalSpace);
-    preferredSize.height = fmin(preferredSize.height, maxAvailableVerticalSpace);
-    return preferredSize;
+    // If the target size doesn't fit anywhere, we'll find the largest rect that does fit that also maintains the original view's aspect ratio.
+    CGFloat aspectRatio = wkViewBounds.size.width / wkViewBounds.size.height;
+    FloatRect maxVerticalTargetSizePreservingAspectRatioRect = largestRectWithAspectRatioInsideRect(aspectRatio, FloatRect(0, 0, maxSpaceAvailableOnYEdge.width, maxSpaceAvailableOnYEdge.height));
+    FloatRect maxHorizontalTargetSizePreservingAspectRatioRect = largestRectWithAspectRatioInsideRect(aspectRatio, FloatRect(0, 0, maxSpaceAvailableOnXEdge.width, maxSpaceAvailableOnXEdge.height));
+
+    NSSize maxVerticalTargetSizePreservingAspectRatio = NSMakeSize(maxVerticalTargetSizePreservingAspectRatioRect.width(), maxVerticalTargetSizePreservingAspectRatioRect.height());
+    NSSize maxHortizontalTargetSizePreservingAspectRatio = NSMakeSize(maxHorizontalTargetSizePreservingAspectRatioRect.width(), maxHorizontalTargetSizePreservingAspectRatioRect.height());
+
+    if ((maxVerticalTargetSizePreservingAspectRatio.width * maxVerticalTargetSizePreservingAspectRatio.height) > (maxHortizontalTargetSizePreservingAspectRatio.width * maxHortizontalTargetSizePreservingAspectRatio.height))
+        return maxVerticalTargetSizePreservingAspectRatio;
+    return maxHortizontalTargetSizePreservingAspectRatio;
 }
 
 #endif // WK_API_ENABLED
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to