Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix popup #304

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions example/QCefViewTest/CefViewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ CefViewWidget::onDraggableRegionChanged(const QRegion& draggableRegion, const QR
}

bool
CefViewWidget::onBeforePopup(qint64 frameId,
const QString& targetUrl,
const QString& targetFrameName,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings)
CefViewWidget::onNewPopup(qint64 sourceFrameId,
const QString& targetUrl,
QString& targetFrameName,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings,
bool& disableJavascriptAccess)
{
// create new QCefView as popup browser
settings.setBackgroundColor(Qt::red);
Expand Down
13 changes: 7 additions & 6 deletions example/QCefViewTest/CefViewWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ protected slots:
void onDraggableRegionChanged(const QRegion& draggableRegion, const QRegion& nonDraggableRegion);

protected:
bool onBeforePopup(qint64 frameId,
const QString& targetUrl,
const QString& targetFrameName,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings) override;
bool onNewPopup(qint64 sourceFrameId,
const QString& targetUrl,
QString& targetFrameName,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings,
bool& disableJavascriptAccess) override;

void onNewDownloadItem(const QSharedPointer<QCefDownloadItem>& item, const QString& suggestedName) override;

Expand Down
3 changes: 2 additions & 1 deletion example/QCefViewTest/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ MainWindow::createRightCefView()

// this site is for test web events
m_pRightCefViewWidget = new CefViewWidget("", &setting, this);
m_pRightCefViewWidget->navigateToUrl("https://fastest.fish/test-files");
m_pRightCefViewWidget->navigateToUrl(
"https://www.javatpoint.com/oprweb/test.jsp?filename=javascript-window-close-method1");

//
// m_pRightCefViewWidget = new CefViewWidget("https://mdn.dev/", &setting, this);
Expand Down
3 changes: 1 addition & 2 deletions example/QCefViewTest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ main(int argc, char* argv[])

// WindowlessRenderingEnabled is set to true by default,
// set to false to disable the OSR mode
config.setWindowlessRenderingEnabled(true);
config.setWindowlessRenderingEnabled(false);

// add command line args, you can any cef supported switches or parameters
config.addCommandLineSwitch("use-mock-keychain");
Expand All @@ -52,4 +52,3 @@ main(int argc, char* argv[])
// flying
return a.exec();
}

46 changes: 23 additions & 23 deletions include/QCefView.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// <returns>The browser id</returns>
int browserId();

/// <summary>
/// Gets whether the browser is created as popup browser
/// </summary>
/// <returns>True if it is popup browser; otherwise false</returns>
bool isPopup();

/// <summary>
/// Navigates to the content.
/// </summary>
Expand Down Expand Up @@ -408,20 +402,25 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// <param name="window">The native browser windows</param>
void nativeBrowserCreated(QWindow* window);

protected:
/// <summary>
/// Gets called right after the popup browser was created.
///
/// </summary>
/// <param name="popup">The new created popup QCefView instance</param>
/// <remarks>
/// The lifecycle of the popup browser is managed by the owner of the popup browser,
/// thus do not try to hold the popup browser instance.
/// If you need to implement browser tab, you should override the <see cref="onBeforePopup"/> method
/// and create your own QCefView browser instance then you can manipulate the created one as whatever
/// you want.
/// </remarks>
void popupCreated(QCefView* popup);
/// <param name="sourceFrameId"></param>
/// <param name="url"></param>
/// <param name="name"></param>
/// <param name="targetDisposition"></param>
/// <param name="rect"></param>
/// <param name="settings"></param>
/// <param name="disableJavascriptAccess"></param>
/// <returns></returns>
virtual QCefView* onNewBrowser(qint64 sourceFrameId,
const QString& url,
const QString& name,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings);

protected:
/// <summary>
/// Gets called before the popup browser created
/// </summary>
Expand All @@ -432,12 +431,13 @@ class QCEFVIEW_EXPORT QCefView : public QWidget
/// <param name="rect">Rect to be used for the popup</param>
/// <param name="settings">Settings to be used for the popup</param>
/// <returns>True to cancel the popup; false to allow</returns>
virtual bool onBeforePopup(qint64 frameId,
const QString& targetUrl,
const QString& targetFrameName,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings);
virtual bool onNewPopup(qint64 frameId,
const QString& targetUrl,
QString& targetFrameName,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings,
bool& disableJavascriptAccess);

/// <summary>
/// Gets called on new download item was required. Keep reference to the download item
Expand Down
48 changes: 31 additions & 17 deletions src/QCefView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ QCefView::~QCefView()
qDebug() << this << "is being destructed";

if (d_ptr) {
// close all popup browsers
d_ptr->closeAllPopupBrowsers();

// destroy under layer cef browser
d_ptr->destroyCefBrowser();
d_ptr.reset();
Expand Down Expand Up @@ -82,14 +79,6 @@ QCefView::browserId()
return d->browserId();
}

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

return d->isPopup();
}

void
QCefView::navigateToString(const QString& content)
{
Expand Down Expand Up @@ -282,13 +271,38 @@ QCefView::setFocus(Qt::FocusReason reason)
d->setCefWindowFocus(true);
}

QCefView*
QCefView::onNewBrowser(qint64 sourceFrameId,
const QString& url,
const QString& name,
CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings)
{
QCefView* popup = new QCefView(url, &settings, nullptr, Qt::WindowFlags());
if (!popup) {
// failed to create QCefView, cancel popup
return nullptr;
}

// config the popup QCefView
if (!name.isEmpty()) {
popup->setWindowTitle(name);
}
popup->resize(rect.size());
popup->show();

return popup;
}

bool
QCefView::onBeforePopup(qint64 frameId,
const QString& targetUrl,
const QString& targetFrameName,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings)
QCefView::onNewPopup(qint64 frameId,
const QString& targetUrl,
QString& targetFrameName,
QCefView::CefWindowOpenDisposition targetDisposition,
QRect& rect,
QCefSetting& settings,
bool& disableJavascriptAccess)
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/details/CCefClientDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class CCefClientDelegate
CefLifeSpanHandler::WindowOpenDisposition targetDisposition,
CefWindowInfo& windowInfo,
CefBrowserSettings& settings,
bool& DisableJavascriptAccess) override;
bool& disableJavascriptAccess) override;
virtual void onAfterCreate(CefRefPtr<CefBrowser>& browser) override;
virtual bool doClose(CefRefPtr<CefBrowser> browser) override;
virtual void onBeforeClose(CefRefPtr<CefBrowser> browser) override;
Expand Down
72 changes: 61 additions & 11 deletions src/details/CCefClientDelegate_LifeSpanHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "QCefSettingPrivate.h"
#include "QCefViewPrivate.h"

#define DEFAULT_POPUP_WIDTH 800
#define DEFAULT_POPUP_HEIGHT 600

bool
CCefClientDelegate::onBeforePopup(CefRefPtr<CefBrowser>& browser,
int64_t frameId,
Expand All @@ -13,24 +16,66 @@ CCefClientDelegate::onBeforePopup(CefRefPtr<CefBrowser>& browser,
CefLifeSpanHandler::WindowOpenDisposition targetDisposition,
CefWindowInfo& windowInfo,
CefBrowserSettings& settings,
bool& DisableJavascriptAccess)
bool& disableJavascriptAccess)
{
if (pCefViewPrivate_) {
bool cancel = true;
if (!pCefViewPrivate_) {
return cancel;
}

auto url = QString::fromStdString(targetUrl);
auto name = QString::fromStdString(targetFrameName);
auto d = (QCefView::CefWindowOpenDisposition)targetDisposition;
auto rc = QRect(windowInfo.bounds.x, windowInfo.bounds.y, windowInfo.bounds.width, windowInfo.bounds.height);

if (rc.width() <= 0) {
rc.setWidth(DEFAULT_POPUP_WIDTH);
}

if (rc.height() <= 0) {
rc.setHeight(DEFAULT_POPUP_HEIGHT);
}

QCefSetting s;
QCefSettingPrivate::CopyFromCefBrowserSettings(&s, &settings);

Qt::ConnectionType c =
pCefViewPrivate_->q_ptr->thread() == QThread::currentThread() ? Qt::DirectConnection : Qt::QueuedConnection;
if (targetDisposition == CefLifeSpanHandler::WindowOpenDisposition::WOD_NEW_POPUP) {
Qt::ConnectionType c = pCefViewPrivate_->q_ptr->thread() == QThread::currentThread() ? Qt::DirectConnection
: Qt::BlockingQueuedConnection;

QMetaObject::invokeMethod(
pCefViewPrivate_,
[=]() {
pCefViewPrivate_->onBeforeCefPopupCreate(
browser, frameId, targetUrl, targetFrameName, targetDisposition, windowInfo, settings);
[&]() {
cancel = pCefViewPrivate_->onBeforeNewPopupCreate(frameId, //
url, //
name, //
d, //
rc, //
s, //
disableJavascriptAccess);
if (!cancel) {
QCefSettingPrivate::CopyToCefBrowserSettings(&s, &settings);
CefString(&windowInfo.window_name) = name.toStdString();
windowInfo.bounds = { rc.x(), rc.y(), rc.width(), rc.height() };
}
},
c);
} else {
cancel = true;
QMetaObject::invokeMethod(
pCefViewPrivate_,
[=]() {
pCefViewPrivate_->onBeforeNewBrowserCreate(frameId, //
url, //
name, //
d, //
rc, //
s);
},
Qt::QueuedConnection);
}

// QCefView doesn't use CEF built-in popup browser
return true;
return cancel;
}

void
Expand Down Expand Up @@ -65,8 +110,13 @@ CCefClientDelegate::onAfterCreate(CefRefPtr<CefBrowser>& browser)
}
}

QMetaObject::invokeMethod(
pCefViewPrivate_, [=]() { pCefViewPrivate_->onCefBrowserCreated(browser, w); }, c);
if (browser->IsPopup()) {
QMetaObject::invokeMethod(
pCefViewPrivate_, [=]() { pCefViewPrivate_->onAfterCefPopupCreated(browser); }, c);
} else {
QMetaObject::invokeMethod(
pCefViewPrivate_, [=]() { pCefViewPrivate_->onCefBrowserCreated(browser, w); }, c);
}
}

bool
Expand Down
Loading
Loading