Page previews should have titles
authorconrad_shultz@apple.com <conrad_shultz@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Nov 2014 05:21:13 +0000 (05:21 +0000)
committerconrad_shultz@apple.com <conrad_shultz@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 24 Nov 2014 05:21:13 +0000 (05:21 +0000)
https://bugs.webkit.org/show_bug.cgi?id=139020

Reviewed by Tim Horton.

* UIProcess/API/Cocoa/WKViewPrivate.h:
Declare -_titleForPreviewOfURL:.

* UIProcess/API/mac/WKView.mm:
(-[WKView _titleForPreviewOfURL:]):
Stub implementation.

* UIProcess/mac/WKActionMenuController.mm:
(+[WKPagePreviewViewController previewPadding]):
Account for the title height.
(-[WKPagePreviewViewController loadView]):
Set correct autoresize masks on some views; create and configure a title view that is
horizontally and vertically centered.
(-[WKActionMenuController pagePreviewViewController:titleForPreviewOfURL:]):
WKPagePreviewViewControllerDelegate method; wrap -[WKView _titleForPreviewOfURL:].

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@176511 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/Cocoa/WKViewPrivate.h
Source/WebKit2/UIProcess/API/mac/WKView.mm
Source/WebKit2/UIProcess/mac/WKActionMenuController.mm

index 2d12a12..857be0c 100644 (file)
@@ -1,3 +1,26 @@
+2014-11-23  Conrad Shultz  <conrad_shultz@apple.com>
+
+        Page previews should have titles
+        https://bugs.webkit.org/show_bug.cgi?id=139020
+
+        Reviewed by Tim Horton.
+
+        * UIProcess/API/Cocoa/WKViewPrivate.h:
+        Declare -_titleForPreviewOfURL:.
+
+        * UIProcess/API/mac/WKView.mm:
+        (-[WKView _titleForPreviewOfURL:]):
+        Stub implementation.
+
+        * UIProcess/mac/WKActionMenuController.mm:
+        (+[WKPagePreviewViewController previewPadding]):
+        Account for the title height.
+        (-[WKPagePreviewViewController loadView]):
+        Set correct autoresize masks on some views; create and configure a title view that is
+        horizontally and vertically centered.
+        (-[WKActionMenuController pagePreviewViewController:titleForPreviewOfURL:]):
+        WKPagePreviewViewControllerDelegate method; wrap -[WKView _titleForPreviewOfURL:].
+
 2014-11-22  Simon Fraser  <simon.fraser@apple.com>
 
         Extend WKRenderObject and WKRenderLayer with some more useful data
index bb0453c..c6ba4c1 100644 (file)
 - (NSArray *)_actionMenuItemsForHitTestResult:(WKHitTestResultRef)hitTestResult withType:(_WKActionMenuType)type defaultActionMenuItems:(NSArray *)defaultMenuItems userData:(WKTypeRef)userData;
 
 - (NSView *)_viewForPreviewingURL:(NSURL *)url initialFrameSize:(NSSize)initialFrameSize;
+- (NSString *)_titleForPreviewOfURL:(NSURL *)url;
 - (void)_finishPreviewingURL:(NSURL *)url withPreviewView:(NSView *)previewView;
 - (void)_handleClickInPreviewView:(NSView *)previewView URL:(NSURL *)url;
 
index 34b8908..35135ec 100644 (file)
@@ -4219,6 +4219,11 @@ static NSString *pathWithUniqueFilenameForPath(NSString *path)
     return nil;
 }
 
+- (NSString *)_titleForPreviewOfURL:(NSURL *)url
+{
+    return nil;
+}
+
 - (void)_finishPreviewingURL:(NSURL *)url withPreviewView:(NSView *)previewView
 {
 }
index a3e4758..74c8c7e 100644 (file)
@@ -72,11 +72,13 @@ using namespace WebKit;
 #if WK_API_ENABLED
 
 static const CGFloat previewViewInset = 3;
+static const CGFloat previewViewTitleHeight = 34;
 
 @class WKPagePreviewViewController;
 
 @protocol WKPagePreviewViewControllerDelegate <NSObject>
 - (NSView *)pagePreviewViewController:(WKPagePreviewViewController *)pagePreviewViewController viewForPreviewingURL:(NSURL *)url initialFrameSize:(NSSize)initialFrameSize;
+- (NSString *)pagePreviewViewController:(WKPagePreviewViewController *)pagePreviewViewController titleForPreviewOfURL:(NSURL *)url;
 - (void)pagePreviewViewControllerWasClicked:(WKPagePreviewViewController *)pagePreviewViewController;
 @end
 
@@ -111,7 +113,7 @@ static const CGFloat previewViewInset = 3;
 
 + (NSSize)previewPadding
 {
-    return NSMakeSize(2 * previewViewInset, 2 * previewViewInset);
+    return NSMakeSize(2 * previewViewInset, previewViewTitleHeight + 2 * previewViewInset);
 }
 
 - (void)loadView
@@ -133,13 +135,43 @@ static const CGFloat previewViewInset = 3;
 
     NSRect previewFrame = [_previewView frame];
     NSRect containerFrame = previewFrame;
-    containerFrame.size.width += 2 * previewViewInset;
-    containerFrame.size.height += 2 * previewViewInset;
+    NSSize totalPadding = [[self class] previewPadding];
+    containerFrame.size.width += totalPadding.width;
+    containerFrame.size.height += totalPadding.height;
     previewFrame = NSOffsetRect(previewFrame, previewViewInset, previewViewInset);
 
     RetainPtr<NSView> containerView = adoptNS([[NSView alloc] initWithFrame:containerFrame]);
+    [containerView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
     [containerView addSubview:_previewView.get()];
     [_previewView setFrame:previewFrame];
+    [_previewView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
+
+    RetainPtr<NSTextField> titleTextField = adoptNS([[NSTextField alloc] init]);
+    [titleTextField setWantsLayer:YES];
+    [titleTextField setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin];
+    [titleTextField setEditable:NO];
+    [titleTextField setBezeled:NO];
+    [titleTextField setDrawsBackground:NO];
+    [titleTextField setAlignment:NSCenterTextAlignment];
+    [titleTextField setUsesSingleLineMode:YES];
+    [titleTextField setLineBreakMode:NSLineBreakByTruncatingTail];
+    [titleTextField setTextColor:[NSColor labelColor]];
+
+    NSString *title = [_delegate pagePreviewViewController:self titleForPreviewOfURL:_url.get()];
+    if (!title)
+        title = [_url absoluteString];
+
+    [titleTextField setStringValue:title ? title : @""];
+
+    [titleTextField sizeToFit];
+    NSSize titleFittingSize = [titleTextField frame].size;
+    CGFloat textFieldCenteringOffset = (NSMaxY(containerFrame) - NSMaxY(previewFrame) - titleFittingSize.height) / 2;
+
+    NSRect titleFrame = previewFrame;
+    titleFrame.size.height = titleFittingSize.height;
+    titleFrame.origin.y = NSMaxY(previewFrame) + textFieldCenteringOffset;
+    [titleTextField setFrame:titleFrame];
+    [containerView addSubview:titleTextField.get()];
 
     // Setting the webView bounds will scale it to 75% of the _mainViewSize.
     [_previewView setBounds:NSMakeRect(0, 0, _mainViewSize.width / _popoverToViewScale, _mainViewSize.height / _popoverToViewScale)];
@@ -1087,6 +1119,11 @@ static NSString *pathToPhotoOnDisk(NSString *suggestedFilename)
     return [_wkView _viewForPreviewingURL:url initialFrameSize:initialFrameSize];
 }
 
+- (NSString *)pagePreviewViewController:(WKPagePreviewViewController *)pagePreviewViewController titleForPreviewOfURL:(NSURL *)url
+{
+    return [_wkView _titleForPreviewOfURL:url];
+}
+
 - (void)pagePreviewViewControllerWasClicked:(WKPagePreviewViewController *)pagePreviewViewController
 {
     if (NSURL *url = pagePreviewViewController->_url.get())