1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

Use QWebEnginePage::RequestClose action to close tabs

It is no longer to use hacky onbeforeunload javascript handling when
closing tabs.
This commit is contained in:
David Rosca 2015-10-12 12:04:48 +02:00
parent e127000cb7
commit 1a068e8c7d
9 changed files with 63 additions and 57 deletions

View File

@ -1413,7 +1413,7 @@ void BrowserWindow::closeTab()
{ {
// Don't close pinned tabs with keyboard shortcuts (Ctrl+W, Ctrl+F4) // Don't close pinned tabs with keyboard shortcuts (Ctrl+W, Ctrl+F4)
if (weView() && !weView()->webTab()->isPinned()) { if (weView() && !weView()->webTab()->isPinned()) {
m_tabWidget->closeTab(); m_tabWidget->requestCloseTab();
} }
} }

View File

@ -393,7 +393,7 @@ void TabBar::updatePinnedTabCloseButton(int index)
void TabBar::closeCurrentTab() void TabBar::closeCurrentTab()
{ {
m_tabWidget->closeTab(currentIndex()); m_tabWidget->requestCloseTab(currentIndex());
} }
void TabBar::closeTabFromButton() void TabBar::closeTabFromButton()
@ -410,7 +410,7 @@ void TabBar::closeTabFromButton()
} }
if (tabToClose != -1) { if (tabToClose != -1) {
m_tabWidget->closeTab(tabToClose); m_tabWidget->requestCloseTab(tabToClose);
} }
} }
@ -577,7 +577,7 @@ void TabBar::mouseReleaseEvent(QMouseEvent* event)
int id = tabAt(event->pos()); int id = tabAt(event->pos());
if (id != -1) { if (id != -1) {
m_tabWidget->closeTab(id); m_tabWidget->requestCloseTab(id);
return; return;
} }
} }

View File

@ -51,7 +51,6 @@ signals:
void reloadTab(int index); void reloadTab(int index);
void stopTab(int index); void stopTab(int index);
void closeAllButCurrent(int index); void closeAllButCurrent(int index);
void closeTab(int index);
void duplicateTab(int index); void duplicateTab(int index);
void detachTab(int index); void detachTab(int index);
@ -63,7 +62,7 @@ private slots:
void reloadTab() { emit reloadTab(m_clickedTab); } void reloadTab() { emit reloadTab(m_clickedTab); }
void stopTab() { emit stopTab(m_clickedTab); } void stopTab() { emit stopTab(m_clickedTab); }
void closeTab() { emit closeTab(m_clickedTab); } void closeTab() { emit tabCloseRequested(m_clickedTab); }
void duplicateTab() { emit duplicateTab(m_clickedTab); } void duplicateTab() { emit duplicateTab(m_clickedTab); }
void detachTab() { emit detachTab(m_clickedTab); } void detachTab() { emit detachTab(m_clickedTab); }

View File

@ -124,10 +124,9 @@ TabWidget::TabWidget(BrowserWindow* window, QWidget* parent)
connect(this, SIGNAL(currentChanged(int)), m_window, SLOT(refreshHistory())); connect(this, SIGNAL(currentChanged(int)), m_window, SLOT(refreshHistory()));
connect(this, SIGNAL(changed()), mApp, SLOT(changeOcurred())); connect(this, SIGNAL(changed()), mApp, SLOT(changeOcurred()));
connect(m_tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int))); connect(m_tabBar, SIGNAL(tabCloseRequested(int)), this, SLOT(requestCloseTab(int)));
connect(m_tabBar, SIGNAL(reloadTab(int)), this, SLOT(reloadTab(int))); connect(m_tabBar, SIGNAL(reloadTab(int)), this, SLOT(reloadTab(int)));
connect(m_tabBar, SIGNAL(stopTab(int)), this, SLOT(stopTab(int))); connect(m_tabBar, SIGNAL(stopTab(int)), this, SLOT(stopTab(int)));
connect(m_tabBar, SIGNAL(closeTab(int)), this, SLOT(closeTab(int)));
connect(m_tabBar, SIGNAL(closeAllButCurrent(int)), this, SLOT(closeAllButCurrent(int))); connect(m_tabBar, SIGNAL(closeAllButCurrent(int)), this, SLOT(closeAllButCurrent(int)));
connect(m_tabBar, SIGNAL(duplicateTab(int)), this, SLOT(duplicateTab(int))); connect(m_tabBar, SIGNAL(duplicateTab(int)), this, SLOT(duplicateTab(int)));
connect(m_tabBar, SIGNAL(detachTab(int)), this, SLOT(detachTab(int))); connect(m_tabBar, SIGNAL(detachTab(int)), this, SLOT(detachTab(int)));
@ -138,7 +137,7 @@ TabWidget::TabWidget(BrowserWindow* window, QWidget* parent)
connect(mApp, SIGNAL(settingsReloaded()), this, SLOT(loadSettings())); connect(mApp, SIGNAL(settingsReloaded()), this, SLOT(loadSettings()));
m_menuTabs = new MenuTabs(this); m_menuTabs = new MenuTabs(this);
connect(m_menuTabs, SIGNAL(closeTab(int)), this, SLOT(closeTab(int))); connect(m_menuTabs, SIGNAL(closeTab(int)), this, SLOT(requestCloseTab(int)));
m_menuClosedTabs = new QMenu(this); m_menuClosedTabs = new QMenu(this);
@ -414,7 +413,7 @@ void TabWidget::addTabFromClipboard()
} }
} }
void TabWidget::closeTab(int index, bool force) void TabWidget::closeTab(int index)
{ {
if (index == -1) if (index == -1)
index = currentIndex(); index = currentIndex();
@ -424,36 +423,11 @@ void TabWidget::closeTab(int index, bool force)
return; return;
TabbedWebView *webView = webTab->webView(); TabbedWebView *webView = webTab->webView();
bool isRestorePage = webView->url().toString() == QL1S("qupzilla:restore");
// Don't close restore page!
if (!force && isRestorePage && mApp->restoreManager())
return;
// window.onbeforeunload handling
if (!force && !webView->onBeforeUnload())
return;
// Save tab url and history // Save tab url and history
if (!isRestorePage) if (webView->url().toString() != QL1S("qupzilla:restore"))
m_closedTabsManager->saveTab(webTab, index); m_closedTabsManager->saveTab(webTab, index);
// This would close last tab, so we close the window instead
if (!force && count() == 1) {
// If we are not closing window upon closing last tab, let's just load new-tab-url
if (m_dontCloseWithOneTab) {
if (webView->url() == m_urlOnNewTab) {
// We don't want to accumulate more than one closed tab, if user tries
// to close the last tab multiple times
m_closedTabsManager->takeLastClosedTab();
}
webView->load(m_urlOnNewTab);
return;
}
m_window->close();
return;
}
m_locationBars->removeWidget(webView->webTab()->locationBar()); m_locationBars->removeWidget(webView->webTab()->locationBar());
disconnect(webView, SIGNAL(wantsCloseTab(int)), this, SLOT(closeTab(int))); disconnect(webView, SIGNAL(wantsCloseTab(int)), this, SLOT(closeTab(int)));
disconnect(webView, SIGNAL(urlChanged(QUrl)), this, SIGNAL(changed())); disconnect(webView, SIGNAL(urlChanged(QUrl)), this, SIGNAL(changed()));
@ -474,6 +448,40 @@ void TabWidget::closeTab(int index, bool force)
emit changed(); emit changed();
} }
void TabWidget::requestCloseTab(int index)
{
if (index == -1)
index = currentIndex();
WebTab *webTab = weTab(index);
if (!webTab || !validIndex(index))
return;
TabbedWebView *webView = webTab->webView();
// Don't close restore page!
if (webView->url().toString() == QL1S("qupzilla:restore") && mApp->restoreManager())
return;
// This would close last tab, so we close the window instead
if (count() == 1) {
// If we are not closing window upon closing last tab, let's just load new-tab-url
if (m_dontCloseWithOneTab) {
if (webView->url() == m_urlOnNewTab) {
// We don't want to accumulate more than one closed tab, if user tries
// to close the last tab multiple times
m_closedTabsManager->takeLastClosedTab();
}
webView->load(m_urlOnNewTab);
return;
}
m_window->close();
return;
}
webView->triggerPageAction(QWebEnginePage::RequestClose);
}
void TabWidget::currentTabChanged(int index) void TabWidget::currentTabChanged(int index)
{ {
if (!validIndex(index)) if (!validIndex(index))
@ -590,7 +598,7 @@ void TabWidget::closeAllButCurrent(int index)
if (akt == widget(tabIndex)) { if (akt == widget(tabIndex)) {
continue; continue;
} }
closeTab(tabIndex); requestCloseTab(tabIndex);
} }
} }
@ -856,7 +864,7 @@ void TabWidget::closeRecoveryTab()
{ {
foreach (WebTab* tab, allTabs(false)) { foreach (WebTab* tab, allTabs(false)) {
if (tab->url().toString() == QLatin1String("qupzilla:restore")) { if (tab->url().toString() == QLatin1String("qupzilla:restore")) {
closeTab(tab->tabIndex(), true); closeTab(tab->tabIndex());
} }
} }
} }

View File

@ -106,7 +106,11 @@ public slots:
void addTabFromClipboard(); void addTabFromClipboard();
int duplicateTab(int index); int duplicateTab(int index);
void closeTab(int index = -1, bool force = false); // Force close tab
void closeTab(int index = -1);
// Request close tab (may be rejected)
void requestCloseTab(int index = -1);
void reloadTab(int index); void reloadTab(int index);
void reloadAllTabs(); void reloadAllTabs();
void stopTab(int index); void stopTab(int index);

View File

@ -228,17 +228,6 @@ void WebView::restoreHistory(const QByteArray &data)
m_page->setupWebChannel(); m_page->setupWebChannel();
} }
bool WebView::onBeforeUnload()
{
const QString &source = QSL("window.onbeforeunload(new Event('beforeunload'))");
const QString &res = page()->execJavaScript(source, 200).toString();
if (!res.isEmpty())
return page()->javaScriptConfirm(url(), res);
return true;
}
QWidget *WebView::inputWidget() const QWidget *WebView::inputWidget() const
{ {
return qobject_cast<QWidget*>(m_rwhvqt); return qobject_cast<QWidget*>(m_rwhvqt);

View File

@ -57,9 +57,6 @@ public:
void restoreHistory(const QByteArray &data); void restoreHistory(const QByteArray &data);
// Executes window.onbeforeunload, returns true if view can be closed
bool onBeforeUnload();
void addNotification(QWidget* notif); void addNotification(QWidget* notif);
bool eventFilter(QObject *obj, QEvent *event); bool eventFilter(QObject *obj, QEvent *event);

View File

@ -216,11 +216,20 @@ void MouseGestures::rightGestured()
void MouseGestures::downRightGestured() void MouseGestures::downRightGestured()
{ {
TabbedWebView *view = qobject_cast<TabbedWebView*>(m_view.data());
if (!view)
return;
BrowserWindow *window = view->browserWindow();
if (!window)
return;
TabWidget *tabWidget = window->tabWidget();
if (!m_view) { if (!m_view) {
return; return;
} }
m_view.data()->closeView(); tabWidget->requestCloseTab(view->tabIndex());
} }
void MouseGestures::downLeftGestured() void MouseGestures::downLeftGestured()

View File

@ -357,7 +357,7 @@ void TabManagerWidget::closeSelectedTabs(const QHash<BrowserWindow*, WebTab*> &t
QList<WebTab*> tabs = tabsHash.values(mainWindow); QList<WebTab*> tabs = tabsHash.values(mainWindow);
foreach (WebTab* webTab, tabs) { foreach (WebTab* webTab, tabs) {
mainWindow->tabWidget()->closeTab(webTab->tabIndex()); mainWindow->tabWidget()->requestCloseTab(webTab->tabIndex());
} }
} }
} }