-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshell.qml
More file actions
651 lines (551 loc) · 26.2 KB
/
shell.qml
File metadata and controls
651 lines (551 loc) · 26.2 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Wayland
import "components"
import "modes"
ShellRoot {
id: shellRoot
Variants {
model: Quickshell.screens
delegate: PanelWindow {
id: root
required property ShellScreen modelData
property var targetScreen: modelData
property string fullScreenshot: ""
property string cropJpg: ""
property string lensHtml: ""
property string ocrPhase: ""
property bool ocrDirect: false
property bool ocrSingleLine: false
property bool ocrRaw: false
property bool grimReady: false
property bool cleanupHandled: false
property real detectedMonitorScale: 1.0
property var pendingOcrRect: null
readonly property bool ctsEnabled: settings.selectionStyle === "circle"
readonly property real monitorScale: detectedMonitorScale
readonly property string activeOcrMode: ocrDirect ? "direct" : ocrSingleLine ? "single" : ocrRaw ? "raw" : ""
function escapeShell(text) {
if (typeof text !== 'string') return '';
return text.replace(/'/g, "'\\''");
}
function cleanup() {
if (cleanupHandled) return;
cleanupHandled = true;
if (fullScreenshot)
Quickshell.execDetached(["rm", "-f", fullScreenshot, cropJpg, lensHtml]);
}
function copyAndQuit(text, wordCount) {
cleanupHandled = true;
const sanitized = selectMode.sanitizeText(text);
const escaped = escapeShell(sanitized);
// For notification: collapse whitespace, trim and truncate
const clean = sanitized.replace(/\n/g, ' ').replace(/\s+/g, ' ').trim();
const truncated = clean.length > 100 ? clean.substring(0, 97) + "..." : clean;
const escapedTrunc = escapeShell(truncated);
ocrCopyProc.command = ["sh", "-c",
`printf '%s' '${escaped}' | wl-copy && ` +
`notify-send 'Copied' '${escapedTrunc}' -t 3000 ; ` +
`rm -f "${fullScreenshot}" "${cropJpg}"`
];
ocrCopyProc.running = true;
}
function setOcrMode(mode) {
console.log("[QuickSnip] OCR Mode set to: " + mode);
ocrDirect = mode === "direct" && !ocrDirect;
ocrSingleLine = mode === "single" && !ocrSingleLine;
ocrRaw = mode === "raw" && !ocrRaw;
if (ocrDirect) { ocrSingleLine = false; ocrRaw = false; }
if (ocrSingleLine) { ocrDirect = false; ocrRaw = false; }
if (ocrRaw) { ocrDirect = false; ocrSingleLine = false; }
}
function buildSearchUrl(text, engine) {
const query = encodeURIComponent(text);
const engines = {
"google": "https://www.google.com/search?q=%s&cs=1",
"duckduckgo": "https://duckduckgo.com/?q=%s",
"ddg": "https://duckduckgo.com/?q=%s",
"bing": "https://www.bing.com/search?q=%s",
"brave": "https://search.brave.com/search?q=%s"
};
const lower = engine.trim().toLowerCase();
let format = engines[lower] || engine.trim();
if (!format.includes("%s"))
format = engines["google"];
return format.replace("%s", query);
}
function buildAiUrl(text, provider) {
const query = encodeURIComponent(text);
const providers = {
"chatgpt": "https://chatgpt.com/?q=%s",
"claude": "https://claude.ai/new?q=%s",
"grok": "https://grok.com/?q=%s"
};
const lower = provider.trim().toLowerCase();
let format = providers[lower] || provider.trim();
if (!format.includes("%s"))
format = providers["chatgpt"];
return format.replace("%s", query);
}
function executeSmartAction(text) {
if (!text || text.length === 0) return;
const sanitized = selectMode.sanitizeText(text);
const action = selectMode.analyzeText(sanitized, settings.smartAiThreshold);
if (action === "url")
openInBrowser(selectMode.cleanUrl(sanitized), settings.smartOpenIn);
else if (action === "code")
openInBrowser(buildAiUrl("Explain this code and show errors if any:\n\n" + sanitized, settings.aiProvider), settings.aiOpenIn);
else if (action === "dictionary")
openInBrowser(buildSearchUrl("define " + sanitized, settings.searchEngine), settings.searchOpenIn);
else if (action === "ai")
openInBrowser(buildAiUrl(sanitized, settings.aiProvider), settings.aiOpenIn);
else
openInBrowser(buildSearchUrl(sanitized, settings.searchEngine), settings.searchOpenIn);
}
function openInBrowser(url, openMethod) {
const sanitizedUrl = selectMode.sanitizeText(url);
if (openMethod === "sidebar") {
const bc = settings.browserClass || "zen";
const escaped = escapeShell(sanitizedUrl);
const openCmd = `OLD_CLIP=$(wl-paste --no-newline --type text/plain 2>/dev/null || true); ` +
`printf '%s' '${escaped}' | wl-copy && ` +
`sleep 0.1 && wlrctl window focus ${bc} && ` +
`sleep 0.2 && wtype -M alt -k s -m alt && ` +
`sleep 0.3 && if [ "$(wl-paste)" = "OPEN_SIDEBAR_PLEASE" ]; then ` +
`wtype -M alt -k g -m alt && ` +
`sleep 0.1 && printf '%s' '${escaped}' | wl-copy ; fi; ` +
`sleep 1; printf '%s' "$OLD_CLIP" | wl-copy`;
Quickshell.execDetached(["sh", "-c", openCmd]);
} else {
Quickshell.execDetached(["xdg-open", sanitizedUrl]);
}
cleanup();
Qt.quit();
}
function executeAction() {
console.log("[QuickSnip] executeAction - screen:", !!targetScreen,
"grimReady:", grimReady, "scale:", monitorScale);
if (!targetScreen || !grimReady) return;
const x = Math.round(selector.selectionX * monitorScale);
const y = Math.round(selector.selectionY * monitorScale);
const w = Math.round(selector.selectionWidth * monitorScale);
const h = Math.round(selector.selectionHeight * monitorScale);
console.log("[QuickSnip] Region physical:", x, y, w, h,
"logical:", selector.selectionX, selector.selectionY,
selector.selectionWidth, selector.selectionHeight);
if (w < 10 || h < 10) {
startOcr();
return;
}
if (ocrDirect || ocrSingleLine || ocrRaw) {
visible = false;
proc.command = ["sh", "-c", ocrMode.getCommand(x, y, w, h, ocrSingleLine, ocrRaw)];
proc.running = true;
} else {
startOcr(x, y, w, h);
}
}
function startOcr(x, y, w, h) {
if (!targetScreen || ocrPhase !== "") {
return;
}
if (!grimReady) {
if (x !== undefined) pendingOcrRect = { x: x, y: y, w: w, h: h };
else pendingOcrRect = {};
return;
}
const hasRegion = x !== undefined;
selectMode.regionX = hasRegion ? x / monitorScale : 0;
selectMode.regionY = hasRegion ? y / monitorScale : 0;
selectMode.regionW = hasRegion ? w / monitorScale : width;
selectMode.regionH = hasRegion ? h / monitorScale : height;
selectMode.loading = true;
ocrPhase = "loading";
const physW = hasRegion ? w : width * monitorScale;
const physH = hasRegion ? h : height * monitorScale;
const area = physW * physH;
const isSmall = area < 40000;
const isTiny = physH < 30;
let upscale = "";
let scaleFactor = 1.0;
if (isTiny) {
upscale = "-resize 400%";
scaleFactor = 4.0;
} else if (isSmall) {
upscale = "-resize 200%";
scaleFactor = 2.0;
}
// Global conversion factor: physical scale × upscale factor
selectMode.monitorScale = monitorScale * scaleFactor;
console.log("[QuickSnip] OCR overlay scale:", selectMode.monitorScale,
"(monitor:", monitorScale, "× upscale:", scaleFactor, ")",
"region logical:", selectMode.regionX, selectMode.regionY,
selectMode.regionW, selectMode.regionH);
let psm = "3";
if (isTiny || physH < 50 || physW / physH > 10) psm = "7";
else if (isSmall) psm = "6";
const cropParams = hasRegion ? `-crop ${w}x${h}+${x}+${y} +repage` : "";
function getPath(filename) {
return Quickshell.cachePath(filename).toString().replace(/^file:\/\//, "");
}
const tmpA = getPath("snip-ocr-a.png");
const tmpB = getPath("snip-ocr-b.png");
const outA = getPath("snip-tsv-a");
const outB = getPath("snip-tsv-b");
ocrTsvProc.command = ["sh", "-c", `
SCORE_AWK='
NR > 1 && $1 == "5" && $12 != "" {
conf = $11 + 0; w = $9 + 0; h = $10 + 0; y = $8 + 0; x = $7 + 0; word = $12; len = length(word)
if (h < 6 || w < 2 || conf < 15) next
word_score = len * conf
ratio = w / h
if (ratio > 0.2 && ratio < 15) word_score *= 1.2
tmp = word; gsub(/[a-zA-Z0-9]/, "", tmp); noise_chars = length(tmp)
if (len > 1 && noise_chars / len > 0.7) word_score *= 0.3
total += word_score; count++
bucket = int(y / (h > 0 ? h : 10)); lines[bucket]++
}
END {
line_bonus = 0
for (b in lines) {
if (lines[b] >= 2) line_bonus += lines[b] * 100
if (lines[b] >= 5) line_bonus += lines[b] * 200
}
printf "%d", total + line_bonus
}
'
tmpA="/dev/shm/qs-a-$(date +%s%N).pnm"
tmpB="/dev/shm/qs-b-$(date +%s%N).pnm"
magick "${fullScreenshot}" ${cropParams} -colorspace Gray ${upscale} -depth 8 \
\\( +clone -negate -write "$tmpB" \\) "$tmpA" &&
tesseract "$tmpA" "${outA}" -l eng --psm ${psm} --oem 1 -c preserve_interword_spaces=1 tsv 2>/dev/null &
PID1=$!
tesseract "$tmpB" "${outB}" -l eng --psm ${psm} --oem 1 -c preserve_interword_spaces=1 tsv 2>/dev/null &
PID2=$!
wait $PID1 $PID2
SCORE_A=$(awk -F'\\t' "$SCORE_AWK" "${outA}.tsv")
SCORE_B=$(awk -F'\\t' "$SCORE_AWK" "${outB}.tsv")
if [ "\${SCORE_A:-0}" -ge "\${SCORE_B:-0}" ]; then
cat "${outA}.tsv"
else
cat "${outB}.tsv"
fi
rm -f "$tmpA" "$tmpB" "${outA}.tsv" "${outB}.tsv"
`];
ocrTsvProc.running = true;
}
function cancelOcr() {
ocrPhase = "";
selectMode.reset();
if (ocrTsvProc.running) ocrTsvProc.running = false;
}
function initPaths() {
cleanupHandled = false;
const ts = Date.now();
const sid = root.targetScreen.name.replace(/[^a-zA-Z0-9]/g, "_");
function getPath(filename) {
return Quickshell.cachePath(filename).toString().replace(/^file:\/\//, "");
}
fullScreenshot = getPath(`snip-full-${sid}-${ts}.png`);
cropJpg = getPath(`snip-crop-${sid}-${ts}.jpg`);
lensHtml = getPath(`snip-lens-${sid}-${ts}.html`);
visible = true;
grimProc.running = true;
}
screen: targetScreen
exclusionMode: ExclusionMode.Ignore
WlrLayershell.layer: WlrLayer.Overlay
WlrLayershell.keyboardFocus: WlrKeyboardFocus.OnDemand
color: "transparent"
visible: false
anchors {
left: true; right: true; top: true; bottom: true
}
Component.onCompleted: {
console.log("[QuickSnip] Screen:", targetScreen?.name,
"Window:", width, "×", height);
initPaths();
}
Settings { id: settings }
FileView {
id: settingsFile
property string configHome: Quickshell.env("XDG_CONFIG_HOME") || (Quickshell.env("HOME") + "/.config")
property string userPath: configHome + "/quickshell/QuickSnip/settings.json"
property string systemPath: "/etc/xdg/quickshell/QuickSnip/settings.json"
path: userPath
onTextChanged: {
try {
const raw = (typeof text === 'function') ? text() : text;
if (raw?.trim().length > 0) {
settings.source = JSON.parse(raw);
} else if (path === userPath) {
console.log("[QuickSnip] User settings not found, trying system path...");
path = systemPath;
}
} catch (e) {
console.warn("Failed to parse settings.json:", e);
}
}
}
LensMode {
id: lensMode
fullScreenshot: root.fullScreenshot
cropJpg: root.cropJpg
lensHtml: root.lensHtml
browserClass: settings.browserClass
}
OcrMode {
id: ocrMode
fullScreenshot: root.fullScreenshot
}
SelectMode {
id: selectMode
stripNewlines: root.ocrSingleLine
rawMode: root.ocrRaw
}
Process {
id: grimProc
command: targetScreen ? ["grim", "-o", targetScreen.name, fullScreenshot] : ["true"]
onExited: (code) => {
if (code === 0) {
console.log("[QuickSnip] Screenshot captured, detecting scale...");
scaleDetectProc.running = true;
} else if (!grimReady) {
console.warn("[QuickSnip] grim failed (code:", code, "), retrying...");
retryTimer.start();
}
}
}
Process {
id: scaleDetectProc
property string _output: ""
command: ["magick", "identify", "-format", "%w", fullScreenshot]
running: false
stdout: StdioCollector {
onStreamFinished: {
scaleDetectProc._output = text || "";
}
}
onExited: (code) => {
if (code === 0 && _output.trim().length > 0) {
const imgWidth = parseInt(_output.trim());
if (imgWidth > 0 && root.width > 0) {
detectedMonitorScale = Math.round((imgWidth / root.width) * 100) / 100;
}
console.log("[QuickSnip] Scale:", detectedMonitorScale,
"(image:", imgWidth, "px, window:", root.width, "px)");
} else {
console.warn("[QuickSnip] Scale detection failed (code:", code, "), using 1.0");
}
_output = "";
grimReady = true;
if (pendingOcrRect) {
if (pendingOcrRect.w !== undefined)
startOcr(pendingOcrRect.x, pendingOcrRect.y, pendingOcrRect.w, pendingOcrRect.h);
else
startOcr();
pendingOcrRect = null;
}
}
}
Process {
id: ocrTsvProc
property string _collected: ""
running: false
stdout: StdioCollector {
onStreamFinished: {
console.log("[QuickSnip] OCR stream finished, length:", text ? text.length : 0);
ocrTsvProc._collected = text || "";
}
}
stderr: StdioCollector {
onStreamFinished: console.log("[QuickSnip] OCR stderr:", text);
}
onExited: (code) => {
console.log("[QuickSnip] OCR exited:", code, "collected:", _collected.length);
if (code === 0 && _collected.length > 0) {
selectMode.words = selectMode.parseTSV(_collected);
selectMode.loading = false;
ocrPhase = "words";
} else {
console.error("[QuickSnip] OCR failed:", code);
cancelOcr();
}
_collected = "";
}
}
Process {
id: ocrCopyProc
running: false
onExited: { cleanup(); Qt.quit(); }
}
Process {
id: proc
onExited: (code) => {
if (code !== 0) console.error("[QuickSnip] Action failed:", code);
Qt.quit();
}
}
Timer {
id: retryTimer
interval: 200
onTriggered: { if (targetScreen) grimProc.running = true; }
}
Image {
id: bgImage
anchors.fill: parent
z: -1
source: grimReady ? "file://" + fullScreenshot : ""
fillMode: Image.Stretch
cache: false
}
ShaderEffect {
anchors.fill: parent
z: 0
property vector4d selectionRect: {
if (ctsEnabled && selector.path.length > 0)
return Qt.vector4d(0, 0, 0, 0);
return Qt.vector4d(selector.selectionX, selector.selectionY,
selector.selectionWidth, selector.selectionHeight);
}
property real dimOpacity: 0.5
property vector2d screenSize: Qt.vector2d(root.width, root.height)
property real borderRadius: 4
property real outlineThickness: 1
fragmentShader: Qt.resolvedUrl("shaders/dimming.frag.qsb")
}
RegionSelector {
id: selector
visible: ocrPhase !== "words" && ocrPhase !== "loading"
ctsEnabled: root.ctsEnabled
onRegionSelected: executeAction()
onFullScreenRequested: startOcr()
onRightClicked: {
path = [];
selectionX = selectionY = selectionWidth = selectionHeight = 0;
}
}
WordOverlay {
id: wordOverlay
visible: ocrPhase === "loading" || ocrPhase === "words"
words: selectMode.words
selectedIndices: selectMode.selectedIndices
startIndex: selectMode.startIndex
endIndex: selectMode.endIndex
isMultiSelect: selectMode.isMultiSelect
regionX: selectMode.regionX
regionY: selectMode.regionY
regionW: selectMode.regionW
regionH: selectMode.regionH
monitorScale: selectMode.monitorScale
loading: selectMode.loading
onWordTapped: (index) => selectMode.selectWord(index)
onWordToggled: (index) => selectMode.toggleWord(index)
onWordRangeExtended: (index) => selectMode.extendToWord(index)
onCtrlReleased: selectMode.convertToRange()
onHandleStartDragged: (index) => selectMode.setStartIndex(index)
onHandleEndDragged: (index) => selectMode.setEndIndex(index)
onSelectAllRequested: selectMode.selectAll()
onWordDoubleTapped: (index) => {
if (selectMode.selectedIndices.indexOf(index) === -1) {
selectMode.selectWord(index);
}
executeSmartAction(selectMode.getSelectedText());
}
onCopyRequested: {
const text = selectMode.getSelectedText();
if (text.length > 0)
copyAndQuit(text, selectMode.selectedIndices.length);
}
onSmartRequested: {
executeSmartAction(selectMode.getSelectedText());
}
onTranslateRequested: {
const text = selectMode.getSelectedText();
if (text.length === 0) return;
const query = encodeURIComponent(text);
openInBrowser(
`https://translate.google.com/?sl=auto&tl=${settings.targetLang}&text=${query}&op=translate`,
settings.translationOpenIn
);
}
onLensRequested: {
const x = selectMode.regionX * monitorScale;
const y = selectMode.regionY * monitorScale;
const w = selectMode.regionW * monitorScale;
const h = selectMode.regionH * monitorScale;
Quickshell.execDetached(["sh", "-c", lensMode.getCommand(x, y, w, h, settings.lensOpenIn)]);
Qt.quit();
}
}
Item {
anchors.fill: parent
z: 999
visible: ocrPhase !== "words"
HoverHandler {
target: null
onPointChanged: {
if (!selector.pressed) {
selector.mouseX = point.position.x;
selector.mouseY = point.position.y;
}
}
}
Column {
visible: activeOcrMode !== "" && ocrPhase === ""
x: selector.mouseX + 15
y: selector.mouseY + 15
spacing: 4
Repeater {
model: [
{ show: ocrDirect, label: "Direct Copy" },
{ show: ocrSingleLine, label: "Single Line" },
{ show: ocrRaw, label: "Raw Copy" }
]
Rectangle {
visible: modelData.show
width: modeLabel.width + 16
height: 24
radius: 12
color: "#cba6f7"
Text {
id: modeLabel
anchors.centerIn: parent
text: modelData.label
color: "#11111b"
font.pixelSize: 11
font.bold: true
}
}
}
}
}
Shortcut {
sequence: "Escape"
onActivated: { cleanup(); Qt.quit(); }
}
Shortcut {
sequence: "Return"
onActivated: {
if (ocrPhase === "words" && selectMode.selectedIndices.length > 0)
copyAndQuit(selectMode.getSelectedText(), selectMode.selectedIndices.length);
}
}
Shortcut {
sequence: "Ctrl+A"
onActivated: { if (ocrPhase === "words") selectMode.selectAll(); }
}
Shortcut {
sequence: "Ctrl+C"
onActivated: {
if (ocrPhase === "words" && selectMode.selectedIndices.length > 0)
copyAndQuit(selectMode.getSelectedText(), selectMode.selectedIndices.length);
}
}
Shortcut { sequence: "r"; onActivated: setOcrMode("raw") }
Shortcut { sequence: "d"; onActivated: setOcrMode("direct") }
Shortcut { sequence: "s"; onActivated: setOcrMode("single") }
}
}
}