Title: [134326] trunk/Source/WebKit2
Revision
134326
Author
[email protected]
Date
2012-11-12 16:25:58 -0800 (Mon, 12 Nov 2012)

Log Message

PDFPlugin should use PDFKit's context menu instead of the default WebKit one
https://bugs.webkit.org/show_bug.cgi?id=101896
<rdar://problem/12555173>

Reviewed by Anders Carlsson.

Show the context menu that PDFKit vends us instead of the default WebKit one
when PDFPlugin is told to show a context menu.

Also, react to the didChangeContentScaleFactor callback, so that when the context
menu (or HUD) is used to change the scale factor, we can inform PluginView.

* WebProcess/Plugins/PDF/PDFLayerControllerDetails.h:
Add pdfLayerController:didChangeContentScaleFactor: delegate callback.
Rename tileScaleFactor to contentScaleFactor.
Add rightMouseDown: and menuForEvent: on PDFLayerController.
* WebProcess/Plugins/PDF/PDFPlugin.h:
(PDFPlugin):
Add notifyContentScaleFactorChanged, handleContextMenuEvent, and nsEventForWebMouseEvent.
* WebProcess/Plugins/PDF/PDFPlugin.mm:
(-[WKPDFLayerControllerDelegate pdfLayerController:didChangeContentScaleFactor:]): Forward content scale factor changes to PDFPlugin.
(WebKit::PDFPlugin::pdfDocumentDidLoad): Rename tileScaleFactor to contentScaleFactor.
(WebKit::PDFPlugin::geometryDidChange): Rename tileScaleFactor to contentScaleFactor.
(WebKit::eventTypeFromWebEvent): It turns out we can infer whether the mouse is being dragged during a MouseMove event
by whether it has a mouse button associated with it or not, so we don't need to track this state manually.
(WebKit::PDFPlugin::nsEventForWebMouseEvent): Factor the part of handleMouseEvent that creates NSEvents* out into its own function.
(WebKit::PDFPlugin::handleMouseEvent):
Don't track mouseButtonIsDown manually, make use of the new nsEventForWebMouseEvent.
Hand rightMouseDown: to PDFLayerController instead of mouseDown: if necessary.
(WebKit::PDFPlugin::handleContextMenuEvent): Grab the relevant NSMenu from PDFKit and display it.
(WebKit::PDFPlugin::handleKeyboardEvent): Don't pass a bogus mouseButtonIsDown to eventTypeFromWebEvent.
(WebKit::PDFPlugin::notifyContentScaleFactorChanged): Update PluginView and co.'s notion of page scale when
PDFKit changes it (generally from the context menu/HUD). Also update page size and scrollbar size/position.
(WebKit::PDFPlugin::setScrollOffset): Don't call SimplePDFPlugin's implementation, as PDFKit handles invalidation itself.
* WebProcess/Plugins/PDF/PDFPluginChoiceAnnotation.mm:
(WebKit::PDFPluginChoiceAnnotation::updateGeometry): Rename tileScaleFactor to contentScaleFactor.
* WebProcess/Plugins/PDF/PDFPluginTextAnnotation.mm:
(WebKit::PDFPluginTextAnnotation::updateGeometry): Rename tileScaleFactor to contentScaleFactor.
* WebProcess/Plugins/PDF/SimplePDFPlugin.h: Make m_scrollOffset protected.

Modified Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (134325 => 134326)


--- trunk/Source/WebKit2/ChangeLog	2012-11-13 00:23:18 UTC (rev 134325)
+++ trunk/Source/WebKit2/ChangeLog	2012-11-13 00:25:58 UTC (rev 134326)
@@ -1,3 +1,45 @@
+2012-11-12  Tim Horton  <[email protected]>
+
+        PDFPlugin should use PDFKit's context menu instead of the default WebKit one
+        https://bugs.webkit.org/show_bug.cgi?id=101896
+        <rdar://problem/12555173>
+
+        Reviewed by Anders Carlsson.
+
+        Show the context menu that PDFKit vends us instead of the default WebKit one
+        when PDFPlugin is told to show a context menu.
+
+        Also, react to the didChangeContentScaleFactor callback, so that when the context
+        menu (or HUD) is used to change the scale factor, we can inform PluginView.
+
+        * WebProcess/Plugins/PDF/PDFLayerControllerDetails.h:
+        Add pdfLayerController:didChangeContentScaleFactor: delegate callback.
+        Rename tileScaleFactor to contentScaleFactor.
+        Add rightMouseDown: and menuForEvent: on PDFLayerController.
+        * WebProcess/Plugins/PDF/PDFPlugin.h:
+        (PDFPlugin):
+        Add notifyContentScaleFactorChanged, handleContextMenuEvent, and nsEventForWebMouseEvent.
+        * WebProcess/Plugins/PDF/PDFPlugin.mm:
+        (-[WKPDFLayerControllerDelegate pdfLayerController:didChangeContentScaleFactor:]): Forward content scale factor changes to PDFPlugin.
+        (WebKit::PDFPlugin::pdfDocumentDidLoad): Rename tileScaleFactor to contentScaleFactor.
+        (WebKit::PDFPlugin::geometryDidChange): Rename tileScaleFactor to contentScaleFactor.
+        (WebKit::eventTypeFromWebEvent): It turns out we can infer whether the mouse is being dragged during a MouseMove event
+        by whether it has a mouse button associated with it or not, so we don't need to track this state manually.
+        (WebKit::PDFPlugin::nsEventForWebMouseEvent): Factor the part of handleMouseEvent that creates NSEvents* out into its own function.
+        (WebKit::PDFPlugin::handleMouseEvent):
+        Don't track mouseButtonIsDown manually, make use of the new nsEventForWebMouseEvent.
+        Hand rightMouseDown: to PDFLayerController instead of mouseDown: if necessary.
+        (WebKit::PDFPlugin::handleContextMenuEvent): Grab the relevant NSMenu from PDFKit and display it.
+        (WebKit::PDFPlugin::handleKeyboardEvent): Don't pass a bogus mouseButtonIsDown to eventTypeFromWebEvent.
+        (WebKit::PDFPlugin::notifyContentScaleFactorChanged): Update PluginView and co.'s notion of page scale when
+        PDFKit changes it (generally from the context menu/HUD). Also update page size and scrollbar size/position.
+        (WebKit::PDFPlugin::setScrollOffset): Don't call SimplePDFPlugin's implementation, as PDFKit handles invalidation itself.
+        * WebProcess/Plugins/PDF/PDFPluginChoiceAnnotation.mm:
+        (WebKit::PDFPluginChoiceAnnotation::updateGeometry): Rename tileScaleFactor to contentScaleFactor.
+        * WebProcess/Plugins/PDF/PDFPluginTextAnnotation.mm:
+        (WebKit::PDFPluginTextAnnotation::updateGeometry): Rename tileScaleFactor to contentScaleFactor.
+        * WebProcess/Plugins/PDF/SimplePDFPlugin.h: Make m_scrollOffset protected.
+
 2012-11-12  Anders Carlsson  <[email protected]>
 
         Add a RemoteLayerTreeHost object in the UI process

Modified: trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFLayerControllerDetails.h (134325 => 134326)


--- trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFLayerControllerDetails.h	2012-11-13 00:23:18 UTC (rev 134325)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFLayerControllerDetails.h	2012-11-13 00:25:58 UTC (rev 134326)
@@ -36,6 +36,7 @@
 
 - (void)pdfLayerController:(PDFLayerController *)pdfLayerController didChangeActiveAnnotation:(PDFAnnotation *)annotation;
 - (void)pdfLayerController:(PDFLayerController *)pdfLayerController clickedLinkWithURL:(NSURL *)url;
+- (void)pdfLayerController:(PDFLayerController *)pdfLayerController didChangeContentScaleFactor:(CGFloat)scaleFactor;
 
 @end
 
@@ -53,8 +54,8 @@
 - (void)setDisplayMode:(int)mode;
 - (void)setDisplaysPageBreaks:(BOOL)pageBreaks;
 
-- (CGFloat)tileScaleFactor;
-- (void)setTileScaleFactor:(CGFloat)scaleFactor;
+- (CGFloat)contentScaleFactor;
+- (void)setContentScaleFactor:(CGFloat)scaleFactor;
 
 - (CGFloat)deviceScaleFactor;
 - (void)setDeviceScaleFactor:(CGFloat)scaleFactor;
@@ -71,12 +72,15 @@
 - (void)scrollWithDelta:(CGSize)delta;
 
 - (void)mouseDown:(NSEvent *)event;
+- (void)rightMouseDown:(NSEvent *)event;
 - (void)mouseMoved:(NSEvent *)event;
 - (void)mouseUp:(NSEvent *)event;
 - (void)mouseDragged:(NSEvent *)event;
 - (void)mouseEntered:(NSEvent *)event;
 - (void)mouseExited:(NSEvent *)event;
 
+- (NSMenu *)menuForEvent:(NSEvent *)event;
+
 - (NSArray *)findString:(NSString *)string caseSensitive:(BOOL)isCaseSensitive highlightMatches:(BOOL)shouldHighlightMatches;
 
 - (id)currentSelection;

Modified: trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPlugin.h (134325 => 134326)


--- trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPlugin.h	2012-11-13 00:23:18 UTC (rev 134325)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPlugin.h	2012-11-13 00:25:58 UTC (rev 134326)
@@ -63,6 +63,7 @@
     void setActiveAnnotation(PDFAnnotation *);
     
     using ScrollableArea::notifyScrollPositionChanged;
+    void notifyContentScaleFactorChanged(CGFloat scaleFactor);
 
     void clickedLink(NSURL *);
 
@@ -82,6 +83,7 @@
     virtual void geometryDidChange(const WebCore::IntSize& pluginSize, const WebCore::IntRect& clipRect, const WebCore::AffineTransform& pluginToRootViewTransform) OVERRIDE;
     virtual void contentsScaleFactorChanged(float) OVERRIDE;
     virtual bool handleMouseEvent(const WebMouseEvent&) OVERRIDE;
+    virtual bool handleContextMenuEvent(const WebMouseEvent&) OVERRIDE;
     virtual bool handleKeyboardEvent(const WebKeyboardEvent&) OVERRIDE;
     virtual bool handleEditingCommand(const String& commandName, const String& argument) OVERRIDE;
     virtual bool isEditingCommandEnabled(const String&) OVERRIDE;
@@ -91,7 +93,9 @@
     virtual void setScrollOffset(const WebCore::IntPoint&) OVERRIDE;
     virtual void invalidateScrollbarRect(WebCore::Scrollbar*, const WebCore::IntRect&) OVERRIDE;
     virtual void invalidateScrollCornerRect(const WebCore::IntRect&) OVERRIDE;
-
+    
+    NSEvent *nsEventForWebMouseEvent(const WebMouseEvent&);
+    
     bool supportsForms();
 
     RetainPtr<CALayer> m_containerLayer;

Modified: trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPlugin.mm (134325 => 134326)


--- trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPlugin.mm	2012-11-13 00:23:18 UTC (rev 134325)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPlugin.mm	2012-11-13 00:25:58 UTC (rev 134326)
@@ -59,6 +59,7 @@
 #import <WebCore/RenderBoxModelObject.h>
 #import <WebCore/ScrollAnimator.h>
 #import <WebCore/ScrollbarTheme.h>
+#import <WebKitSystemInterface.h>
 
 using namespace WebCore;
 
@@ -189,6 +190,11 @@
     _pdfPlugin->setActiveAnnotation(annotation);
 }
 
+- (void)pdfLayerController:(PDFLayerController *)pdfLayerController didChangeContentScaleFactor:(CGFloat)scaleFactor
+{
+    _pdfPlugin->notifyContentScaleFactorChanged(scaleFactor);
+}
+
 @end
 
 namespace WebKit {
@@ -296,7 +302,7 @@
     [m_pdfLayerController.get() setDeviceScaleFactor:controller()->contentsScaleFactor()];
     
     if (handlesPageScaleFactor())
-        pluginView()->setPageScaleFactor([m_pdfLayerController.get() tileScaleFactor], IntPoint());
+        pluginView()->setPageScaleFactor([m_pdfLayerController.get() contentScaleFactor], IntPoint());
 
     notifyScrollPositionChanged(IntPoint([m_pdfLayerController.get() scrollPosition]));
 
@@ -322,7 +328,7 @@
 void PDFPlugin::destroy()
 {
     m_pdfLayerController.get().delegate = 0;
-    
+
     if (webFrame()) {
         if (FrameView* frameView = webFrame()->coreFrame()->view())
             frameView->removeScrollableArea(this);
@@ -397,7 +403,7 @@
 
 void PDFPlugin::geometryDidChange(const IntSize& pluginSize, const IntRect&, const AffineTransform& pluginToRootViewTransform)
 {
-    if (size() == pluginSize && pluginView()->pageScaleFactor() == [m_pdfLayerController.get() tileScaleFactor])
+    if (size() == pluginSize && pluginView()->pageScaleFactor() == [m_pdfLayerController.get() contentScaleFactor])
         return;
 
     setSize(pluginSize);
@@ -410,7 +416,7 @@
     transform = CATransform3DTranslate(transform, 0, -pluginSize.height(), 0);
     
     if (handlesPageScaleFactor()) {
-        CGFloat magnification = pluginView()->pageScaleFactor() - [m_pdfLayerController.get() tileScaleFactor];
+        CGFloat magnification = pluginView()->pageScaleFactor() - [m_pdfLayerController.get() contentScaleFactor];
 
         // FIXME: Instead of m_lastMousePoint, we should use the zoom origin from PluginView::setPageScaleFactor.
         if (magnification)
@@ -435,7 +441,7 @@
         | (event.metaKey() ? NSCommandKeyMask : 0);
 }
     
-static NSEventType eventTypeFromWebEvent(const WebEvent& event, bool mouseButtonIsDown)
+static NSEventType eventTypeFromWebEvent(const WebEvent& event)
 {
     switch (event.type()) {
     case WebEvent::KeyDown:
@@ -464,28 +470,45 @@
         }
         break;
     case WebEvent::MouseMove:
-        if (mouseButtonIsDown) {
-            switch (static_cast<const WebMouseEvent&>(event).button()) {
-            case WebMouseEvent::LeftButton:
-                return NSLeftMouseDragged;
-            case WebMouseEvent::RightButton:
-                return NSRightMouseDragged;
-            default:
-                return 0;
-            }
-        } else
+        switch (static_cast<const WebMouseEvent&>(event).button()) {
+        case WebMouseEvent::LeftButton:
+            return NSLeftMouseDragged;
+        case WebMouseEvent::RightButton:
+            return NSRightMouseDragged;
+        case WebMouseEvent::NoButton:
             return NSMouseMoved;
+        default:
+            return 0;
+        }
         break;
 
     default:
         return 0;
     }
 }
+    
+NSEvent *PDFPlugin::nsEventForWebMouseEvent(const WebMouseEvent& event)
+{
+    IntPoint mousePosition = event.position();
 
+    IntPoint positionInPDFView(mousePosition);
+    positionInPDFView = m_rootViewToPluginTransform.mapPoint(positionInPDFView);
+    positionInPDFView.setY(size().height() - positionInPDFView.y());
+
+    m_lastMousePoint = positionInPDFView;
+
+    NSEventType eventType = eventTypeFromWebEvent(event);
+
+    if (!eventType)
+        return 0;
+
+    NSUInteger modifierFlags = modifierFlagsFromWebEvent(event);
+
+    return [NSEvent mouseEventWithType:eventType location:positionInPDFView modifierFlags:modifierFlags timestamp:0 windowNumber:0 context:nil eventNumber:0 clickCount:event.clickCount() pressure:0];
+}
+
 bool PDFPlugin::handleMouseEvent(const WebMouseEvent& event)
 {
-    static bool mouseButtonIsDown;
-
     m_lastMouseEvent = event;
 
     IntPoint mousePosition = event.position();
@@ -496,50 +519,69 @@
         || IntRect(m_scrollCornerLayer.get().frame).contains(mousePosition))
         return false;
 
-    IntPoint positionInPDFView(mousePosition);
-    positionInPDFView = m_rootViewToPluginTransform.mapPoint(positionInPDFView);
-    positionInPDFView.setY(size().height() - positionInPDFView.y());
-    
-    m_lastMousePoint = positionInPDFView;
+    NSEvent *nsEvent = nsEventForWebMouseEvent(event);
 
-    NSEventType eventType = eventTypeFromWebEvent(event, mouseButtonIsDown);
-
-    if (!eventType)
-        return false;
-
-    NSUInteger modifierFlags = modifierFlagsFromWebEvent(event);
-
-    NSEvent *fakeEvent = [NSEvent mouseEventWithType:eventType location:positionInPDFView modifierFlags:modifierFlags timestamp:0 windowNumber:0 context:nil eventNumber:0 clickCount:event.clickCount() pressure:0];
-
     switch (event.type()) {
     case WebEvent::MouseMove:
-        if (mouseButtonIsDown)
-            [m_pdfLayerController.get() mouseDragged:fakeEvent];
-        else
-            [m_pdfLayerController.get() mouseMoved:fakeEvent];
         mouseMovedInContentArea();
-        return true;
-    case WebEvent::MouseDown: {
-        mouseButtonIsDown = true;
-        [m_pdfLayerController.get() mouseDown:fakeEvent];
-        return true;
-    }
-    case WebEvent::MouseUp: {
-        [m_pdfLayerController.get() mouseUp:fakeEvent];
-        mouseButtonIsDown = false;
-        PlatformMouseEvent platformEvent = platform(event);
-        return true;
-    }
+
+        switch (event.button()) {
+        case WebMouseEvent::LeftButton:
+            [m_pdfLayerController.get() mouseDragged:nsEvent];
+            return true;
+        case WebMouseEvent::RightButton:
+        case WebMouseEvent::MiddleButton:
+            return false;
+        case WebMouseEvent::NoButton:
+            [m_pdfLayerController.get() mouseMoved:nsEvent];
+            return true;
+        }
+    case WebEvent::MouseDown:
+        switch (event.button()) {
+        case WebMouseEvent::LeftButton:
+            [m_pdfLayerController.get() mouseDown:nsEvent];
+            return true;
+        case WebMouseEvent::RightButton:
+            [m_pdfLayerController.get() rightMouseDown:nsEvent];
+            return true;
+        case WebMouseEvent::MiddleButton:
+        case WebMouseEvent::NoButton:
+            return false;
+        }
+    case WebEvent::MouseUp:
+        switch (event.button()) {
+        case WebMouseEvent::LeftButton:
+            [m_pdfLayerController.get() mouseUp:nsEvent];
+            return true;
+        case WebMouseEvent::RightButton:
+        case WebMouseEvent::MiddleButton:
+        case WebMouseEvent::NoButton:
+            return false;
+        }
     default:
         break;
     }
-        
+
     return false;
 }
+    
+bool PDFPlugin::handleContextMenuEvent(const WebMouseEvent& event)
+{
+    NSMenu *nsMenu = [m_pdfLayerController.get() menuForEvent:nsEventForWebMouseEvent(event)];
 
+    FrameView* frameView = webFrame()->coreFrame()->view();
+    IntPoint point = frameView->contentsToScreen(IntRect(event.position(), IntSize())).location();
+    if (nsMenu) {
+        WKPopupContextMenu(nsMenu, point);
+        return true;
+    }
+    
+    return false;
+}
+
 bool PDFPlugin::handleKeyboardEvent(const WebKeyboardEvent& event)
 {
-    NSEventType eventType = eventTypeFromWebEvent(event, false);
+    NSEventType eventType = eventTypeFromWebEvent(event);
     NSUInteger modifierFlags = modifierFlagsFromWebEvent(event);
     
     NSEvent *fakeEvent = [NSEvent keyEventWithType:eventType location:NSZeroPoint modifierFlags:modifierFlags timestamp:0 windowNumber:0 context:0 characters:event.text() charactersIgnoringModifiers:event.unmodifiedText() isARepeat:event.isAutoRepeat() keyCode:event.nativeVirtualKeyCode()];
@@ -577,7 +619,7 @@
 
 void PDFPlugin::setScrollOffset(const IntPoint& offset)
 {
-    SimplePDFPlugin::setScrollOffset(offset);
+    m_scrollOffset = IntSize(offset.x(), offset.y());
 
     [CATransaction begin];
     [m_pdfLayerController.get() setScrollPosition:offset];
@@ -638,6 +680,15 @@
     return webFrame()->isMainFrame();
 }
 
+void PDFPlugin::notifyContentScaleFactorChanged(CGFloat scaleFactor)
+{
+    if (handlesPageScaleFactor())
+        pluginView()->setPageScaleFactor(scaleFactor, IntPoint());
+
+    calculateSizes();
+    updateScrollbars();
+}
+
 } // namespace WebKit
 
 #endif // ENABLE(PDFKIT_PLUGIN)

Modified: trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPluginChoiceAnnotation.mm (134325 => 134326)


--- trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPluginChoiceAnnotation.mm	2012-11-13 00:23:18 UTC (rev 134325)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPluginChoiceAnnotation.mm	2012-11-13 00:25:58 UTC (rev 134326)
@@ -56,7 +56,7 @@
     PDFPluginAnnotation::updateGeometry();
 
     StyledElement* styledElement = static_cast<StyledElement*>(element());
-    styledElement->setInlineStyleProperty(CSSPropertyFontSize, choiceAnnotation().font.pointSize * pdfLayerController().tileScaleFactor, CSSPrimitiveValue::CSS_PX);
+    styledElement->setInlineStyleProperty(CSSPropertyFontSize, choiceAnnotation().font.pointSize * pdfLayerController().contentScaleFactor, CSSPrimitiveValue::CSS_PX);
 }
 
 void PDFPluginChoiceAnnotation::commit()

Modified: trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPluginTextAnnotation.mm (134325 => 134326)


--- trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPluginTextAnnotation.mm	2012-11-13 00:23:18 UTC (rev 134325)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PDF/PDFPluginTextAnnotation.mm	2012-11-13 00:25:58 UTC (rev 134326)
@@ -107,7 +107,7 @@
     PDFPluginAnnotation::updateGeometry();
 
     StyledElement* styledElement = static_cast<StyledElement*>(element());
-    styledElement->setInlineStyleProperty(CSSPropertyFontSize, textAnnotation().font.pointSize * pdfLayerController().tileScaleFactor, CSSPrimitiveValue::CSS_PX);
+    styledElement->setInlineStyleProperty(CSSPropertyFontSize, textAnnotation().font.pointSize * pdfLayerController().contentScaleFactor, CSSPrimitiveValue::CSS_PX);
 }
 
 void PDFPluginTextAnnotation::commit()

Modified: trunk/Source/WebKit2/WebProcess/Plugins/PDF/SimplePDFPlugin.h (134325 => 134326)


--- trunk/Source/WebKit2/WebProcess/Plugins/PDF/SimplePDFPlugin.h	2012-11-13 00:23:18 UTC (rev 134325)
+++ trunk/Source/WebKit2/WebProcess/Plugins/PDF/SimplePDFPlugin.h	2012-11-13 00:25:58 UTC (rev 134326)
@@ -171,6 +171,8 @@
 
     virtual bool shouldAllowScripting() OVERRIDE { return false; }
 
+    WebCore::IntSize m_scrollOffset;
+
 private:
 
     JSObjectRef makeJSPDFDoc(JSContextRef);
@@ -191,8 +193,6 @@
     RefPtr<WebCore::Scrollbar> m_verticalScrollbar;
 
     WebFrame* m_frame;
-
-    WebCore::IntSize m_scrollOffset;
 };
 
 } // namespace WebKit
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to