summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantin Tokarev <annulen@yandex.ru>2017-02-03 00:28:43 +0300
committerKonstantin Tokarev <annulen@yandex.ru>2017-02-13 10:22:27 +0000
commitc3b710c3f4de2da0fd67c858949c3f420dfd175a (patch)
tree1c8bb81c78cf6e670ac23f526c7977d5975d4ea2
parenta4eec4118333b59d10fd6cc9f126b1af2e8f505c (diff)
Support custom items in context menu
This is particularly important for Inspector, which lacks a lot of useful context menu actions without this patch. Change-Id: I8170c806028ff140206f13e5e1de0409e945e905 Reviewed-by: Konstantin Tokarev <annulen@yandex.ru>
-rw-r--r--Source/WebKit/qt/WebCoreSupport/FrameLoaderClientQt.cpp4
-rw-r--r--Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.cpp17
-rw-r--r--Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.h4
-rw-r--r--Source/WebKit/qt/WidgetApi/qwebpage.cpp45
-rw-r--r--Source/WebKit/qt/WidgetApi/qwebpage.h2
-rw-r--r--Source/WebKit/qt/WidgetApi/qwebpage_p.h3
6 files changed, 68 insertions, 7 deletions
diff --git a/Source/WebKit/qt/WebCoreSupport/FrameLoaderClientQt.cpp b/Source/WebKit/qt/WebCoreSupport/FrameLoaderClientQt.cpp
index 09b0aae82..0b811c8f2 100644
--- a/Source/WebKit/qt/WebCoreSupport/FrameLoaderClientQt.cpp
+++ b/Source/WebKit/qt/WebCoreSupport/FrameLoaderClientQt.cpp
@@ -735,8 +735,10 @@ void FrameLoaderClientQt::dispatchDidClearWindowObjectInWorld(DOMWrapperWorld* w
if (world != mainThreadNormalWorld())
return;
- if (m_webFrame)
+ if (m_webFrame) {
m_webFrame->didClearWindowObject();
+ m_webFrame->pageAdapter->clearCustomActions();
+ }
}
void FrameLoaderClientQt::documentElementAvailable()
diff --git a/Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.cpp b/Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.cpp
index 554ab8927..a4e1f7464 100644
--- a/Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.cpp
+++ b/Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.cpp
@@ -874,8 +874,11 @@ void QWebPageAdapter::dynamicPropertyChangeEvent(QObject* obj, QDynamicPropertyC
#define MAP_ACTION_FROM_VALUE(Name, Value) \
case Value: return QWebPageAdapter::Name
-static QWebPageAdapter::MenuAction adapterActionForContextMenuAction(WebCore::ContextMenuAction action)
+static int adapterActionForContextMenuAction(WebCore::ContextMenuAction action)
{
+ if (action >= ContextMenuItemBaseCustomTag && action <= ContextMenuItemLastCustomTag)
+ return action;
+
switch (action) {
FOR_EACH_MAPPED_MENU_ACTION(MAP_ACTION_FROM_VALUE, SEMICOLON_SEPARATOR);
#if ENABLE(INSPECTOR)
@@ -899,7 +902,7 @@ QList<MenuItem> descriptionForPlatformMenu(const Vector<ContextMenuItem>& items,
switch (item.type()) {
case WebCore::CheckableActionType: /* fall through */
case WebCore::ActionType: {
- QWebPageAdapter::MenuAction action = adapterActionForContextMenuAction(item.action());
+ int action = adapterActionForContextMenuAction(item.action());
if (action > QWebPageAdapter::NoAction) {
description.type = MenuItem::Action;
description.action = action;
@@ -1207,6 +1210,16 @@ void QWebPageAdapter::triggerAction(QWebPageAdapter::MenuAction action, QWebHitT
}
}
+void QWebPageAdapter::triggerCustomAction(int action, const QString &title)
+{
+ if (action >= ContextMenuItemBaseCustomTag && action <= ContextMenuItemLastCustomTag) {
+ ContextMenuItem item(ActionType, static_cast<ContextMenuAction>(action), title);
+ page->contextMenuController()->contextMenuItemSelected(&item);
+ } else {
+ ASSERT_NOT_REACHED();
+ }
+}
+
QString QWebPageAdapter::contextMenuItemTagForAction(QWebPageAdapter::MenuAction action, bool* checkable) const
{
diff --git a/Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.h b/Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.h
index ee4d2595a..fcdc25596 100644
--- a/Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.h
+++ b/Source/WebKit/qt/WebCoreSupport/QWebPageAdapter.h
@@ -204,6 +204,7 @@ public:
virtual void createUndoStep(QSharedPointer<UndoStepQt>) = 0;
virtual void updateNavigationActions() = 0;
+ virtual void clearCustomActions() = 0;
virtual QWebFrameAdapter* mainFrameAdapter() = 0;
@@ -249,7 +250,7 @@ public:
Separator,
SubMenu
} type;
- MenuAction action;
+ int action;
enum Trait {
None = 0,
Enabled = 1,
@@ -317,6 +318,7 @@ public:
QWebHitTestResultPrivate* updatePositionDependentMenuActions(const QPoint&, QBitArray*);
void updateActionInternal(MenuAction, const char* commandName, bool* enabled, bool* checked);
void triggerAction(MenuAction, QWebHitTestResultPrivate*, const char* commandName, bool endToEndReload);
+ void triggerCustomAction(int action, const QString &title);
QString contextMenuItemTagForAction(MenuAction, bool* checkable) const;
QStringList supportedContentTypes() const;
diff --git a/Source/WebKit/qt/WidgetApi/qwebpage.cpp b/Source/WebKit/qt/WidgetApi/qwebpage.cpp
index 863621391..388456616 100644
--- a/Source/WebKit/qt/WidgetApi/qwebpage.cpp
+++ b/Source/WebKit/qt/WidgetApi/qwebpage.cpp
@@ -238,6 +238,8 @@ QWebPagePrivate::~QWebPagePrivate()
// in order for various destruction callbacks out of WebCore to
// work.
deletePage();
+
+ clearCustomActions();
}
void QWebPagePrivate::show()
@@ -499,8 +501,15 @@ QMenu *createContextMenu(QWebPage* page, const QList<MenuItem>& items, QBitArray
const MenuItem &item = items.at(i);
switch (item.type) {
case MenuItem::Action: {
- QWebPage::WebAction action = webActionForAdapterMenuAction(item.action);
- QAction *a = page->action(action);
+ QAction* a = nullptr;
+ if (item.action < QWebPageAdapter::ActionCount) {
+ QWebPage::WebAction action = webActionForAdapterMenuAction(static_cast<QWebPageAdapter::MenuAction>(item.action));
+ a = page->action(action);
+ if (a)
+ visitedWebActions->setBit(action);
+ } else {
+ a = page->customAction(item.action);
+ }
if (a) {
a->setText(item.title);
a->setEnabled(item.traits & MenuItem::Enabled);
@@ -508,7 +517,6 @@ QMenu *createContextMenu(QWebPage* page, const QList<MenuItem>& items, QBitArray
a->setChecked(item.traits & MenuItem::Checked);
menu->addAction(a);
- visitedWebActions->setBit(action);
}
break;
}
@@ -562,6 +570,16 @@ void QWebPagePrivate::_q_webActionTriggered(bool checked)
QWebPage::WebAction action = static_cast<QWebPage::WebAction>(a->data().toInt());
q->triggerAction(action, checked);
}
+
+void QWebPagePrivate::_q_customActionTriggered(bool checked)
+{
+ Q_UNUSED(checked);
+ QAction* a = qobject_cast<QAction*>(q->sender());
+ if (!a)
+ return;
+ int action = a->data().toInt();
+ triggerCustomAction(action, a->text());
+}
#endif // QT_NO_ACTION
void QWebPagePrivate::updateAction(QWebPage::WebAction action)
@@ -625,6 +643,12 @@ void QWebPagePrivate::updateNavigationActions()
updateAction(QWebPage::ReloadAndBypassCache);
}
+void QWebPagePrivate::clearCustomActions()
+{
+ qDeleteAll(customActions);
+ customActions.clear();
+}
+
QObject *QWebPagePrivate::inspectorHandle()
{
return getOrCreateInspector();
@@ -2509,6 +2533,21 @@ QAction *QWebPage::action(WebAction action) const
d->updateAction(action);
return a;
}
+
+QAction* QWebPage::customAction(int action) const
+{
+ auto actionIter = d->customActions.constFind(action);
+ if (actionIter != d->customActions.constEnd())
+ return *actionIter;
+
+ QAction* a = new QAction(d->q);
+ a->setData(action);
+ connect(a, SIGNAL(triggered(bool)),
+ this, SLOT(_q_customActionTriggered(bool)));
+
+ d->customActions.insert(action, a);
+ return a;
+}
#endif // QT_NO_ACTION
/*!
diff --git a/Source/WebKit/qt/WidgetApi/qwebpage.h b/Source/WebKit/qt/WidgetApi/qwebpage.h
index 01e77d44b..822bd2194 100644
--- a/Source/WebKit/qt/WidgetApi/qwebpage.h
+++ b/Source/WebKit/qt/WidgetApi/qwebpage.h
@@ -301,6 +301,7 @@ public:
#ifndef QT_NO_ACTION
QAction *action(WebAction action) const;
+ QAction *customAction(int action) const;
#endif
virtual void triggerAction(WebAction action, bool checked = false);
@@ -443,6 +444,7 @@ private:
Q_PRIVATE_SLOT(d, void _q_onLoadProgressChanged(int))
#ifndef QT_NO_ACTION
Q_PRIVATE_SLOT(d, void _q_webActionTriggered(bool checked))
+ Q_PRIVATE_SLOT(d, void _q_customActionTriggered(bool checked))
#endif
Q_PRIVATE_SLOT(d, void _q_cleanupLeakMessages())
Q_PRIVATE_SLOT(d, void _q_updateScreen(QScreen*))
diff --git a/Source/WebKit/qt/WidgetApi/qwebpage_p.h b/Source/WebKit/qt/WidgetApi/qwebpage_p.h
index 604c0a540..34cf288b4 100644
--- a/Source/WebKit/qt/WidgetApi/qwebpage_p.h
+++ b/Source/WebKit/qt/WidgetApi/qwebpage_p.h
@@ -120,6 +120,7 @@ public:
virtual const char* editorCommandForKeyEvent(QKeyEvent*) OVERRIDE;
void updateNavigationActions() OVERRIDE;
+ void clearCustomActions() OVERRIDE;
virtual QObject* inspectorHandle() OVERRIDE;
virtual void setInspectorFrontend(QObject*) OVERRIDE;
@@ -145,6 +146,7 @@ public:
void createMainFrame();
void _q_webActionTriggered(bool checked);
+ void _q_customActionTriggered(bool checked);
void updateAction(QWebPage::WebAction);
void updateEditorActions();
@@ -198,6 +200,7 @@ public:
bool useFixedLayout;
QAction *actions[QWebPage::WebActionCount];
+ QHash<int, QAction*> customActions;
QPointer <QWindow> window;
QWidget* inspectorFrontend;