Skip to content

Commit

Permalink
[UnifiedPDF] Text annotations do not have blue background on hover
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=268497
rdar://problem/122041412

Reviewed by Tim Horton.

In order to display the blue rectangle that appears when a mouse cursor
is over a text annotation we must first start keeping track of that
in AnnotationTrackingState. We can simply add a new field there that
keepts track of this and can be queried later on when we need to check
if we need to paint it over the text annotation field.

startAnnotationTracking will set this field if the mouse event type
is a mouse move and no mouse button was pressed. This bit will get
reset when the client determines the mouse has moved off the text
annotation and calls finishAnnotationTracking().

If the mouse cursor is hovering over a text annotation and the left
mouse button is clicked to bring up the HTML input, then the blue
rectangle should still appear after the text has been inputted and if
the cursor did not move off the annotation. If the cursor moves off
the annotation then the blue rectangle should not appear the text has
been inputted even if it is brought back on top of the cursor while the
HTML input is up.

* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.h:
(WebKit::AnnotationTrackingState::trackedAnnotation const):
* Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.mm:
(WebKit::UnifiedPDFPlugin::paintPDFContent):
(WebKit::textAnnotationHoverColor):
(WebKit::UnifiedPDFPlugin::paintPDFOverlays):
(WebKit::UnifiedPDFPlugin::handleMouseEvent):
(WebKit::AnnotationTrackingState::startAnnotationTracking):
(WebKit::AnnotationTrackingState::finishAnnotationTracking):
(WebKit::AnnotationTrackingState::handleMouseDraggedOffTrackedAnnotation):
(WebKit::AnnotationTrackingState::isBeingHovered const):
(WebKit::AnnotationTrackingState::resetAnnotationTrackingState):

Canonical link: https://commits.webkit.org/274272@main
  • Loading branch information
sammygill committed Feb 8, 2024
1 parent 2133d1c commit 8147bb3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ class AnnotationTrackingState {
void startAnnotationTracking(RetainPtr<PDFAnnotation>&&, const WebEventType&, const WebMouseEventButton&);
void finishAnnotationTracking(const WebEventType&, const WebMouseEventButton&);
const PDFAnnotation *trackedAnnotation() const { return m_trackedAnnotation.get(); }
bool isBeingHovered() const;
private:
void handleMouseDraggedOffTrackedAnnotation();
void resetAnnotationTrackingState();
RetainPtr<PDFAnnotation> m_trackedAnnotation;
bool m_isBeingHovered { false };
};

enum class WebEventModifier : uint8_t;
Expand Down Expand Up @@ -211,6 +214,7 @@ class UnifiedPDFPlugin final : public PDFPluginBase, public WebCore::GraphicsLay
bool layerNeedsPlatformContext(const WebCore::GraphicsLayer*) const override { return true; }

void paintPDFContent(WebCore::GraphicsContext&, const WebCore::FloatRect& clipRect);
void paintPDFOverlays(WebCore::GraphicsContext&);
void ensureLayers();
void updateLayerHierarchy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ static String mutationObserverNotificationString()
isVisibleAndActive = page->isVisibleAndActive();
[m_currentSelection drawForPage:page.get() withBox:kCGPDFCropBox active:isVisibleAndActive inContext:context.platformContext()];
}
paintPDFOverlays(context);
}

float UnifiedPDFPlugin::scaleForActualSize() const
Expand Down Expand Up @@ -464,6 +465,19 @@ static String mutationObserverNotificationString()
return 1;
}

static const WebCore::Color textAnnotationHoverColor()
{
static constexpr auto textAnnotationHoverAlpha = 0.12;
static NeverDestroyed color = WebCore::Color::createAndPreserveColorSpace([[[WebCore::CocoaColor systemBlueColor] colorWithAlphaComponent:textAnnotationHoverAlpha] CGColor]);
return color;
}

void UnifiedPDFPlugin::paintPDFOverlays(WebCore::GraphicsContext& context)
{
if (auto* trackedAnnotation = m_annotationTrackingState.trackedAnnotation(); trackedAnnotation && [trackedAnnotation isKindOfClass:getPDFAnnotationTextWidgetClass()] && m_annotationTrackingState.isBeingHovered())
context.fillRect(IntRect { [trackedAnnotation bounds] }, textAnnotationHoverColor());
}

CGFloat UnifiedPDFPlugin::scaleFactor() const
{
return m_scaleFactor;
Expand Down Expand Up @@ -1072,10 +1086,19 @@ static AffineTransform documentSpaceToPageSpaceTransform(const IntDegrees& pageR
auto altKeyIsActive = event.altKey() ? AltKeyIsActive::Yes : AltKeyIsActive::No;
auto pdfElementTypes = pdfElementTypesForPluginPoint(lastKnownMousePositionInView());
notifyCursorChanged(toWebCoreCursorType(pdfElementTypes, altKeyIsActive));
if (m_annotationTrackingState.trackedAnnotation()) {

RetainPtr annotationUnderMouse = annotationForRootViewPoint(event.position());
bool annotationTrackingStateChanged = false;
if (auto* currentTrackedAnnotation = m_annotationTrackingState.trackedAnnotation(); (currentTrackedAnnotation && currentTrackedAnnotation != annotationUnderMouse) || (currentTrackedAnnotation && !m_annotationTrackingState.isBeingHovered())) {
m_annotationTrackingState.finishAnnotationTracking(mouseEventType, mouseEventButton);
updateLayerHierarchy();
annotationTrackingStateChanged = true;
}
if (!m_annotationTrackingState.trackedAnnotation() && annotationUnderMouse && [annotationUnderMouse isKindOfClass:getPDFAnnotationTextWidgetClass()]) {
m_annotationTrackingState.startAnnotationTracking(WTFMove(annotationUnderMouse), mouseEventType, mouseEventButton);
annotationTrackingStateChanged = true;
}
if (annotationTrackingStateChanged)
updateLayerHierarchy();
return true;
}
case WebMouseEventButton::Left: {
Expand Down Expand Up @@ -1130,7 +1153,7 @@ static AffineTransform documentSpaceToPageSpaceTransform(const IntDegrees& pageR

switch (auto mouseEventButton = event.button()) {
case WebMouseEventButton::Left:
if (RetainPtr trackedAnnotation = m_annotationTrackingState.trackedAnnotation()) {
if (RetainPtr trackedAnnotation = m_annotationTrackingState.trackedAnnotation(); trackedAnnotation && ![trackedAnnotation isKindOfClass:getPDFAnnotationTextWidgetClass()]) {
m_annotationTrackingState.finishAnnotationTracking(mouseEventType, mouseEventButton);

if ([trackedAnnotation isKindOfClass:getPDFAnnotationLinkClass()])
Expand Down Expand Up @@ -1523,6 +1546,8 @@ static IntRect computeMarqueeSelectionRect(const WebCore::IntPoint& point1, cons

if ([m_trackedAnnotation isKindOfClass:getPDFAnnotationButtonWidgetClass()])
[m_trackedAnnotation setHighlighted:YES];
if (mouseEventType == WebEventType::MouseMove && mouseEventButton == WebMouseEventButton::None)
m_isBeingHovered = true;
}

void AnnotationTrackingState::finishAnnotationTracking(const WebEventType& mouseEventType, const WebMouseEventButton& mouseEventButton)
Expand All @@ -1541,15 +1566,27 @@ static IntRect computeMarqueeSelectionRect(const WebCore::IntPoint& point1, cons
}
} else if (mouseEventType == WebEventType::MouseMove && mouseEventButton == WebMouseEventButton::Left)
handleMouseDraggedOffTrackedAnnotation();
m_trackedAnnotation = nullptr;
resetAnnotationTrackingState();
}

void AnnotationTrackingState::handleMouseDraggedOffTrackedAnnotation()
{
ASSERT(m_trackedAnnotation);

[m_trackedAnnotation setHighlighted:NO];
}

bool AnnotationTrackingState::isBeingHovered() const
{
ASSERT(m_trackedAnnotation);
return m_isBeingHovered;
}

void AnnotationTrackingState::resetAnnotationTrackingState()
{
ASSERT(m_trackedAnnotation);
m_trackedAnnotation = nullptr;
m_isBeingHovered = false;
}

void UnifiedPDFPlugin::attemptToUnlockPDF(const String& password)
Expand Down

0 comments on commit 8147bb3

Please sign in to comment.