diff --git a/3rdparty/CefViewCore/include/CefVersion.h b/3rdparty/CefViewCore/include/CefVersion.h index 705594b..da85c65 100644 --- a/3rdparty/CefViewCore/include/CefVersion.h +++ b/3rdparty/CefViewCore/include/CefVersion.h @@ -13,12 +13,12 @@ #pragma once // clang-format off -#define CEF_VERSION "134.3.2+g615db2f+chromium-134.0.6998.89" -#define CEF_VERSION_MAJOR 134 +#define CEF_VERSION "127.3.5+g114ea2a+chromium-127.0.6533.120" +#define CEF_VERSION_MAJOR 127 #define CEF_VERSION_MINOR 3 -#define CEF_VERSION_PATCH 2 -#define CEF_COMMIT_NUMBER 3151 -#define CEF_COMMIT_HASH "615db2f583e46822d28a4f2a57c3ef7316c5b17d" +#define CEF_VERSION_PATCH 5 +#define CEF_COMMIT_NUMBER 3037 +#define CEF_COMMIT_HASH "114ea2af1ba9da18c4ac5e599ccdbb17d01ba75a" // clang-format on #endif // CefVersion diff --git a/3rdparty/libcef_dll_wrapper/lib/libcef_dll_wrapper.a b/3rdparty/libcef_dll_wrapper/lib/libcef_dll_wrapper.a index e47177d..92a9bb7 100644 Binary files a/3rdparty/libcef_dll_wrapper/lib/libcef_dll_wrapper.a and b/3rdparty/libcef_dll_wrapper/lib/libcef_dll_wrapper.a differ diff --git a/CMakeLists.txt b/CMakeLists.txt index 33a2ef4..5457050 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,7 +94,7 @@ MESSAGE(STATUS "---------- END---------------") #增加sui的cpp FILE(GLOB src_sui "${PROJECT_SOURCE_DIR}/src/*.*") -FILE(GLOB browser "${PROJECT_SOURCE_DIR}/src/Cef/*.*") +FILE(GLOB browser "${PROJECT_SOURCE_DIR}/src/cef/*.*") SET(PROJECT_SOURCES ${browser} ${src_sui}) # 设置消息策略为qFatal来禁用qDebug信息 diff --git a/src/Cef/CefViewWidget.cpp b/src/Cef/CefViewWidget.cpp index fe2a4cc..bf1b892 100644 --- a/src/Cef/CefViewWidget.cpp +++ b/src/Cef/CefViewWidget.cpp @@ -14,7 +14,7 @@ #include #include -#include "CefDownloadManager.h" +#include "DownloadManager.h" CefViewWidget::CefViewWidget(const QString url, const QCefSetting* setting, QWidget* parent /* = 0*/) : QCefView(url, setting, parent) @@ -74,7 +74,7 @@ CefViewWidget::onNewDownloadItem(const QSharedPointer& item, c { // keep the item into list or map, and call item->start() to allow the download - CefDownloadManager::getInstance().AddNewDownloadItem(item); + DownloadManager::getInstance().AddNewDownloadItem(item); } void @@ -83,7 +83,7 @@ CefViewWidget::onUpdateDownloadItem(const QSharedPointer& item // control the download by invoking item->pause(), item->resume(), item->cancel() - CefDownloadManager::getInstance().UpdateDownloadItem(item); + DownloadManager::getInstance().UpdateDownloadItem(item); } void diff --git a/src/Cef/CefViewWidget.h b/src/Cef/CefViewWidget.h index be60d87..7c25899 100644 --- a/src/Cef/CefViewWidget.h +++ b/src/Cef/CefViewWidget.h @@ -1,5 +1,5 @@ -#ifndef CEFVIEWWIDGET_H -#define CEFVIEWWIDGET_H +#ifndef CUSTOMCEFVIEW_H +#define CUSTOMCEFVIEW_H #include @@ -13,7 +13,7 @@ class CefViewWidget : public QCefView Q_OBJECT public: - CefViewWidget(const QString url, const QCefSetting* setting, QWidget* parent = 0); + CefViewWidget(const QString url, const QCefSetting* setting, QWidget* parent = 0); ~CefViewWidget(); diff --git a/src/Cef/CefWidget.cpp b/src/Cef/CefWidget.cpp index b06857b..96e990e 100644 --- a/src/Cef/CefWidget.cpp +++ b/src/Cef/CefWidget.cpp @@ -1,149 +1,30 @@ #include "CefWidget.h" -CefWidget::CefWidget(QWidget* parent) -: QMainWindow(parent /*, Qt::FramelessWindowHint*/) +CefWidget::CefWidget(QWidget *parent) + : QWidget(parent) { - initBrowser(); + QWidget* widget = new QWidget(); + + this->layout()->addWidget(widget); } + CefWidget::~CefWidget() { -} -void CefWidget::initBrowser() -{ - m_centralWidget = new QWidget(this); - if (m_viewWidget) { - m_viewWidget->deleteLater(); - m_viewWidget = nullptr; - } - - QCefSetting setting; - setting.setWindowlessFrameRate(1000); - setting.setHardwareAcceleration(false); - // setting.setBackgroundColor(Qt::magenta); - - m_viewWidget = new CefViewWidget("https://www.baidu.com/", &setting, this); - // connect the invokeMethod to the slot - // connect(m_viewWidget, &QCefView::invokeMethod, this, &CefWidget::onInvokeMethod); - - // // connect the cefQueryRequest to the slot - // connect(m_viewWidget, &QCefView::cefUrlRequest, this, &CefWidget::onQCefUrlRequest); - // connect(m_viewWidget, &QCefView::cefQueryRequest, this, &CefWidget::onQCefQueryRequest); - // connect(m_viewWidget, &QCefView::reportJavascriptResult, this, &CefWidget::onJavascriptResult); - // connect(m_viewWidget, &QCefView::loadStart, this, &CefWidget::onLoadStart); - // connect(m_viewWidget, &QCefView::loadEnd, this, &CefWidget::onLoadEnd); - // connect(m_viewWidget, &QCefView::loadError, this, &CefWidget::onLoadError); - m_centralWidget->layout()->addWidget(m_viewWidget); - setCentralWidget(m_centralWidget); -} -void -CefWidget::onInvokeMethod(const QCefBrowserId& browserId, - const QCefFrameId& frameId, - const QString& method, - const QVariantList& arguments) -{ - // extract the arguments and dispatch the invocation to corresponding handler - if (0 == method.compare("TestMethod")) { - QString title("QCef InvokeMethod Notify"); - QString text = QString("================== Current Thread: QT_UI ==================\r\n" - "Frame: %1\r\n" - "Method: %2\r\n" - "Arguments:\r\n") - .arg(frameId) - .arg(method); - for (int i = 0; i < arguments.size(); i++) { - auto jv = QJsonValue::fromVariant(arguments[i]); - - // clang-format off - text.append( - QString("%1 Type:%2, Value:%3\r\n") - .arg(i).arg(arguments[i].typeName()).arg(arguments[i].toString()) - ); - // clang-format on - } - - auto jsonValue = QJsonDocument::fromVariant(arguments); - auto jsonString = QString(jsonValue.toJson()); - text.append(QString("\r\nArguments List in JSON format:\r\n%1").arg(jsonString)); - - QMessageBox::information(this->window(), title, text); - } else { - } } -void -CefWidget::onQCefUrlRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& url) +void CefWidget::WaitCefQuit() { - QString title("QCef URL Request"); - QString text = QString("Current Thread: QT_UI\r\n" - "URL: %1") - .arg(url); - QMessageBox::information(this->window(), title, text); } -void -CefWidget::onQCefQueryRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QCefQuery& query) +void CefWidget::resizeEvent(QResizeEvent *ev) { - QString title("QCef Query Request"); - QString text = QString("Current Thread: QT_UI\r\n" - "Query: %1") - .arg(query.request()); - - QMessageBox::information(this->window(), title, text); - - QString response = query.request().toUpper(); - query.setResponseResult(true, response); - m_viewWidget->responseQCefQuery(query); } -void -CefWidget::onJavascriptResult(const QCefBrowserId& browserId, - const QCefFrameId& frameId, - const QString& context, - const QVariant& result) +void CefWidget::closeEvent(QCloseEvent* ev) { - auto jsonValue = QJsonDocument::fromVariant(result); - auto jsonString = QString(jsonValue.toJson()); - QString title("Javascript result notification"); - QString text = QString("Context: %1\r\nResult in JSON format:\r\n%2").arg(context).arg(jsonString); - - QMessageBox::information(this->window(), title, text); } -void -CefWidget::onLoadingStateChanged(const QCefBrowserId& browserId, bool isLoading, bool canGoBack, bool canGoForward) -{ - qDebug() << "onLoadingStateChanged, browserId:" << browserId << ", isLoading:" << isLoading - << ", canGoBack:" << canGoBack << ", canGoForward:" << canGoForward; -} -void -CefWidget::onLoadStart(const QCefBrowserId& browserId, - const QCefFrameId& frameId, - bool isMainFrame, - int transitionType) -{ - qDebug() << "onLoadStart, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame - << ", transitionType:" << transitionType; -} - -void -CefWidget::onLoadEnd(const QCefBrowserId& browserId, const QCefFrameId& frameId, bool isMainFrame, int httpStatusCode) -{ - qDebug() << "onLoadEnd, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame - << ", httpStatusCode:" << httpStatusCode; -} - -void -CefWidget::onLoadError(const QCefBrowserId& browserId, - const QCefFrameId& frameId, - bool isMainFrame, - int errorCode, - const QString& errorMsg, - const QString& failedUrl) -{ - qDebug() << "onLoadError, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame - << ", errorCode:" << errorCode; -} \ No newline at end of file diff --git a/src/Cef/CefWidget.h b/src/Cef/CefWidget.h index 1a88f97..53d403f 100644 --- a/src/Cef/CefWidget.h +++ b/src/Cef/CefWidget.h @@ -1,53 +1,23 @@ -#ifndef BROWSER_H -#define BROWSER_H +#ifndef CEFWIDGET_H +#define CEFWIDGET_H #include -#include -#include -#include -#include -#include "CefviewWidget.h" -#include -class CefWidget : public QMainWindow +#include +#include + +class CefWidget : public QWidget { Q_OBJECT public: CefWidget(QWidget *parent = nullptr); ~CefWidget(); - + void WaitCefQuit(); private: void initBrowser(); - CefViewWidget* m_viewWidget={}; - QWidget* m_centralWidget={}; - // QCefView slots -protected slots: - void onInvokeMethod(const QCefBrowserId &browserId, - const QCefFrameId &frameId, - const QString &method, - const QVariantList &arguments); - - void onQCefUrlRequest(const QCefBrowserId &browserId, const QCefFrameId &frameId, const QString &url); - - void onQCefQueryRequest(const QCefBrowserId &browserId, const QCefFrameId &frameId, const QCefQuery &query); - - void onJavascriptResult(const QCefBrowserId &browserId, - const QCefFrameId &frameId, - const QString &context, - const QVariant &result); - - void onLoadingStateChanged(const QCefBrowserId &browserId, bool isLoading, bool canGoBack, bool canGoForward); - - void onLoadStart(const QCefBrowserId &browserId, const QCefFrameId &frameId, bool isMainFrame, int transitionType); - - void onLoadEnd(const QCefBrowserId &browserId, const QCefFrameId &frameId, bool isMainFrame, int httpStatusCode); - - void onLoadError(const QCefBrowserId &browserId, - const QCefFrameId &frameId, - bool isMainFrame, - int errorCode, - const QString &errorMsg, - const QString &failedUrl); - + //CefRefPtr simple_handler_; + QWidget* widget; +protected: + void resizeEvent(QResizeEvent *event) override; + void closeEvent(QCloseEvent* ev) override; }; - #endif \ No newline at end of file diff --git a/src/Cef/CefDownloadManager.cpp b/src/Cef/DownloadManager.cpp similarity index 70% rename from src/Cef/CefDownloadManager.cpp rename to src/Cef/DownloadManager.cpp index d21b565..38ada2a 100644 --- a/src/Cef/CefDownloadManager.cpp +++ b/src/Cef/DownloadManager.cpp @@ -1,18 +1,18 @@ -#include "CefDownloadManager.h" +#include "DownloadManager.h" #include -CefDownloadManager& -CefDownloadManager::getInstance() +DownloadManager& +DownloadManager::getInstance() { - static CefDownloadManager s_instance; + static DownloadManager s_instance; return s_instance; } void -CefDownloadManager::AddNewDownloadItem(const QSharedPointer& item) +DownloadManager::AddNewDownloadItem(const QSharedPointer& item) { - qDebug() << "downloadManager::AddNewDownloadItem:" + qDebug() << "DownloadManager::AddNewDownloadItem:" << " id: " << item->id() << "\n" << " name: " << item->suggestedFileName() << "\n" << " path: " << item->fullPath() << "\n" @@ -26,9 +26,9 @@ CefDownloadManager::AddNewDownloadItem(const QSharedPointer& i } void -CefDownloadManager::UpdateDownloadItem(const QSharedPointer& item) +DownloadManager::UpdateDownloadItem(const QSharedPointer& item) { - qDebug() << "downloadManager::UpdateDownloadItem:" + qDebug() << "DownloadManager::UpdateDownloadItem:" << " id: " << item->id() << "\n" << " name: " << item->suggestedFileName() << "\n" << " path: " << item->fullPath() << "\n" @@ -41,9 +41,9 @@ CefDownloadManager::UpdateDownloadItem(const QSharedPointer& i m_mapDownloadingItem.remove(item->id()); } -CefDownloadManager::CefDownloadManager() {} +DownloadManager::DownloadManager() {} -CefDownloadManager::~CefDownloadManager() +DownloadManager::~DownloadManager() { for (auto& item : m_mapDownloadingItem) { item->cancel(); diff --git a/src/Cef/CefDownloadManager.h b/src/Cef/DownloadManager.h similarity index 65% rename from src/Cef/CefDownloadManager.h rename to src/Cef/DownloadManager.h index 048d20c..e21c76d 100644 --- a/src/Cef/CefDownloadManager.h +++ b/src/Cef/DownloadManager.h @@ -1,5 +1,5 @@ -#ifndef CEFDOWNLOADMANAGER_H -#define CEFDOWNLOADMANAGER_H +#ifndef DOWNLOADMANAGER_H +#define DOWNLOADMANAGER_H #pragma once #include @@ -7,18 +7,18 @@ #include -class CefDownloadManager +class DownloadManager { public: - static CefDownloadManager& getInstance(); + static DownloadManager& getInstance(); void AddNewDownloadItem(const QSharedPointer& item); void UpdateDownloadItem(const QSharedPointer& item); private: - CefDownloadManager(); - ~CefDownloadManager(); + DownloadManager(); + ~DownloadManager(); QMap> m_mapDownloadingItem; }; diff --git a/src/Cef/MainWindow.cpp b/src/Cef/MainWindow.cpp new file mode 100644 index 0000000..f61893b --- /dev/null +++ b/src/Cef/MainWindow.cpp @@ -0,0 +1,317 @@ +#include "MainWindow.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define URL_ROOT "http://QCefViewDoc" +#define LEFT_INDEX_URL URL_ROOT "/left.html" +#define RIGHT_INDEX_URL URL_ROOT "/right.html" + +MainWindow::MainWindow(QWidget* parent) + : QMainWindow(parent /*, Qt::FramelessWindowHint*/) +{ + m_ui.setupUi(this); + +#ifdef Q_OS_MACOS + this->m_ui.nativeContainer->setContentsMargins(0, 28, 0, 0); +#endif + + setupWindow(); + + // setWindowFlags(Qt::FramelessWindowHint); + // setAttribute(Qt::WA_TranslucentBackground); + + connect(m_ui.btn_showDevTools, &QPushButton::clicked, this, &MainWindow::onBtnShowDevToolsClicked); + connect(m_ui.btn_reloadRight, &QPushButton::clicked, this, &MainWindow::onBtnReloadRightViewClicked); + connect(m_ui.btn_recreateRight, &QPushButton::clicked, this, &MainWindow::onBtnRecreateRightViewClicked); + connect(m_ui.btn_changeColor, &QPushButton::clicked, this, &MainWindow::onBtnChangeColorClicked); + connect(m_ui.btn_setFocus, &QPushButton::clicked, this, &MainWindow::onBtnSetFocusClicked); + connect(m_ui.btn_callJSCode, &QPushButton::clicked, this, &MainWindow::onBtnCallJSCodeClicked); + connect(m_ui.btn_newBrowser, &QPushButton::clicked, this, &MainWindow::onBtnNewBrowserClicked); + connect(m_ui.btn_quitApp, &QPushButton::clicked, qApp, &QCoreApplication::quit); + + // build the path to the web resource + QDir dir = QCoreApplication::applicationDirPath(); +#if defined(Q_OS_MACOS) + QString webResourceDir = /*QString("file://") +*/ QDir::toNativeSeparators(dir.filePath("../Resources/webres")); +#else + QString webResourceDir = /*QString("file://") +*/ QDir::toNativeSeparators(dir.filePath("webres")); +#endif + + // add a local folder to URL map (global) + QCefContext::instance()->addLocalFolderResource(webResourceDir, URL_ROOT); + + createLeftCefView(); + createRightCefView(); +} + +MainWindow::~MainWindow() {} + +void +MainWindow::createLeftCefView() +{ + if (m_pLeftCefViewWidget) { + m_pLeftCefViewWidget->deleteLater(); + m_pLeftCefViewWidget = nullptr; + } + + QCefSetting setting; + setting.setWindowlessFrameRate(1000); + setting.setHardwareAcceleration(false); + // setting.setBackgroundColor(Qt::magenta); + + m_pLeftCefViewWidget = new CefViewWidget("https://www.testufo.com/", &setting, this); + // connect the invokeMethod to the slot + connect(m_pLeftCefViewWidget, &QCefView::invokeMethod, this, &MainWindow::onInvokeMethod); + + // connect the cefQueryRequest to the slot + connect(m_pLeftCefViewWidget, &QCefView::cefUrlRequest, this, &MainWindow::onQCefUrlRequest); + connect(m_pLeftCefViewWidget, &QCefView::cefQueryRequest, this, &MainWindow::onQCefQueryRequest); + connect(m_pLeftCefViewWidget, &QCefView::reportJavascriptResult, this, &MainWindow::onJavascriptResult); + connect(m_pLeftCefViewWidget, &QCefView::loadStart, this, &MainWindow::onLoadStart); + connect(m_pLeftCefViewWidget, &QCefView::loadEnd, this, &MainWindow::onLoadEnd); + connect(m_pLeftCefViewWidget, &QCefView::loadError, this, &MainWindow::onLoadError); + + m_ui.leftCefViewContainer->layout()->addWidget(m_pLeftCefViewWidget); +} + +void +MainWindow::createRightCefView() +{ + if (m_pRightCefViewWidget) { + m_pRightCefViewWidget->deleteLater(); + m_pRightCefViewWidget = nullptr; + } + + // build settings for per QCefView + QCefSetting setting; +#if CEF_VERSION_MAJOR < 100 + setting.setPlugins(false); +#endif + setting.setWindowlessFrameRate(1000); + setting.setHardwareAcceleration(true); + QColor background(0, 255, 0, 255); + setting.setBackgroundColor(background); + + // create the QCefView widget and add it to the layout container + // m_pRightCefViewWidget = new QCefView(RIGHT_INDEX_URL, &setting, this); + m_pRightCefViewWidget = new QCefView("https://www.testufo.com/", &setting, this); + + // auto vl = new QVBoxLayout(m_pRightCefViewWidget); + // auto btn = new QPushButton("TEST BUTTON OVERLAY", m_pRightCefViewWidget); + //// btn->setFixedSize(320, 240); + // btn->setStyleSheet("background-color: rgba(1, 1, 1, 0);"); + // btn->setAttribute(Qt::WA_TranslucentBackground); + // btn->setWindowFlags(Qt::FramelessWindowHint); + // btn->setAttribute(Qt::WA_NoSystemBackground); + // vl->setAlignment(Qt::AlignVCenter); + + // vl->addWidget(btn); + // m_pRightCefViewWidget->setLayout(vl); + + // all the following values will disable the context menu for both NCW and OSR mode + // m_pRightCefViewWidget->setContextMenuPolicy(Qt::NoContextMenu); + // m_pRightCefViewWidget->setContextMenuPolicy(Qt::ActionsContextMenu); + // m_pRightCefViewWidget->setContextMenuPolicy(Qt::CustomContextMenu); + // m_pRightCefViewWidget->setContextMenuPolicy(Qt::PreventContextMenu); + m_pRightCefViewWidget->setContextMenuPolicy(Qt::DefaultContextMenu); + + // add the QCefView widget to the layout + m_ui.rightCefViewContainer->layout()->addWidget(m_pRightCefViewWidget); +} + +void +MainWindow::onInvokeMethod(const QCefBrowserId& browserId, + const QCefFrameId& frameId, + const QString& method, + const QVariantList& arguments) +{ + // extract the arguments and dispatch the invocation to corresponding handler + if (0 == method.compare("TestMethod")) { + QString title("QCef InvokeMethod Notify"); + QString text = QString("================== Current Thread: QT_UI ==================\r\n" + "Frame: %1\r\n" + "Method: %2\r\n" + "Arguments:\r\n") + .arg(frameId) + .arg(method); + + for (int i = 0; i < arguments.size(); i++) { + auto jv = QJsonValue::fromVariant(arguments[i]); + + // clang-format off + text.append( + QString("%1 Type:%2, Value:%3\r\n") + .arg(i).arg(arguments[i].typeName()).arg(arguments[i].toString()) + ); + // clang-format on + } + + auto jsonValue = QJsonDocument::fromVariant(arguments); + auto jsonString = QString(jsonValue.toJson()); + text.append(QString("\r\nArguments List in JSON format:\r\n%1").arg(jsonString)); + + QMessageBox::information(this->window(), title, text); + } else { + } +} + +void +MainWindow::onQCefUrlRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& url) +{ + QString title("QCef URL Request"); + QString text = QString("Current Thread: QT_UI\r\n" + "URL: %1") + .arg(url); + + QMessageBox::information(this->window(), title, text); +} + +void +MainWindow::onQCefQueryRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QCefQuery& query) +{ + QString title("QCef Query Request"); + QString text = QString("Current Thread: QT_UI\r\n" + "Query: %1") + .arg(query.request()); + + QMessageBox::information(this->window(), title, text); + + QString response = query.request().toUpper(); + query.setResponseResult(true, response); + m_pLeftCefViewWidget->responseQCefQuery(query); +} + +void +MainWindow::onJavascriptResult(const QCefBrowserId& browserId, + const QCefFrameId& frameId, + const QString& context, + const QVariant& result) +{ + auto jsonValue = QJsonDocument::fromVariant(result); + auto jsonString = QString(jsonValue.toJson()); + + QString title("Javascript result notification"); + QString text = QString("Context: %1\r\nResult in JSON format:\r\n%2").arg(context).arg(jsonString); + + QMessageBox::information(this->window(), title, text); +} + +void +MainWindow::onLoadingStateChanged(const QCefBrowserId& browserId, bool isLoading, bool canGoBack, bool canGoForward) +{ + qDebug() << "onLoadingStateChanged, browserId:" << browserId << ", isLoading:" << isLoading + << ", canGoBack:" << canGoBack << ", canGoForward:" << canGoForward; +} + +void +MainWindow::onLoadStart(const QCefBrowserId& browserId, + const QCefFrameId& frameId, + bool isMainFrame, + int transitionType) +{ + qDebug() << "onLoadStart, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame + << ", transitionType:" << transitionType; +} + +void +MainWindow::onLoadEnd(const QCefBrowserId& browserId, const QCefFrameId& frameId, bool isMainFrame, int httpStatusCode) +{ + qDebug() << "onLoadEnd, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame + << ", httpStatusCode:" << httpStatusCode; +} + +void +MainWindow::onLoadError(const QCefBrowserId& browserId, + const QCefFrameId& frameId, + bool isMainFrame, + int errorCode, + const QString& errorMsg, + const QString& failedUrl) +{ + qDebug() << "onLoadError, browserId:" << browserId << ", frameId:" << frameId << ", isMainFrame:" << isMainFrame + << ", errorCode:" << errorCode; +} + +void +MainWindow::onBtnShowDevToolsClicked() +{ + if (m_pLeftCefViewWidget) { + m_pLeftCefViewWidget->showDevTools(); + } +} + +void +MainWindow::onBtnReloadRightViewClicked() +{ + if (m_pRightCefViewWidget) { + m_pRightCefViewWidget->navigateToUrl("https://www.google.com"); + } +} + +void +MainWindow::onBtnRecreateRightViewClicked() +{ + createRightCefView(); +} + +void +MainWindow::onBtnChangeColorClicked() +{ + if (m_pLeftCefViewWidget) { + // create a random color + QColor color(QRandomGenerator::global()->generate()); + + // create the cef event and set the arguments + QCefEvent event("colorChange"); + event.arguments().append(QVariant::fromValue(color.name(QColor::HexArgb))); + + // broadcast the event to all frames in all browsers created by this QCefView widget + m_pLeftCefViewWidget->broadcastEvent(event); + } +} + +void +MainWindow::onBtnCallJSCodeClicked() +{ + QString context = "helloQCefView"; + QString code = "alert('hello QCefView'); return {k1: 'str', k2: true, k3: 100};"; + m_pLeftCefViewWidget->executeJavascriptWithResult(QCefView::MainFrameID, code, "", context); +} + +void +MainWindow::onBtnSetFocusClicked() +{ + if (m_pLeftCefViewWidget) { + m_pLeftCefViewWidget->setFocus(); + } +} + +void +MainWindow::onBtnNewBrowserClicked() +{ + QMainWindow* w = new QMainWindow(nullptr); + w->setAttribute(Qt::WA_DeleteOnClose); + + QCefSetting settings; + QCefView* view = new QCefView("https://cefview.github.io/QCefView/", &settings, w); + + w->setCentralWidget(view); + w->resize(1024, 768); + w->show(); +} + +#ifndef Q_OS_MACOS +void +MainWindow::setupWindow() +{ +} +#endif diff --git a/src/Cef/MainWindow.h b/src/Cef/MainWindow.h new file mode 100644 index 0000000..019e16c --- /dev/null +++ b/src/Cef/MainWindow.h @@ -0,0 +1,77 @@ +#ifndef QCEFVIEWTEST_H +#define QCEFVIEWTEST_H + +#include +#include + +#include "ui_MainWindow.h" + +#include "CefViewWidget.h" + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget* parent = 0); + ~MainWindow(); + +protected: + void createLeftCefView(); + void createRightCefView(); + void setupWindow(); + + // QCefView slots +protected slots: + + void onInvokeMethod(const QCefBrowserId& browserId, + const QCefFrameId& frameId, + const QString& method, + const QVariantList& arguments); + + void onQCefUrlRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& url); + + void onQCefQueryRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QCefQuery& query); + + void onJavascriptResult(const QCefBrowserId& browserId, + const QCefFrameId& frameId, + const QString& context, + const QVariant& result); + + void onLoadingStateChanged(const QCefBrowserId& browserId, bool isLoading, bool canGoBack, bool canGoForward); + + void onLoadStart(const QCefBrowserId& browserId, const QCefFrameId& frameId, bool isMainFrame, int transitionType); + + void onLoadEnd(const QCefBrowserId& browserId, const QCefFrameId& frameId, bool isMainFrame, int httpStatusCode); + + void onLoadError(const QCefBrowserId& browserId, + const QCefFrameId& frameId, + bool isMainFrame, + int errorCode, + const QString& errorMsg, + const QString& failedUrl); + + // ui slots +protected slots: + void onBtnShowDevToolsClicked(); + + void onBtnReloadRightViewClicked(); + + void onBtnRecreateRightViewClicked(); + + void onBtnChangeColorClicked(); + + void onBtnSetFocusClicked(); + + void onBtnCallJSCodeClicked(); + + void onBtnNewBrowserClicked(); + +private: + Ui::MainWindow m_ui; + + QCefView* m_pLeftCefViewWidget = nullptr; + QCefView* m_pRightCefViewWidget = nullptr; +}; + +#endif // QCEFVIEWTEST_H diff --git a/src/Cef/MainWindow.ui b/src/Cef/MainWindow.ui new file mode 100644 index 0000000..8aefc20 --- /dev/null +++ b/src/Cef/MainWindow.ui @@ -0,0 +1,250 @@ + + + MainWindow + + + + 0 + 0 + 1280 + 900 + + + + QCefViewTest + + + + + 3 + + + 2 + + + 2 + + + 2 + + + 2 + + + + + + 0 + 0 + + + + + #nativeContainer { + background-color: rgb(170, 255, 255); + } + + + + + 6 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + #label{ + font: 12pt "MS Shell Dlg 2"; + } + + + + Native Area + + + Qt::AlignCenter + + + + + + + Show Left DevTools + + + + + + + Reload Right QCefView + + + + + + + Recreate Right QCefView + + + + + + + ChangeColor + + + + + + + + + + SetFocus + + + + + + + + + + CallJSCode + + + + + + + + + + NewBrowser + + + + + + + Exit App + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + 0 + 0 + + + + QFrame::Plain + + + Qt::Horizontal + + + + QWidget#leftCefViewContainer { + background-color: rgb(217, 183, 255); +} + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + QWidget#rightCefViewContainer { + background-color: rgb(217, 183, 255); +} + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + + + + + + btn_showDevTools + btn_reloadRight + btn_recreateRight + btn_changeColor + lineEdit + lineEdit_2 + btn_callJSCode + lineEdit_3 + btn_newBrowser + btn_quitApp + + + + diff --git a/src/compatibility.manifest b/src/compatibility.manifest new file mode 100644 index 0000000..755c272 --- /dev/null +++ b/src/compatibility.manifest @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/ctai.exe.manifest b/src/ctai.exe.manifest new file mode 100644 index 0000000..d36f084 --- /dev/null +++ b/src/ctai.exe.manifest @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/main.cpp b/src/main.cpp index fd7ad2f..3251c09 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,83 +4,34 @@ #include "sui_base_ex.h" #include "ctai.h" #include -#include "QCefContext.h" -#include "Cef/CefWidget.h" -// void init_Browser(QApplication& instance,int argc, char *argv[]){ -// // build QCefConfig -// QCefConfig config; -// // set user agent -// config.setUserAgent("QCefViewTest"); -// // set log level -// config.setLogLevel(QCefConfig::LOGSEVERITY_DEFAULT); -// // set JSBridge object name (default value is CefViewClient) -// config.setBridgeObjectName("CallBridge"); -// // set Built-in scheme name (default value is CefView) -// config.setBuiltinSchemeName("CefView"); -// // port for remote debugging (default is 0 and means to disable remote debugging) -// //config.setRemoteDebuggingPort(9000); -// // set background color for all browsers -// // (QCefSetting.setBackgroundColor will overwrite this value for specified browser instance) -// // config.setBackgroundColor(Qt::lightGray); - -// // WindowlessRenderingEnabled is set to true by default, -// // set to false to disable the OSR mode -// //config.setWindowlessRenderingEnabled(true); -// //config.addCommandLineSwitch("no-sandbox"); -// // add command line args, you can any cef supported switches or parameters -// //config.addCommandLineSwitch("use-mock-keychain"); -// //config.addCommandLineSwitch("disable-gpu"); -// // config.addCommandLineSwitch("enable-media-stream"); -// // config.addCommandLineSwitch("allow-file-access-from-files"); -// // config.addCommandLineSwitch("disable-spell-checking"); -// // config.addCommandLineSwitch("disable-site-isolation-trials"); -// // config.addCommandLineSwitch("enable-aggressive-domstorage-flushing"); -// //config.addCommandLineSwitchWithValue("renderer-process-limit", "1"); -// // allow remote debugging -// //config.addCommandLineSwitchWithValue("remote-allow-origins", "*"); -// // config.addCommandLineSwitchWithValue("disable-features", "BlinkGenPropertyTrees,TranslateUI,site-per-process"); - -// // set cache folder -// config.setCachePath(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)); - -// // create QCefContext instance with config, -// // the lifecycle of cefContext must be the same as QApplication instance -// QCefContext cefContext(&instance, argc, argv, &config); -// } -// int main(int argc, char *argv[]) -// { -// QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); -// qputenv("QT_QPA_PLATFORM", "windows:fontengine=freetype"); -// QApplication a(argc, argv); -// //加载语言包 -// QTranslator qtBaseTranslator; -// if (!qtBaseTranslator.load(QStringLiteral(":/res/translator/qtbase_zh_CN.qm"))) -// { -// // 处理加载翻译文件失败的情况 -// qDebug() << "Failed to load translation file."; -// return -1; -// } -// a.installTranslator(&qtBaseTranslator); -// //初始化浏览器 -// init_Browser(a,argc,argv); -// //读取窗体的配置并初始化 -// sui_init_config(); -// ctai x; -// x.init_layout(); -// // 仅限于windows平台 -// #if defined(__WIN32__) -// x.title()->set_type(QD_TYPE::QD_EXIT); -// #endif -// x.show(); -// CefWidget w; -// w.show(); -// return a.exec(); -// } #include -int -main(int argc, char* argv[]) +#include "cef/MainWindow.h" + +int main(int argc, char *argv[]) { -#if (QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)) + QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); + qputenv("QT_QPA_PLATFORM", "windows:fontengine=freetype"); + QApplication a(argc, argv); + // 加载语言包 + QTranslator qtBaseTranslator; + if (!qtBaseTranslator.load(QStringLiteral(":/res/translator/qtbase_zh_CN.qm"))) + { + // 处理加载翻译文件失败的情况 + qDebug() << "Failed to load translation file."; + return -1; + } + a.installTranslator(&qtBaseTranslator); + // 读取窗体的配置并初始化 + sui_init_config(); + ctai x; + x.init_layout(); + // 仅限于windows平台 +#if defined(__WIN32__) + x.title()->set_type(QD_TYPE::QD_EXIT); +#endif + x.show(); + //初始化CEF + #if (QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)) // For off-screen rendering, Qt::AA_EnableHighDpiScaling must be enabled. If not, // then all devicePixelRatio methods will always return 1.0, // so CEF will not scale the web content @@ -90,10 +41,6 @@ main(int argc, char* argv[]) QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); #endif #endif - - // create QApplication instance - QApplication a(argc, argv); - // build QCefConfig QCefConfig config; // set user agent @@ -113,9 +60,16 @@ main(int argc, char* argv[]) // WindowlessRenderingEnabled is set to true by default, // set to false to disable the OSR mode config.setWindowlessRenderingEnabled(true); - // add command line args, you can any cef supported switches or parameters config.addCommandLineSwitch("use-mock-keychain"); + config.addCommandLineSwitch("single-process"); + config.addCommandLineSwitch("enable-gpu"); + config.addCommandLineSwitch("enable-gpu-rasterization"); + config.addCommandLineSwitch("enable-webgl"); + config.addCommandLineSwitch("enable-impl-side-painting"); + config.addCommandLineSwitch("disable-software-rasterizer"); + config.addCommandLineSwitch("multi-threaded-message-loop"); + // config.addCommandLineSwitch("disable-gpu"); // config.addCommandLineSwitch("enable-media-stream"); // config.addCommandLineSwitch("allow-file-access-from-files"); @@ -134,11 +88,8 @@ main(int argc, char* argv[]) // the lifecycle of cefContext must be the same as QApplication instance QCefContext cefContext(&a, argc, argv, &config); - // application window - CefWidget w; + MainWindow w; w.show(); - - // flying return a.exec(); }