From 57caef55b5dd4319af416df9c21018b1193b9194 Mon Sep 17 00:00:00 2001 From: LSuper <120311070@qq.com> Date: Wed, 6 Dec 2023 21:18:13 +0800 Subject: [PATCH] fix: white screen and flicker 1. fix white screen when window created 2. eliminate flicker --- include/QCefSetting.h | 15 ++++++++++++++- src/QCefSetting.cpp | 16 +++++++++++++++- src/details/QCefSettingPrivate.h | 2 ++ src/details/QCefViewPrivate.cpp | 20 +++++++++++++++++--- src/details/QCefWindow.cpp | 3 ++- 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/include/QCefSetting.h b/include/QCefSetting.h index 014e77b2..cc3e099c 100644 --- a/include/QCefSetting.h +++ b/include/QCefSetting.h @@ -14,6 +14,7 @@ #pragma region qt_headers #include #include +#include #include #pragma endregion qt_headers @@ -52,11 +53,23 @@ class QCEFVIEW_EXPORT QCefSetting /// ~QCefSetting(); + /// + /// Sets the initial size of the browser + /// + /// The initial size + void setInitSize(const QSize& size); + + /// + /// Gets the initial size of the browser + /// + /// + const QSize initSize() const; + /// /// Sets the standard font family /// /// The font family - void setStandardFontFamily(const QString value); + void setStandardFontFamily(const QString& value); /// /// Gets the standard font family diff --git a/src/QCefSetting.cpp b/src/QCefSetting.cpp index 2228ee5a..c05ab472 100644 --- a/src/QCefSetting.cpp +++ b/src/QCefSetting.cpp @@ -30,7 +30,21 @@ QCefSetting::operator=(const QCefSetting& other) QCefSetting::~QCefSetting() {} void -QCefSetting::setStandardFontFamily(const QString value) +QCefSetting::setInitSize(const QSize& size) +{ + Q_D(QCefSetting); + d->initSize_ = size; +} + +const QSize +QCefSetting::initSize() const +{ + Q_D(const QCefSetting); + return d->initSize_; +} + +void +QCefSetting::setStandardFontFamily(const QString& value) { Q_D(QCefSetting); d->standardFontFamily_ = value.toStdString(); diff --git a/src/details/QCefSettingPrivate.h b/src/details/QCefSettingPrivate.h index 2e2956ad..38132829 100644 --- a/src/details/QCefSettingPrivate.h +++ b/src/details/QCefSettingPrivate.h @@ -30,6 +30,8 @@ class QCefSettingPrivate public: explicit QCefSettingPrivate(); + QSize initSize_; + std::string standardFontFamily_; std::string fixedFontFamily_; std::string serifFontFamily_; diff --git a/src/details/QCefViewPrivate.cpp b/src/details/QCefViewPrivate.cpp index 29f4e830..284d36ba 100644 --- a/src/details/QCefViewPrivate.cpp +++ b/src/details/QCefViewPrivate.cpp @@ -89,14 +89,27 @@ QCefViewPrivate::createCefBrowser(QCefView* view, const QString& url, const QCef windowInfo.SetAsWindowless(0); } else { // create CEF browser parent window + auto initSize = q_ptr->size(); + if (setting) { + initSize = setting->initSize(); + } + qDebug() << "Browser init size:" << initSize; + ncw.qBrowserWindow_ = new QCefWindow(); + ncw.qBrowserWindow_->resize(initSize); ncw.qBrowserWindow_->setFlags(Qt::Window | Qt::FramelessWindowHint); - // use INT_MAX as the width and height to prevent black screen blink +#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) + qreal scaleFactor = q_ptr->devicePixelRatioF(); +#else + qreal scaleFactor = q_ptr->devicePixelRatio(); +#endif + auto width = initSize.width() * scaleFactor; + auto height = initSize.height() * scaleFactor; #if CEF_VERSION_MAJOR > 85 - windowInfo.SetAsChild((CefWindowHandle)ncw.qBrowserWindow_->winId(), { 0, 0, INT_MAX, INT_MAX }); + windowInfo.SetAsChild((CefWindowHandle)ncw.qBrowserWindow_->winId(), { 0, 0, (int)width, (int)height }); #else - windowInfo.SetAsChild((CefWindowHandle)ncw.qBrowserWindow_->winId(), 0, 0, INT_MAX, INT_MAX); + windowInfo.SetAsChild((CefWindowHandle)ncw.qBrowserWindow_->winId(), 0, 0, (int)width, (int)height); #endif } @@ -235,6 +248,7 @@ QCefViewPrivate::onCefBrowserCreated(CefRefPtr browser, QWindow* win ncw.qBrowserWindow_->applyMask(q_ptr->mask()); // resize to eliminate flicker + qDebug() << "Host QCefView size:" << q_ptr->size(); ncw.qBrowserWidget_->resize(q_ptr->size()); // initialize the layout and add browser widget to the layout diff --git a/src/details/QCefWindow.cpp b/src/details/QCefWindow.cpp index 15a663fc..76b8937e 100644 --- a/src/details/QCefWindow.cpp +++ b/src/details/QCefWindow.cpp @@ -5,7 +5,6 @@ QCefWindow::QCefWindow() : QWindow() { - this->setFlag(Qt::FramelessWindowHint); } QCefWindow::~QCefWindow() @@ -43,7 +42,9 @@ QCefWindow::detachCefWindow() #else cefWindow_->hide(); +#if defined(Q_OS_LINUX) cefWindow_->setParent(nullptr); +#endif #endif cefWindow_ = nullptr; }