-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntervalCommandSettings.qml
More file actions
562 lines (495 loc) · 19.3 KB
/
IntervalCommandSettings.qml
File metadata and controls
562 lines (495 loc) · 19.3 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
import QtQuick
import qs.Common
import qs.Services
import qs.Widgets
import qs.Modules.Plugins
import "icons.js" as Icons
PluginSettings {
id: root
pluginId: "intervalCommand"
property var iconNames: Icons.names
// Track selected widget index in the variants array
property int selectedIndex: 0
// Get the currently selected variant data (null if none)
property var selectedVariant: variants.length > 0 && selectedIndex >= 0 && selectedIndex < variants.length
? variants[selectedIndex] : null
// Re-sync all form fields when switching widgets (bindings break after user edits)
property bool _syncing: false
onSelectedVariantChanged: {
if (!selectedVariant) return;
_syncing = true;
nameField.text = selectedVariant.name ?? "";
commandField.text = selectedVariant.command ?? "";
iconDropdownLoader.active = false;
iconDropdownLoader.active = true;
accentToggle.checked = selectedVariant.useAccentColor ?? false;
clickCommandField.text = selectedVariant.clickCommand ?? "";
refreshSlider.value = selectedVariant.refreshInterval ?? 10;
popoutToggle.checked = selectedVariant.popoutEnabled ?? false;
popoutRefreshSlider.value = selectedVariant.popoutRefreshInterval ?? 5;
popoutWidthSlider.value = selectedVariant.popoutWidth ?? 600;
popoutHeightSlider.value = selectedVariant.popoutHeight ?? 450;
_syncing = false;
}
// Re-sync selection when variants change, and auto-migrate if needed
property bool migrationDone: false
onVariantsChanged: {
// Auto-migrate base widget to variant on first load
if (!migrationDone && variants.length === 0 && pluginService) {
let baseCommand = loadValue("command", "");
if (baseCommand) {
migrationDone = true;
migrateBaseToVariant();
return; // migrateBaseToVariant creates a variant, which re-triggers onVariantsChanged
}
}
// Clamp selection to valid range
if (selectedIndex >= variants.length) {
selectedIndex = Math.max(0, variants.length - 1);
}
}
// Auto-incrementing counter for default widget names
property int widgetCounter: 0
function nextWidgetName() {
widgetCounter++;
return "Widget " + widgetCounter;
}
function migrateBaseToVariant() {
let config = {
command: loadValue("command", ""),
icon: loadValue("icon", "info"),
refreshInterval: loadValue("refreshInterval", 10),
clickCommand: loadValue("clickCommand", ""),
popoutEnabled: loadValue("popoutEnabled", false),
popoutRefreshInterval: loadValue("popoutRefreshInterval", 5),
popoutWidth: loadValue("popoutWidth", 600),
popoutHeight: loadValue("popoutHeight", 450)
};
let variantId = createVariant("Widget", config);
if (!variantId) return;
// Replace base widget ID in all bar configs
let fullId = "intervalCommand:" + variantId;
let configs = JSON.parse(JSON.stringify(SettingsData.barConfigs));
let changed = false;
for (let i = 0; i < configs.length; i++) {
let sections = ["leftWidgets", "centerWidgets", "rightWidgets"];
for (let s = 0; s < sections.length; s++) {
let widgets = configs[i][sections[s]];
if (!widgets) continue;
for (let w = 0; w < widgets.length; w++) {
let wid = typeof widgets[w] === "string" ? widgets[w] : widgets[w].id;
if (wid === "intervalCommand") {
if (typeof widgets[w] === "string") {
widgets[w] = fullId;
} else {
widgets[w].id = fullId;
}
changed = true;
}
}
}
}
if (changed) {
SettingsData.barConfigs = configs;
SettingsData.updateBarConfigs();
}
selectedIndex = 0;
}
// Save a single field for a specific variant (or selected if no id given)
function saveFieldFor(variantId, key, value) {
if (!variantId) return;
// Strip newlines from text fields (can be pasted in but break shell commands)
if (typeof value === "string")
value = value.replace(/[\r\n]+/g, " ").trim();
let config = {};
config[key] = value;
updateVariant(variantId, config);
}
function saveField(key, value) {
if (!selectedVariant) return;
saveFieldFor(selectedVariant.id, key, value);
}
// Debounced save for text fields — saves after 500ms of no typing
// Tracks the variant ID at the time typing started so switching widgets
// doesn't save to the wrong variant
property string _pendingKey: ""
property string _pendingValue: ""
property string _pendingVariantId: ""
Timer {
id: saveDebounce
interval: 500
onTriggered: {
if (root._pendingKey !== "" && root._pendingVariantId !== "")
root.saveFieldFor(root._pendingVariantId, root._pendingKey, root._pendingValue);
}
}
function debounceSave(key, value, field) {
if (_syncing) return;
if (field && !field.getActiveFocus()) return;
_pendingKey = key;
_pendingValue = value;
_pendingVariantId = selectedVariant ? selectedVariant.id : "";
saveDebounce.restart();
}
// Flush any pending debounced save immediately
function flushPendingSave() {
if (saveDebounce.running) {
saveDebounce.stop();
if (_pendingKey !== "" && _pendingVariantId !== "")
saveFieldFor(_pendingVariantId, _pendingKey, _pendingValue);
_pendingKey = "";
_pendingVariantId = "";
}
}
// ── Title & Description ──
StyledText {
width: parent.width
text: "Interval Command"
font.pixelSize: Appearance.fontSize.large
font.weight: Font.Bold
color: Theme.surfaceText
}
// ── State 1: No widgets — show "Add Widget" button ──
Item { width: 1; height: Appearance.spacing.small; visible: root.variants.length === 0 }
DankButton {
visible: root.variants.length === 0
text: "Add Widget"
iconName: "add"
buttonHeight: 28
horizontalPadding: Theme.spacingS
iconSize: 16
onClicked: {
createVariant(root.nextWidgetName(), {});
loadVariants();
root.selectedIndex = 0;
}
}
// ── State 2: One or more widgets exist ──
Column {
id: widgetListSection
visible: root.variants.length > 0
width: parent.width
spacing: Theme.spacingXS
StyledText {
text: "Widgets"
font.pixelSize: Appearance.fontSize.normal
font.weight: Font.Medium
color: Theme.surfaceText
}
Column {
id: widgetList
width: parent.width
spacing: Theme.spacingXS
Repeater {
model: root.variantsModel
delegate: Rectangle {
id: widgetRow
required property int index
required property var model
width: widgetList.width
height: rowContent.implicitHeight + Theme.spacingM * 2
radius: Theme.cornerRadius
color: "transparent"
border.width: 1
border.color: index === root.selectedIndex
? Theme.primary
: rowMouseArea.containsMouse
? Theme.outline
: Theme.outlineMedium
Row {
id: rowContent
anchors.fill: parent
anchors.margins: Theme.spacingM
spacing: Theme.spacingM
DankIcon {
name: widgetRow.model.icon || "info"
size: Theme.iconSizeSmall
color: Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter
}
StyledText {
text: widgetRow.model.name || "Unnamed"
font.pixelSize: Appearance.fontSize.small
color: Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter
elide: Text.ElideRight
width: rowContent.width - rowContent.spacing * 2
- deleteBtn.width - Theme.iconSizeSmall
}
Rectangle {
id: deleteBtn
width: 28
height: 28
radius: 14
color: deleteArea.containsMouse ? Theme.errorHover : "transparent"
anchors.verticalCenter: parent.verticalCenter
DankIcon {
anchors.centerIn: parent
name: "delete"
size: 16
color: deleteArea.containsMouse
? Theme.error : Theme.surfaceVariantText
}
MouseArea {
id: deleteArea
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
removeVariant(widgetRow.model.id);
}
}
}
}
MouseArea {
id: rowMouseArea
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
root.flushPendingSave();
root.selectedIndex = widgetRow.index;
}
z: -1
}
}
}
}
Item { width: 1; height: Appearance.spacing.small }
DankButton {
text: "Add Another Widget"
iconName: "add"
buttonHeight: 28
horizontalPadding: Theme.spacingS
iconSize: 16
onClicked: {
createVariant(root.nextWidgetName(), {});
loadVariants();
root.selectedIndex = root.variants.length - 1;
}
}
}
// ── Settings form for selected widget ──
Column {
id: settingsForm
visible: root.selectedVariant !== null
width: parent.width
spacing: Theme.spacingM
Column {
width: parent.width
spacing: Theme.spacingXS
StyledText {
text: "Name"
font.pixelSize: Appearance.fontSize.normal
color: Theme.surfaceText
}
DankTextField {
id: nameField
width: parent.width
text: root.selectedVariant?.name ?? ""
placeholderText: "Widget name"
onTextEdited: root.debounceSave("name", text, nameField)
onEditingFinished: root.flushPendingSave()
}
}
Column {
width: parent.width
spacing: Theme.spacingXS
StyledText {
text: "Command"
font.pixelSize: Appearance.fontSize.normal
color: Theme.surfaceText
}
StyledText {
text: "Shell command to run (e.g. ~/.config/DankMaterialShell/plugins/intervalCommand/uptime-compact.sh)"
font.pixelSize: Appearance.fontSize.small
color: Theme.surfaceVariantText
wrapMode: Text.WordWrap
width: parent.width
}
DankTextField {
id: commandField
width: parent.width
text: root.selectedVariant?.command ?? ""
placeholderText: "echo 'hello'"
onTextEdited: root.debounceSave("command", text, commandField)
onEditingFinished: root.flushPendingSave()
}
}
// Icon dropdown with fuzzy search.
// resetSearch() was added upstream to fix stale search text on reopen.
// Fall back to Loader destroy/recreate for older DMS versions without it.
Loader {
id: iconDropdownLoader
width: parent.width
active: true
sourceComponent: DankDropdown {
id: iconDropdown
text: "Icon"
description: "Material Design icon name"
enableFuzzySearch: true
dropdownWidth: 280
options: root.iconNames
optionIcons: root.iconNames
currentValue: root.selectedVariant?.icon ?? "info"
onValueChanged: value => {
if (root.selectedVariant) root.selectedVariant.icon = value;
root.saveField("icon", value);
if (typeof iconDropdown.resetSearch === "function") {
iconDropdown.resetSearch();
} else {
iconReloadTimer.restart();
}
}
}
}
// Fallback for DMS versions without DankDropdown.resetSearch()
Timer {
id: iconReloadTimer
interval: 100
onTriggered: {
iconDropdownLoader.active = false;
iconDropdownLoader.active = true;
}
}
DankToggle {
id: accentToggle
width: parent.width
text: "Use Accent Color for Icon"
description: "Colors the widget icon with your theme's accent color"
checked: root.selectedVariant?.useAccentColor ?? false
onToggled: isChecked => {
checked = isChecked;
root.saveField("useAccentColor", isChecked);
}
}
Column {
width: parent.width
spacing: Theme.spacingXS
StyledText {
text: "Refresh Interval"
font.pixelSize: Appearance.fontSize.normal
color: Theme.surfaceText
}
StyledText {
text: "How often to run the command (in seconds)"
font.pixelSize: Appearance.fontSize.small
color: Theme.surfaceVariantText
wrapMode: Text.WordWrap
width: parent.width
}
DankSlider {
id: refreshSlider
width: parent.width
value: root.selectedVariant?.refreshInterval ?? 10
minimum: 1
maximum: 300
unit: "s"
leftIcon: "schedule"
onSliderDragFinished: finalValue => root.saveField("refreshInterval", finalValue)
}
}
Column {
width: parent.width
spacing: Theme.spacingXS
StyledText {
text: "Click Command"
font.pixelSize: Appearance.fontSize.normal
color: Theme.surfaceText
}
StyledText {
text: "Command to run when the widget is clicked (leave empty for no action)"
font.pixelSize: Appearance.fontSize.small
color: Theme.surfaceVariantText
wrapMode: Text.WordWrap
width: parent.width
}
DankTextField {
id: clickCommandField
width: parent.width
text: root.selectedVariant?.clickCommand ?? ""
placeholderText: "notify-send 'hello'"
onTextEdited: root.debounceSave("clickCommand", text, clickCommandField)
onEditingFinished: root.flushPendingSave()
}
}
DankToggle {
id: popoutToggle
width: parent.width
text: "Show Click Output in Popout"
description: "Clicking the widget opens a panel showing the full click command output"
checked: root.selectedVariant?.popoutEnabled ?? false
onToggled: isChecked => {
checked = isChecked;
root.saveField("popoutEnabled", isChecked);
}
}
Column {
visible: popoutToggle.checked
width: parent.width
spacing: Theme.spacingXS
StyledText {
text: "Popout Refresh Interval"
font.pixelSize: Appearance.fontSize.normal
color: Theme.surfaceText
}
StyledText {
text: "How often to refresh the click command output in the popout (in seconds)"
font.pixelSize: Appearance.fontSize.small
color: Theme.surfaceVariantText
wrapMode: Text.WordWrap
width: parent.width
}
DankSlider {
id: popoutRefreshSlider
width: parent.width
value: root.selectedVariant?.popoutRefreshInterval ?? 5
minimum: 1
maximum: 300
unit: "s"
leftIcon: "refresh"
onSliderDragFinished: finalValue => root.saveField("popoutRefreshInterval", finalValue)
}
}
Column {
visible: popoutToggle.checked
width: parent.width
spacing: Theme.spacingXS
StyledText {
text: "Popout Width"
font.pixelSize: Appearance.fontSize.normal
color: Theme.surfaceText
}
DankSlider {
id: popoutWidthSlider
width: parent.width
value: root.selectedVariant?.popoutWidth ?? 600
minimum: 200
maximum: 1920
unit: "px"
leftIcon: "width"
onSliderDragFinished: finalValue => root.saveField("popoutWidth", finalValue)
}
}
Column {
visible: popoutToggle.checked
width: parent.width
spacing: Theme.spacingXS
StyledText {
text: "Popout Max Height"
font.pixelSize: Appearance.fontSize.normal
color: Theme.surfaceText
}
DankSlider {
id: popoutHeightSlider
width: parent.width
value: root.selectedVariant?.popoutHeight ?? 450
minimum: 100
maximum: 1080
unit: "px"
leftIcon: "height"
onSliderDragFinished: finalValue => root.saveField("popoutHeight", finalValue)
}
}
}
}