1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls.Material
import Qt.labs.qmlmodels
import Main
SwipeView {
id: pageStack
currentIndex: 0
onCurrentIndexChanged: {
const newIndex = pageStack.currentIndex;
const indexHistoryLength = indexHistory.length;
if (indexHistoryLength > 0 && indexHistory[indexHistoryLength - 1] === newIndex) {
indexHistory.pop();
} else if (newIndex !== 0 || indexHistoryLength > 0) {
indexHistory.push(newIndex);
}
if (!goingBackAndForth) {
indexForward.splice(0, indexForward.length);
}
goingBackAndForth = false;
App.setCurrentControls(window.visible, newIndex);
pageStack.updateSearchText(pageStack.children[newIndex]?.currentItem?.model?.filterRegularExpressionPattern() ?? "", false);
}
StartPage {
id: startPage
pages: pageStack
onQuitRequested: closeDialog.open()
}
DirsPage {
id: dirsPage
}
DevsPage {
id: devsPage
}
ChangesPage {
id: changesPage
}
AdvancedPage {
id: advancedPage
pages: pageStack
}
SettingsPage {
id: settingsPage
}
required property var window
property string searchText
readonly property list<Item> children: [startPage, dirsPage, devsPage, changesPage, advancedPage, settingsPage]
readonly property var currentChild: children[currentIndex]
readonly property var currentPage: currentChild.currentItem ?? currentChild
readonly property var currentDepth: currentChild?.depth ?? 1
readonly property var currentActions: currentPage.actions ?? []
readonly property var currentExtraActions: currentPage.extraActions ?? []
property var indexHistory: []
property var indexForward: []
property var setPageHistory: []
property bool goingBackAndForth: false
signal changesMightBeDiscarded
function resetHistory() {
pageStack.indexHistory = [];
pageStack.indexForward = [];
pageStack.setPageHistory = [];
}
function serialize() {
return {
"index": pageStack.currentIndex,
"indexHistory": pageStack.indexHistory,
"indexForward": pageStack.indexForward,
"setPageHistory": pageStack.setPageHistory,
"children": pageStack.children.map(child => child.currentItem?.serialize?.()),
};
}
function pop(force) {
const currentChild = pageStack.currentChild;
const currentPage = currentChild.currentItem ?? currentChild;
// handle "dangerous flag" and unsaved changes
if (!force && currentPage.hasUnsavedChanges) {
const parentPage = currentPage.parentPage;
if (parentPage?.hasUnsavedChanges !== undefined) {
parentPage.hasUnsavedChanges = true;
currentPage.hasUnsavedChanges = false;
// propagate "dangerous flag" to parent page if changes on a dangerous page were made
if (currentPage.isDangerous) {
parentPage.isDangerous = true;
}
} else {
changesMightBeDiscarded();
return true;
}
}
// go back within the current page (e.g. on level up in the file browser) or go one config object page up
const wentBack = currentPage.back?.() || currentChild.pop?.();
const lastPageSet = setPageHistory[setPageHistory.length - 1];
const previousPage = indexHistory[indexHistory.length - 1];
if (currentChild.depth === 1 && lastPageSet !== undefined && lastPageSet === previousPage) {
// go back to where we actually came from (e.g. start page) when back on a top-level page (e.g. folders page)
setPageHistory.pop();
return pageStack.back() || wentBack;
} else {
// go back to the previous page if we didn't go back within the current page or config object hierarchy
return wentBack || pageStack.back();
}
}
function back() {
if (indexHistory.length < 1) {
return false;
}
const currentIndex = indexHistory.pop();
const previousIndex = indexHistory.pop();
indexForward.push(currentIndex);
goingBackAndForth = true;
pageStack.setCurrentIndex(previousIndex);
return true;
}
function forward() {
if (indexForward.length < 1) {
return false;
}
goingBackAndForth = true;
pageStack.setCurrentIndex(indexForward.pop());
return true;
}
function showPage(index) {
pageStack.setPageHistory.push(index);
pageStack.setCurrentIndex(index);
return pageStack.children[index];
}
function addDir(dirId, dirName, shareWithDeviceIds, existing) {
showPage(1).add(dirId, dirName, shareWithDeviceIds, existing);
}
function addDevice(deviceId, deviceName) {
showPage(2).add(deviceId, deviceName);
}
function updateSearchText(searchText, updateModel = true) {
if (pageStack.searchText === searchText) {
return;
}
pageStack.searchText = searchText;
if (!updateModel) {
return;
}
const model = pageStack.children[pageStack.currentIndex]?.currentItem?.model;
if (model?.filterRegularExpression !== undefined) {
model.setFilterRegularExpressionPattern(searchText);
}
}
}
|