-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsole.qml
295 lines (255 loc) · 8.16 KB
/
console.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.2
import AlgWidgets 2.0
import AlgWidgets.Style 2.0
import "AlgSuggestions" 1.0
Rectangle {
id: root
objectName: "QML Console"
color: AlgStyle.background.color.mainWindow
height: 200
property string fontFamilyCode: "Consolas"
property real layoutStretch: 0.3
QtObject {
id: internal_
property var history: []
property int historyIndex: -1
}
Component.onCompleted: {
Suggestions.loadPackage(alg, "alg")
}
ColumnLayout {
anchors.fill: root
spacing: 0
ListModel {
id: outputModel
}
AlgScrollView {
id: outputScrollView
Layout.fillWidth: true
Layout.fillHeight: true
Layout.bottomMargin: 1
clip: true
ListView {
id: listView
spacing: 4
implicitWidth: outputScrollView.viewportWidth
implicitHeight: contentHeight
model: outputModel
delegate: ColumnLayout {
width: root.width
GridLayout {
columns: 2
Layout.topMargin: 2
Layout.leftMargin: 4
Layout.rightMargin: outputScrollView.rightMargin + 4
Layout.fillWidth: true
Rectangle {
Layout.column: 0
Layout.row: 0
Layout.columnSpan: 2
Layout.fillWidth: true
Layout.fillHeight: true
color: AlgStyle.colors.gray(10)
radius: 2
}
AlgLabel {
Layout.alignment: Qt.AlignTop
Layout.column: 0
Layout.row: 0
Layout.margins: 4
Layout.rightMargin: 0
text: ">"
font.family: root.fontFamilyCode
}
AlgTextEdit {
Layout.fillWidth: true
Layout.column: 1
Layout.row: 0
Layout.margins: 4
Layout.leftMargin: 0
text: command
textFormat: Text.PlainText
wrapMode: TextEdit.Wrap
backgroundColor: AlgStyle.colors.gray(10)
readOnly: true
selectByMouse: true
font.family: root.fontFamilyCode
borderActivated: false
}
AlgTextEdit {
Layout.fillWidth: true
Layout.column: 1
Layout.row: 1
text: output
wrapMode: TextEdit.Wrap
readOnly: true
selectByMouse: true
backgroundColor: AlgStyle.background.color.mainWindow
font.family: root.fontFamilyCode
borderActivated: false
textFormat: outputFormat
}
AlgLabel {
Layout.alignment: Qt.AlignRight
Layout.column: 1
Layout.row: 2
text: qsTr('Time: %1ms').arg(model.elapsedMs)
font.family: root.fontFamilyCode
}
}
Rectangle {
Layout.fillWidth: true
height: 1
color: AlgStyle.colors.gray(10)
}
}
}
}
// Resize handling
Rectangle {
Layout.fillWidth: true
height: 3
color: AlgStyle.colors.gray(10)
MouseArea {
anchors.fill: parent
anchors.topMargin: -6
cursorShape: Qt.SplitVCursor
onPositionChanged: {
var minStretch = 0.1;
var rootRelativeY = parent.mapToItem(root, mouse.x, mouse.y).y;
root.layoutStretch = Math.max(Math.min(1 - rootRelativeY / root.height, 1 - minStretch), minStretch);
}
}
}
// Suggestions Flow
SuggestionsFlow {
id: suggestions_flow
Layout.fillWidth: true
}
// Textedit scrolling implementation based on https://doc.qt.io/qt-5/qml-qtquick-textedit.html#details
AlgScrollView {
id: inputScrollView
Layout.topMargin: 0
Layout.fillWidth: true
Layout.maximumHeight: Math.max(20, root.height * root.layoutStretch)
Layout.minimumHeight: Layout.maximumHeight
contentWidth: Math.max(width, edit.paintedWidth)
contentHeight: Math.max(height, edit.paintedHeight)
function ensureVisible(cursor) {
if (contentX >= cursor.x) {
contentX = cursor.x;
}
else if(contentX + viewportWidth <= cursor.x + cursor.width) {
contentX = cursor.x + cursor.width - viewportWidth;
}
if (contentY >= cursor.y) {
contentY = cursor.y;
}
else if(contentY + viewportHeight <= cursor.y + cursor.height) {
contentY = cursor.y + cursor.height - viewportHeight;
}
}
ColumnLayout {
width: inputScrollView.viewportWidth
SuggestionsTextEdit {
id: edit
Layout.minimumHeight: inputScrollView.viewportHeight
Layout.minimumWidth: inputScrollView.viewportWidth
font.family: root.fontFamilyCode
suggestionsFlow: suggestions_flow
onCursorRectangleChanged: inputScrollView.ensureVisible(cursorRectangle)
MouseArea {
anchors.fill: parent
cursorShape: Qt.IBeamCursor
acceptedButtons: Qt.NoButton
}
Text {
id: placeholder
anchors.fill: edit
anchors.margins: 4
font.family: root.fontFamilyCode
text: qsTr("Write script...")
color: AlgStyle.text.color.disabled
visible: !edit.text
}
function requestEval(evalString) { return eval(evalString) }
function execJavascript(command) {
var object = {
command: command,
output: "",
outputFormat: TextEdit.PlainText,
elapsedMs: 0
};
var startTime = new Date().getTime();
try {
var result = eval(command)
object.ellapsedMs = new Date().getTime() - startTime;
parseText()
if (result) object.output = JSON.stringify(result, null, ' ');
}
catch(exception) {
object.output =
'<font color="#cc2a2a">
Exception <b>%1</b> at line %2:<br/>
%3
</font>'
.arg(exception.name)
.arg(exception.lineNumber)
.arg(exception.message);
object.outputFormat = TextEdit.RichText;
}
outputModel.append(object);
listView.forceLayout();
outputScrollView.contentY = outputScrollView.contentHeight - outputScrollView.viewportHeight;
}
function parseCommand(command) {
var lowerCaseCommand = command.toLowerCase();
switch(lowerCaseCommand) {
case "clear":
outputModel.clear();
break;
default:
execJavascript(command);
break;
}
}
onReturnPressed: {
var command = text.trim();
event.accepted = command.length > 0 && !(event.modifiers & Qt.ShiftModifier);
if (!event.accepted) {
return;
}
parseCommand(command);
internal_.history.splice(0, 0, command);
internal_.historyIndex = -1;
text = "";
}
function setScriptContent(content) {
text = content
cursorPosition = text.length
}
Keys.onPressed: {
switch(event.key) {
case Qt.Key_Up: // Older command
if (internal_.historyIndex < internal_.history.length - 1) {
setScriptContent(internal_.history[++internal_.historyIndex]);
}
break;
case Qt.Key_Down:
if (internal_.historyIndex > 0) { // More recent command
setScriptContent(internal_.history[--internal_.historyIndex]);
}
else { // New command
internal_.historyIndex = -1;
setScriptContent("");
}
break;
}
}
}
}
}
}
}