From 6b08e17dcd1e9ec2d4ad8cf8dfcb236b182cb554 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Tue, 2 Jan 2018 18:05:19 +0100 Subject: [PATCH] BrowserWindow: Save ui state separately for each window --- src/lib/app/browserwindow.cpp | 82 ++++++++++++++++++++++++++--------- src/lib/app/browserwindow.h | 9 ++-- src/lib/app/settings.cpp | 7 ++- src/lib/app/settings.h | 4 +- 4 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/lib/app/browserwindow.cpp b/src/lib/app/browserwindow.cpp index 73065d995..b20f58769 100644 --- a/src/lib/app/browserwindow.cpp +++ b/src/lib/app/browserwindow.cpp @@ -85,7 +85,7 @@ #include #endif -static const int savedWindowVersion = 1; +static const int savedWindowVersion = 2; BrowserWindow::SavedWindow::SavedWindow() { @@ -95,6 +95,7 @@ BrowserWindow::SavedWindow::SavedWindow(BrowserWindow *window) { windowState = window->isFullScreen() ? QByteArray() : window->saveState(); windowGeometry = window->saveGeometry(); + windowUiState = window->saveUiState(); #ifdef QZ_WS_X11 virtualDesktop = window->getCurrentVirtualDesktop(); #endif @@ -148,6 +149,8 @@ QDataStream &operator<<(QDataStream &stream, const BrowserWindow::SavedWindow &w stream << window.tabs.at(i); } + stream << window.windowUiState; + return stream; } @@ -175,6 +178,10 @@ QDataStream &operator>>(QDataStream &stream, BrowserWindow::SavedWindow &window) window.tabs.append(tab); } + if (version >= 2) { + stream >> window.windowUiState; + } + return stream; } @@ -329,8 +336,7 @@ void BrowserWindow::postLaunch() void BrowserWindow::setupUi() { - int locationBarWidth; - int websearchBarWidth; + QHash uiState; QDesktopWidget* desktop = mApp->desktop(); int windowWidth = desktop->availableGeometry().width() / 1.3; @@ -365,8 +371,18 @@ void BrowserWindow::setupUi() #endif } - locationBarWidth = settings.value("LocationBarWidth", 480).toInt(); - websearchBarWidth = settings.value("WebSearchBarWidth", 140).toInt(); + const QStringList keys = { + QSL("LocationBarWidth"), + QSL("WebSearchBarWidth"), + QSL("SideBarWidth"), + QSL("WebViewWidth") + }; + for (const QString &key : keys) { + if (settings.contains(key)) { + uiState[key] = settings.value(key); + } + } + settings.endGroup(); QWidget* widget = new QWidget(this); @@ -381,7 +397,6 @@ void BrowserWindow::setupUi() m_tabWidget = new TabWidget(this); m_superMenu = new QMenu(this); m_navigationToolbar = new NavigationBar(this); - m_navigationToolbar->setSplitterSizes(locationBarWidth, websearchBarWidth); m_bookmarksToolbar = new BookmarksToolbar(this); m_navigationContainer = new NavigationContainer(this); @@ -414,6 +429,8 @@ void BrowserWindow::setupUi() pal.setColor(QPalette::Window, col); QToolTip::setPalette(pal); + restoreUiState(uiState); + // Set some sane minimum width setMinimumWidth(300); } @@ -513,6 +530,28 @@ void BrowserWindow::createEncodingSubMenu(const QString &name, QStringList &code menu->addMenu(subMenu); } +QHash BrowserWindow::saveUiState() +{ + saveSideBarWidth(); + + QHash state; + state[QSL("LocationBarWidth")] = m_navigationToolbar->splitter()->sizes().at(0); + state[QSL("WebSearchBarWidth")] = m_navigationToolbar->splitter()->sizes().at(1); + state[QSL("SideBarWidth")] = m_sideBarWidth; + state[QSL("WebViewWidth")] = m_webViewWidth; + return state; +} + +void BrowserWindow::restoreUiState(const QHash &state) +{ + const int locationBarWidth = state.value(QSL("LocationBarWidth"), 480).toInt(); + const int websearchBarWidth = state.value(QSL("WebSearchBarWidth"), 140).toInt(); + m_navigationToolbar->setSplitterSizes(locationBarWidth, websearchBarWidth); + + m_sideBarWidth = state.value(QSL("SideBarWidth"), 250).toInt(); + m_webViewWidth = state.value(QSL("WebViewWidth"), 2000).toInt(); +} + void BrowserWindow::loadSettings() { Settings settings; @@ -533,8 +572,6 @@ void BrowserWindow::loadSettings() bool showBookmarksToolbar = settings.value("showBookmarksToolbar", true).toBool(); bool showNavigationToolbar = settings.value("showNavigationToolbar", true).toBool(); bool showMenuBar = settings.value("showMenubar", false).toBool(); - m_sideBarWidth = settings.value("SideBarWidth", 250).toInt(); - m_webViewWidth = settings.value("WebViewWidth", 2000).toInt(); const QString activeSideBar = settings.value("SideBar", "None").toString(); // Make sure both menubar and navigationbar are not hidden @@ -796,7 +833,6 @@ SideBar* BrowserWindow::addSideBar() m_mainSplitter->insertWidget(0, m_sideBar.data()); m_mainSplitter->setCollapsible(0, false); - m_mainSplitter->setSizes(QList() << m_sideBarWidth << m_webViewWidth); return m_sideBar.data(); @@ -804,9 +840,12 @@ SideBar* BrowserWindow::addSideBar() void BrowserWindow::saveSideBarWidth() { + if (!m_sideBar) { + return; + } + // That +1 is important here, without it, the sidebar width would // decrease by 1 pixel every close - m_sideBarWidth = m_mainSplitter->sizes().at(0) + 1; m_webViewWidth = width() - m_sideBarWidth; } @@ -979,6 +1018,7 @@ void BrowserWindow::restoreWindow(const SavedWindow &window) { restoreState(window.windowState); restoreGeometry(window.windowGeometry); + restoreUiState(window.windowUiState); #ifdef QZ_WS_X11 moveToVirtualDesktop(window.virtualDesktop); #endif @@ -1494,20 +1534,20 @@ void BrowserWindow::closeWindow() void BrowserWindow::saveSettings() { - if (m_sideBar) { - saveSideBarWidth(); + if (mApp->isPrivate()) { + return; } - if (!mApp->isPrivate()) { - Settings settings; - settings.beginGroup("Browser-View-Settings"); - settings.setValue("LocationBarWidth", m_navigationToolbar->splitter()->sizes().at(0)); - settings.setValue("WebSearchBarWidth", m_navigationToolbar->splitter()->sizes().at(1)); - settings.setValue("SideBarWidth", m_sideBarWidth); - settings.setValue("WebViewWidth", m_webViewWidth); - settings.setValue("WindowGeometry", saveGeometry()); - settings.endGroup(); + Settings settings; + settings.beginGroup("Browser-View-Settings"); + settings.setValue("WindowGeometry", saveGeometry()); + + const auto state = saveUiState(); + for (auto it = state.constBegin(); it != state.constEnd(); ++it) { + settings.setValue(it.key(), it.value()); } + + settings.endGroup(); } void BrowserWindow::closeTab() diff --git a/src/lib/app/browserwindow.h b/src/lib/app/browserwindow.h index fdc36af18..712861f50 100644 --- a/src/lib/app/browserwindow.h +++ b/src/lib/app/browserwindow.h @@ -61,6 +61,7 @@ public: struct SavedWindow { QByteArray windowState; QByteArray windowGeometry; + QHash windowUiState; int virtualDesktop = -1; int currentTab = -1; QVector tabs; @@ -82,7 +83,6 @@ public: void setStartPage(WebPage* page); void restoreWindow(const SavedWindow &window); - void saveSideBarWidth(); bool fullScreenNavigationVisible() const; void showNavigationWithFullScreen(); @@ -101,6 +101,7 @@ public: void removeActions(const QList &actions); SideBar* addSideBar(); + void saveSideBarWidth(); TabbedWebView* weView() const; TabbedWebView* weView(int index) const; @@ -186,10 +187,12 @@ private: void setupMenu(); void updateStartupFocus(); - QAction *createEncodingAction(const QString &codecName, const QString &activeCodecName, - QMenu *menu); + QAction *createEncodingAction(const QString &codecName, const QString &activeCodecName, QMenu *menu); void createEncodingSubMenu(const QString &name, QStringList &codecNames, QMenu *menu); + QHash saveUiState(); + void restoreUiState(const QHash &state); + QUrl m_startUrl; QUrl m_homepage; Qz::BrowserWindowType m_windowType; diff --git a/src/lib/app/settings.cpp b/src/lib/app/settings.cpp index c9ec64fef..f85adf251 100644 --- a/src/lib/app/settings.cpp +++ b/src/lib/app/settings.cpp @@ -1,6 +1,6 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2010-2014 David Rosca +* Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -46,6 +46,11 @@ void Settings::syncSettings() s_settings->sync(); } +bool Settings::contains(const QString &key) const +{ + return s_settings->contains(key); +} + void Settings::setValue(const QString &key, const QVariant &defaultValue) { s_settings->setValue(key, defaultValue); diff --git a/src/lib/app/settings.h b/src/lib/app/settings.h index b45a8cd44..5939a9877 100644 --- a/src/lib/app/settings.h +++ b/src/lib/app/settings.h @@ -1,6 +1,6 @@ /* ============================================================ * Falkon - Qt web browser -* Copyright (C) 2010-2014 David Rosca +* Copyright (C) 2010-2018 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,6 +38,8 @@ public: static QSettings* globalSettings(); static QzSettings* staticSettings(); + bool contains(const QString &key) const; + void setValue(const QString &key, const QVariant &defaultValue = QVariant()); QVariant value(const QString &key, const QVariant &defaultValue = QVariant());