From 37d8d5c68d2d94c5a6ba16876079aa3973a5e227 Mon Sep 17 00:00:00 2001 From: miruka Date: Mon, 9 Dec 2019 13:21:12 -0400 Subject: [PATCH] History persistence for DebugConsole --- src/python/backend.py | 4 +++- src/python/config_files.py | 13 +++++++++++++ src/qml/DebugConsole.qml | 9 +++++++-- src/qml/Python.qml | 5 ++++- src/qml/Window.qml | 3 +++ 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/python/backend.py b/src/python/backend.py index b2c5c4d0..6070fd53 100644 --- a/src/python/backend.py +++ b/src/python/backend.py @@ -23,6 +23,7 @@ class Backend: self.saved_accounts = config_files.Accounts(self) self.ui_settings = config_files.UISettings(self) self.ui_state = config_files.UIState(self) + self.history = config_files.History(self) self.models = ModelStore(allowed_key_types={ Account, # Logged-in accounts @@ -139,9 +140,10 @@ class Backend: from .config_files import Theme settings = await self.ui_settings.read() ui_state = await self.ui_state.read() + history = await self.history.read() theme = await Theme(self, settings["theme"]).read() - return (settings, ui_state, theme) + return (settings, ui_state, history, theme) async def get_flat_sidepane_data(self) -> List[Dict[str, Any]]: diff --git a/src/python/config_files.py b/src/python/config_files.py index 342a06b6..9d83c80e 100644 --- a/src/python/config_files.py +++ b/src/python/config_files.py @@ -174,6 +174,19 @@ class UIState(JSONConfigFile): } +@dataclass +class History(JSONConfigFile): + filename: str = "history.json" + + @property + def path(self) -> Path: + return Path(self.backend.app.appdirs.user_data_dir) / self.filename + + + async def default_data(self) -> JsonData: + return {"console": []} + + @dataclass class Theme(ConfigFile): @property diff --git a/src/qml/DebugConsole.qml b/src/qml/DebugConsole.qml index 73b617c1..6a4b7e97 100644 --- a/src/qml/DebugConsole.qml +++ b/src/qml/DebugConsole.qml @@ -19,9 +19,10 @@ HDrawer { property var target: null property alias t: debugConsole.target - property var history: [] + property var history: window.history.console property alias his: debugConsole.history property int historyEntry: -1 + property int maxHistoryLength: 5 property string help: qsTr( `Javascript debugging console @@ -67,7 +68,11 @@ HDrawer { function runJS(input) { - if (history.slice(-1)[0] !== input) history.push(input) + if (history.slice(-1)[0] !== input) { + history.push(input) + while (history.length > maxHistoryLength) history.shift() + window.historyChanged() + } let output = "" let error = false diff --git a/src/qml/Python.qml b/src/qml/Python.qml index 9ccbd96e..240ec0c0 100644 --- a/src/qml/Python.qml +++ b/src/qml/Python.qml @@ -83,9 +83,12 @@ Python { } function loadSettings(callback=null) { - return callCoro("load_settings", [], ([settings, uiState, theme]) => { + let func = "load_settings" + + return callCoro(func, [], ([settings, uiState, history, theme]) => { window.settings = settings window.uiState = uiState + window.history = history window.theme = Qt.createQmlObject(theme, window, "theme") if (callback) { callback(settings, uiState, theme) } diff --git a/src/qml/Window.qml b/src/qml/Window.qml index ecd49861..a80f9c60 100644 --- a/src/qml/Window.qml +++ b/src/qml/Window.qml @@ -36,6 +36,9 @@ ApplicationWindow { property var uiState: ({}) onUiStateChanged: py.saveConfig("ui_state", uiState) + property var history: ({}) + onHistoryChanged: py.saveConfig("history", history) + property var theme: null readonly property alias py: py