Repository navigation
Expand file tree
/
Copy pathTranscripts.qml
More file actions
483 lines (458 loc) · 19.9 KB
/
Copy pathTranscripts.qml
File metadata and controls
483 lines (458 loc) · 19.9 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import Quickshell
import Quickshell.Io
import qs.Commons
import qs.Ui
Panel {
id: root
moduleName: "io.github.davefano.transcripts"
ipcTarget: "io.github.davefano.transcripts"
readonly property string helper: decodeURIComponent(Qt.resolvedUrl("transcripts.py").toString().replace(/^file:\/\//, ""))
property var entries: []
property var selected: null
property int total: 0
property bool hasMore: false
property int offset: 0
property bool paused: false
property bool showTrash: false
property bool pendingRefresh: false
property string notice: ""
property string query: ""
property string requestQuery: ""
property int requestOffset: 0
property bool requestTrash: false
property var deferredResult: null
readonly property bool interacting: listArea.visible
&& (listHover.hovered || listArea.activeFocus || history.moving || writer.running)
readonly property color fg: Color.foreground
readonly property color muted: Qt.rgba(fg.r, fg.g, fg.b, 0.6)
implicitWidth: button.implicitWidth
implicitHeight: button.implicitHeight
function refresh() {
if (reader.running) { pendingRefresh = true; return }
requestQuery = query
requestOffset = offset
requestTrash = showTrash
reader.command = ["python3", helper, "list", "--page-only", "--query=" + query, "--offset", String(offset)]
.concat(showTrash ? ["--trash"] : [])
reader.running = true
}
function applyResult(result) {
var currentId = history.currentItem ? history.currentItem.modelData.id : -1
if (JSON.stringify(entries) !== JSON.stringify(result.entries)) {
entries = result.entries
history.currentIndex = entries.findIndex(function(entry) { return entry.id === currentId })
}
total = result.total === null ? -1 : result.total
hasMore = result.has_more
offset = result.offset
paused = result.paused
}
function applyDeferredResult() {
if (interacting || !deferredResult) return
var saved = deferredResult
deferredResult = null
if (opened && saved.query === query && saved.offset === offset && saved.trash === showTrash)
applyResult(saved.result)
}
function receiveResult(result) {
if (!opened || requestQuery !== query || requestOffset !== offset || requestTrash !== showTrash) return
// Also defer requests that finished after interaction began.
if (interacting) {
deferredResult = { result: result, query: requestQuery,
offset: requestOffset, trash: requestTrash }
paused = result.paused
} else {
deferredResult = null
applyResult(result)
}
}
onInteractingChanged: if (!interacting) Qt.callLater(applyDeferredResult)
function action(command, id) {
if (writer.running) return
notice = ""
writer.actionName = command
writer.command = ["python3", helper, command].concat(id === undefined ? [] : [String(id)])
writer.running = true
}
function stamp(value) {
return new Date(value).toLocaleString(Qt.locale(), "MMM d · h:mm AP")
}
onOpenedChanged: {
if (opened) {
notice = ""
refresh()
} else {
selected = null
deferredResult = null
entries = []
}
}
onQueryChanged: { deferredResult = null; offset = 0; debounce.restart() }
onShowTrashChanged: { deferredResult = null; offset = 0; selected = null; refresh() }
onSelectedChanged: Qt.callLater(function() {
if (!root.opened) return
if (root.selected) backButton.forceActiveFocus()
else search.forceActiveFocus()
})
Timer { id: debounce; interval: 180; onTriggered: root.refresh() }
Timer { interval: 2000; repeat: true; running: root.opened && !root.interacting; onTriggered: root.refresh() }
Process {
id: reader
stdout: StdioCollector {
waitForEnd: true
onStreamFinished: {
try {
root.receiveResult(JSON.parse(text))
} catch (e) { root.notice = "Could not read history." }
}
}
stderr: StdioCollector {}
onExited: function(code) {
if (code !== 0) root.notice = "Could not read history. Check storage access."
if (root.pendingRefresh) { root.pendingRefresh = false; Qt.callLater(root.refresh) }
}
}
Process {
id: writer
objectName: "transcriptWriter"
property string actionName: ""
stdout: StdioCollector {}
stderr: StdioCollector {}
onExited: function(code) {
if (code !== 0) root.notice = "Could not complete action. Check clipboard or storage access."
else if (actionName === "copy") root.notice = "Copied — ready to paste."
else if (actionName === "capture-clipboard") root.notice = "Clipboard text saved."
else if (actionName === "trash" || actionName === "restore") {
root.selected = null
root.notice = actionName === "trash" ? "Moved to Trash. You can restore it there." : "Restored to history."
}
root.refresh()
}
}
BarIconButton {
id: button
anchors.fill: parent
bar: root.bar
text: ""
tooltipText: "Transcripts"
onPressed: root.toggle()
}
KeyboardPanel {
id: panel
anchorItem: button
owner: root
bar: root.bar
open: root.opened
focusTarget: search
contentWidth: panel.fittedContentWidth(Style.space(460))
contentHeight: panel.fittedContentHeight(Style.space(590), Style.space(590))
ColumnLayout {
anchors.fill: parent
spacing: Style.space(10)
Keys.onEscapePressed: {
if (root.selected) { root.selected = null; search.forceActiveFocus() }
else root.close()
}
RowLayout {
Layout.fillWidth: true
Text {
text: "Transcripts"
color: root.fg
font.family: Style.font.family
font.pixelSize: Style.font.heading
font.bold: true
Layout.fillWidth: true
}
PanelActionButton {
iconText: root.paused ? "▶" : "Ⅱ"
tooltipText: root.paused ? "Resume saving transcripts" : "Pause saving transcripts"
focusable: true
enabled: !writer.running
onClicked: root.action(root.paused ? "resume" : "pause")
}
PanelActionButton {
iconText: "×"
tooltipText: "Close"
focusable: true
onClicked: root.close()
}
}
Text {
Layout.fillWidth: true
text: root.paused ? "Saving paused · your history stays here" : "Saved on this device · connected dictation tools"
color: root.paused ? Color.accent : root.muted
font.family: Style.font.family
font.pixelSize: Style.font.caption
wrapMode: Text.Wrap
}
TextField {
id: search
objectName: "transcriptSearch"
Layout.fillWidth: true
visible: !root.selected
placeholderText: "Search transcripts or tools…"
color: root.fg
placeholderTextColor: root.muted
font.family: Style.font.family
font.pixelSize: Style.font.body
selectByMouse: true
onTextChanged: root.query = text
background: Rectangle {
color: "transparent"
radius: Style.cornerRadius
border.color: search.activeFocus ? Color.accent : root.muted
}
Keys.onDownPressed: { history.forceActiveFocus(); history.currentIndex = 0 }
onAccepted: if (root.entries.length) root.selected = root.entries[0]
}
RowLayout {
visible: !root.selected
Layout.fillWidth: true
ActionButton {
label: root.showTrash ? "← History" : "Trash"
onChosen: root.showTrash = !root.showTrash
}
Item { Layout.fillWidth: true }
ActionButton {
label: "Save clipboard"
enabled: !writer.running && !root.paused
onChosen: root.action("capture-clipboard")
}
}
FocusScope {
id: listArea
Layout.fillWidth: true
Layout.fillHeight: true
visible: !root.selected
HoverHandler { id: listHover }
ListView {
id: history
objectName: "transcriptHistory"
anchors.fill: parent
clip: true
spacing: Style.space(7)
model: root.entries
boundsBehavior: Flickable.StopAtBounds
ScrollBar.vertical: ScrollBar {}
Keys.onReturnPressed: if (currentItem) root.selected = root.entries[currentIndex]
Keys.onEnterPressed: if (currentItem) root.selected = root.entries[currentIndex]
Keys.onTabPressed: function(event) {
if (currentItem) currentItem.focusActions()
else event.accepted = false
}
delegate: Rectangle {
id: entryRow
required property var modelData
required property int index
readonly property bool actionsActive: rowHover.hovered
|| (history.activeFocus && history.currentIndex === index)
|| copyButton.activeFocus || openButton.activeFocus
function focusActions() {
if (copyButton.enabled) copyButton.forceActiveFocus()
else openButton.forceActiveFocus()
}
width: history.width
height: entryColumn.implicitHeight + Style.space(24)
radius: Style.cornerRadius
color: actionsActive
? Qt.rgba(root.fg.r, root.fg.g, root.fg.b, 0.10)
: Qt.rgba(root.fg.r, root.fg.g, root.fg.b, 0.04)
HoverHandler { id: rowHover; cursorShape: Qt.PointingHandCursor }
Column {
id: entryColumn
anchors { left: parent.left; right: parent.right; top: parent.top; margins: Style.space(12) }
spacing: Style.space(6)
Text {
width: parent.width - rowActions.width - Style.space(6)
height: Math.max(implicitHeight, rowActions.height)
verticalAlignment: Text.AlignVCenter
text: modelData.source + " · " + root.stamp(modelData.created_at)
textFormat: Text.PlainText
color: root.muted
font.family: Style.font.family
font.pixelSize: Style.font.caption
elide: Text.ElideRight
}
Text {
width: parent.width
text: modelData.text
textFormat: Text.PlainText
color: root.fg
font.family: Style.font.family
font.pixelSize: Style.font.body
wrapMode: Text.Wrap
maximumLineCount: 3
elide: Text.ElideRight
}
}
MouseArea {
anchors.fill: parent
onClicked: root.selected = modelData
}
Row {
id: rowActions
anchors { top: parent.top; right: parent.right; margins: Style.space(12) }
spacing: Style.space(4)
width: copyButton.size + spacing + openButton.size
height: Math.max(copyButton.size, openButton.size)
visible: entryRow.actionsActive
PanelActionButton {
id: copyButton
objectName: "copyTranscript"
iconText: "\uf0c5"
tooltipText: "Copy transcript"
size: Style.space(22)
focusable: true
enabled: !writer.running
Accessible.role: Accessible.Button
Accessible.name: tooltipText
KeyNavigation.tab: openButton
KeyNavigation.backtab: history
onClicked: root.action("copy", entryRow.modelData.id)
}
PanelActionButton {
id: openButton
iconText: "\uf08e"
tooltipText: "Open full transcript"
size: Style.space(22)
focusable: true
Accessible.role: Accessible.Button
Accessible.name: tooltipText
KeyNavigation.tab: search
KeyNavigation.backtab: copyButton
onClicked: root.selected = entryRow.modelData
}
}
}
}
Text {
anchors.centerIn: parent
width: parent.width - Style.space(20)
visible: root.entries.length === 0
text: root.query ? "No matching transcripts."
: root.showTrash ? "Trash is empty."
: "Your words, kept here.\n\nDictate with a connected tool and the transcript will appear automatically."
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.Wrap
color: root.muted
font.family: Style.font.family
font.pixelSize: Style.font.body
}
}
ColumnLayout {
visible: !!root.selected
Layout.fillWidth: true
Layout.fillHeight: true
spacing: Style.space(10)
RowLayout {
Layout.fillWidth: true
ActionButton { id: backButton; label: "← Back"; onChosen: { root.selected = null; search.forceActiveFocus() } }
Item { Layout.fillWidth: true }
ActionButton {
label: root.showTrash ? "Restore" : "Move to Trash"
enabled: !writer.running
onChosen: if (root.selected) root.action(root.showTrash ? "restore" : "trash", root.selected.id)
}
ActionButton {
label: "Copy"
enabled: !writer.running
onChosen: if (root.selected) root.action("copy", root.selected.id)
}
}
Text {
Layout.fillWidth: true
text: root.selected ? root.selected.source + " · " + root.stamp(root.selected.created_at) : ""
textFormat: Text.PlainText
color: root.muted
font.family: Style.font.family
font.pixelSize: Style.font.caption
wrapMode: Text.Wrap
}
ScrollView {
Layout.fillWidth: true
Layout.fillHeight: true
clip: true
contentWidth: availableWidth
TextArea {
text: root.selected ? root.selected.text : ""
textFormat: TextEdit.PlainText
readOnly: true
selectByMouse: true
wrapMode: TextEdit.Wrap
color: root.fg
font.family: Style.font.family
font.pixelSize: Style.font.body
background: null
}
}
}
RowLayout {
visible: !root.selected
Layout.fillWidth: true
ActionButton {
label: "←"
enabled: root.offset > 0
onChosen: { root.offset = Math.max(0, root.offset - 50); root.refresh(); history.positionViewAtBeginning() }
}
Text {
Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter
text: root.total === 0 ? "0 transcripts" : (root.offset + 1) + "–" + (root.offset + root.entries.length)
+ (root.total >= 0 ? " of " + root.total : "")
color: root.muted
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
ActionButton {
label: "→"
enabled: root.hasMore
onChosen: { root.offset += 50; root.refresh(); history.positionViewAtBeginning() }
}
}
Text {
Layout.fillWidth: true
visible: root.notice !== ""
text: root.notice
textFormat: Text.PlainText
color: Color.accent
wrapMode: Text.Wrap
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
}
}
component ActionButton: Rectangle {
id: control
property string label: ""
signal chosen()
implicitWidth: labelText.implicitWidth + Style.space(18)
implicitHeight: Style.space(30)
radius: Style.cornerRadius
color: activeFocus || actionMouse.containsMouse ? Qt.rgba(root.fg.r, root.fg.g, root.fg.b, 0.12) : "transparent"
opacity: enabled ? 1 : 0.4
activeFocusOnTab: true
Accessible.role: Accessible.Button
Accessible.name: label
Accessible.onPressAction: if (enabled) chosen()
Keys.onReturnPressed: chosen()
Keys.onSpacePressed: chosen()
Text {
id: labelText
anchors.centerIn: parent
text: control.label
color: root.fg
font.family: Style.font.family
font.pixelSize: Style.font.caption
}
MouseArea {
id: actionMouse
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: control.chosen()
}
}
}