From 98c2efb773daec82ab819d2538e81447770abda1 Mon Sep 17 00:00:00 2001 From: miruka Date: Tue, 10 Dec 2019 16:29:49 -0400 Subject: [PATCH] Unified API to save/restore state properties --- src/qml/Base/HDrawer.qml | 23 ++++++++--------------- src/qml/Base/HTextField.qml | 13 ++++++------- src/qml/Chat/RoomPane/MemberView.qml | 4 +++- src/qml/Chat/RoomPane/RoomPane.qml | 3 ++- src/qml/MainPane/MainPane.qml | 2 +- src/qml/MainPane/MainPaneToolBar.qml | 3 ++- src/qml/Window.qml | 27 +++++++++++++++++++++++++++ src/qml/utils.js | 12 ++++++++++++ 8 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/qml/Base/HDrawer.qml b/src/qml/Base/HDrawer.qml index cd28ec05..c88fb93a 100644 --- a/src/qml/Base/HDrawer.qml +++ b/src/qml/Base/HDrawer.qml @@ -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) } } } diff --git a/src/qml/Base/HTextField.qml b/src/qml/Base/HTextField.qml index 22329590..c7595829 100644 --- a/src/qml/Base/HTextField.qml +++ b/src/qml/Base/HTextField.qml @@ -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 diff --git a/src/qml/Chat/RoomPane/MemberView.qml b/src/qml/Chat/RoomPane/MemberView.qml index 6693c123..9d4c56d1 100644 --- a/src/qml/Chat/RoomPane/MemberView.qml +++ b/src/qml/Chat/RoomPane/MemberView.qml @@ -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 diff --git a/src/qml/Chat/RoomPane/RoomPane.qml b/src/qml/Chat/RoomPane/RoomPane.qml index c2243352..09a917a3 100644 --- a/src/qml/Chat/RoomPane/RoomPane.qml +++ b/src/qml/Chat/RoomPane/RoomPane.qml @@ -4,7 +4,8 @@ import "../../Base" HDrawer { id: roomPane - objectName: "roomPane" + saveName: "roomPane" + edge: Qt.RightEdge defaultSize: buttonRepeater.childrenImplicitWidth minimumSize: diff --git a/src/qml/MainPane/MainPane.qml b/src/qml/MainPane/MainPane.qml index 32626771..502bb98b 100644 --- a/src/qml/MainPane/MainPane.qml +++ b/src/qml/MainPane/MainPane.qml @@ -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 diff --git a/src/qml/MainPane/MainPaneToolBar.qml b/src/qml/MainPane/MainPaneToolBar.qml index d05023fe..61cbfdfb 100644 --- a/src/qml/MainPane/MainPaneToolBar.qml +++ b/src/qml/MainPane/MainPaneToolBar.qml @@ -22,7 +22,8 @@ HRowLayout { HTextField { id: filterField - objectName: "roomFilterField" + saveName: "roomFilterField" + placeholderText: qsTr("Filter rooms") backgroundColor: theme.mainPane.filterRooms.background bordered: false diff --git a/src/qml/Window.qml b/src/qml/Window.qml index e49750bc..a97bb143 100644 --- a/src/qml/Window.qml +++ b/src/qml/Window.qml @@ -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 } diff --git a/src/qml/utils.js b/src/qml/utils.js index 7bfefea7..09a5df8c 100644 --- a/src/qml/utils.js +++ b/src/qml/utils.js @@ -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) }