summaryrefslogtreecommitdiff
path: root/quickshell/mpd/shell.qml.bak
blob: be04cf047fcf55c0cc7ddb4e7e827643e75a1493 (plain)
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
import Quickshell
import Quickshell.Io
import QtQuick
import QtQuick.Layouts
ShellRoot {

    property color colBg:     "#000000"
    property color colFg:     "#ffffff"
    property color colMuted:  "#313244"
    property color colCyan:   "#89dceb"
    property color colPurple: "#cba6f7"
    property color colRed:    "#f38ba8"
    property color colYellow: "#f9e2af"
    property color colBlue:   "#89b4fa"
    property color colGreen:  "#A3BE8C"
    property string fontFamily: "Iosevka Nerd Font Propo"
    property int fontSize: 16


    property string mpd_title:    ""
    property string mpd_artist:   ""
    property string mpd_elapsed:  "0:00"
    property string mpd_duration: "0:00"
    property string mpd_file:     ""
    property real   mpd_progress: 0.0
    property bool   isPlaying:    false
    property bool   isPaused:     false
    property bool   isActive:     isPlaying || isPaused
    property bool isSeeking: false
    property bool cardVisible: false
    property string artPath: "/tmp/mpdrop_art.png"
    property string artCache: ""

    function timeToSecs(t) {
        var p = t.split(":")
        if (p.length === 2) return parseInt(p[0]) * 60 + parseInt(p[1])
        if (p.length === 3) return parseInt(p[0]) * 3600 + parseInt(p[1]) * 60 + parseInt(p[2])
        return 0
    }

    // Art Extractor

    Process {
        id: artProc
        property  string filePath: ""
        command: ["sh", "-c", "ffmpeg -i \"" + Qt.musicFolder + filePath + "\" -an -vcodec copy /tm/mdrop_art.png -y 2>/dev/null"]
        running: false
        onExited: {
            artCache = ""
            artCache = artPath
        }
    }

    // File Path Folder
    Process {
        id: fileProc
        command: ["mpc", "--format", "%file%", "current"]
        stdout: StdioCollector {
            onStreamFinished: {
                var f = this.text.trim()
                if (f !== "" && f !== mpd_file) {
                    mpd_file = f
                    artProc.filePath = f
                    artProc.running = false
                    artProc.running = true
                }
            }
        }
    }

    // Idle Watcher
    
    Process {
        id: idleProc
        command: ["mpc", "idlewait"]
        running: true
        onExited: {
            statusProc.running = true
        }
    }

    // Status Fetcher
    Process {
        id: statusProc
        command: ["mpc", "status", "--format", "%title%||%artist||%duration%"]
        stdout: StdioCollector {
            onStreamFinished: {
                var lines = this.text.trim().split("\n")
                if (lines.length >= 2) {
                    var meta = lines[0].split("||")
                    var newTitle = meta[0] || "Unknown"

                    if (newTitle !== mpd_title) fileProc.running = true

                    mpd_title = newTitle 
                    mpd_artist = meta[1] || ""
                    mpd_duration = meta[2] || "0:00"

                    var sl = lines[1]
                    isPlaying = sl.indexOf("[playing]") !== -1
                    isPaused = sl.indexOf("[paused]") !== -1

                    var tm = sl.match(/(\d+:\d+)\/(\d+:\d+)/)
                    if (tm) {
                        mpd_elapsed = tm[1]
                        var total = timeToSecs(tm[2])
                        if (!isSeeking) {
                            mpd_progress = total > 0 ? timeToSecs(tm[1]) / total : 0
                        }


                    }
                    else {
                        mpd_title = ""; mpd_artist = ""
                        mpd_elapsed = "0:00"; mpd_duration = "0:00"
                        mpd_progress = 0; isPlaying = false; isPaused = false
                }
            }
        }
    }
    onExited: idleProc.running = true
}
    property real progressPerSecond: {
        var total = timeToSecs(mpd_duration)
        return total > 0 ? 1.0 / total : 0
    }

    Timer {
        id: progressTimer
        interval: 1000
        running: isPlaying && !isSeeking
        repeat: true
        onTriggered: {
                mpd_progress = Math.min(1.0, mpd_progress + progressPerSecond)
                var elapsed = Math.round(mpd_progress * timeToSecs(mpd_duration))
                var m = Math.floor(elapsed / 60)
                var s = elapsed % 60
                mpd_elapsed = m + ":" + (s < 10 ? "0" + s : s)
            }
        }


    Timer {
        id: seekResetTimer
        interval: 1100
        repeat: false
        onTriggered: isSeeking = false
    }

    //  Control process
    Process {
        id: ctrlProc
        property var args: ["toggle"]
        command: ["mpc"].concat(args)
        running: false
        onExited: statusProc.running = true
    }

    // Hover Card
    PanelWindow {
        visible: cardVisible && isActive
        screen: Quickshell.screens[0]
        exclusionMode: ExclusionMode.Ignore
        anchors { bottom: true; left: true }
        implicitWidth: 280
        implicitHeight: 320
        color: "transparent"

        Rectangle {
            anchors.fill: parent
            anchors.margin: 12
            radius: 12
            color: colBg
            border.color: colMuted
            border.width: 1

            opacity: cardVisible ? 1 : 0
            Behavior on opacity { NumberAnimation { duration: 200 } }

            Column {
                anchors.fill: parent
                anchors.margin: 12
                spacing: 10

                // Album Art
                Rectangle {
                    width: parent.width
                    height: parent.width
                    radius: 8
                    color: colMuted
                    clip: true


                    Image {
                        id: artImage
                        anchors.fill: parent
                        source: artCache !== "" ? "file://" + artCache : ""
                        fillMode: Image.PreserveAspectCrop
                        cache: false
                        smooth: true

                        //Placeholder for when no art
                        Text {
                            anchors.centerIn: parent
                            text: "😼"
                            font.pixelSize: 48
                            font.family: fontFamily
                            color: colMuted
                            visible: artImage.status !== Image.Ready
                        }
                    }
                }

                // Title
                Text {
                    text: mpd_title
                    color: colFg
                    font.pixelSize: 13
                    font.family: fontFamily
                    font.bold: true
                    elide: Text.ElideRight
                    width: parent.width
                }

                // Artist
                Text {
                    text: mpd_artist
                    color: colMuted
                    font.pixelSize: 12
                    font.family: fontFamily
                    elide: Text.ElideRight
                    width: parent.width
                    visible: mpd_artist !== ""
                }

                // Progress Bar
                Rectangle {
                    width: parent.width
                    height: 3
                    radius: 2
                    color: colMuted

                    Rectangle: {
                        width: parent.width * mpd_progress
                        height: parent.height
                        radius: 2
                        color: isPlaying ? colCyan : colYellow
                        Behavior on width {
                            enabled: !isSeeking
                            NumberAnimation { duration: 950; easing.type: Easing.Linear }
                        }
                    
                    }
                }

                // Time
                RowLayout {
                    width: parent.width
                    Text {
                        text: mpd_elapsed
                        color: colFg
                        font.pixelSize: 11
                        font.family: fontFamily
                    }
                    Item { Layout.fillWidth: true }
                    Text {
                        text: mpd_duration
                        color: colMuted
                        font.pixelSize: 11
                        font.family: fontFamily
                    }

                }
            }
        }
    } 

    // ── The bar ────────────────────────────────────────────────
    PanelWindow {
        id: mainBar
        visible: isActive
        screen: Quickshell.screens[0]
        exclusionMode: PanelWindow 
        anchors { bottom: true; left: true; right: true }
        implicitHeight: 36
        color: colBg

        // hover Detection
        HoverHandler {
            id: barHover
            onHoveredChanged: cardVisible = barHover.hovered
        }



        Rectangle {
            anchors.fill: parent
            color: colBg
            Rectangle {
                anchors.top: parent.top
                anchors.left: parent.left
                anchors.right: parent.right
                height: 1
                color: colMuted
            }
            RowLayout {
                anchors.fill: parent
                anchors.leftMargin: 8
                anchors.rightMargin: 8
                spacing: 0
                // ── Music icon ─────────────────────────────────
                Text {
                    text: isPaused ? "󰎊" : "󰎈"
                    color: isPaused ? colYellow : colCyan
                    font.pixelSize: fontSize
                    font.family: fontFamily
                    font.bold: true
                    Behavior on color { ColorAnimation { duration: 200 } }
                }
                Item { width: 8 }
                // ── Title ──────────────────────────────────────
                Text {
                    text: mpd_title || "Unknown"
                    color: colPurple
                    font.pixelSize: fontSize
                    font.family: fontFamily
                    font.bold: true
                    elide: Text.ElideRight
                    Layout.maximumWidth: 220
                }
                // ── Separator + Artist ─────────────────────────
                Rectangle {
                    Layout.preferredWidth: 1; Layout.preferredHeight: 16
                    Layout.leftMargin: 8; Layout.rightMargin: 8
                    color: colMuted
                    visible: mpd_artist !== ""
                }
                Text {
                    text: mpd_artist
                    color: colCyan
                    font.pixelSize: fontSize
                    font.family: fontFamily
                    font.bold: true
                    elide: Text.ElideRight
                    Layout.maximumWidth: 180
                    visible: mpd_artist !== ""
                }
                // ── Separator ──────────────────────────────────
                Rectangle {
                    Layout.preferredWidth: 1; Layout.preferredHeight: 16
                    Layout.leftMargin: 8; Layout.rightMargin: 8
                    color: colMuted
                }
                // ── Prev ───────────────────────────────────────
                Text {
                    text: "󰒮"
                    color: prevH.containsMouse ? colCyan : colFg
                    font.pixelSize: fontSize
                    font.family: fontFamily
                    font.bold: true
                    Layout.rightMargin: 10
                    HoverHandler { id: prevH }
                    TapHandler { onTapped: { if (!ctrlProc.running) { ctrlProc.args = ["prev"]; ctrlProc.running = true } } }
                    Behavior on color { ColorAnimation { duration: 100 } }
                }
                // ── Play / Pause ───────────────────────────────
                Text {
                    text: isPlaying ? "󰏤" : "󰐊"
                    color: playH.containsMouse ? colPurple : colCyan
                    font.pixelSize: fontSize + 2
                    font.family: fontFamily
                    font.bold: true
                    Layout.rightMargin: 10
                    HoverHandler { id: playH }
                    TapHandler { onTapped: { if (!ctrlProc.running) { ctrlProc.args = ["toggle"]; ctrlProc.running = true } } }
                    Behavior on color { ColorAnimation { duration: 100 } }
                }
                // ── Next ───────────────────────────────────────
                Text {
                    text: "󰒭"
                    color: nextH.containsMouse ? colCyan : colFg
                    font.pixelSize: fontSize
                    font.family: fontFamily
                    font.bold: true
                    Layout.rightMargin: 10
                    HoverHandler { id: nextH }
                    TapHandler { onTapped: { if (!ctrlProc.running) { ctrlProc.args = ["next"]; ctrlProc.running = true } } }
                    Behavior on color { ColorAnimation { duration: 100 } }
                }
                // ── Stop ───────────────────────────────────────
                Text {
                    text: "󰓛"
                    color: stopH.containsMouse ? colRed : colMuted
                    font.pixelSize: fontSize
                    font.family: fontFamily
                    font.bold: true
                    HoverHandler { id: stopH }
                    TapHandler { onTapped: { if (!ctrlProc.running) { ctrlProc.args = ["stop"]; ctrlProc.running = true } } }
                    Behavior on color { ColorAnimation { duration: 100 } }
                }
                // ── Separator ──────────────────────────────────
                Rectangle {
                    Layout.preferredWidth: 1; Layout.preferredHeight: 16
                    Layout.leftMargin: 8; Layout.rightMargin: 8
                    color: colMuted
                }
                // ── Elapsed ────────────────────────────────────
                Text {
                    text: mpd_elapsed
                    color: colFg
                    font.pixelSize: fontSize
                    font.family: fontFamily
                    font.bold: true
                    Layout.rightMargin: 8
                }
                // ── Progress bar ───────────────────────────────
                Rectangle {
                    id: progressTrack
                    Layout.fillWidth: true
                    height: 4
                    radius: 2
                    color: colMuted

                    // Fill
                    Rectangle {
                        id: progressFill
                        width: progressTrack.width * mpd_progress
                        height: parent.height
                        radius: 2
                        color: isPlaying ? colCyan : colYellow
                        Behavior on width {
                            enabled: !isSeeking
                            NumberAnimation { duration: 950; easing.type: Easing.Linear }
                        }
                        Behavior on color { ColorAnimation { duration: 300 } }
                    }

                    // Seek — child of progressTrack so parent.width works
                    MouseArea {
                        anchors.fill: parent
                        anchors.topMargin: -6
                        anchors.bottomMargin: -6
                        cursorShape: Qt.PointingHandCursor
                        onClicked: (mouse) => {
                            if (!ctrlProc.running) {
                            var pct = Math.max(0, Math.min(1, mouse.x / progressTrack.width))
                            var pctInt = Math.round(pct * 100)
                            isSeeking = true
                            mpd_progress = pct
                            ctrlProc.args = ["seek", pctInt + "%"]
                            ctrlProc.running = true
                            seekResetTimer.restart()
                        }
                        }
                    }
                }
                // ── Duration ───────────────────────────────────
                Text {
                    text: mpd_duration
                    color: colMuted
                    font.pixelSize: fontSize
                    font.family: fontFamily
                    font.bold: true
                    Layout.leftMargin: 8
                }
                Item { width: 8 }
            }
        }
    }
}