Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Support contextmenu ("right click") events using sendEvent('contextmenu') #13870

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/webpage.cpp
Expand Up @@ -33,6 +33,7 @@
#include <math.h>

#include <QApplication>
#include <QContextMenuEvent>
#include <QDesktopServices>
#include <QDateTime>
#include <QDir>
Expand Down Expand Up @@ -1514,6 +1515,29 @@ void WebPage::sendEvent(const QString& type, const QVariant& arg1, const QVarian
return;
}

// context click
if (type == "contextmenu") {
QContextMenuEvent::Reason reason = QContextMenuEvent::Mouse;

// Gather coordinates
if (arg1.isValid() && arg2.isValid()) {
m_mousePos.setX(arg1.toInt());
m_mousePos.setY(arg2.toInt());
}

// Prepare the context menu event
qDebug() << "Context Menu Event:" << eventType << "(" << reason << "," << m_mousePos << ")";
QContextMenuEvent* event = new QContextMenuEvent(reason, m_mousePos, QCursor::pos(), keyboardModifiers);

// Post and process events
// Send the context menu event directly to QWebPage::swallowContextMenuEvent which forwards it to JS
// If we fire the event using postEvent to m_customWebPage, it will end up in QWebPagePrivate::contextMenuEvent,
// which will not forward it to JS at all
m_customWebPage->swallowContextMenuEvent(event);
return;
}


// mouse click events: Qt doesn't provide this as a separate events,
// so we compose it with a mousedown/mouseup sequence
// mouse doubleclick events: It is not enough to simply send a
Expand Down
32 changes: 32 additions & 0 deletions test/module/webpage/contextclick-event.js
@@ -0,0 +1,32 @@
test(function () {
var page = require('webpage').create();

page.evaluate(function() {
window.addEventListener('contextmenu', function(event) {
window.loggedEvent = window.loggedEvent || {};
window.loggedEvent.contextmenu = event;
}, false);
});
page.sendEvent('contextmenu', 42, 217);

var event = page.evaluate(function() {
return window.loggedEvent;
});
assert_equals(event.contextmenu.clientX, 42);
assert_equals(event.contextmenu.clientY, 217);

// click with modifier key
page.evaluate(function() {
window.addEventListener('contextmenu', function(event) {
window.loggedEvent = window.loggedEvent || {};
window.loggedEvent.contextmenu = event;
}, false);
});
page.sendEvent('contextmenu', 100, 100, 'left', page.event.modifier.shift);

var event = page.evaluate(function() {
return window.loggedEvent.contextmenu;
});
assert_is_true(event.shiftKey);

}, "context click events");