Skip to content

Commit

Permalink
Clicking on save button dispatches a MouseEvent, button location in e…
Browse files Browse the repository at this point in the history
…lement's saveButtonClientRect

https://bugs.webkit.org/show_bug.cgi?id=252967
rdar://problem/105952126

Reviewed by Tim Nguyen.

Instead of sending a CustomEvent with some hand-picked details, a click on the save button now dispatches a full copy of the original MouseEvent, and the location of the (shadow) save button can be found the element's saveButtonClientRect property.

* Source/WebCore/html/HTMLAttachmentElement.cpp:
(WebCore::HTMLAttachmentElement::saveButtonClientRect const):
* Source/WebCore/html/HTMLAttachmentElement.h:
* Source/WebCore/html/HTMLAttachmentElement.idl:

Canonical link: https://commits.webkit.org/261101@main
  • Loading branch information
squelart authored and nt1m committed Mar 2, 2023
1 parent 4396721 commit b9dad07
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Source/WebCore/html/HTMLAttachmentElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#include "AddEventListenerOptions.h"
#include "AttachmentElementClient.h"
#include "CustomEvent.h"
#include "DOMRectReadOnly.h"
#include "DOMURL.h"
#include "Document.h"
#include "Editor.h"
Expand All @@ -45,6 +45,7 @@
#include "HTMLNames.h"
#include "HTMLStyleElement.h"
#include "MIMETypeRegistry.h"
#include "MouseEvent.h"
#include "RenderAttachment.h"
#include "ShadowRoot.h"
#include "SharedBuffer.h"
Expand Down Expand Up @@ -201,15 +202,17 @@ class AttachmentSaveEventListener final : public EventListener {
void handleEvent(ScriptExecutionContext&, Event& event) final
{
if (event.type() == eventNames().clickEvent) {
auto& mouseEvent = downcast<MouseEvent>(event);
auto copiedEvent = MouseEvent::create(
m_attachment->attributeWithoutSynchronization(saveAttr()), Event::CanBubble::No, Event::IsCancelable::No, Event::IsComposed::No,
mouseEvent.view(), mouseEvent.detail(), mouseEvent.screenX(), mouseEvent.screenY(), mouseEvent.clientX(), mouseEvent.clientY(),
mouseEvent.modifierKeys(), mouseEvent.button(), mouseEvent.buttons(), mouseEvent.syntheticClickType(), nullptr);

event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();

CustomEvent::Init init;
init.bubbles = true;
init.cancelable = true;
init.composed = true;
m_attachment->dispatchEvent(CustomEvent::create(m_attachment->attributeWithoutSynchronization(saveAttr()), init, event.isTrusted() ? Event::IsTrusted::Yes : Event::IsTrusted::No));
m_attachment->dispatchEvent(copiedEvent);
} else
ASSERT_NOT_REACHED();
}
Expand Down Expand Up @@ -243,6 +246,16 @@ void HTMLAttachmentElement::updateSaveButton(const AtomString& eventTypeName)
}
}

DOMRectReadOnly* HTMLAttachmentElement::saveButtonClientRect() const
{
if (!m_saveButton)
return nullptr;

bool unusedIsReplaced;
auto rect = m_saveButton->pixelSnappedRenderRect(&unusedIsReplaced);
m_saveButtonClientRect = DOMRectReadOnly::create(rect.x(), rect.y(), rect.width(), rect.height());
return m_saveButtonClientRect.get();
}

RenderPtr<RenderElement> HTMLAttachmentElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition& position)
{
Expand Down
5 changes: 5 additions & 0 deletions Source/WebCore/html/HTMLAttachmentElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

namespace WebCore {

class DOMRectReadOnly;
class File;
class HTMLImageElement;
class RenderAttachment;
Expand Down Expand Up @@ -80,6 +81,7 @@ class HTMLAttachmentElement final : public HTMLElement {
void requestIconWithSize(const FloatSize&) const;
FloatSize iconSize() const { return m_iconSize; }
void invalidateRendering();
DOMRectReadOnly* saveButtonClientRect() const;

#if ENABLE(SERVICE_CONTROLS)
bool isImageMenuEnabled() const { return m_isImageMenuEnabled; }
Expand All @@ -89,6 +91,8 @@ class HTMLAttachmentElement final : public HTMLElement {
bool isImageOnly() const { return m_implementation == Implementation::ImageOnly; }

private:
friend class AttachmentSaveEventListener;

HTMLAttachmentElement(const QualifiedName&, Document&);
virtual ~HTMLAttachmentElement();

Expand Down Expand Up @@ -126,6 +130,7 @@ class HTMLAttachmentElement final : public HTMLElement {
RefPtr<HTMLElement> m_titleElement;
RefPtr<HTMLElement> m_subtitleElement;
RefPtr<HTMLElement> m_saveButton;
mutable RefPtr<DOMRectReadOnly> m_saveButtonClientRect;

#if ENABLE(SERVICE_CONTROLS)
bool m_isImageMenuEnabled { false };
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/html/HTMLAttachmentElement.idl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
] interface HTMLAttachmentElement : HTMLElement {
attribute File? file;
readonly attribute DOMString uniqueIdentifier;
readonly attribute DOMRectReadOnly? saveButtonClientRect;

static DOMString getAttachmentIdentifier(HTMLImageElement imageElement);
};

0 comments on commit b9dad07

Please sign in to comment.