Unified API to save/restore state properties

This commit is contained in:
miruka 2019-12-10 16:29:49 -04:00
parent 2cd177dc13
commit 98c2efb773
8 changed files with 61 additions and 26 deletions

View File

@ -4,7 +4,6 @@ import "../utils.js" as Utils
Drawer { Drawer {
id: drawer id: drawer
objectName: "" // Set one to allow storing the user size to a file
implicitWidth: horizontal ? calculatedSize : parent.width implicitWidth: horizontal ? calculatedSize : parent.width
implicitHeight: vertical ? calculatedSize : parent.height implicitHeight: vertical ? calculatedSize : parent.height
@ -25,14 +24,18 @@ Drawer {
background: Rectangle { id: bg; color: theme.colors.strongBackground } background: Rectangle { id: bg; color: theme.colors.strongBackground }
property string saveName: ""
property string saveId: ""
property var saveProperties: ["preferredSize"]
//
property alias color: bg.color property alias color: bg.color
property int defaultSize: 300 property int defaultSize: 300
property int preferredSize: property int preferredSize:
window.uiState[objectName] ? window.getState(this, "preferredSize", defaultSize)
(window.uiState[objectName].size || defaultSize) :
defaultSize
property int minimumSize: resizeAreaSize property int minimumSize: resizeAreaSize
property int maximumSize: property int maximumSize:
@ -109,17 +112,7 @@ Drawer {
(drawer.edge === Qt.BottomEdge ? -mouseY : mouseY) (drawer.edge === Qt.BottomEdge ? -mouseY : mouseY)
} }
onReleased: { onReleased: window.saveState(drawer)
if (! drawer.objectName) {
console.warn("Can't save pane size, no objectName set")
return
}
window.uiState[drawer.objectName] = {
size: drawer.preferredSize,
}
window.uiStateChanged()
}
} }
} }
} }

View File

@ -3,7 +3,6 @@ import QtQuick.Controls 2.12
TextField { TextField {
id: field id: field
objectName: "" // Set one to allow remembering the text using a file
selectByMouse: true selectByMouse: true
leftPadding: theme.spacing leftPadding: theme.spacing
rightPadding: leftPadding rightPadding: leftPadding
@ -32,13 +31,9 @@ TextField {
// Set it only on component creation to avoid binding loops // Set it only on component creation to avoid binding loops
Component.onCompleted: Component.onCompleted:
if (! text && uiState[objectName]) text = uiState[objectName].text if (! text) text = window.getState(this, "text", "")
onTextChanged: { onTextChanged: window.saveState(this)
if (! objectName) return
window.uiState[objectName] = {text}
window.uiStateChanged()
}
Keys.onPressed: if ( Keys.onPressed: if (
event.modifiers & Qt.AltModifier || event.modifiers & Qt.AltModifier ||
@ -46,6 +41,10 @@ TextField {
) event.accepted = true // XXX Still needed? ) event.accepted = true // XXX Still needed?
property string saveName: ""
property string saveId: ""
property var saveProperties: ["text"]
property bool error: false property bool error: false
property alias radius: textFieldBackground.radius property alias radius: textFieldBackground.radius

View File

@ -47,7 +47,9 @@ HColumnLayout {
HTextField { HTextField {
id: filterField id: filterField
objectName: "memberFilterField" saveName: "memberFilterField"
saveId: chat.roomId
placeholderText: qsTr("Filter members") placeholderText: qsTr("Filter members")
backgroundColor: theme.chat.roomPane.filterMembers.background backgroundColor: theme.chat.roomPane.filterMembers.background
bordered: false bordered: false

View File

@ -4,7 +4,8 @@ import "../../Base"
HDrawer { HDrawer {
id: roomPane id: roomPane
objectName: "roomPane" saveName: "roomPane"
edge: Qt.RightEdge edge: Qt.RightEdge
defaultSize: buttonRepeater.childrenImplicitWidth defaultSize: buttonRepeater.childrenImplicitWidth
minimumSize: minimumSize:

View File

@ -5,7 +5,7 @@ import "../utils.js" as Utils
HDrawer { HDrawer {
id: mainPane id: mainPane
objectName: "mainPane" saveName: "mainPane"
color: theme.mainPane.background color: theme.mainPane.background
minimumSize: theme.controls.avatar.size + theme.spacing * 2 minimumSize: theme.controls.avatar.size + theme.spacing * 2

View File

@ -22,7 +22,8 @@ HRowLayout {
HTextField { HTextField {
id: filterField id: filterField
objectName: "roomFilterField" saveName: "roomFilterField"
placeholderText: qsTr("Filter rooms") placeholderText: qsTr("Filter rooms")
backgroundColor: theme.mainPane.filterRooms.background backgroundColor: theme.mainPane.filterRooms.background
bordered: false bordered: false

View File

@ -1,6 +1,7 @@
import QtQuick 2.12 import QtQuick 2.12
import QtQuick.Controls 2.12 import QtQuick.Controls 2.12
import "Base" import "Base"
import "utils.js" as Utils
ApplicationWindow { ApplicationWindow {
id: window id: window
@ -44,6 +45,32 @@ ApplicationWindow {
readonly property alias py: py readonly property alias py: py
function saveState(obj) {
if (! obj.saveName || ! obj.saveProperties ||
obj.saveProperties.length < 1) return
let propertyValues = {}
for (let prop of obj.saveProperties) {
propertyValues[prop] = obj[prop]
}
Utils.objectUpdateRecursive(uiState, {
[obj.saveName]: { [obj.saveId || "ALL"]: propertyValues },
})
uiStateChanged()
}
function getState(obj, property, defaultValue=undefined) {
try {
return uiState[obj.saveName][obj.saveId || "ALL"][property]
} catch(err) {
return defaultValue
}
}
Python { id: py } Python { id: py }
HoverHandler { id: windowHover } HoverHandler { id: windowHover }

View File

@ -52,6 +52,18 @@ function isEmptyObject(obj) {
} }
function objectUpdateRecursive(current, update) {
for (const key of Object.keys(update)) {
if ((key in current) && typeof(current[key]) === "object" &&
typeof(update[key]) === "object") {
objectUpdateRecursive(current[key], update[key])
} else {
current[key] = update[key]
}
}
}
function numberWrapAt(num, max) { function numberWrapAt(num, max) {
return num < 0 ? max + (num % max) : (num % max) return num < 0 ? max + (num % max) : (num % max)
} }