Verified Commit 897db27e authored by Rinigus Saar's avatar Rinigus Saar Committed by Jonah Brüchert

use tabsmodel to keep reflection of current state

parent 4f043b3a
......@@ -66,7 +66,7 @@ private Q_SLOTS:
}
void testLoad() {
m_tabsModel->load("https://debian.org");
m_tabsModel->setUrl(0, "https://debian.org");
// Number of tabs must not change
QCOMPARE(m_tabsModel->tabs().count(), 1);
......@@ -109,7 +109,7 @@ private Q_SLOTS:
}
void testSetTab() {
m_tabsModel->setTab(0, QStringLiteral("https://debian.org"));
m_tabsModel->setUrl(0, QStringLiteral("https://debian.org"));
QCOMPARE(m_tabsModel->tabs(), QVector<TabState>({TabState("https://debian.org", true), TabState("third", true)}));
}
......
......@@ -22,8 +22,9 @@
import QtQuick 2.3
import QtQuick.Controls 2.0
import QtQml.Models 2.1
import QtWebEngine 1.6
import org.kde.kirigami 2.7 as Kirigami
import org.kde.mobile.angelfish 1.0
......@@ -31,7 +32,7 @@ Repeater {
id: tabs
clip: true
property bool activeTabs: true
property bool activeTabs: false
property bool privateTabsMode: false
property alias currentIndex: tabsModel.currentTab
......@@ -41,6 +42,7 @@ Repeater {
model: TabsModel {
id: tabsModel
isMobileDefault: Kirigami.Settings.isMobile
privateMode: privateTabsMode
Component.onCompleted: tabsModel.loadInitialTabs()
}
......@@ -53,6 +55,7 @@ Repeater {
}
privateMode: tabs.privateTabsMode
url: model.pageurl
userAgent.isMobile: model.isMobile
width: tabs.width
property bool showView: index === tabs.currentIndex
......@@ -65,8 +68,16 @@ Repeater {
tabs.currentItem = webView
}
}
onUrlChanged: {
tabs.tabsModel.setTab(index, url)
tabsModel.setUrl(index, url);
}
Connections {
target: webView.userAgent
onUserAgentChanged: {
tabsModel.setIsMobile(index, webView.userAgent.isMobile);
}
}
}
}
......@@ -27,8 +27,6 @@ import QtQuick.Controls 2.0 as Controls
import org.kde.kirigami 2.5 as Kirigami
import org.kde.mobile.angelfish 1.0
Item {
id: navigation
......
......@@ -89,9 +89,9 @@ Controls.Drawer {
function applyUrl() {
if (text.match(RegexWebUrl.re_weburl)) {
tabs.tabsModel.load(UrlUtils.urlFromUserInput(text))
currentWebView.url = UrlUtils.urlFromUserInput(text);
} else {
tabs.tabsModel.load(UrlUtils.urlFromUserInput(BrowserManager.searchBaseUrl + text))
currentWebView.url = UrlUtils.urlFromUserInput(BrowserManager.searchBaseUrl + text);
}
overlay.close();
}
......
......@@ -26,8 +26,6 @@ import QtQuick.Layouts 1.11
import org.kde.kirigami 2.7 as Kirigami
import org.kde.mobile.angelfish 1.0
import org.kde.mobile.angelfish 1.0
Kirigami.ScrollablePage {
title: i18n("Settings")
......
......@@ -46,7 +46,7 @@ Kirigami.ScrollablePage {
text: i18n("New")
onTriggered: {
// Somewhat weird behaviour, consider always just opening "about:blank"
tabs.tabsModel.newTab(rootPage.privateMode ? "about:blank" : BrowserManager.homepage, Kirigami.Settings.isMobile)
tabs.tabsModel.newTab(rootPage.privateMode ? "about:blank" : BrowserManager.homepage)
pageStack.pop()
}
}
......
......@@ -39,9 +39,7 @@ Kirigami.SwipeListItem {
Kirigami.Theme.colorSet: Kirigami.Theme.View
onClicked: {
tabs.tabsModel.load(url)
//tabs.newTab(url)
//contentView.state = "hidden"
currentWebView.url = url;
}
signal removed
......
......@@ -40,12 +40,11 @@ WebEngineView {
property alias userAgent: userAgent
property alias thumb: thumb
property bool reloadOnVisible: false
property bool reloadOnVisible: true
UserAgentGenerator {
id: userAgent
isMobile: model.isMobile
onUserAgentChanged: tabs.tabsModel.setTab(index, webEngineView.url, isMobile)
onUserAgentChanged: webEngineView.reload()
}
Image {
......
......@@ -139,6 +139,11 @@ Kirigami.ApplicationWindow {
Kirigami.ColumnView.pinned: true
Kirigami.ColumnView.preventStealing: true
// Required to enforce active tab reload
// on start. As a result, mixed isMobile
// tabs will work correctly
property bool initialized: false
property bool privateMode: false
ListWebView {
......@@ -149,7 +154,7 @@ Kirigami.ApplicationWindow {
right: parent.right
bottom: navigation.top
}
activeTabs: !rootPage.privateMode
activeTabs: rootPage.initialized && !rootPage.privateMode
}
ListWebView {
......@@ -160,7 +165,7 @@ Kirigami.ApplicationWindow {
right: parent.right
bottom: navigation.top
}
activeTabs: rootPage.privateMode
activeTabs: rootPage.initialized && rootPage.privateMode
privateTabsMode: true
}
......@@ -289,12 +294,7 @@ Kirigami.ApplicationWindow {
checkable: true
checked: !currentWebView.userAgent.isMobile
onTriggered: {
if (currentWebView.userAgent.isMobile) {
currentWebView.userAgent.isMobile = false
} else {
currentWebView.userAgent.isMobile = true
}
currentWebView.reload()
currentWebView.userAgent.isMobile = !currentWebView.userAgent.isMobile;
}
}
]
......@@ -349,4 +349,6 @@ Kirigami.ApplicationWindow {
webBrowser.pageStack.pop();
}
}
Component.onCompleted: rootPage.initialized = true
}
......@@ -67,27 +67,6 @@ int TabsModel::rowCount(const QModelIndex &parent) const
return parent.isValid() ? 0 : m_tabs.count();
}
/**
* @brief TabsModel::setTabUrl sets the properties of a tab at a given index.
* It should be used in cases were a reload of the web engine after the url change
* is not wanted, e.g if this function is already triggered by a load of the web engine.
* @param index
* @param url
* @param isMobile
*/
void TabsModel::setTab(int index, const QString &url, bool isMobile)
{
if (index < 0 && index >= m_tabs.count())
return; // index out of bounds
m_tabs[index].setUrl(url);
m_tabs[index].setIsMobile(isMobile);
saveTabs();
tabsChanged();
}
/**
* @brief TabsModel::tab returns the tab at the given index
* @param index
......@@ -118,10 +97,10 @@ void TabsModel::loadInitialTabs()
if (!m_privateMode) {
if (AngelFish::BrowserManager::instance()->initialUrl().isEmpty()) {
if (m_tabs.first().url() == QStringLiteral("about:blank"))
load(AngelFish::BrowserManager::instance()->homepage());
setUrl(0, AngelFish::BrowserManager::instance()->homepage());
} else {
if (m_tabs.first().url() == QStringLiteral("about:blank"))
load(AngelFish::BrowserManager::instance()->initialUrl());
setUrl(0, AngelFish::BrowserManager::instance()->initialUrl());
else
newTab(AngelFish::BrowserManager::instance()->initialUrl());
}
......@@ -192,11 +171,8 @@ bool TabsModel::loadTabs()
}
endResetModel();
tabsChanged();
currentTabChanged();
inputFile.close();
return true;
}
return false;
......@@ -236,12 +212,27 @@ bool TabsModel::saveTabs() const
document.setObject(tabsStorage);
outputFile.write(document.toJson());
outputFile.close();
return true;
}
return false;
}
bool TabsModel::isMobileDefault() const
{
return m_isMobileDefault;
}
void TabsModel::setIsMobileDefault(bool def)
{
m_isMobileDefault = def;
isMobileDefaultChanged();
// used in initialization of the tab
if (m_tabs.count() == 1) {
setIsMobile(0, def);
}
}
bool TabsModel::privateMode() const
{
return m_privateMode;
......@@ -266,16 +257,17 @@ void TabsModel::createEmptyTab()
* @param url
* @param isMobile
*/
void TabsModel::newTab(const QString &url, bool isMobile) {
void TabsModel::newTab(const QString &url) {
beginInsertRows({}, m_tabs.count(), m_tabs.count());
m_tabs.append(TabState(url, isMobile));
m_tabs.append(TabState(url, m_isMobileDefault));
endInsertRows();
// Switch to last tab
m_currentTab = m_tabs.count() - 1;
currentTabChanged();
saveTabs();
}
/**
......@@ -319,20 +311,31 @@ void TabsModel::closeTab(int index) {
saveTabs();
}
void TabsModel::setIsMobile(int index, bool isMobile)
{
qDebug() << "Setting isMobile:" << index << isMobile << "tabs open" << m_tabs.count();
if (index < 0 && index >= m_tabs.count())
return; // index out of bounds
m_tabs[index].setIsMobile(isMobile);
QModelIndex mindex = createIndex(index, index);
dataChanged(mindex, mindex, { RoleNames::IsMobileRole });
saveTabs();
}
/**
* Load a url in the current tab
*/
void TabsModel::load(const QString &url) {
qDebug() << "Loading url:" << url;
qDebug() << "current tab" << m_currentTab << "tabs open" << m_tabs.count();
m_tabs[m_currentTab].setUrl(url);
void TabsModel::setUrl(int index, const QString &url)
{
qDebug() << "Setting URL:" << index << url << "tabs open" << m_tabs.count();
if (index < 0 && index >= m_tabs.count())
return; // index out of bounds
QModelIndex index = createIndex(m_currentTab, m_currentTab);
dataChanged(index, index);
m_tabs[index].setUrl(url);
tabsChanged();
QModelIndex mindex = createIndex(index, index);
dataChanged(mindex, mindex, { RoleNames::UrlRole });
saveTabs();
}
QString TabState::url() const
......
......@@ -48,6 +48,7 @@ class TabsModel : public QAbstractListModel
Q_OBJECT
Q_PROPERTY(int currentTab READ currentTab WRITE setCurrentTab NOTIFY currentTabChanged)
Q_PROPERTY(bool isMobileDefault READ isMobileDefault WRITE setIsMobileDefault NOTIFY isMobileDefaultChanged)
Q_PROPERTY(bool privateMode READ privateMode WRITE setPrivateMode NOTIFY privateModeChanged)
enum RoleNames {
......@@ -67,15 +68,19 @@ public:
QVector<TabState> tabs() const;
Q_INVOKABLE void setTab(int index, const QString &url, bool isMobile = true);
Q_INVOKABLE TabState tab(int index);
Q_INVOKABLE void loadInitialTabs();
Q_INVOKABLE void newTab(const QString &url, bool isMobile = true);
Q_INVOKABLE void newTab(const QString &url);
Q_INVOKABLE void createEmptyTab();
Q_INVOKABLE void closeTab(int index);
Q_INVOKABLE void load(const QString &url);
Q_INVOKABLE void setUrl(int index, const QString &url);
Q_INVOKABLE void setIsMobile(int index, bool isMobile);
bool isMobileDefault() const;
void setIsMobileDefault(bool def);
bool privateMode() const;
void setPrivateMode(bool privateMode);
......@@ -89,10 +94,11 @@ private:
QVector<TabState> m_tabs {};
bool m_privateMode = false;
bool m_tabsReadOnly = false;
bool m_isMobileDefault = false;
signals:
void currentTabChanged();
void tabsChanged();
void isMobileDefaultChanged();
void privateModeChanged();
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment