summaryrefslogtreecommitdiff
path: root/raveos-hyprland-theme/theme-data/DankMaterialShell/quickshell/Modules/DankBar/Popouts/BatteryPopout.qml
blob: f0cb9d846d33cb6009bc3edac9ef976342d6a5af (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
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
import QtQuick
import Quickshell
import Quickshell.Services.UPower
import qs.Common
import qs.Services
import qs.Widgets

DankPopout {
    id: root

    layerNamespace: "dms:battery"

    property var triggerScreen: null

    function isActiveProfile(profile) {
        if (typeof PowerProfiles === "undefined") {
            return false;
        }

        return PowerProfiles.profile === profile;
    }

    function setProfile(profile) {
        if (typeof PowerProfiles === "undefined") {
            ToastService.showError(I18n.tr("power-profiles-daemon not available"));
            return;
        }
        PowerProfiles.profile = profile;
        if (PowerProfiles.profile !== profile) {
            ToastService.showError(I18n.tr("Failed to set power profile"));
        }
    }

    popupWidth: 400
    popupHeight: contentLoader.item ? contentLoader.item.implicitHeight : 0
    triggerWidth: 70
    positioning: ""
    screen: triggerScreen

    onBackgroundClicked: close()

    content: Component {
        Rectangle {
            id: batteryContent

            LayoutMirroring.enabled: I18n.isRtl
            LayoutMirroring.childrenInherit: true

            implicitHeight: contentColumn.implicitHeight + Theme.spacingL * 2
            color: "transparent"
            focus: true
            Component.onCompleted: {
                if (root.shouldBeVisible) {
                    forceActiveFocus();
                }
            }
            Keys.onPressed: function (event) {
                if (event.key === Qt.Key_Escape) {
                    root.close();
                    event.accepted = true;
                }
            }

            Connections {
                function onShouldBeVisibleChanged() {
                    if (root.shouldBeVisible) {
                        Qt.callLater(function () {
                            batteryContent.forceActiveFocus();
                        });
                    }
                }

                target: root
            }

            Column {
                id: contentColumn

                width: parent.width - Theme.spacingL * 2
                anchors.left: parent.left
                anchors.top: parent.top
                anchors.margins: Theme.spacingL
                spacing: Theme.spacingL

                Row {
                    width: parent.width
                    height: 48
                    spacing: Theme.spacingM

                    DankIcon {
                        name: {
                            if (!BatteryService.batteryAvailable)
                                return "power";

                            if (!BatteryService.isCharging && BatteryService.isPluggedIn) {
                                if (BatteryService.batteryLevel >= 90) {
                                    return "battery_charging_full";
                                }
                                if (BatteryService.batteryLevel >= 80) {
                                    return "battery_charging_90";
                                }
                                if (BatteryService.batteryLevel >= 60) {
                                    return "battery_charging_80";
                                }
                                if (BatteryService.batteryLevel >= 50) {
                                    return "battery_charging_60";
                                }
                                if (BatteryService.batteryLevel >= 30) {
                                    return "battery_charging_50";
                                }
                                if (BatteryService.batteryLevel >= 20) {
                                    return "battery_charging_30";
                                }
                                return "battery_charging_20";
                            }
                            if (BatteryService.isCharging) {
                                if (BatteryService.batteryLevel >= 90) {
                                    return "battery_charging_full";
                                }
                                if (BatteryService.batteryLevel >= 80) {
                                    return "battery_charging_90";
                                }
                                if (BatteryService.batteryLevel >= 60) {
                                    return "battery_charging_80";
                                }
                                if (BatteryService.batteryLevel >= 50) {
                                    return "battery_charging_60";
                                }
                                if (BatteryService.batteryLevel >= 30) {
                                    return "battery_charging_50";
                                }
                                if (BatteryService.batteryLevel >= 20) {
                                    return "battery_charging_30";
                                }
                                return "battery_charging_20";
                            } else {
                                if (BatteryService.batteryLevel >= 95) {
                                    return "battery_full";
                                }
                                if (BatteryService.batteryLevel >= 85) {
                                    return "battery_6_bar";
                                }
                                if (BatteryService.batteryLevel >= 70) {
                                    return "battery_5_bar";
                                }
                                if (BatteryService.batteryLevel >= 55) {
                                    return "battery_4_bar";
                                }
                                if (BatteryService.batteryLevel >= 40) {
                                    return "battery_3_bar";
                                }
                                if (BatteryService.batteryLevel >= 25) {
                                    return "battery_2_bar";
                                }
                                return "battery_1_bar";
                            }
                        }
                        size: Theme.iconSizeLarge
                        color: {
                            if (BatteryService.isLowBattery && !BatteryService.isCharging)
                                return Theme.error;
                            if (BatteryService.isCharging || BatteryService.isPluggedIn)
                                return Theme.primary;
                            return Theme.surfaceText;
                        }
                        anchors.verticalCenter: parent.verticalCenter
                    }

                    Column {
                        id: headerInfoColumn
                        spacing: Theme.spacingXS
                        anchors.verticalCenter: parent.verticalCenter
                        width: parent.width - Theme.iconSizeLarge - 32 - Theme.spacingM * 2
                        readonly property string timeInfoText: {
                            if (!BatteryService.batteryAvailable)
                                return I18n.tr("Power profile management available");
                            const time = BatteryService.formatTimeRemaining();
                            if (time !== "Unknown") {
                                return BatteryService.isCharging ? I18n.tr("Time until full: %1").arg(time) : I18n.tr("Time remaining: %1").arg(time);
                            }
                            return "";
                        }
                        readonly property bool showPowerRate: BatteryService.batteryAvailable && Math.abs(BatteryService.changeRate) > 0.05
                        readonly property bool isOnAC: BatteryService.batteryAvailable && (BatteryService.isCharging || BatteryService.isPluggedIn)
                        readonly property bool isDischarging: BatteryService.batteryAvailable && !BatteryService.isCharging && !BatteryService.isPluggedIn

                        Row {
                            spacing: Theme.spacingS

                            StyledText {
                                text: BatteryService.batteryAvailable ? `${BatteryService.batteryLevel}%` : I18n.tr("Power")
                                font.pixelSize: Theme.fontSizeXLarge
                                color: {
                                    if (BatteryService.isLowBattery && !BatteryService.isCharging) {
                                        return Theme.error;
                                    }
                                    if (BatteryService.isCharging) {
                                        return Theme.primary;
                                    }
                                    return Theme.surfaceText;
                                }
                                font.weight: Font.Bold
                            }

                            StyledText {
                                text: BatteryService.batteryStatus
                                font.pixelSize: Theme.fontSizeLarge
                                color: {
                                    if (BatteryService.isLowBattery && !BatteryService.isCharging) {
                                        return Theme.error;
                                    }
                                    if (BatteryService.isCharging) {
                                        return Theme.primary;
                                    }
                                    return Theme.surfaceText;
                                }
                                font.weight: Font.Medium
                                visible: BatteryService.batteryAvailable
                                anchors.verticalCenter: parent.verticalCenter
                            }
                        }

                        Row {
                            width: parent.width
                            spacing: Theme.spacingS
                            visible: headerInfoColumn.timeInfoText.length > 0

                            StyledText {
                                id: powerRateText
                                text: `${headerInfoColumn.isOnAC ? "+" : (headerInfoColumn.isDischarging ? "-" : "")}${Math.abs(BatteryService.changeRate).toFixed(1)}W`
                                font.pixelSize: Theme.fontSizeSmall
                                color: {
                                    if (headerInfoColumn.isOnAC) {
                                        return Theme.primary;
                                    }
                                    if (headerInfoColumn.isDischarging) {
                                        return Theme.warning;
                                    }
                                    return Theme.surfaceTextMedium;
                                }
                                font.weight: Font.Medium
                                visible: headerInfoColumn.showPowerRate
                            }

                            StyledText {
                                text: headerInfoColumn.timeInfoText
                                font.pixelSize: Theme.fontSizeSmall
                                color: Theme.surfaceTextMedium
                                elide: Text.ElideRight
                                width: parent.width - (powerRateText.visible ? (powerRateText.implicitWidth + parent.spacing) : 0)
                            }
                        }
                    }

                    Rectangle {
                        width: 32
                        height: 32
                        radius: 16
                        color: closeBatteryArea.containsMouse ? Theme.errorHover : "transparent"
                        anchors.top: parent.top

                        DankIcon {
                            anchors.centerIn: parent
                            name: "close"
                            size: Theme.iconSize - 4
                            color: closeBatteryArea.containsMouse ? Theme.error : Theme.surfaceText
                        }

                        MouseArea {
                            id: closeBatteryArea
                            anchors.fill: parent
                            hoverEnabled: true
                            cursorShape: Qt.PointingHandCursor
                            onPressed: {
                                root.close();
                            }
                        }
                    }
                }

                Row {
                    width: parent.width
                    spacing: Theme.spacingM
                    visible: BatteryService.batteryAvailable

                    StyledRect {
                        width: (parent.width - Theme.spacingM) / 2
                        height: 64
                        radius: Theme.cornerRadius
                        color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
                        border.width: 0

                        Column {
                            anchors.centerIn: parent
                            spacing: Theme.spacingXS

                            StyledText {
                                text: I18n.tr("Health")
                                font.pixelSize: Theme.fontSizeSmall
                                color: Theme.primary
                                font.weight: Font.Medium
                                anchors.horizontalCenter: parent.horizontalCenter
                            }

                            StyledText {
                                text: BatteryService.batteryHealth
                                font.pixelSize: Theme.fontSizeLarge
                                color: {
                                    if (BatteryService.batteryHealth === "N/A") {
                                        return Theme.surfaceText;
                                    }
                                    const healthNum = parseInt(BatteryService.batteryHealth);
                                    return healthNum < 80 ? Theme.error : Theme.surfaceText;
                                }
                                font.weight: Font.Bold
                                anchors.horizontalCenter: parent.horizontalCenter
                            }
                        }
                    }

                    StyledRect {
                        width: (parent.width - Theme.spacingM) / 2
                        height: 64
                        radius: Theme.cornerRadius
                        color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
                        border.width: 0

                        Column {
                            anchors.centerIn: parent
                            spacing: Theme.spacingXS

                            StyledText {
                                text: I18n.tr("Capacity")
                                font.pixelSize: Theme.fontSizeSmall
                                color: Theme.primary
                                font.weight: Font.Medium
                                anchors.horizontalCenter: parent.horizontalCenter
                            }

                            StyledText {
                                text: BatteryService.batteryCapacity > 0 ? `${BatteryService.batteryCapacity.toFixed(1)} Wh` : I18n.tr("Unknown")
                                font.pixelSize: Theme.fontSizeLarge
                                color: Theme.surfaceText
                                font.weight: Font.Bold
                                anchors.horizontalCenter: parent.horizontalCenter
                            }
                        }
                    }
                }

                // Individual battery details for multiple batteries
                Column {
                    width: parent.width
                    spacing: Theme.spacingS
                    visible: !BatteryService.usePreferred && BatteryService.batteries.length > 1

                    StyledText {
                        text: I18n.tr("Individual Batteries")
                        font.pixelSize: Theme.fontSizeSmall
                        color: Theme.surfaceTextMedium
                        font.weight: Font.Medium
                    }

                    Repeater {
                        model: ScriptModel {
                            values: BatteryService.batteries
                        }

                        delegate: StyledRect {
                            required property var modelData
                            required property int index

                            width: parent.width
                            height: batteryColumn.implicitHeight + Theme.spacingM * 2
                            radius: Theme.cornerRadius
                            color: Theme.withAlpha(Theme.surfaceContainer, Theme.popupTransparency)
                            border.width: 0

                            Column {
                                id: batteryColumn
                                anchors.fill: parent
                                anchors.margins: Theme.spacingM
                                spacing: Theme.spacingS

                                // Top row: name and percentage
                                Row {
                                    width: parent.width
                                    spacing: Theme.spacingM

                                    Column {
                                        spacing: Theme.spacingXS
                                        anchors.verticalCenter: parent.verticalCenter
                                        width: parent.width - percentText.width - chargingIcon.width - Theme.spacingM * 2

                                        StyledText {
                                            text: modelData.model || I18n.tr("Battery %1").arg(index + 1)
                                            font.pixelSize: Theme.fontSizeSmall
                                            color: Theme.surfaceText
                                            font.weight: Font.Medium
                                            elide: Text.ElideRight
                                            width: parent.width
                                        }

                                        StyledText {
                                            text: modelData.nativePath
                                            font.pixelSize: Theme.fontSizeSmall
                                            color: Theme.surfaceTextMedium
                                            elide: Text.ElideMiddle
                                            width: parent.width
                                        }
                                    }

                                    Item {
                                        width: 1
                                        height: parent.height
                                    }

                                    StyledText {
                                        id: percentText
                                        text: `${Math.round(100 * modelData.percentage)}%`
                                        font.pixelSize: Theme.fontSizeMedium
                                        color: Theme.surfaceText
                                        font.weight: Font.Bold
                                        anchors.verticalCenter: parent.verticalCenter
                                    }

                                    DankIcon {
                                        id: chargingIcon
                                        name: modelData.state === UPowerDeviceState.Charging ? "bolt" : ""
                                        size: Theme.iconSizeSmall
                                        color: Theme.primary
                                        anchors.verticalCenter: parent.verticalCenter
                                        visible: modelData.state === UPowerDeviceState.Charging
                                    }
                                }

                                // Bottom row: Health, Capacity and Time
                                Flow {
                                    width: parent.width
                                    spacing: Theme.spacingS

                                    StyledRect {
                                        width: (parent.width - Theme.spacingS * 2) / 3
                                        height: 48
                                        radius: Theme.cornerRadius
                                        color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
                                        border.width: 0

                                        Column {
                                            anchors.centerIn: parent
                                            spacing: 2

                                            StyledText {
                                                text: I18n.tr("Health")
                                                font.pixelSize: Theme.fontSizeSmall
                                                color: Theme.surfaceTextMedium
                                                font.weight: Font.Medium
                                                anchors.horizontalCenter: parent.horizontalCenter
                                            }

                                            StyledText {
                                                text: {
                                                    if (!modelData.healthSupported || modelData.healthPercentage <= 0)
                                                        return "N/A";
                                                    return `${Math.round(modelData.healthPercentage)}%`;
                                                }
                                                font.pixelSize: Theme.fontSizeSmall
                                                color: {
                                                    if (!modelData.healthSupported || modelData.healthPercentage <= 0)
                                                        return Theme.surfaceText;
                                                    return modelData.healthPercentage < 80 ? Theme.error : Theme.surfaceText;
                                                }
                                                font.weight: Font.Bold
                                                anchors.horizontalCenter: parent.horizontalCenter
                                            }
                                        }
                                    }

                                    StyledRect {
                                        width: (parent.width - Theme.spacingS * 2) / 3
                                        height: 48
                                        radius: Theme.cornerRadius
                                        color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
                                        border.width: 0

                                        Column {
                                            anchors.centerIn: parent
                                            spacing: 2

                                            StyledText {
                                                text: I18n.tr("Capacity")
                                                font.pixelSize: Theme.fontSizeSmall
                                                color: Theme.surfaceTextMedium
                                                font.weight: Font.Medium
                                                anchors.horizontalCenter: parent.horizontalCenter
                                            }

                                            StyledText {
                                                text: modelData.energyCapacity > 0 ? `${modelData.energyCapacity.toFixed(1)}` : "N/A"
                                                font.pixelSize: Theme.fontSizeSmall
                                                color: Theme.surfaceText
                                                font.weight: Font.Bold
                                                anchors.horizontalCenter: parent.horizontalCenter
                                            }
                                        }
                                    }

                                    StyledRect {
                                        width: (parent.width - Theme.spacingS * 2) / 3
                                        height: 48
                                        radius: Theme.cornerRadius
                                        color: Theme.withAlpha(Theme.surfaceContainerHigh, Theme.popupTransparency)
                                        border.width: 0

                                        Column {
                                            anchors.centerIn: parent
                                            spacing: 2

                                            StyledText {
                                                text: modelData.state === UPowerDeviceState.Charging ? I18n.tr("To Full") : modelData.state === UPowerDeviceState.Discharging ? I18n.tr("Left") : ""
                                                font.pixelSize: Theme.fontSizeSmall
                                                color: Theme.surfaceTextMedium
                                                font.weight: Font.Medium
                                                anchors.horizontalCenter: parent.horizontalCenter
                                            }

                                            StyledText {
                                                text: {
                                                    const time = modelData.state === UPowerDeviceState.Charging ? modelData.timeToFull : modelData.state === UPowerDeviceState.Discharging && BatteryService.changeRate > 0 ? (3600 * modelData.energy) / BatteryService.changeRate : 0;

                                                    if (!time || time <= 0 || time > 86400)
                                                        return "N/A";

                                                    const hours = Math.floor(time / 3600);
                                                    const minutes = Math.floor((time % 3600) / 60);
                                                    return hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
                                                }
                                                font.pixelSize: Theme.fontSizeSmall
                                                color: Theme.surfaceText
                                                font.weight: Font.Bold
                                                anchors.horizontalCenter: parent.horizontalCenter
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                Item {
                    width: parent.width
                    height: profileButtonGroup.height * profileButtonGroup.scale

                    DankButtonGroup {
                        id: profileButtonGroup

                        property var profileModel: (typeof PowerProfiles !== "undefined") ? [PowerProfile.PowerSaver, PowerProfile.Balanced].concat(PowerProfiles.hasPerformanceProfile ? [PowerProfile.Performance] : []) : [PowerProfile.PowerSaver, PowerProfile.Balanced, PowerProfile.Performance]
                        property int currentProfileIndex: {
                            if (typeof PowerProfiles === "undefined")
                                return 1;
                            return profileModel.findIndex(profile => root.isActiveProfile(profile));
                        }

                        scale: Math.min(1, parent.width / implicitWidth)
                        transformOrigin: Item.Center
                        anchors.horizontalCenter: parent.horizontalCenter
                        model: profileModel.map(profile => Theme.getPowerProfileLabel(profile))
                        currentIndex: currentProfileIndex
                        selectionMode: "single"
                        onSelectionChanged: (index, selected) => {
                            if (!selected)
                                return;
                            root.setProfile(profileModel[index]);
                        }
                    }
                }

                StyledRect {
                    width: parent.width
                    height: degradationContent.implicitHeight + Theme.spacingL * 2
                    radius: Theme.cornerRadius
                    color: Qt.rgba(Theme.error.r, Theme.error.g, Theme.error.b, 0.12)
                    border.color: Qt.rgba(Theme.error.r, Theme.error.g, Theme.error.b, 0.3)
                    border.width: 0
                    visible: (typeof PowerProfiles !== "undefined") && PowerProfiles.degradationReason !== PerformanceDegradationReason.None

                    Column {
                        id: degradationContent
                        width: parent.width - Theme.spacingL * 2
                        anchors.left: parent.left
                        anchors.top: parent.top
                        anchors.margins: Theme.spacingL
                        spacing: Theme.spacingS

                        Row {
                            width: parent.width
                            spacing: Theme.spacingM

                            DankIcon {
                                name: "warning"
                                size: Theme.iconSize
                                color: Theme.error
                                anchors.verticalCenter: parent.verticalCenter
                            }

                            Column {
                                spacing: Theme.spacingXS
                                anchors.verticalCenter: parent.verticalCenter
                                width: parent.width - Theme.iconSize - Theme.spacingM

                                StyledText {
                                    text: I18n.tr("Power Profile Degradation")
                                    font.pixelSize: Theme.fontSizeLarge
                                    color: Theme.error
                                    font.weight: Font.Medium
                                }

                                StyledText {
                                    text: (typeof PowerProfiles !== "undefined") ? PerformanceDegradationReason.toString(PowerProfiles.degradationReason) : ""
                                    font.pixelSize: Theme.fontSizeSmall
                                    color: Qt.rgba(Theme.error.r, Theme.error.g, Theme.error.b, 0.8)
                                    wrapMode: Text.WordWrap
                                    width: parent.width
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}