summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell/YtDlpWidget.qml
blob: ff338d49435414aa74cdd3129f96a0b6c50e3a12 (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
import QtQml.Models
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls.Material
import Quickshell
import Quickshell.Io
import Quickshell.Widgets
import Custom as Custom

Item {
  id: root

  height: parent.height
  width: icon.width
  anchors.verticalCenter: parent.verticalCenter

  visible: Boolean(unitsModel.values.length)

  WrapperMouseArea {
    id: widgetMouseArea

    anchors.fill: parent
    hoverEnabled: true

    Item {
      anchors.fill: parent

      MaterialDesignIcon {
        id: icon

	implicitSize: 14
	anchors.centerIn: parent

	icon: "tray-arrow-down"
	color: "white"
      }
    }
  }

  PopupWindow {
    id: tooltip

    property bool openPopup: false
    property bool nextVisible: widgetMouseArea.containsMouse || tooltipMouseArea.containsMouse || openPopup

    anchor {
      item: widgetMouseArea
      edges: Edges.Bottom | Edges.Left
    }
    visible: false

    onNextVisibleChanged: hangTimer.restart()

    Timer {
      id: hangTimer
      interval: 100
      onTriggered: tooltip.visible = tooltip.nextVisible
    }

    implicitWidth: tooltipContent.width
    implicitHeight: tooltipContent.height
    color: "transparent"

    Rectangle {
      width: tooltip.width
      height: tooltipLayout.childrenRect.height + 16
      color: "black"
    }

    WrapperItem {
      id: tooltipContent

      WrapperMouseArea {
	id: tooltipMouseArea

	hoverEnabled: true
	enabled: true

	WrapperItem {
	  margin: 8

	  child: GridLayout {
	    id: tooltipLayout

	    columns: 2
          }

	  Repeater {
	    model: ScriptModel {
	      id: unitsModel

	      values: [...Custom.Systemd.userUnits.values].filter(unit => unit.id.match(/^yt-dlp@.+\.service$/) && unit.activeState === "active" && unit.runtimeDirectory)
	    }

	    Item {
	      id: unitDelegate

	      Component.onCompleted: {
		while (children.length) { children[0].parent = tooltipLayout; }
	      }

	      required property var modelData
	      required property int index

	      property var state: null

	      Socket {
	        id: socket

		path: Quickshell.env("XDG_RUNTIME_DIR") + "/" + unitDelegate.modelData.runtimeDirectory[0] + "/status.sock"
		connected: true
		onError: e => {
		  console.warn("YtDlpWidget: Error in Socket:", socket.path, e);
		}
		parser: SplitParser {
		  onRead: line => {
		    try {
		      unitDelegate.state = JSON.parse(line)
		    } catch (e) {
		      console.warn("YtDlpWidget: Failed to parse state:", line, e)
		    }
		  }
		}
	      }

	      WrapperItem {
		Layout.column: 0
		Layout.row: unitDelegate.index

		implicitWidth: descText.contentWidth
		implicitHeight: descText.contentHeight

		Text {
		  id: descText

		  color: "white"
		  font.pointSize: 10
		  font.family: "Fira Sans"

		  text: unitDelegate.state?.title || unitDelegate.modelData.id
		}
	      }

	      /*
	      WrapperItem {
		Layout.column: 1
		Layout.row: unitDelegate.index

		implicitWidth: fragText.contentWidth
		implicitHeight: fragText.contentHeight

		visible: Boolean(unitDelegate.state)

		Text {
		  id: fragText

		  color: "white"
		  font.pointSize: 10
		  font.family: "Fira Sans"

		  text: `${unitDelegate.state?.fragment_index}/${unitDelegate.state?.fragment_count}`
		}
	      }
	      */

	      WrapperItem {
		Layout.column: 1
		Layout.row: unitDelegate.index

		visible: Boolean(unitDelegate.state)

		ProgressBar {
		  id: progress

		  Material.background: "black"
		  Material.accent: "white"

		  from: 0
		  to: unitDelegate.state?.total_bytes || unitDelegate.state?.total_bytes_estimate || 0
		  value: unitDelegate.state?.downloaded_bytes || 0
		  indeterminate: !Boolean(unitDelegate.state?.total_bytes || unitDelegate.state?.total_bytes_estimate) || unitDelegate.state?.status !== "downloading"
		}
	      }
	    }
	  }
	}
      }
    }
  }
}