Skip to content

Commit

Permalink
Unified PDF: HUD zoom buttons don't work on embedded pdfs
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=270536
rdar://122034458

Reviewed by Abrar Rahman Protyasha.

* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.h:
Document the scale factors.

* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.mm:
(WebKit::UnifiedPDFPlugin::ensureLayers):
Always apply the "page scale" (which is actually determined by us, not the page)
to the root layer, even for embedded plugins.

(WebKit::UnifiedPDFPlugin::setScaleFactor):
(WebKit::UnifiedPDFPlugin::performContextMenuAction):
(WebKit::UnifiedPDFPlugin::zoomIn):
(WebKit::UnifiedPDFPlugin::zoomOut):
(WebKit::UnifiedPDFPlugin::resetZoom):
(WebKit::UnifiedPDFPlugin::updateLayout):
Factor setScaleFactor (for the UnifiedPDF-owned scale) out, and adopt it internally.

(WebKit::UnifiedPDFPlugin::setPageScaleFactor):
For actual page scale changes, bail if we are an embedded plugin. We don't want
the PDF content to follow page scale changes for embeds, only for the main frame,
where page scale and UnifiedPDF-scale are synchronized.

* Source/WebKit/WebProcess/Plugins/PluginView.cpp:
(WebKit::PluginView::setPageScaleFactor):
(WebKit::PluginView::pluginScaleFactorDidChange):
Factor pluginScaleFactorDidChange out of setPageScaleFactor so that we can
call it from setScaleFactor in UnifiedPDFPlugin instead of having to round-trip
through PluginView::setPageScaleFactor, which allows us to correctly identify
UnifiedPDFPlugin-originated scales vs. changes to the main frame page scale.

* Source/WebKit/WebProcess/Plugins/PluginView.h:

Canonical link: https://commits.webkit.org/275712@main
  • Loading branch information
hortont424 committed Mar 5, 2024
1 parent c0e1cd6 commit 2bcded2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 17 deletions.
18 changes: 18 additions & 0 deletions Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,30 @@ class UnifiedPDFPlugin final : public PDFPluginBase, public WebCore::GraphicsLay
float initialScale() const;
float scaleForFitToView() const;

/*
Unified PDF Plugin scales, in depth order:
- "device": same as the rest of WebKit. CSS-to-screen pixel ratio.
- "page": the scale of the WebPage.
- "scale factor": the user's chosen scale for the PDF contents (by zoom buttons, pinch-zoom, etc.).
for main frame plugins, this is synced with the page scale
for embedded plugins, this is on top of the page scale
- "document layout scale": the scale between contents and document space, to fit the pages in the scroll view's contents
Convenience names:
- "contentScaleFactor": the scale between the plugin and document space (scaleFactor * document layout scale)
*/
CGFloat scaleFactor() const override;
float contentScaleFactor() const final;

void didBeginMagnificationGesture() override;
void didEndMagnificationGesture() override;
void setPageScaleFactor(double scale, std::optional<WebCore::IntPoint> origin) final;
void setScaleFactor(double scale, std::optional<WebCore::IntPoint> origin = std::nullopt);

WebCore::IntSize documentSize() const;
WebCore::IntSize contentsSize() const override;
Expand Down
38 changes: 24 additions & 14 deletions Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,7 @@ static String mutationObserverNotificationString()
m_rootLayer = createGraphicsLayer("UnifiedPDFPlugin root"_s, GraphicsLayer::Type::Normal);
m_rootLayer->setAnchorPoint({ });
m_rootLayer->setBackgroundColor(WebCore::roundAndClampToSRGBALossy([WebCore::CocoaColor grayColor].CGColor));
if (handlesPageScaleFactor())
m_rootLayer->setAppliesPageScale();
m_rootLayer->setAppliesPageScale();
}

if (!m_scrollContainerLayer) {
Expand Down Expand Up @@ -894,13 +893,8 @@ static String mutationObserverNotificationString()
m_rootLayer->noteDeviceOrPageScaleFactorChangedIncludingDescendants();
}

void UnifiedPDFPlugin::setPageScaleFactor(double scale, std::optional<WebCore::IntPoint> origin)
void UnifiedPDFPlugin::setScaleFactor(double scale, std::optional<WebCore::IntPoint> origin)
{
if (!handlesPageScaleFactor()) {
m_rootLayer->noteDeviceOrPageScaleFactorChangedIncludingDescendants();
return;
}

RefPtr page = this->page();
if (!page)
return;
Expand All @@ -911,7 +905,7 @@ static String mutationObserverNotificationString()
updateScrollbars();
updateScrollingExtents();

if (!m_inMagnificationGesture)
if (!m_inMagnificationGesture || !handlesPageScaleFactor())
m_rootLayer->noteDeviceOrPageScaleFactorChangedIncludingDescendants();

updateLayerPositions();
Expand Down Expand Up @@ -939,6 +933,22 @@ static String mutationObserverNotificationString()
page->protectedScrollingCoordinator()->requestScrollToPosition(*this, newPosition, options);

scheduleRenderingUpdate();

m_view->pluginScaleFactorDidChange();
}

void UnifiedPDFPlugin::setPageScaleFactor(double scale, std::optional<WebCore::IntPoint> origin)
{
if (!handlesPageScaleFactor()) {
m_rootLayer->noteDeviceOrPageScaleFactorChangedIncludingDescendants();
return;
}

RefPtr page = this->page();
if (!page)
return;

setScaleFactor(scale, origin);
}

bool UnifiedPDFPlugin::geometryDidChange(const IntSize& pluginSize, const AffineTransform& pluginToRootViewTransform)
Expand Down Expand Up @@ -1002,7 +1012,7 @@ static String mutationObserverNotificationString()
if (shouldAdjustScale == AdjustScaleAfterLayout::Yes && m_view) {
auto initialScaleFactor = initialScale();
LOG_WITH_STREAM(PDF, stream << "UnifiedPDFPlugin::updateLayout - on first layout, chose scale for actual size " << initialScaleFactor);
m_view->setPageScaleFactor(initialScaleFactor, std::nullopt);
setScaleFactor(initialScaleFactor);
}
}

Expand Down Expand Up @@ -2336,7 +2346,7 @@ static bool isContextMenuEvent(const WebMouseEvent& event)
zoomOut();
break;
case ContextMenuItemTag::ActualSize:
m_view->setPageScaleFactor(scaleForActualSize(), std::nullopt);
setScaleFactor(scaleForActualSize());
break;
default:
RELEASE_ASSERT_NOT_REACHED();
Expand Down Expand Up @@ -2997,18 +3007,18 @@ static NSStringCompareOptions compareOptionsForFindOptions(WebCore::FindOptions
void UnifiedPDFPlugin::zoomIn()
{
m_documentLayout.setShouldUpdateAutoSizeScale(PDFDocumentLayout::ShouldUpdateAutoSizeScale::No);
m_view->setPageScaleFactor(std::clamp(m_scaleFactor * zoomIncrement, minimumZoomScale, maximumZoomScale), std::nullopt);
setScaleFactor(std::clamp(m_scaleFactor * zoomIncrement, minimumZoomScale, maximumZoomScale));
}

void UnifiedPDFPlugin::zoomOut()
{
m_documentLayout.setShouldUpdateAutoSizeScale(PDFDocumentLayout::ShouldUpdateAutoSizeScale::No);
m_view->setPageScaleFactor(std::clamp(m_scaleFactor / zoomIncrement, minimumZoomScale, maximumZoomScale), std::nullopt);
setScaleFactor(std::clamp(m_scaleFactor / zoomIncrement, minimumZoomScale, maximumZoomScale));
}

void UnifiedPDFPlugin::resetZoom()
{
m_view->setPageScaleFactor(initialScale(), { });
setScaleFactor(initialScale());
}

#endif // ENABLE(PDF_HUD)
Expand Down
12 changes: 9 additions & 3 deletions Source/WebKit/WebProcess/Plugins/PluginView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,7 @@ void PluginView::setPageScaleFactor(double scaleFactor, std::optional<IntPoint>
if (!m_isInitialized)
return;

RefPtr webPage = m_webPage.get();
webPage->send(Messages::WebPageProxy::PluginScaleFactorDidChange(scaleFactor));
webPage->send(Messages::WebPageProxy::PluginZoomFactorDidChange(scaleFactor));
pluginScaleFactorDidChange();
protectedPlugin()->setPageScaleFactor(scaleFactor, origin);
}

Expand All @@ -357,6 +355,14 @@ double PluginView::pageScaleFactor() const
return protectedPlugin()->scaleFactor();
}

void PluginView::pluginScaleFactorDidChange()
{
auto scaleFactor = pageScaleFactor();
RefPtr webPage = m_webPage.get();
webPage->send(Messages::WebPageProxy::PluginScaleFactorDidChange(scaleFactor));
webPage->send(Messages::WebPageProxy::PluginZoomFactorDidChange(scaleFactor));
}

void PluginView::webPageDestroyed()
{
m_webPage = nullptr;
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/WebProcess/Plugins/PluginView.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class PluginView final : public WebCore::PluginViewBase {
void didEndMagnificationGesture();
void setPageScaleFactor(double, std::optional<WebCore::IntPoint> origin);
double pageScaleFactor() const;
void pluginScaleFactorDidChange();

void topContentInsetDidChange();

Expand Down

0 comments on commit 2bcded2

Please sign in to comment.