History persistence for DebugConsole

This commit is contained in:
miruka 2019-12-09 13:21:12 -04:00
parent 718aef8414
commit 37d8d5c68d
5 changed files with 30 additions and 4 deletions

View File

@ -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]]:

View File

@ -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

View File

@ -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

View File

@ -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) }

View File

@ -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