-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathshell.qml
More file actions
527 lines (451 loc) · 19 KB
/
shell.qml
File metadata and controls
527 lines (451 loc) · 19 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
/*
* 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 tempPath: ""
property string mode: "region"
property var modes: ["edit", "region", "window", "temp"]
property bool tempActive: false
property bool editActive: false
property bool shareActive: false
property int connectivityStatus: 0
property string lastSavedPath: ""
property string lastTimestamp: ""
property bool overlayVisible: false
readonly property real targetMenuWidth: (modes.length - (editActive ? 1 : 0) - (tempActive ? 1 : 0)) * 100 + 8
property var theme: themeObj
function parseTOML(text) {
let result = {
};
let section = "";
const lines = text.split(/\r?\n/);A
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;
}
}
console.log("Parsed TOML:", JSON.stringify(result));
return result;
}
function shellEscape(s) {
return "'" + s.replace(/'/g, "'\\''") + "'";
}
function calculateCrop(x, y, width, height, screenName) {
let minX = Infinity;
let minY = Infinity;
let targetMonitor = null;
const monitors = Hyprland.monitors.values;
for (const m of monitors) {
minX = Math.min(minX, m.lastIpcObject.x);
minY = Math.min(minY, m.lastIpcObject.y);
if (m.name === screenName)
targetMonitor = m;
}
if (!targetMonitor)
targetMonitor = hyprlandMonitor;
const scale = targetMonitor.scale;
const monitorX = targetMonitor.lastIpcObject.x;
const monitorY = targetMonitor.lastIpcObject.y;
const globalX = Math.round((x + monitorX) * scale);
const globalY = Math.round((y + monitorY) * scale);
return {
"cropX": globalX - Math.round(minX * scale),
"cropY": globalY - Math.round(minY * scale),
"scaledWidth": Math.round(width * scale),
"scaledHeight": Math.round(height * scale)
};
}
function cleanup() {
Quickshell.execDetached(["rm", "-f", tempPath]);
}
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 crop = calculateCrop(x, y, width, height, screenName);
const picturesBase = Quickshell.env("XDG_PICTURES_DIR") || (Quickshell.env("HOME") + "/Pictures");
const picturesDir = picturesBase + "/Screenshots";
const now = new Date();
const timestamp = Qt.formatDateTime(now, "yyyy-MM-dd_hh-mm-ss");
const outputPath = `${picturesDir}/screenshot-${timestamp}.png`;
root.lastTimestamp = timestamp;
root.lastSavedPath = root.tempActive ? "" : outputPath;
const tempSnip = Quickshell.cachePath(`snip-${timestamp}.png`);
const ePicturesDir = shellEscape(picturesDir);
const eOutputPath = shellEscape(outputPath);
const eTempPath = shellEscape(tempPath);
const eTempSnip = shellEscape(tempSnip);
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) => {
return root.shareActive ? ` && ${shareCmd} ${escapedPath}` : "";
};
const shareTag = root.shareActive ? " & phone" : "";
const mkdirCmd = `mkdir -p ${ePicturesDir}`;
const cropCmd = `magick ${eTempPath} -crop ` + `${crop.scaledWidth}x${crop.scaledHeight}` + `+${crop.cropX}+${crop.cropY} +repage`;
const sattyCommand = `${mkdirCmd} && ${cropCmd} png:- ` + `| satty --filename - ` + `--output-filename ${eOutputPath} --early-exit --init-tool brush ` + `&& wl-copy --type image/png < ${eOutputPath}` + `${maybeShare(eOutputPath)}; rm -f ${eTempPath}`;
const gradiaCommand = `${mkdirCmd} && ${cropCmd} ${eOutputPath} && hyprctl dispatch exec -- "gradia ${eOutputPath} || flatpak run be.alexandervanhee.gradia ${eOutputPath}"; sleep 0.5; rm -f ${eTempPath}`;
const defaultSaveCommand = `${mkdirCmd} && ${cropCmd} ${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}"; rm -f ${eTempPath}`;
const defaultTempCommand = `${cropCmd} ${eTempSnip} ` + `&& wl-copy --type image/png < ${eTempSnip}` + `${maybeShare(eTempSnip)} ` + `&& notify-send -a "HyprQuickFrame" "Screenshot Copied" ` + `"Copied to clipboard${shareTag}"; ` + `rm -f ${eTempPath} ${eTempSnip}`;
let cmd;
console.log("Evaluated annotationTool value:", theme.annotationTool);
if (root.editActive)
cmd = theme.annotationTool === "gradia" ? gradiaCommand : sattyCommand;
else if (root.tempActive)
cmd = defaultTempCommand;
else
cmd = defaultSaveCommand;
screenshotProcess.command = ["sh", "-c", cmd];
screenshotProcess.running = true;
root.overlayVisible = false;
}
Component.onCompleted: {
const timestamp = Date.now();
const rand = Math.floor(Math.random() * 100000);
const path = Quickshell.cachePath(`screenshot-${timestamp}-${rand}.png`);
tempPath = path;
captureProcess.command = ["grim", "-l", "0", path];
captureProcess.running = true;
connectivityProcess.running = true;
}
Process {
id: captureProcess
running: false
onExited: (code) => {
if (code === 0) {
showTimer.start();
} else {
cleanup();
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);
}
}
}
Timer {
id: showTimer
interval: 50
running: false
repeat: false
onTriggered: root.overlayVisible = true
}
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", "kdeconnect-cli -l | grep 'reachable'"]
onExited: (code) => {
root.connectivityStatus = (code === 0 ? 1 : 2);
}
}
Variants {
model: Quickshell.screens
FreezeScreen {
id: overlay
required property var modelData
property bool isFocused: root.hyprlandMonitor ? modelData.name === root.hyprlandMonitor.name : true
property var themeRef: root.theme
property var hyprMonitor: {
const monitors = Hyprland.monitors.values;
for (const m of monitors) {
if (m.name === modelData.name)
return m;
}
return null;
}
targetScreen: modelData
visible: root.overlayVisible
onVisibleChanged: {
if (visible && 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: {
root.cleanup();
Qt.quit();
}
}
Shortcut {
sequence: "r"
onActivated: root.mode = "region"
}
Shortcut {
sequence: "w"
onActivated: root.mode = "window"
}
Shortcut {
sequence: "s"
onActivated: root.saveScreenshot(0, 0, overlay.width, overlay.height, overlay.modelData.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"
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"
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
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
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
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
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 {
anchors.fill: parent
z: 999
HoverHandler {
onPointChanged: {
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;
}
}
}
}
}
}
}