Debian Codegraph @

src/qml/MpvVideo.qml

raw
1 /*
2 * SPDX-FileCopyrightText: 2020 George Florea Bănuș <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 pragma ComponentBehavior: Bound
8
9 import QtQuick
10 import QtQuick.Controls
11 import QtQuick.Layouts
12 import QtQuick.Window
13 import Qt5Compat.GraphicalEffects
14
15 import org.kde.haruna
16 import org.kde.haruna.playlist
17 import org.kde.haruna.utilities
18 import org.kde.haruna.settings
19
20 import org.kde.kirigami as Kirigami
21
22 MpvItem {
23 id: root
24
25 required property Osd osd
26 required property MouseActionsModel mouseActionsModel
27 property var window: Window.window
28 property int preMinimizePlaybackState: MpvItem.PlaybackState.Playing
29 property alias mouseY: mouseArea.mouseY
30
31 signal mousePositionChanged(double x, double y)
32 signal openPlaylist()
33 signal closePlaylist()
34
35 onPlaybackStateChanged: {
36 switch(playbackState) {
37 case MpvItem.PlaybackState.Stopped:
38 loadingIndicator.play = false
39 loadingIndicatorSprite.currentFrame = 0
40 break
41 case MpvItem.PlaybackState.Playing:
42 loadingIndicator.play = false
43 break
44 case MpvItem.PlaybackState.Loading:
45 const url = currentUrl.toString()
46 if (typeof url === "string" && url.startsWith("http")) {
47 loadingIndicator.play = true
48 }
49 break
50 }
51 }
52
53 onVolumeChanged: {
54 osd.message(i18nc("@info:tooltip", "Volume: %1", root.volume))
55 }
56
57 onOsdMessage: function(text) {
58 osd.message(text);
59 }
60
61 onRaise: { HarunaApp.raiseWindow() }
62 onPlayNext: { HarunaApp.actions.playNextAction.trigger() }
63 onPlayPrevious: { HarunaApp.actions.playPreviousAction.trigger() }
64 onOpenUri: {
65 root.window.openFile(uri, RecentFilesModel.OpenedFrom.ExternalApp)
66 }
67
68 Timer {
69 id: hideCursorTimer
70
71 running: root.window.isFullScreen() && mouseArea.containsMouse
72
73 repeat: true
74 interval: 2000
75 onTriggered: mouseArea.hideCursor = true
76 }
77
78 MouseArea {
79 id: mouseArea
80
81 property bool hideCursor: false
82
83 acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MiddleButton | Qt.ForwardButton | Qt.BackButton
84 anchors.fill: parent
85 hoverEnabled: true
86 cursorShape: hideCursor && root.window.isFullScreen() ? Qt.BlankCursor : Qt.ArrowCursor
87
88 onPositionChanged: {
89 root.mousePositionChanged(mouseX, mouseY)
90
91 hideCursor = false
92 hideCursorTimer.restart()
93
94 if (!PlaylistSettings.canToggleWithMouse) {
95 return
96 }
97 if (PlaylistSettings.position === "right") {
98 if (mouseX > width - 50) {
99 root.openPlaylist()
100 } else {
101 root.closePlaylist()
102 }
103 } else {
104 if (mouseX < 50) {
105 root.openPlaylist()
106 } else {
107 root.closePlaylist()
108 }
109 }
110 }
111
112 onWheel: function(wheel) {
113 const model = root.mouseActionsModel
114 let actionName
115 let modifier = wheel.modifiers
116 if (wheel.angleDelta.y > 0) {
117 actionName = model.getAction(MouseActionsModel.ScrollUp, modifier)
118 } else if (wheel.angleDelta.y) {
119 actionName = model.getAction(MouseActionsModel.ScrollDown, modifier)
120 }
121
122 if (actionName) {
123 HarunaApp.actions[actionName].trigger()
124 }
125 }
126
127 onPressed: function(mouse) {
128 focus = true
129 const model = root.mouseActionsModel
130 let actionName
131 let modifier = mouse.modifiers
132 if (mouse.button === Qt.LeftButton) {
133 actionName = model.getAction(Qt.LeftButton, modifier)
134 } else if (mouse.button === Qt.MiddleButton) {
135 actionName = model.getAction(Qt.MiddleButton, modifier)
136 } else if (mouse.button === Qt.RightButton) {
137 actionName = model.getAction(Qt.RightButton, modifier)
138 } else if (mouse.button === Qt.ForwardButton) {
139 actionName = model.getAction(Qt.ForwardButton, modifier)
140 } else if (mouse.button === Qt.BackButton) {
141 actionName = model.getAction(Qt.BackButton, modifier)
142 }
143
144 if (actionName) {
145 HarunaApp.actions[actionName].trigger()
146 }
147 }
148
149 onDoubleClicked: function(mouse) {
150 focus = true
151 const model = root.mouseActionsModel
152 let actionName
153 let modifier = mouse.modifiers
154 if (mouse.button === Qt.LeftButton) {
155 actionName = model.getAction(Qt.LeftButton, modifier, true)
156 } else if (mouse.button === Qt.MiddleButton) {
157 actionName = model.getAction(Qt.MiddleButton, modifier, true)
158 } else if (mouse.button === Qt.RightButton) {
159 actionName = model.getAction(Qt.RightButton, modifier, true)
160 } else if (mouse.button === Qt.ForwardButton) {
161 actionName = model.getAction(Qt.ForwardButton, modifier, true)
162 } else if (mouse.button === Qt.BackButton) {
163 actionName = model.getAction(Qt.BackButton, modifier, true)
164 }
165
166 if (actionName) {
167 HarunaApp.actions[actionName].trigger()
168 }
169 }
170 }
171
172 DropArea {
173 id: dropArea
174
175 property bool dragMedia: false
176 property bool dragSubtitle: false
177 property int subtitleIndex: -1
178 property bool mouseOnTopPart: containsDrag && dragMedia && drag.y <= height * 0.25
179 property bool mouseOnBottomPart: containsDrag && dragMedia && drag.y > height * 0.25
180
181 anchors.fill: parent
182 keys: ["text/uri-list"]
183
184 onEntered: function (drag) {
185 for (var i = 0; i < drag.urls.length; ++i) {
186 let mimeType = MiscUtils.mimeType(drag.urls[i])
187 let isDir = root.defaultFilterProxyModel.isDirectory(drag.urls[i])
188 if (mimeType.startsWith("video/") || mimeType.startsWith("audio/") || isDir) {
189 dragMedia = true
190 return
191 }
192 if (root.window.acceptedSubtitleTypes.includes(mimeType)) {
193 dragSubtitle = true
194 subtitleIndex = i
195 }
196 }
197 }
198
199 onExited: {
200 dragMedia = false
201 dragSubtitle = false
202 subtitleIndex = -1
203 }
204
205 onDropped: function (drop) {
206 if (dragSubtitle && !dragMedia) {
207 // If dragging multiple items and none of them is a media, and if there is a subtitle, load it
208 mpv.addSubtitles(drop.urls[subtitleIndex])
209 return
210 }
211 if (mouseOnTopPart) {
212 // Append to playlist
213 root.defaultFilterProxyModel.addFilesAndFolders(drop.urls, PlaylistModel.Append)
214 return
215 }
216 if (mouseOnBottomPart) {
217 // Clear the playlist
218 let isDir = root.defaultFilterProxyModel.isDirectory(drop.urls[0])
219 if (drop.urls.length > 1 || isDir) {
220 // More than one file/folder dragged. Or at least one folder dragged.
221 root.defaultFilterProxyModel.addFilesAndFolders(drop.urls, PlaylistModel.Clear)
222 }
223 else {
224 // One file dragged
225 root.window.openFile(drop.urls[0], RecentFilesModel.OpenedFrom.ExternalApp)
226 }
227 return
228 }
229 }
230
231 Rectangle {
232 id: topDropRect
233
234 visible: dropArea.containsDrag
235 color: "transparent"
236 width: parent.width
237 height: parent.height * 0.25
238 radius: Kirigami.Units.cornerRadius
239
240 anchors {
241 top: dropArea.top
242 left: dropArea.left
243 right: dropArea.right
244 margins: Kirigami.Units.smallSpacing
245 }
246
247 border {
248 color: dropArea.mouseOnTopPart
249 ? Kirigami.Theme.activeTextColor
250 : "transparent"
251 width: dropArea.mouseOnTopPart ? 3 : 0
252 }
253
254 Label {
255 anchors.fill: parent
256 anchors.centerIn: parent
257
258 text: i18nc("@info:drag&drop append items", "Add to Playlist")
259 elide: Text.ElideRight
260 color: Kirigami.Theme.textColor
261 opacity: dropArea.mouseOnTopPart ? 1 : 0.7
262
263 font.pointSize: dropArea.calculateAdaptiveFontSize(12, 30, 400, 1200)
264 font.bold: true
265 horizontalAlignment: Qt.AlignCenter
266 verticalAlignment: Qt.AlignVCenter
267 layer.enabled: true
268 layer.effect: DropShadow {
269 cached: true
270 verticalOffset: 1
271 horizontalOffset: 1
272 color: Kirigami.Theme.backgroundColor
273 radius: 12
274 spread: 0.4
275 samples: 20
276 }
277 }
278 }
279
280 Rectangle {
281 id: bottomDropRect
282
283 visible: dropArea.containsDrag
284 width: parent.width
285 color: "transparent"
286 radius: Kirigami.Units.cornerRadius
287
288 anchors {
289 top: topDropRect.bottom
290 left: dropArea.left
291 right: dropArea.right
292 bottom: dropArea.bottom
293 margins: Kirigami.Units.smallSpacing
294 }
295
296 border {
297 color: dropArea.mouseOnBottomPart
298 ? Kirigami.Theme.activeTextColor
299 : "transparent"
300 width: dropArea.mouseOnBottomPart ? 3 : 0
301 }
302
303 Label {
304 anchors.fill: parent
305 anchors.centerIn: parent
306
307 text: i18nc("@info:drag&drop play now", "Play Now")
308 elide: Text.ElideRight
309 color: Kirigami.Theme.textColor
310 opacity: dropArea.mouseOnBottomPart ? 1 : 0.7
311
312 font.pointSize: dropArea.calculateAdaptiveFontSize(18, 36, 400, 1200)
313 font.bold: true
314 horizontalAlignment: Qt.AlignCenter
315 verticalAlignment: Qt.AlignVCenter
316 layer.enabled: true
317 layer.smooth: true
318 layer.effect: DropShadow {
319 cached: true
320 verticalOffset: 1
321 horizontalOffset: 1
322 color: Kirigami.Theme.backgroundColor
323 radius: 12
324 spread: 0.4
325 samples: 20
326 }
327 }
328 }
329
330 function calculateAdaptiveFontSize(minPoint, maxPoint, maxWinHeight, maxWinWidth) {
331 let width = root.window.width
332 let height = root.window.height
333 let cappedWidth = Math.min(Math.max(root.window.minimumWidth, width), maxWinWidth)
334 let cappedHeight = Math.min(Math.max(root.window.minimumHeight, height), maxWinHeight)
335 const widthScale = maxWinWidth - root.window.minimumWidth
336 const heightScale = maxWinHeight - root.window.minimumHeight
337 const pointScale = maxPoint - minPoint
338
339 // calculate a font point size separately by considering both current width and height
340 let wPoint = (((cappedWidth - root.window.minimumWidth) * pointScale) / widthScale) + minPoint
341 let hPoint = (((cappedHeight - root.window.minimumHeight) * pointScale) / heightScale) + minPoint
342 return Math.min(wPoint, hPoint)
343 }
344 }
345
346 Rectangle {
347 id: loadingIndicator
348
349 property bool play: false
350 property int size: 200
351
352 visible: root.playbackState === MpvItem.PlaybackState.Stopped || root.playbackState === MpvItem.PlaybackState.Loading
353 anchors.centerIn: parent
354 radius: Kirigami.Units.cornerRadius
355 width: root.width > size ? size : root.width - 10
356 height: root.height > size ? size : root.height - 10
357 color: play ? Qt.alpha(Kirigami.Theme.backgroundColor, 0.6) : "transparent"
358
359 ColumnLayout {
360 anchors.centerIn: parent
361
362 Item {
363 property int spriteSize: {
364 const _paddedMaxHeight = root.height - Kirigami.Units.largeSpacing * 2 - loadingText.implicitHeight
365 const _clampedHeight = Math.min(Kirigami.Units.iconSizes.enormous, _paddedMaxHeight)
366 return Math.max(Kirigami.Units.iconSizes.small, _clampedHeight)
367 }
368 Layout.alignment: Qt.AlignHCenter
369 Layout.preferredWidth: spriteSize
370 Layout.preferredHeight: spriteSize
371
372 AnimatedSprite {
373 id: loadingIndicatorSprite
374
375 anchors.fill: parent
376 currentFrame: 0
377 finishBehavior: AnimatedSprite.FinishAtInitialFrame
378 frameCount: 8
379 // Do NOT use frameRate. It causes flickering. Use frameDuration instead
380 frameDuration: Math.round(1000.0 / 30.0)
381 frameHeight: 192
382 frameWidth: 192
383 interpolate: false
384 source: "qrc:/data/animations/192-haruna-animation-sliding.png"
385 reverse: false
386 running: loadingIndicator.play
387 }
388 }
389
390 Text {
391 id: loadingText
392
393 text: i18nc("@label:title", "Loading…")
394 visible: loadingIndicator.play
395
396 font {
397 bold: true
398 pointSize: 16
399 }
400
401 Layout.alignment: Qt.AlignHCenter
402 }
403
404 layer.enabled: true
405 layer.samplerName: "maskSource"
406 layer.effect: ShaderEffect {
407 property color colorSource: loadingIndicator.play ? Qt.alpha(Kirigami.Theme.textColor, 0.65) : "#222"
408 fragmentShader: "qrc:/shader/colorfill.frag.qsb"
409 Kirigami.Theme.inherit: false
410 Kirigami.Theme.colorSet: Kirigami.Theme.Window
411 }
412 }
413 }
414 }