Skip to content

Commit

Permalink
Merge pull request #124 from CefView/feature/context-menu
Browse files Browse the repository at this point in the history
Feature/context menu
  • Loading branch information
tishion authored Jul 1, 2022
2 parents 131aa4b + 70f13f4 commit 5cc8864
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 62 deletions.
8 changes: 6 additions & 2 deletions example/QCefViewTest/MainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "MainWindow.h"
#include "MainWindow.h"

#include <QCoreApplication>
#include <QDir>
Expand Down Expand Up @@ -66,14 +66,18 @@ MainWindow::createCefView()
// this site is for test web events
cefViewWidget = new CefViewWidget("http://output.jsbin.com/rinece", &setting, this);

//
// cefViewWidget = new CefViewWidget("https://mdn.dev/", &setting, this);

// this site is for test OSR performance
// cefViewWidget = new CefViewWidget("https://www.testufo.com", &setting, this);

// this site is test for input devices
// cefViewWidget = new CefViewWidget("https://devicetests.com", &setting);

ui.cefContainer->layout()->addWidget(cefViewWidget);
cefViewWidget->setStyleSheet("background-color: blue;");

// cefViewWidget->setCefContextMenuPolicy(Qt::CefDisableAllContextMenu);

// connect the invokeMethod to the slot
connect(cefViewWidget, &QCefView::invokeMethod, this, &MainWindow::onInvokeMethod);
Expand Down
54 changes: 35 additions & 19 deletions include/QCefView.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,28 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
QScopedPointer<QCefViewPrivate> d_ptr;

public:
/// <summary>
/// The main frame identity
/// </summary>
static const qint64 MainFrameID = 0;

public:
/// <summary>
///
/// Represents the CEF popup windows open disposition
/// </summary>
enum WindowOpenDisposition
enum CefWindowOpenDisposition
{
WOD_UNKNOWN,
WOD_CURRENT_TAB,
WOD_SINGLETON_TAB,
WOD_NEW_FOREGROUND_TAB,
WOD_NEW_BACKGROUND_TAB,
WOD_NEW_POPUP,
WOD_NEW_WINDOW,
WOD_SAVE_TO_DISK,
WOD_OFF_THE_RECORD,
WOD_IGNORE_ACTION
CefWindowOpenDispositionUnknown,
CefWindowOpenDispositionCurrentTab,
CefWindowOpenDispositionSingletonTab,
CefWindowOpenDispositionNewForeGroundTab,
CefWindowOpenDispositionNewBackgroundTab,
CefWindowOpenDispositionNewPopup,
CefWindowOpenDispositionNewWindow,
CefWindowOpenDispositionSaveToDisk,
CefWindowOpenDispositionOffTheRecord,
CefWindowOpenDispositionIgnoreAction,
};
Q_ENUM(WindowOpenDisposition)
Q_ENUM(CefWindowOpenDisposition)

public:
/// <summary>
Expand Down Expand Up @@ -205,10 +207,22 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// The preference value, if this value is QVariant::UnknownType or QVariant::Invalid, the
/// preference will be restored to default value
/// </param>
/// <param name="error">The error message populated on failure</param> <returns>True
/// on successful; otherwise false</returns>
/// <param name="error">The error message populated on failure</param>
// <returns>True on successful; otherwise false</returns>
bool setPreference(const QString& name, const QVariant& value, const QString& error);

/// <summary>
/// Sets whether to disable the context menu for popup browser
/// </summary>
/// <param name="disable">True to disable; otherwise false</param>
void setDisablePopupContextMenu(bool disable);

/// <summary>
/// Gets whether to disable the context menu for popup browser
/// </summary>
/// <returns>True to disable; otherwise false</returns>
bool isPopupContextMenuDisabled();

signals:
/// <summary>
/// Gets called on loading state changed
Expand Down Expand Up @@ -344,7 +358,7 @@ public slots:
virtual void onBrowserWindowCreated(QWindow* win);

/// <summary>
/// Gets called before the pop-up browser created
/// Gets called before the popup browser created
/// </summary>
/// <param name="frameId">The source frame id</param>
/// <param name="targetUrl">The target URL</param>
Expand All @@ -356,12 +370,12 @@ public slots:
virtual bool onBeforePopup(qint64 frameId,
const QString& targetUrl,
const QString& targetFrameName,
QCefView::WindowOpenDisposition targetDisposition,
QCefView::CefWindowOpenDisposition targetDisposition,
QCefSetting& settings,
bool& DisableJavascriptAccess);

/// <summary>
/// Gets called right after the pop-up browser was created
/// Gets called right after the popup browser was created
/// </summary>
/// <param name="wnd">The host window of new created browser</param>
virtual void onPopupCreated(QWindow* wnd);
Expand Down Expand Up @@ -449,6 +463,8 @@ public slots:
/// Please refer to QWidget::wheelEvent
/// </summary>
void wheelEvent(QWheelEvent* event) override;

void contextMenuEvent(QContextMenuEvent* event) override;
#pragma endregion
};

Expand Down
37 changes: 34 additions & 3 deletions src/QCefView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "details/QCefEventPrivate.h"
#include "details/QCefViewPrivate.h"
#include "details/utils/CommonUtils.h"

QCefView::QCefView(const QString url, const QCefSetting* setting, QWidget* parent /*= 0*/)
: QWidget(parent)
Expand Down Expand Up @@ -191,6 +192,22 @@ QCefView::setPreference(const QString& name, const QVariant& value, const QStrin
return d->setPreference(name, value, error);
}

void
QCefView::setDisablePopupContextMenu(bool disable)
{
Q_D(QCefView);

d->disablePopuContextMenu_ = disable;
}

bool
QCefView::isPopupContextMenuDisabled()
{
Q_D(QCefView);

return d->disablePopuContextMenu_;
}

void
QCefView::setFocus(Qt::FocusReason reason)
{
Expand All @@ -207,7 +224,7 @@ bool
QCefView::onBeforePopup(qint64 frameId,
const QString& targetUrl,
const QString& targetFrameName,
QCefView::WindowOpenDisposition targetDisposition,
QCefView::CefWindowOpenDisposition targetDisposition,
QCefSetting& settings,
bool& DisableJavascriptAccess)
{
Expand Down Expand Up @@ -249,7 +266,7 @@ QCefView::paintEvent(QPaintEvent* event)
opt.initFrom(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);

// 4. paint the CEF view and pop-up
// 4. paint the CEF view and popup
// get current scale factor
qreal scaleFactor = devicePixelRatio();

Expand All @@ -262,7 +279,7 @@ QCefView::paintEvent(QPaintEvent* event)
painter.drawImage(QRect{ 0, 0, width, height }, d->osr.qCefViewFrame_);
}
{
// paint cef pop-up
// paint cef popup
QMutexLocker lock(&(d->osr.qPopupPaintLock_));
if (d->osr.showPopup_) {
painter.drawImage(d->osr.qPopupRect_, d->osr.qCefPopupFrame_);
Expand Down Expand Up @@ -368,3 +385,17 @@ QCefView::wheelEvent(QWheelEvent* event)
d->onViewWheelEvent(event);
QWidget::wheelEvent(event);
}

void
QCefView::contextMenuEvent(QContextMenuEvent* event)
{
FLog();

#if defined(CEF_USE_OSR)
Q_D(QCefView);

if (d->osr.isShowingContextMenu_) {
d->osr.contextMenu_->popup(mapToGlobal(event->pos()));
}
#endif
}
61 changes: 53 additions & 8 deletions src/details/CCefClientDelegate_ContextMenuHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "CCefClientDelegate.h"

#include <QDebug>
#include <QThread>

#include "utils/CommonUtils.h"
#include "utils/MenuBuilder.h"

#include "QCefViewPrivate.h"

void
Expand All @@ -8,12 +14,31 @@ CCefClientDelegate::onBeforeContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model)
{
#if defined(CEF_USE_OSR)
// Context menu will not disappear on left click under OSR
// mode, so we just disable the default one. You need to
// implement your own context menu.
model->Clear();
#endif
FLog();

if (!pCefViewPrivate_)
return;

// popup browser doesn't involve off-screen rendering
if (browser->IsPopup()) {
if (pCefViewPrivate_->disablePopuContextMenu_) {
model->Clear();
}

return;
}

// main browser
auto policy = pCefViewPrivate_->q_ptr->contextMenuPolicy();
if (Qt::DefaultContextMenu != policy) {
model->Clear();
return;
}

auto menuData = MenuBuilder::CreateMenuDataFromCefMenu(model.get());
QMetaObject::invokeMethod(pCefViewPrivate_, [=]() { pCefViewPrivate_->onBeforeCefContextMenu(menuData); });

return;
}

bool
Expand All @@ -23,7 +48,21 @@ CCefClientDelegate::onRunContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefMenuModel> model,
CefRefPtr<CefRunContextMenuCallback> callback)
{
return false;
FLog();

if (browser->IsPopup()) {
return false;
}

auto policy = pCefViewPrivate_->q_ptr->contextMenuPolicy();
if (Qt::DefaultContextMenu != policy) {
return false;
}

QPoint pos(params->GetXCoord(), params->GetYCoord());
QMetaObject::invokeMethod(pCefViewPrivate_, [=]() { pCefViewPrivate_->onRunCefContextMenu(pos, callback); });

return true;
}

bool
Expand All @@ -33,9 +72,15 @@ CCefClientDelegate::onContextMenuCommand(CefRefPtr<CefBrowser> browser,
int command_id,
CefContextMenuHandler::EventFlags event_flags)
{
FLog();

return false;
}

void
CCefClientDelegate::onContextMenuDismissed(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame)
{}
{
FLog();

QMetaObject::invokeMethod(pCefViewPrivate_, [=]() { pCefViewPrivate_->onCefContextMenuDismissed(); });
}
2 changes: 0 additions & 2 deletions src/details/CCefClientDelegate_KeyboardHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "CCefClientDelegate.h"

#include <QDebug>
#include <QImage>
#include <QScreen>

#include "QCefViewPrivate.h"

Expand Down
20 changes: 10 additions & 10 deletions src/details/CCefClientDelegate_LifeSpanHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ CCefClientDelegate::onBeforePopup(CefRefPtr<CefBrowser>& browser,
auto name = QString::fromStdString(targetFrameName);

QCefSetting s;
QCefView::WindowOpenDisposition d = (QCefView::WindowOpenDisposition)targetDisposition;
QCefView::CefWindowOpenDisposition d = (QCefView::CefWindowOpenDisposition)targetDisposition;
QCefSettingPrivate::CopyFromCefBrowserSettings(&s, &settings);

Qt::ConnectionType c = pCefViewPrivate_->q_ptr->thread() == QThread::currentThread() ? Qt::DirectConnection
: Qt::BlockingQueuedConnection;
QMetaObject::invokeMethod(pCefViewPrivate_->q_ptr,
"onBeforePopup", //
c, //
Q_RETURN_ARG(bool, result), //
Q_ARG(qint64, frameId), //
Q_ARG(const QString&, url), //
Q_ARG(const QString&, name), //
Q_ARG(QCefView::WindowOpenDisposition, d), //
Q_ARG(QCefSetting&, s), //
Q_ARG(bool&, DisableJavascriptAccess) //
"onBeforePopup", //
c, //
Q_RETURN_ARG(bool, result), //
Q_ARG(qint64, frameId), //
Q_ARG(const QString&, url), //
Q_ARG(const QString&, name), //
Q_ARG(QCefView::CefWindowOpenDisposition, d), //
Q_ARG(QCefSetting&, s), //
Q_ARG(bool&, DisableJavascriptAccess) //
);
QCefSettingPrivate::CopyToCefBrowserSettings(&s, &settings);
}
Expand Down
Loading

0 comments on commit 5cc8864

Please sign in to comment.