-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathshell.qml
More file actions
548 lines (471 loc) · 19.9 KB
/
Copy pathshell.qml
File metadata and controls
548 lines (471 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
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
/*
* Copyright (c) 2026 Ronin-CK
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import QtQuick
import QtQuick.Controls
import Quickshell
import Quickshell.Hyprland
import Quickshell.Io
import Quickshell.Wayland
import Quickshell.Widgets
import "components"
Scope {
id: root
property var hyprlandMonitor: Hyprland.focusedMonitor
property string activeScreenName: hyprlandMonitor ? hyprlandMonitor.name : (Quickshell.screens.length > 0 ? Quickshell.screens[0].name : "")
property string mode: ["region", "window"].indexOf(Quickshell.env("HQF_MODE")) !== -1 ? Quickshell.env("HQF_MODE") : "region"
property var modes: ["edit", "region", "window", "temp"]
property bool tempActive: Quickshell.env("HQF_ACTION") === "temp"
property bool editActive: Quickshell.env("HQF_ACTION") === "edit"
property bool shareActive: Quickshell.env("HQF_ACTION") === "share"
property int connectivityStatus: 0
property string lastSavedPath: ""
property string lastTimestamp: ""
readonly property real targetMenuWidth: (modes.length - (editActive ? 1 : 0) - (tempActive ? 1 : 0)) * 100 + 8
property var theme: themeObj
property bool capturing: false
property bool overlaysVisible: true
function parseTOML(text) {
let result = {
};
let section = "";
const lines = text.split(/\r?\n/);
for (let i = 0; i < lines.length; i++) {
let line = lines[i].trim();
if (!line || line.startsWith("#"))
continue;
const secMatch = line.match(/^\[(\w+)\]$/);
if (secMatch) {
section = secMatch[1];
continue;
}
const quotedMatch = line.match(/^(\w+)\s*=\s*"([^"]*)"/);
if (quotedMatch) {
const rawKey = quotedMatch[1];
const key = section ? section + rawKey.charAt(0).toUpperCase() + rawKey.slice(1) : rawKey;
result[key] = quotedMatch[2];
continue;
}
const unquotedMatch = line.match(/^(\w+)\s*=\s*([^\s#]+)/);
if (unquotedMatch) {
const rawKey = unquotedMatch[1];
const key = section ? section + rawKey.charAt(0).toUpperCase() + rawKey.slice(1) : rawKey;
let val = unquotedMatch[2];
if (val === "true") {
val = true;
} else if (val === "false") {
val = false;
} else {
const num = parseFloat(val);
if (!isNaN(num))
val = num;
}
result[key] = val;
continue;
}
}
return result;
}
function shellEscape(s) {
return "'" + s.replace(/'/g, "'\\''") + "'";
}
function grimGeometry(x, y, width, height, screenName) {
let target = null;
for (const m of Hyprland.monitors.values) {
if (m.name === screenName) { target = m; break; }
}
if (!target) target = hyprlandMonitor;
const mx = target.lastIpcObject.x;
const my = target.lastIpcObject.y;
return `${Math.round(x + mx)},${Math.round(y + my)} ${Math.round(width)}x${Math.round(height)}`;
}
function runPostSaveHook() {
const hook = theme.postSaveHook;
if (!hook || !root.lastSavedPath)
return ;
const filePath = root.lastSavedPath;
const fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
const dirPath = filePath.substring(0, filePath.lastIndexOf('/'));
let cmd = hook;
cmd = cmd.replace(/%f/g, shellEscape(filePath));
cmd = cmd.replace(/%n/g, shellEscape(fileName));
cmd = cmd.replace(/%d/g, shellEscape(dirPath));
cmd = cmd.replace(/%t/g, shellEscape(root.lastTimestamp));
Quickshell.execDetached(["sh", "-c", cmd]);
}
function saveScreenshot(x, y, width, height, screenName) {
const geom = grimGeometry(x, y, width, height, screenName);
const picturesBase = Quickshell.env("XDG_PICTURES_DIR") || (Quickshell.env("HOME") + "/Pictures");
const picturesDir = picturesBase + "/Screenshots";
const timestamp = Qt.formatDateTime(new Date(), "yyyy-MM-dd_hh-mm-ss");
const outputPath = `${picturesDir}/screenshot-${timestamp}.png`;
root.lastTimestamp = timestamp;
root.lastSavedPath = root.tempActive ? "" : outputPath;
const ePicturesDir = shellEscape(picturesDir);
const eOutputPath = shellEscape(outputPath);
const eGeom = shellEscape(geom);
const grimRegion = `timeout 5 grim -l 1 -g ${eGeom}`;
const shareCmd = "kdeconnect-cli -l | grep 'reachable' | grep -oP '[a-f0-9-]{8,}'"
+ " | head -1 | xargs -I{} sh -c"
+ " 'kdeconnect-cli -d {} --share \"$1\" && sleep 0.2"
+ " && kdeconnect-cli -d {} --send-clipboard' --";
const maybeShare = (escapedPath) => root.shareActive ? ` && ${shareCmd} ${escapedPath}` : "";
const shareTag = root.shareActive ? " & phone" : "";
const mkdirCmd = `mkdir -p ${ePicturesDir}`;
const sattyCommand =
`${mkdirCmd} && ${grimRegion} - `
+ `| satty --filename - --output-filename ${eOutputPath} --early-exit --init-tool brush --copy-command "wl-copy --type image/png" `
+ `; if [ -f ${eOutputPath} ]; then wl-copy --type image/png < ${eOutputPath}${maybeShare(eOutputPath)}; fi`;
const gradiaCommand =
`${mkdirCmd} && ${grimRegion} ${eOutputPath} `
+ `&& hyprctl dispatch exec -- "gradia ${eOutputPath} || flatpak run be.alexandervanhee.gradia ${eOutputPath}"`;
const defaultSaveCommand =
`${mkdirCmd} && ${grimRegion} ${eOutputPath} `
+ `&& wl-copy --type image/png < ${eOutputPath}`
+ `${maybeShare(eOutputPath)} `
+ `&& notify-send -a "HyprQuickFrame" -i ${eOutputPath} `
+ `-h string:image-path:${eOutputPath} "Screenshot Saved" `
+ `"Saved to ${picturesDir}"`;
const eTempSnip = shellEscape(Quickshell.cachePath(`snip-${timestamp}.png`));
const tempShareCommand =
`${grimRegion} ${eTempSnip} `
+ `&& wl-copy --type image/png < ${eTempSnip}`
+ `${maybeShare(eTempSnip)} `
+ `&& notify-send -a "HyprQuickFrame" "Screenshot Copied" "Copied to clipboard${shareTag}"; `
+ `rm -f ${eTempSnip}`;
const tempPlainCommand =
`${grimRegion} - | wl-copy --type image/png `
+ `&& notify-send -a "HyprQuickFrame" "Screenshot Copied" "Copied to clipboard"`;
const defaultTempCommand = root.shareActive ? tempShareCommand : tempPlainCommand;
let cmd;
if (root.editActive)
cmd = theme.annotationTool === "gradia" ? gradiaCommand : sattyCommand;
else if (root.tempActive)
cmd = defaultTempCommand;
else
cmd = defaultSaveCommand;
root._pendingCmd = cmd;
root.capturing = true;
captureDelayTimer.start();
if (root.editActive)
hideOverlaysTimer.start();
}
property string _pendingCmd: ""
Timer {
id: captureDelayTimer
interval: 100
repeat: false
onTriggered: {
screenshotProcess.command = ["sh", "-c", root._pendingCmd];
screenshotProcess.running = true;
}
}
Timer {
id: hideOverlaysTimer
interval: 300
repeat: false
onTriggered: root.overlaysVisible = false
}
Component.onCompleted: {
connectivityProcess.running = true;
readyWatchdog.start();
}
Timer {
id: readyWatchdog
interval: 4000
repeat: false
onTriggered: {
for (const w of overlayVariants.instances) {
if (w && w.isReady) return;
}
console.error("HyprQuickFrame: screencopy never produced a frame; exiting.");
Qt.quit();
}
}
Theme {
id: themeObj
}
FileView {
id: themeFile
property string configHome: Quickshell.env("XDG_CONFIG_HOME") || (Quickshell.env("HOME") + "/.config")
property string userPath1: configHome + "/hyprquickframe/theme.toml"
property string userPath2: configHome + "/quickshell/HyprQuickFrame/theme.toml"
property string defaultPath: Quickshell.shellDir.toString().replace(/^file:\/\//, "") + "/theme.toml"
path: defaultPath
Component.onCompleted: {
themePathCheck.command = ["sh", "-c", `if [ -f "${userPath1}" ]; then echo "${userPath1}";
elif [ -f "${userPath2}" ]; then echo "${userPath2}";
else echo "${defaultPath}"; fi`];
themePathCheck.running = true;
}
onTextChanged: {
try {
let rawText = (typeof text === 'function') ? text() : text;
themeObj.source = root.parseTOML(rawText);
} catch (e) {
console.warn("Failed to parse theme.toml:", e);
}
}
}
Process {
id: themePathCheck
running: false
stdout: StdioCollector {
onStreamFinished: {
themeFile.path = this.text.trim();
console.log("Theme loaded from:", themeFile.path);
}
}
}
Process {
id: screenshotProcess
running: false
onExited: (code) => {
if (code !== 0)
console.error("Screenshot pipeline failed with exit code:", code);
else
root.runPostSaveHook();
Qt.quit();
}
stdout: StdioCollector {
onStreamFinished: {
if (this.text.trim())
console.log(this.text);
}
}
stderr: StdioCollector {
onStreamFinished: {
if (this.text.trim())
console.warn(this.text);
}
}
}
Process {
id: connectivityProcess
command: ["sh", "-c", "timeout 5 kdeconnect-cli -l | grep 'reachable'"]
onExited: (code) => {
root.connectivityStatus = (code === 0 ? 1 : 2);
}
}
Variants {
id: overlayVariants
model: Quickshell.screens
FreezeScreen {
id: overlay
required property var modelData
property bool isFocused: modelData.name === root.activeScreenName
property var themeRef: root.theme
property var hyprMonitor: {
const monitors = Hyprland.monitors;
const monitorsList = (typeof monitors.values === 'function') ? Array.from(monitors.values()) : (monitors.values || monitors);
for (let i = 0; i < monitorsList.length; i++) {
const m = monitorsList[i];
if (m && m.name === modelData.name)
return m;
}
return null;
}
targetScreen: modelData
visible: root.overlaysVisible
Component.onCompleted: {
if (isFocused)
cursorPosProcess.running = true;
}
Process {
id: cursorPosProcess
command: ["hyprctl", "cursorpos", "-j"]
running: false
stdout: StdioCollector {
onStreamFinished: {
try {
const pos = JSON.parse(this.text.trim());
const monitorPos = Qt.point(pos.x - modelData.x, pos.y - modelData.y);
regionSelector.mouseX = monitorPos.x;
regionSelector.mouseY = monitorPos.y;
windowSelector.mouseX = monitorPos.x;
windowSelector.mouseY = monitorPos.y;
} catch (e) {
console.warn("Failed to parse cursorpos:", e);
}
}
}
}
Shortcut {
sequences: ["Escape", "q"]
onActivated: Qt.quit()
}
Shortcut {
sequence: "r"
onActivated: root.mode = "region"
}
Shortcut {
sequence: "w"
onActivated: root.mode = "window"
}
Shortcut {
sequence: "s"
onActivated: {
let targetMon = overlay.modelData;
if (root.activeScreenName && root.activeScreenName !== targetMon.name) {
const screens = Quickshell.screens;
for (let i = 0; i < screens.length; i++) {
if (screens[i].name === root.activeScreenName) {
targetMon = screens[i];
break;
}
}
}
root.saveScreenshot(0, 0, targetMon.width, targetMon.height, targetMon.name);
}
}
Shortcut {
sequence: "e"
onActivated: {
root.editActive = !root.editActive;
if (root.editActive)
root.tempActive = false;
}
}
Shortcut {
sequence: "t"
onActivated: {
root.tempActive = !root.tempActive;
if (root.tempActive)
root.editActive = false;
}
}
Shortcut {
sequence: "k"
onActivated: {
root.shareActive = !root.shareActive;
if (root.shareActive && !connectivityProcess.running && root.connectivityStatus !== 0)
connectivityProcess.running = true;
}
}
RegionSelector {
id: regionSelector
visible: root.mode === "region" && overlay.isReady && !root.capturing
anchors.fill: parent
dimOpacity: overlay.themeRef.dimOpacity
borderRadius: overlay.themeRef.borderRadius
outlineThickness: overlay.themeRef.outlineThickness
globalAnimations: overlay.themeRef.animations
onRegionSelected: (x, y, width, height) => {
root.saveScreenshot(x, y, width, height, overlay.modelData.name);
}
}
WindowSelector {
id: windowSelector
visible: root.mode === "window" && !root.capturing
anchors.fill: parent
monitor: overlay.hyprMonitor
dimOpacity: overlay.themeRef.dimOpacity
borderRadius: overlay.themeRef.borderRadius
outlineThickness: overlay.themeRef.outlineThickness
animateSelection: overlay.themeRef.animations
onRegionSelected: (x, y, width, height) => {
root.saveScreenshot(x, y, width, height, overlay.modelData.name);
}
}
ControlBar {
id: segmentedControl
visible: overlay.isFocused && overlay.isReady && !root.capturing
modes: root.modes
mode: root.mode
tempActive: root.tempActive
editActive: root.editActive
theme: overlay.themeRef
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
anchors.bottomMargin: overlay.themeRef ? overlay.themeRef.bottomMargin : 60
onModeSelected: (m) => {
return root.mode = m;
}
onTempToggled: {
root.tempActive = true;
root.editActive = false;
}
onEditToggled: {
root.editActive = true;
root.tempActive = false;
}
}
QuickToggle {
id: editToggleButton
visible: overlay.isFocused && !root.capturing
active: root.editActive
icon: "" // ""
imageSource: Qt.resolvedUrl("assets/icons/edit.svg")
iconColor: overlay.themeRef.toggleEdit
backgroundColor: overlay.themeRef.toggleBackground
shadowColor: overlay.themeRef.toggleShadow
borderColor: overlay.themeRef.barBorder
borderWidth: 1
targetX: (overlay.width - root.targetMenuWidth) / 2 - 15 - width
targetY: segmentedControl.y + segmentedControl.height / 2
sourceX: overlay.width / 2 - 204 + 32
onClicked: root.editActive = false
}
QuickToggle {
id: tempToggleButton
visible: overlay.isFocused && !root.capturing
active: root.tempActive
icon: "" // ""
imageSource: Qt.resolvedUrl("assets/icons/temp.svg")
iconColor: overlay.themeRef.toggleTemp
backgroundColor: overlay.themeRef.toggleBackground
shadowColor: overlay.themeRef.toggleShadow
borderColor: overlay.themeRef.barBorder
borderWidth: 1
targetX: (overlay.width + root.targetMenuWidth) / 2 + 15
targetY: segmentedControl.y + segmentedControl.height / 2
sourceX: overlay.width / 2 - 204 + 332
onClicked: root.tempActive = false
}
QuickToggle {
id: shareToggleButton
visible: overlay.isFocused && !root.capturing
active: root.shareActive
icon: ""
imageSource: root.connectivityStatus === 2 ? Qt.resolvedUrl("assets/icons/share-error.svg") : Qt.resolvedUrl("assets/icons/share.svg")
iconColor: {
if (root.connectivityStatus === 1)
return overlay.themeRef.shareConnected;
if (root.connectivityStatus === 2)
return overlay.themeRef.shareErrorIcon;
return overlay.themeRef.sharePending;
}
backgroundColor: root.connectivityStatus === 2 ? overlay.themeRef.shareErrorBackground : overlay.themeRef.toggleBackground
shadowColor: overlay.themeRef.toggleShadow
borderColor: overlay.themeRef.barBorder
borderWidth: 1
pulse: root.connectivityStatus === 0
targetX: (overlay.width + root.targetMenuWidth) / 2 + 15 + (root.tempActive ? 44 + 10 : 0)
targetY: segmentedControl.y + segmentedControl.height / 2
sourceX: overlay.width / 2 + (root.targetMenuWidth / 2) - 22
onClicked: root.shareActive = false
}
Item {
visible: !root.capturing
anchors.fill: parent
z: 999
HoverHandler {
onPointChanged: {
root.activeScreenName = overlay.modelData.name;
if (root.mode === "region" && !regionSelector.pressed) {
regionSelector.mouseX = point.position.x;
regionSelector.mouseY = point.position.y;
}
if (root.mode === "window" && !windowSelector.pressed) {
windowSelector.mouseX = point.position.x;
windowSelector.mouseY = point.position.y;
}
}
}
}
}
}
}