Unified API to save/restore state properties
This commit is contained in:
parent
2cd177dc13
commit
98c2efb773
|
@ -4,7 +4,6 @@ import "../utils.js" as Utils
|
|||
|
||||
Drawer {
|
||||
id: drawer
|
||||
objectName: "" // Set one to allow storing the user size to a file
|
||||
implicitWidth: horizontal ? calculatedSize : parent.width
|
||||
implicitHeight: vertical ? calculatedSize : parent.height
|
||||
|
||||
|
@ -25,14 +24,18 @@ Drawer {
|
|||
background: Rectangle { id: bg; color: theme.colors.strongBackground }
|
||||
|
||||
|
||||
property string saveName: ""
|
||||
property string saveId: ""
|
||||
property var saveProperties: ["preferredSize"]
|
||||
|
||||
//
|
||||
|
||||
property alias color: bg.color
|
||||
|
||||
property int defaultSize: 300
|
||||
|
||||
property int preferredSize:
|
||||
window.uiState[objectName] ?
|
||||
(window.uiState[objectName].size || defaultSize) :
|
||||
defaultSize
|
||||
window.getState(this, "preferredSize", defaultSize)
|
||||
|
||||
property int minimumSize: resizeAreaSize
|
||||
property int maximumSize:
|
||||
|
@ -109,17 +112,7 @@ Drawer {
|
|||
(drawer.edge === Qt.BottomEdge ? -mouseY : mouseY)
|
||||
}
|
||||
|
||||
onReleased: {
|
||||
if (! drawer.objectName) {
|
||||
console.warn("Can't save pane size, no objectName set")
|
||||
return
|
||||
}
|
||||
|
||||
window.uiState[drawer.objectName] = {
|
||||
size: drawer.preferredSize,
|
||||
}
|
||||
window.uiStateChanged()
|
||||
}
|
||||
onReleased: window.saveState(drawer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ import QtQuick.Controls 2.12
|
|||
|
||||
TextField {
|
||||
id: field
|
||||
objectName: "" // Set one to allow remembering the text using a file
|
||||
selectByMouse: true
|
||||
leftPadding: theme.spacing
|
||||
rightPadding: leftPadding
|
||||
|
@ -32,13 +31,9 @@ TextField {
|
|||
|
||||
// Set it only on component creation to avoid binding loops
|
||||
Component.onCompleted:
|
||||
if (! text && uiState[objectName]) text = uiState[objectName].text
|
||||
if (! text) text = window.getState(this, "text", "")
|
||||
|
||||
onTextChanged: {
|
||||
if (! objectName) return
|
||||
window.uiState[objectName] = {text}
|
||||
window.uiStateChanged()
|
||||
}
|
||||
onTextChanged: window.saveState(this)
|
||||
|
||||
Keys.onPressed: if (
|
||||
event.modifiers & Qt.AltModifier ||
|
||||
|
@ -46,6 +41,10 @@ TextField {
|
|||
) event.accepted = true // XXX Still needed?
|
||||
|
||||
|
||||
property string saveName: ""
|
||||
property string saveId: ""
|
||||
property var saveProperties: ["text"]
|
||||
|
||||
property bool error: false
|
||||
|
||||
property alias radius: textFieldBackground.radius
|
||||
|
|
|
@ -47,7 +47,9 @@ HColumnLayout {
|
|||
|
||||
HTextField {
|
||||
id: filterField
|
||||
objectName: "memberFilterField"
|
||||
saveName: "memberFilterField"
|
||||
saveId: chat.roomId
|
||||
|
||||
placeholderText: qsTr("Filter members")
|
||||
backgroundColor: theme.chat.roomPane.filterMembers.background
|
||||
bordered: false
|
||||
|
|
|
@ -4,7 +4,8 @@ import "../../Base"
|
|||
|
||||
HDrawer {
|
||||
id: roomPane
|
||||
objectName: "roomPane"
|
||||
saveName: "roomPane"
|
||||
|
||||
edge: Qt.RightEdge
|
||||
defaultSize: buttonRepeater.childrenImplicitWidth
|
||||
minimumSize:
|
||||
|
|
|
@ -5,7 +5,7 @@ import "../utils.js" as Utils
|
|||
|
||||
HDrawer {
|
||||
id: mainPane
|
||||
objectName: "mainPane"
|
||||
saveName: "mainPane"
|
||||
color: theme.mainPane.background
|
||||
minimumSize: theme.controls.avatar.size + theme.spacing * 2
|
||||
|
||||
|
|
|
@ -22,7 +22,8 @@ HRowLayout {
|
|||
|
||||
HTextField {
|
||||
id: filterField
|
||||
objectName: "roomFilterField"
|
||||
saveName: "roomFilterField"
|
||||
|
||||
placeholderText: qsTr("Filter rooms")
|
||||
backgroundColor: theme.mainPane.filterRooms.background
|
||||
bordered: false
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import QtQuick 2.12
|
||||
import QtQuick.Controls 2.12
|
||||
import "Base"
|
||||
import "utils.js" as Utils
|
||||
|
||||
ApplicationWindow {
|
||||
id: window
|
||||
|
@ -44,6 +45,32 @@ ApplicationWindow {
|
|||
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 }
|
||||
|
||||
HoverHandler { id: windowHover }
|
||||
|
|
|
@ -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) {
|
||||
return num < 0 ? max + (num % max) : (num % max)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user