| 1 |
// SPDX-FileCopyrightText: Nheko Contributors |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
import "../" |
| 6 |
import "../../" |
| 7 |
import QtMultimedia |
| 8 |
import QtQuick |
| 9 |
import QtQuick.Controls |
| 10 |
import QtQuick.Layouts |
| 11 |
import im.nheko |
| 12 |
|
| 13 |
Rectangle { |
| 14 |
id: control |
| 15 |
|
| 16 |
property alias desiredVolume: volumeSlider.desiredVolume |
| 17 |
property bool muted: false |
| 18 |
property bool playingVideo: false |
| 19 |
property var mediaState |
| 20 |
property bool mediaLoaded: false |
| 21 |
property var duration |
| 22 |
property var positionValue: 0 |
| 23 |
property var position |
| 24 |
property bool shouldShowControls: !playingVideo || playerMouseArea.shouldShowControls || volumeSlider.state == "shown" |
| 25 |
|
| 26 |
signal playPauseActivated() |
| 27 |
signal loadActivated() |
| 28 |
|
| 29 |
function showControls() { |
| 30 |
controlHideTimer.restart(); |
| 31 |
} |
| 32 |
|
| 33 |
function durationToString(duration) { |
| 34 |
function maybeZeroPrepend(time) { |
| 35 |
return (time < 10) ? "0" + time.toString() : time.toString(); |
| 36 |
} |
| 37 |
|
| 38 |
var totalSeconds = Math.floor(duration / 1000); |
| 39 |
var seconds = totalSeconds % 60; |
| 40 |
var minutes = (Math.floor(totalSeconds / 60)) % 60; |
| 41 |
var hours = (Math.floor(totalSeconds / (60 * 60))) % 24; |
| 42 |
// Always show minutes and don't prepend zero into the leftmost element |
| 43 |
var ss = maybeZeroPrepend(seconds); |
| 44 |
var mm = (hours > 0) ? maybeZeroPrepend(minutes) : minutes.toString(); |
| 45 |
var hh = hours.toString(); |
| 46 |
if (hours < 1) |
| 47 |
return mm + ":" + ss; |
| 48 |
|
| 49 |
return hh + ":" + mm + ":" + ss; |
| 50 |
} |
| 51 |
|
| 52 |
color: { |
| 53 |
var wc = palette.alternateBase; |
| 54 |
return Qt.rgba(wc.r, wc.g, wc.b, 0.5); |
| 55 |
} |
| 56 |
opacity: control.shouldShowControls ? 1 : 0 |
| 57 |
height: controlLayout.implicitHeight |
| 58 |
|
| 59 |
HoverHandler { |
| 60 |
id: playerMouseArea |
| 61 |
|
| 62 |
property bool shouldShowControls: hovered || controlHideTimer.running || control.mediaState != MediaPlayer.PlayingState |
| 63 |
|
| 64 |
onHoveredChanged: showControls() |
| 65 |
} |
| 66 |
|
| 67 |
ColumnLayout { |
| 68 |
id: controlLayout |
| 69 |
|
| 70 |
enabled: control.shouldShowControls |
| 71 |
spacing: 0 |
| 72 |
anchors.bottom: control.bottom |
| 73 |
anchors.left: control.left |
| 74 |
width: Math.max(implicitWidth, control.width) |
| 75 |
|
| 76 |
NhekoSlider { |
| 77 |
Layout.fillWidth: true |
| 78 |
Layout.leftMargin: Nheko.paddingSmall |
| 79 |
Layout.rightMargin: Nheko.paddingSmall |
| 80 |
enabled: control.mediaLoaded |
| 81 |
value: control.positionValue |
| 82 |
onMoved: control.position = value |
| 83 |
from: 0 |
| 84 |
to: control.duration |
| 85 |
alwaysShowSlider: false |
| 86 |
} |
| 87 |
|
| 88 |
RowLayout { |
| 89 |
Layout.margins: Nheko.paddingSmall |
| 90 |
spacing: Nheko.paddingSmall |
| 91 |
Layout.fillWidth: true |
| 92 |
|
| 93 |
// Cache/Play/pause button |
| 94 |
ImageButton { |
| 95 |
id: playbackStateImage |
| 96 |
|
| 97 |
Layout.alignment: Qt.AlignLeft |
| 98 |
buttonTextColor: palette.text |
| 99 |
Layout.preferredHeight: 24 |
| 100 |
Layout.preferredWidth: 24 |
| 101 |
image: { |
| 102 |
if (control.mediaLoaded) { |
| 103 |
if (control.mediaState == MediaPlayer.PlayingState) |
| 104 |
return ":/icons/icons/ui/pause-symbol.svg"; |
| 105 |
else |
| 106 |
return ":/icons/icons/ui/play-sign.svg"; |
| 107 |
} else { |
| 108 |
return ":/icons/icons/ui/download.svg"; |
| 109 |
} |
| 110 |
} |
| 111 |
onClicked: control.mediaLoaded ? control.playPauseActivated() : control.loadActivated() |
| 112 |
} |
| 113 |
|
| 114 |
ImageButton { |
| 115 |
id: volumeButton |
| 116 |
|
| 117 |
Layout.alignment: Qt.AlignLeft |
| 118 |
buttonTextColor: palette.text |
| 119 |
Layout.preferredHeight: 24 |
| 120 |
Layout.preferredWidth: 24 |
| 121 |
image: { |
| 122 |
if (control.muted || control.desiredVolume <= 0) |
| 123 |
return ":/icons/icons/ui/volume-off-indicator.svg"; |
| 124 |
else |
| 125 |
return ":/icons/icons/ui/volume-up.svg"; |
| 126 |
} |
| 127 |
onClicked: control.muted = !control.muted |
| 128 |
} |
| 129 |
|
| 130 |
NhekoSlider { |
| 131 |
id: volumeSlider |
| 132 |
|
| 133 |
property real desiredVolume: volumeSlider.value |
| 134 |
|
| 135 |
state: "" |
| 136 |
Layout.alignment: Qt.AlignLeft |
| 137 |
Layout.preferredWidth: 100 |
| 138 |
opacity: 0 |
| 139 |
enabled: false |
| 140 |
orientation: Qt.Horizontal |
| 141 |
value: 1 |
| 142 |
onDesiredVolumeChanged: { |
| 143 |
control.muted = !(desiredVolume > 0); |
| 144 |
} |
| 145 |
transitions: [ |
| 146 |
Transition { |
| 147 |
from: "" |
| 148 |
to: "shown" |
| 149 |
|
| 150 |
SequentialAnimation { |
| 151 |
PauseAnimation { |
| 152 |
duration: 50 |
| 153 |
} |
| 154 |
|
| 155 |
NumberAnimation { |
| 156 |
duration: 100 |
| 157 |
properties: "opacity" |
| 158 |
easing.type: Easing.InQuad |
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
}, |
| 163 |
Transition { |
| 164 |
from: "shown" |
| 165 |
to: "" |
| 166 |
|
| 167 |
SequentialAnimation { |
| 168 |
PauseAnimation { |
| 169 |
duration: 100 |
| 170 |
} |
| 171 |
|
| 172 |
ParallelAnimation { |
| 173 |
NumberAnimation { |
| 174 |
duration: 100 |
| 175 |
properties: "opacity" |
| 176 |
easing.type: Easing.InQuad |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
} |
| 185 |
] |
| 186 |
|
| 187 |
states: State { |
| 188 |
name: "shown" |
| 189 |
when: Settings.mobileMode || volumeButton.hovered || volumeSlider.hovered || volumeSlider.pressed |
| 190 |
|
| 191 |
PropertyChanges { |
| 192 |
volumeSlider.opacity: 1 |
| 193 |
volumeSlider.enabled: true |
| 194 |
} |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
Item { |
| 201 |
Layout.fillWidth: true |
| 202 |
} |
| 203 |
|
| 204 |
Label { |
| 205 |
Layout.alignment: Qt.AlignRight |
| 206 |
text: (!control.mediaLoaded ? "-- " : durationToString(control.positionValue)) + " / " + durationToString(control.duration) |
| 207 |
color: palette.text |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
// For hiding controls on stationary cursor |
| 215 |
Timer { |
| 216 |
id: controlHideTimer |
| 217 |
|
| 218 |
interval: 1500 //ms |
| 219 |
repeat: false |
| 220 |
} |
| 221 |
|
| 222 |
// Fade controls in/out |
| 223 |
Behavior on opacity { |
| 224 |
OpacityAnimator { |
| 225 |
duration: 100 |
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
} |