From 58d491a2b3a54b9b5a94dfcd38146d7915c831e3 Mon Sep 17 00:00:00 2001 From: miruka Date: Thu, 5 Sep 2019 15:05:57 -0400 Subject: [PATCH] Add basic JS/QML debug console --- src/python/config_files.py | 5 ++- src/qml/DebugConsole.qml | 90 ++++++++++++++++++++++++++++++++++++++ src/qml/Shortcuts.qml | 8 +++- src/qml/UI.qml | 6 +++ 4 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 src/qml/DebugConsole.qml diff --git a/src/python/config_files.py b/src/python/config_files.py index 70d174af..6b7344c1 100644 --- a/src/python/config_files.py +++ b/src/python/config_files.py @@ -108,8 +108,9 @@ class UISettings(JSONConfigFile): "theme": "Default.qpl", "writeAliases": {}, "keys": { - "startDebugger": "Alt+Shift+D", - "reloadConfig": "Alt+Shift+R", + "startPythonDebugger": "Alt+Shift+D", + "toggleDebugConsole": "Alt+Shift+C", + "reloadConfig": "Alt+Shift+R", "scrollUp": ["Alt+Up", "Alt+K"], "scrollDown": ["Alt+Down", "Alt+J"], diff --git a/src/qml/DebugConsole.qml b/src/qml/DebugConsole.qml new file mode 100644 index 00000000..df290280 --- /dev/null +++ b/src/qml/DebugConsole.qml @@ -0,0 +1,90 @@ +import QtQuick 2.12 +import QtQuick.Window 2.12 +import QtQuick.Layouts 1.12 +import "Base" + +Window { + title: qsTr("Debug console") + width: 640 + height: 480 + visible: false + flags: Qt.WA_TranslucentBackground + color: "transparent" + + + function runJS(input) { + let error = false + + try { + var output = input.startsWith("j ") ? + JSON.stringify(eval(input.substring(2)), null, 4) : + String(eval(input)) + } catch (err) { + error = true + var output = err.toString() + } + + commandsView.model.insert(0, { input, output, error }) + } + + + HColumnLayout { + anchors.fill: parent + + HListView { + id: commandsView + spacing: theme.spacing + topMargin: theme.spacing + bottomMargin: topMargin + leftMargin: theme.spacing + rightMargin: rightMargin + clip: true + verticalLayoutDirection: ListView.BottomToTop + + Layout.fillWidth: true + Layout.fillHeight: true + + model: ListModel {} + + delegate: HColumnLayout { + width: commandsView.width + + HLabel { + text: "> " + model.input + wrapMode: Text.Wrap + color: theme.chat.message.quote + visible: model.input + + Layout.fillWidth: true + } + + HLabel { + text: "" + model.output + wrapMode: Text.Wrap + color: model.error ? + theme.colors.errorText : theme.colors.text + visible: model.output + + Layout.fillWidth: true + } + } + + Rectangle { + z: -10 + anchors.fill: parent + color: theme.colors.weakBackground + } + } + + HTextField { + focus: true + onAccepted: if (text) runJS(text) + backgroundColor: Qt.hsla(0, 0, 0, 0.85) + bordered: false + placeholderText: qsTr("Type some JavaScript...") + + Layout.fillWidth: true + + } + } +} diff --git a/src/qml/Shortcuts.qml b/src/qml/Shortcuts.qml index d39fab79..01150223 100644 --- a/src/qml/Shortcuts.qml +++ b/src/qml/Shortcuts.qml @@ -9,10 +9,16 @@ HShortcutHandler { HShortcut { enabled: debugMode - sequences: settings.keys.startDebugger + sequences: settings.keys.startPythonDebugger onPressed: py.call("APP.pdb") } + HShortcut { + enabled: debugMode + sequences: settings.keys.toggleDebugConsole + onPressed: mainUI.debugConsole.visible = ! mainUI.debugConsole.visible + } + HShortcut { sequences: settings.keys.reloadConfig onPressed: py.loadSettings(() => { mainUI.pressAnimation.start() }) diff --git a/src/qml/UI.qml b/src/qml/UI.qml index ae654c32..2e584512 100644 --- a/src/qml/UI.qml +++ b/src/qml/UI.qml @@ -15,6 +15,7 @@ Item { property alias sidePane: sidePane property alias pageLoader: pageLoader property alias pressAnimation: pressAnimation + property alias debugConsole: debugConsoleLoader.item SequentialAnimation { id: pressAnimation @@ -138,4 +139,9 @@ Item { } } } + + HLoader { + id: debugConsoleLoader + source: debugMode ? "DebugConsole.qml" : "" + } }