blob: fd03162723a6ccf8c441d8d3714513f2927bd23d (
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
 | import QtQuick
import Quickshell
import Quickshell.Widgets
import Quickshell.Services.UPower
Item {
  id: root
  height: parent.height
  width: batteryIcon.width + 8
  anchors.verticalCenter: parent.verticalCenter
  property var batteryDevice: Array.from(UPower.devices.values).find(dev => dev.isLaptopBattery)
  WrapperMouseArea {
    id: widgetMouseArea
    anchors.fill: parent
    hoverEnabled: true
    Item {
      anchors.fill: parent
      MaterialDesignIcon {
	id: batteryIcon
	implicitSize: 14
	anchors.centerIn: parent
	icon: {
	  if (!root.batteryDevice?.ready)
	    return "battery-unknown";
	  if (root.batteryDevice.state == UPowerDeviceState.FullyCharged)
	    return "power-plug-battery";
	  const perdec = 10 * Math.max(1, Math.ceil(root.batteryDevice.percentage * 10));
	  if (root.batteryDevice.state == UPowerDeviceState.Charging)
	    return `battery-charging-${perdec}`;
	  if (perdec == 100)
	    return "battery";
	  return `battery-${perdec}`;
	}
	color: {
	  if (!root.batteryDevice?.ready)
	    return "#555";
	  if (root.batteryDevice.state != UPowerDeviceState.FullyCharged && root.batteryDevice.state != UPowerDeviceState.Charging && root.batteryDevice.timeToEmpty < 20 * 60)
	    return "#f2201f";
	  if (root.batteryDevice.state != UPowerDeviceState.FullyCharged && root.batteryDevice.state != UPowerDeviceState.Charging && root.batteryDevice.timeToEmpty < 40 * 60)
	    return "#f28a21";
	  if (root.batteryDevice.state != UPowerDeviceState.FullyCharged)
	    return "#fff";
	  return "#555";
	}
      }
    }
  }
  PopupWindow {
    id: tooltip
    property bool nextVisible: widgetMouseArea.containsMouse || tooltipMouseArea.containsMouse
    anchor {
      item: widgetMouseArea
      edges: Edges.Bottom | Edges.Left
    }
    visible: false
    onNextVisibleChanged: hangTimer.restart()
    Timer {
      id: hangTimer
      interval: 100
      onTriggered: tooltip.visible = tooltip.nextVisible
    }
    implicitWidth: widgetTooltipText.contentWidth + 16
    implicitHeight: widgetTooltipText.contentHeight + 16
    color: "black"
    WrapperMouseArea {
      id: tooltipMouseArea
      hoverEnabled: true
      enabled: true
      anchors.centerIn: parent
      Text {
	id: widgetTooltipText
	font.pointSize: 10
	font.family: "Fira Sans"
	color: "white"
	text: {
	  const stateStr = UPowerDeviceState.toString(root.batteryDevice.state);
	  var outStr = stateStr;
	  if (root.batteryDevice.state != UPowerDeviceState.FullyCharged)
	    outStr += ` ${Math.round(root.batteryDevice.percentage * 100)}%`;
	  function formatTime(t) {
	    var res = "";
	    for (const unit of [{ "s": "h", "v": 3600 }, { "s": "m", "v": 60 }, { "s": "s", "v": 1 }]) {
	      if (t < unit.v)
	        continue;
	      res += Math.floor(t / unit.v) + unit.s;
	      t %= unit.v;
	    }
	    return res;
	  }
	  if (root.batteryDevice.timeToEmpty != 0) {
	    const tStr = formatTime(Math.floor(root.batteryDevice.timeToEmpty / 60) * 60);
	    if (tStr)
	      outStr += " " + tStr;
	  } else if (root.batteryDevice.timeToFull != 0) {
	    const tStr = formatTime(Math.ceil(root.batteryDevice.timeToFull / 60) * 60);
	    if (tStr)
	      outStr += " " + tStr;
	  }
	  return outStr;
	}
      }
    }
  }
}
 |