| 
									
										
										
										
											2019-12-19 07:46:16 -04:00
										 |  |  | // SPDX-License-Identifier: LGPL-3.0-or-later
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  | import QtQuick 2.12 | 
					
						
							|  |  |  | import QtQuick.Window 2.12 | 
					
						
							|  |  |  | import QtQuick.Layouts 1.12 | 
					
						
							|  |  |  | import "Base" | 
					
						
							| 
									
										
										
										
											2020-03-28 12:31:42 -04:00
										 |  |  | import "ShortcutBundles" | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-09 08:01:01 -04:00
										 |  |  | HDrawer { | 
					
						
							| 
									
										
										
										
											2019-09-05 16:28:06 -04:00
										 |  |  |     id: debugConsole | 
					
						
							| 
									
										
										
										
											2020-03-28 07:18:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 13:41:57 -04:00
										 |  |  |     property Item previouslyFocused: null | 
					
						
							| 
									
										
										
										
											2019-12-09 13:34:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-19 13:41:57 -04:00
										 |  |  |     property QtObject target: null | 
					
						
							| 
									
										
										
										
											2019-09-05 16:09:04 -04:00
										 |  |  |     property alias t: debugConsole.target | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-09 13:21:12 -04:00
										 |  |  |     property var history: window.history.console | 
					
						
							| 
									
										
										
										
											2019-09-19 18:26:52 -04:00
										 |  |  |     property alias his: debugConsole.history | 
					
						
							|  |  |  |     property int historyEntry: -1 | 
					
						
							| 
									
										
										
										
											2019-12-09 13:51:50 -04:00
										 |  |  |     property int maxHistoryLength: 4096 | 
					
						
							| 
									
										
										
										
											2019-09-19 18:26:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-09 08:01:01 -04:00
										 |  |  |     property string help: qsTr( | 
					
						
							|  |  |  |         `Javascript debugging console | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-09 12:56:25 -04:00
										 |  |  |         Useful variables: | 
					
						
							| 
									
										
										
										
											2020-05-21 00:42:23 -04:00
										 |  |  |             window, theme, settings, utils, mainPane, mainUI, pageLoader | 
					
						
							| 
									
										
										
										
											2019-12-09 12:56:25 -04:00
										 |  |  |             py    Python interpreter | 
					
						
							|  |  |  |             this  The console itself | 
					
						
							|  |  |  |             t     Target item to debug for which this console was opened | 
					
						
							|  |  |  |             his   History, list of commands entered | 
					
						
							| 
									
										
										
										
											2019-12-09 08:01:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Special commands: | 
					
						
							|  |  |  |             .j OBJECT, .json OBJECT  Print OBJECT as human-readable JSON | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             .t, .top     Attach the console to the parent window's top | 
					
						
							|  |  |  |             .b, .bottom  Attach the console to the parent window's bottom | 
					
						
							|  |  |  |             .l, .left    Attach the console to the parent window's left | 
					
						
							|  |  |  |             .r, .right   Attach the console to the parent window's right | 
					
						
							|  |  |  |             .h, .help    Show this help`.replace(/^ {8}/gm, "") | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 17:27:35 -04:00
										 |  |  |     property bool doUselessThing: false | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-09 13:00:42 -04:00
										 |  |  |     readonly property alias commandsView: commandsView | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 05:41:08 -04:00
										 |  |  |     function toggle(targetItem=null, js="", addToHistory=false) { | 
					
						
							| 
									
										
										
										
											2020-04-01 05:37:44 -04:00
										 |  |  |         if (debugConsole.visible) { | 
					
						
							|  |  |  |             debugConsole.visible = false | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         debugConsole.visible = true | 
					
						
							| 
									
										
										
										
											2020-04-01 05:41:08 -04:00
										 |  |  |         debugConsole.target  = | 
					
						
							|  |  |  |             ! targetItem && ! debugConsole.target ? mainUI : | 
					
						
							|  |  |  |             targetItem ? targetItem : | 
					
						
							|  |  |  |             debugConsole.target | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 05:37:44 -04:00
										 |  |  |         if (js) debugConsole.runJS(js, addToHistory) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-28 07:18:00 -04:00
										 |  |  |     function runJS(input, addToHistory=true) { | 
					
						
							|  |  |  |         if (addToHistory && history.slice(-1)[0] !== input) { | 
					
						
							| 
									
										
										
										
											2019-12-09 13:21:12 -04:00
										 |  |  |             history.push(input) | 
					
						
							|  |  |  |             while (history.length > maxHistoryLength) history.shift() | 
					
						
							|  |  |  |             window.historyChanged() | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-09-19 18:26:52 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-09 08:01:01 -04:00
										 |  |  |         let output = "" | 
					
						
							|  |  |  |         let error  = false | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2019-12-09 08:01:01 -04:00
										 |  |  |             if ([".h", ".help"].includes(input)) { | 
					
						
							|  |  |  |                 output = debugConsole.help | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } else if ([".t", ".top"].includes(input)) { | 
					
						
							|  |  |  |                 debugConsole.edge = Qt.TopEdge | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } else if ([".b", ".bottom"].includes(input)) { | 
					
						
							|  |  |  |                 debugConsole.edge = Qt.BottomEdge | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } else if ([".l", ".left"].includes(input)) { | 
					
						
							|  |  |  |                 debugConsole.edge = Qt.LeftEdge | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } else if ([".r", ".right"].includes(input)) { | 
					
						
							|  |  |  |                 debugConsole.edge = Qt.RightEdge | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } else if (input.startsWith(".j ") || input.startsWith(".json ")) { | 
					
						
							|  |  |  |                 output = JSON.stringify(eval(input.substring(2)), null, 4) | 
					
						
							| 
									
										
										
										
											2019-09-19 18:26:52 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 let result = eval(input) | 
					
						
							| 
									
										
										
										
											2019-12-09 08:01:01 -04:00
										 |  |  |                 output     = result instanceof Array ? | 
					
						
							| 
									
										
										
										
											2019-09-19 18:26:52 -04:00
										 |  |  |                              "[" + String(result) + "]" : String(result) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  |         } catch (err) { | 
					
						
							| 
									
										
										
										
											2019-12-09 08:01:01 -04:00
										 |  |  |             error  = true | 
					
						
							|  |  |  |             output = err.toString() | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         commandsView.model.insert(0, { input, output, error }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-12 00:25:57 -04:00
										 |  |  |     objectName: "debugConsole" | 
					
						
							|  |  |  |     edge: Qt.TopEdge | 
					
						
							|  |  |  |     x: horizontal ? 0 : referenceSizeParent.width / 2 - width / 2 | 
					
						
							|  |  |  |     y: vertical ? 0 : referenceSizeParent.height / 2 - height / 2 | 
					
						
							|  |  |  |     width: horizontal ? calculatedSize : Math.min(window.width, 720) | 
					
						
							|  |  |  |     height: vertical ? calculatedSize : Math.min(window.height, 720) | 
					
						
							|  |  |  |     defaultSize: 400 | 
					
						
							|  |  |  |     z: 9999 | 
					
						
							|  |  |  |     position: 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onTargetChanged: { | 
					
						
							|  |  |  |         commandsView.model.insert(0, { | 
					
						
							|  |  |  |             input: "t = " + String(target), | 
					
						
							|  |  |  |             output: "", | 
					
						
							|  |  |  |             error: false, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onVisibleChanged: { | 
					
						
							|  |  |  |         if (visible) { | 
					
						
							|  |  |  |             previouslyFocused = window.activeFocusItem | 
					
						
							|  |  |  |             forceActiveFocus() | 
					
						
							|  |  |  |         } else if (previouslyFocused) { | 
					
						
							|  |  |  |             previouslyFocused.forceActiveFocus() | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onHistoryEntryChanged: | 
					
						
							|  |  |  |         inputField.text = | 
					
						
							|  |  |  |             historyEntry === -1 ? "" : history.slice(-historyEntry - 1)[0] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-28 07:18:00 -04:00
										 |  |  |     HShortcut { | 
					
						
							|  |  |  |         sequences: settings.keys.toggleDebugConsole | 
					
						
							| 
									
										
										
										
											2020-04-01 05:37:44 -04:00
										 |  |  |         onActivated: debugConsole.toggle() | 
					
						
							| 
									
										
										
										
											2020-03-28 07:18:00 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  |     HColumnLayout { | 
					
						
							|  |  |  |         anchors.fill: parent | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         HListView { | 
					
						
							|  |  |  |             id: commandsView | 
					
						
							|  |  |  |             spacing: theme.spacing | 
					
						
							|  |  |  |             topMargin: theme.spacing | 
					
						
							|  |  |  |             bottomMargin: topMargin | 
					
						
							|  |  |  |             leftMargin: theme.spacing | 
					
						
							| 
									
										
										
										
											2019-09-05 16:09:04 -04:00
										 |  |  |             rightMargin: leftMargin | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  |             clip: true | 
					
						
							|  |  |  |             verticalLayoutDirection: ListView.BottomToTop | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Layout.fillWidth: true | 
					
						
							|  |  |  |             Layout.fillHeight: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             model: ListModel {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             delegate: HColumnLayout { | 
					
						
							| 
									
										
										
										
											2019-09-05 16:09:04 -04:00
										 |  |  |                 width: commandsView.width - | 
					
						
							|  |  |  |                        commandsView.leftMargin - commandsView.rightMargin | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 HLabel { | 
					
						
							|  |  |  |                     text: "> " + model.input | 
					
						
							|  |  |  |                     wrapMode: Text.Wrap | 
					
						
							|  |  |  |                     color: theme.chat.message.quote | 
					
						
							| 
									
										
										
										
											2019-09-05 16:09:04 -04:00
										 |  |  |                     font.family: theme.fontFamily.mono | 
					
						
							| 
									
										
										
										
											2019-09-05 16:24:49 -04:00
										 |  |  |                     visible: Boolean(model.input) | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     Layout.fillWidth: true | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 HLabel { | 
					
						
							|  |  |  |                     text: "" + model.output | 
					
						
							|  |  |  |                     wrapMode: Text.Wrap | 
					
						
							|  |  |  |                     color: model.error ? | 
					
						
							|  |  |  |                            theme.colors.errorText : theme.colors.text | 
					
						
							| 
									
										
										
										
											2019-09-05 16:09:04 -04:00
										 |  |  |                     font.family: theme.fontFamily.mono | 
					
						
							| 
									
										
										
										
											2019-09-05 16:24:49 -04:00
										 |  |  |                     visible: Boolean(model.output) | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     Layout.fillWidth: true | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-28 12:31:42 -04:00
										 |  |  |             FlickShortcuts { | 
					
						
							| 
									
										
										
										
											2020-04-01 08:01:13 -04:00
										 |  |  |                 active: debugConsole.visible | 
					
						
							| 
									
										
										
										
											2020-03-28 12:31:42 -04:00
										 |  |  |                 flickable: commandsView | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  |             Rectangle { | 
					
						
							|  |  |  |                 z: -10 | 
					
						
							|  |  |  |                 anchors.fill: parent | 
					
						
							|  |  |  |                 color: theme.colors.weakBackground | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         HTextField { | 
					
						
							| 
									
										
										
										
											2019-09-19 18:26:52 -04:00
										 |  |  |             id: inputField | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  |             focus: true | 
					
						
							| 
									
										
										
										
											2019-09-19 18:43:25 -04:00
										 |  |  |             onAccepted: if (text) { runJS(text); text = ""; historyEntry = -1 } | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  |             backgroundColor: Qt.hsla(0, 0, 0, 0.85) | 
					
						
							|  |  |  |             bordered: false | 
					
						
							| 
									
										
										
										
											2019-12-22 07:21:47 -08:00
										 |  |  |             placeholderText: qsTr("JavaScript debug console - Try .help") | 
					
						
							| 
									
										
										
										
											2019-09-05 16:09:04 -04:00
										 |  |  |             font.family: theme.fontFamily.mono | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-19 18:26:52 -04:00
										 |  |  |             Keys.onUpPressed: | 
					
						
							|  |  |  |                 if (historyEntry + 1 < history.length ) historyEntry += 1 | 
					
						
							| 
									
										
										
										
											2020-05-14 02:24:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-19 18:26:52 -04:00
										 |  |  |             Keys.onDownPressed: | 
					
						
							|  |  |  |                 if (historyEntry - 1 >= -1) historyEntry -= 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 02:24:28 -04:00
										 |  |  |             Keys.onEscapePressed: debugConsole.close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  |             Layout.fillWidth: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-01 17:27:35 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     NumberAnimation { | 
					
						
							|  |  |  |         running: doUselessThing | 
					
						
							| 
									
										
										
										
											2020-04-29 14:00:02 -04:00
										 |  |  |         target: mainUI.mainPane.roomList | 
					
						
							| 
									
										
										
										
											2020-04-01 17:27:35 -04:00
										 |  |  |         property: "rotation" | 
					
						
							|  |  |  |         duration: 250 | 
					
						
							|  |  |  |         from: 360 | 
					
						
							|  |  |  |         to: 0 | 
					
						
							|  |  |  |         loops: Animation.Infinite | 
					
						
							|  |  |  |         onStopped: target.rotation = 0 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-09-05 15:05:57 -04:00
										 |  |  | } |