diff --git a/src/lib/tools/scripts.h b/src/lib/tools/scripts.h index 82389a995..76bd34808 100644 --- a/src/lib/tools/scripts.h +++ b/src/lib/tools/scripts.h @@ -23,6 +23,8 @@ #include "qzcommon.h" +class QWebEngineView; + class QUPZILLA_EXPORT Scripts { public: diff --git a/src/lib/webengine/webinspector.cpp b/src/lib/webengine/webinspector.cpp index 6514bc2f2..9a0f00838 100644 --- a/src/lib/webengine/webinspector.cpp +++ b/src/lib/webengine/webinspector.cpp @@ -16,59 +16,72 @@ * along with this program. If not, see . * ============================================================ */ #include "webinspector.h" -#include "toolbutton.h" -#include "iconprovider.h" +#include "mainapplication.h" -#if QTWEBENGINE_DISABLED +#include +#include +#include +#include -#include +QList WebInspector::s_views; -WebInspector::WebInspector(QWidget* parent) - : QWebInspector(parent) - , m_closeButton(0) - , m_blockHideEvent(true) +WebInspector::WebInspector(QWidget *parent) + : QWebEngineView(parent) { setObjectName(QSL("web-inspector")); setMinimumHeight(80); + + registerView(this); + + connect(page(), &QWebEnginePage::windowCloseRequested, this, &WebInspector::deleteLater); + connect(page(), &QWebEnginePage::loadFinished, this, &WebInspector::updateCloseButton); +} + +WebInspector::~WebInspector() +{ + unregisterView(this); +} + +void WebInspector::setView(QWebEngineView *view) +{ + QUrl inspectorUrl = QUrl(QSL("http://localhost:%1").arg(WEBINSPECTOR_PORT)); + int index = s_views.indexOf(view); + + QNetworkReply *reply = mApp->networkManager()->get(QNetworkRequest(inspectorUrl.resolved(QUrl("json/list")))); + connect(reply, &QNetworkReply::finished, this, [=]() { + QJsonArray clients = QJsonDocument::fromJson(reply->readAll()).array(); + QUrl pageUrl; + if (clients.size() > index) { + QJsonObject object = clients.at(index).toObject(); + pageUrl = inspectorUrl.resolved(QUrl(object.value(QSL("devtoolsFrontendUrl")).toString())); + } + load(pageUrl); + pushView(this); + show(); + }); +} + +void WebInspector::pushView(QWebEngineView *view) +{ + s_views.removeOne(view); + s_views.prepend(view); +} + +void WebInspector::registerView(QWebEngineView *view) +{ + s_views.prepend(view); +} + +void WebInspector::unregisterView(QWebEngineView *view) +{ + s_views.removeOne(view); } void WebInspector::updateCloseButton() { - if (!m_closeButton) { - m_closeButton = new ToolButton(this); - m_closeButton->setAutoRaise(true); - m_closeButton->setIcon(IconProvider::standardIcon(QStyle::SP_DialogCloseButton)); - connect(m_closeButton, SIGNAL(clicked()), this, SLOT(hideInspector())); - } - - m_closeButton->show(); - m_closeButton->move(width() - m_closeButton->width(), 0); + page()->runJavaScript(QL1S("var button = document.getElementsByClassName('toolbar-close-button-item')[0];" + "button.style.display = 'inline-block';" + "button.addEventListener('click', function() {" + " window.close();" + "})")); } - -void WebInspector::hideInspector() -{ - m_blockHideEvent = false; - hide(); - m_blockHideEvent = true; - - // This is needed to correctly show close button after QWebInspector re-initialization - m_closeButton->deleteLater(); - m_closeButton = 0; -} - -void WebInspector::hideEvent(QHideEvent* event) -{ - // Prevent re-initializing QWebInspector after changing tab - if (!m_blockHideEvent) { - QWebInspector::hideEvent(event); - } -} - -void WebInspector::resizeEvent(QResizeEvent* event) -{ - QWebInspector::resizeEvent(event); - - QTimer::singleShot(0, this, SLOT(updateCloseButton())); -} - -#endif diff --git a/src/lib/webengine/webinspector.h b/src/lib/webengine/webinspector.h index 9ef8eb63f..2901a87ee 100644 --- a/src/lib/webengine/webinspector.h +++ b/src/lib/webengine/webinspector.h @@ -18,31 +18,33 @@ #ifndef WEBINSPECTORDOCKWIDGET_H #define WEBINSPECTORDOCKWIDGET_H -#if QTWEBENGINE_DISABLED -#include +#define WEBINSPECTOR_PORT "33417" + +#include #include "qzcommon.h" class ToolButton; -class QUPZILLA_EXPORT WebInspector : public QWebInspector +class QUPZILLA_EXPORT WebInspector : public QWebEngineView { Q_OBJECT public: - explicit WebInspector(QWidget* parent = 0); + explicit WebInspector(QWidget *parent = Q_NULLPTR); + ~WebInspector(); + + void setView(QWebEngineView *view); + + static void pushView(QWebEngineView *view); + static void registerView(QWebEngineView *view); + static void unregisterView(QWebEngineView *view); private slots: void updateCloseButton(); - void hideInspector(); private: - void hideEvent(QHideEvent* event); - void resizeEvent(QResizeEvent* event); - - ToolButton* m_closeButton; - bool m_blockHideEvent; + static QList s_views; }; -#endif #endif // WEBINSPECTORDOCKWIDGET_H diff --git a/src/lib/webengine/webview.cpp b/src/lib/webengine/webview.cpp index 2461f25c4..86822646c 100644 --- a/src/lib/webengine/webview.cpp +++ b/src/lib/webengine/webview.cpp @@ -32,6 +32,7 @@ #include "qzsettings.h" #include "enhancedmenu.h" #include "locationbar.h" +#include "webinspector.h" #ifdef USE_HUNSPELL #include "qtwebkit/spellcheck/speller.h" @@ -64,6 +65,7 @@ WebView::WebView(QWidget* parent) , m_page(0) , m_disableTouchMocking(false) , m_isReloading(false) + , m_firstLoad(false) { connect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted())); connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int))); @@ -76,6 +78,8 @@ WebView::WebView(QWidget* parent) installEventFilter(this); + WebInspector::registerView(this); + #ifdef Q_OS_MAC new MacWebViewScroller(this); #endif @@ -83,7 +87,7 @@ WebView::WebView(QWidget* parent) WebView::~WebView() { - //delete m_page; + WebInspector::unregisterView(this); } QIcon WebView::icon() const @@ -180,6 +184,16 @@ void WebView::setPage(QWebEnginePage* page) #endif } +void WebView::load(const QUrl &url) +{ + QWebEngineView::load(url); + + if (!m_firstLoad) { + m_firstLoad = true; + WebInspector::pushView(this); + } +} + void WebView::load(const LoadRequest &request) { const QUrl reqUrl = request.url(); @@ -1594,7 +1608,7 @@ void WebView::loadRequest(const LoadRequest &req) else QWebEngineView::load(req.networkRequest(), QNetworkAccessManager::PostOperation, req.data()); #else - QWebEngineView::load(req.url()); + load(req.url()); #endif } diff --git a/src/lib/webengine/webview.h b/src/lib/webengine/webview.h index e6c3f48b6..0c263e41a 100644 --- a/src/lib/webengine/webview.h +++ b/src/lib/webengine/webview.h @@ -44,6 +44,7 @@ public: WebPage* page() const; void setPage(QWebEnginePage* page); + void load(const QUrl &url); void load(const LoadRequest &request); bool isLoading() const; @@ -214,6 +215,7 @@ private: bool m_disableTouchMocking; bool m_isReloading; + bool m_firstLoad; static bool s_forceContextMenuOnMouseRelease; }; diff --git a/src/lib/webtab/webtab.cpp b/src/lib/webtab/webtab.cpp index 0441a71cf..3a2173696 100644 --- a/src/lib/webtab/webtab.cpp +++ b/src/lib/webtab/webtab.cpp @@ -87,7 +87,6 @@ QDataStream &operator >>(QDataStream &stream, WebTab::SavedTab &tab) WebTab::WebTab(BrowserWindow* window) : QWidget() , m_window(window) - , m_inspector(0) , m_tabBar(0) , m_isPinned(false) { @@ -130,15 +129,12 @@ TabbedWebView* WebTab::webView() const void WebTab::showWebInspector() { -#if QTWEBENGINE_DISABLED - if (!m_inspector) { - m_inspector = new WebInspector(this); - m_inspector->setPage(m_webView->page()); - m_splitter->addWidget(m_inspector); - } + if (m_splitter->count() != 1) + return; - m_inspector->show(); -#endif + WebInspector *inspector = new WebInspector(this); + inspector->setView(m_webView); + m_splitter->addWidget(inspector); } QUrl WebTab::url() const diff --git a/src/main/main.cpp b/src/main/main.cpp index e57906957..458b915b3 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . * ============================================================ */ #include "mainapplication.h" +#include "webinspector.h" #include "proxystyle.h" #include "datapaths.h" @@ -154,6 +155,8 @@ int main(int argc, char* argv[]) argv = args; } + qputenv("QTWEBENGINE_REMOTE_DEBUGGING", WEBINSPECTOR_PORT); + MainApplication app(argc, argv); if (app.isClosing()) diff --git a/src/main/main.pro b/src/main/main.pro index c1a1b8717..adfc0464a 100644 --- a/src/main/main.pro +++ b/src/main/main.pro @@ -20,6 +20,7 @@ unix:!contains(DEFINES, "DISABLE_DBUS") QT += dbus INCLUDEPATH += ../lib/3rdparty \ ../lib/app \ ../lib/session \ + ../lib/webengine \ ../lib/webtab \ DEPENDPATH += $$INCLUDEPATH