nheko/resources/qml/ui/media/VolumeControl.qml

118 lines
4.2 KiB
QML
Raw Normal View History

// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2021-11-11 06:16:25 +01:00
import "../"
import QtMultimedia 5.15
import QtQuick 2.15
import QtQuick.Controls 2.15
2021-11-11 06:16:25 +01:00
import im.nheko 1.0
// Volume slider activator
Image {
2021-11-10 04:17:00 +01:00
// TODO: add icons for different volume levels
id: volumeImage
property alias desiredVolume: volumeSlider.desiredVolume
property alias orientation: volumeSlider.orientation
property alias controlsVisible: volumeSliderRect.visible
property bool muted: false
2021-11-10 04:17:00 +01:00
property color controlColor: (volumeImageArea.containsMouse) ? Nheko.colors.highlight : Nheko.colors.text
2021-11-11 06:16:25 +01:00
width: sourceSize.width + volumeSliderRect.implicitWidth
2021-11-10 04:17:00 +01:00
source: (desiredVolume > 0 && !muted) ? "image://colorimage/:/icons/icons/ui/volume-up.png?" + controlColor : "image://colorimage/:/icons/icons/ui/volume-off-indicator.png?" + controlColor
fillMode: Image.PreserveAspectFit
MouseArea {
2021-11-10 04:17:00 +01:00
id: volumeImageArea
anchors.fill: parent
hoverEnabled: true
onExited: volumeSliderHideTimer.start()
onPositionChanged: volumeSliderHideTimer.start()
onClicked: volumeImage.muted = !volumeImage.muted
2021-11-10 04:17:00 +01:00
// For hiding volume slider after a while
Timer {
id: volumeSliderHideTimer
2021-11-10 04:17:00 +01:00
interval: 1500
repeat: false
running: false
}
2021-11-10 04:17:00 +01:00
}
2021-11-10 04:17:00 +01:00
Rectangle {
id: volumeSliderRect
2021-11-10 04:17:00 +01:00
opacity: (visible) ? 1 : 0
2021-11-11 06:16:25 +01:00
anchors.bottom: volumeSlider.orientation == Qt.Vertical ? volumeImage.top : undefined
anchors.left: volumeSlider.orientation == Qt.Vertical ? undefined : volumeImage.right
anchors.horizontalCenter: volumeSlider.orientation == Qt.Vertical ? volumeImage.horizontalCenter : undefined
anchors.verticalCenter: volumeSlider.orientation == Qt.Vertical ? undefined : volumeImage.verticalCenter
color: {
2021-11-11 06:16:25 +01:00
if (volumeSlider.orientation == Qt.Vertical) {
var wc = Nheko.colors.window;
return Qt.rgba(wc.r, wc.g, wc.b, 0.5);
} else {
return "transparent";
}
}
/* TODO: base width on the slider width (some issue with it not having a geometry
when using the width here?) */
2021-11-11 06:16:25 +01:00
width: volumeSlider.orientation == Qt.Vertical ? volumeImage.width * 0.7 : 100
radius: volumeSlider.width / 2
2021-11-11 06:16:25 +01:00
height: volumeSlider.orientation == Qt.Vertical ? 100 : volumeImage.height * 0.7
2021-11-10 04:17:00 +01:00
visible: volumeImageArea.containsMouse || volumeSliderHideTimer.running || volumeSliderRectMouseArea.containsMouse
2021-11-11 06:16:25 +01:00
NhekoSlider {
// TODO: the slider is slightly off-center on the left for some reason...
id: volumeSlider
2021-11-11 06:16:25 +01:00
sliderWidth: 8
sliderHeight: 8
// Desired value to avoid loop onMoved -> media.volume -> value -> onMoved...
2021-11-10 04:17:00 +01:00
property real desiredVolume: QtMultimedia.convertVolume(volumeSlider.value, QtMultimedia.LogarithmicVolumeScale, QtMultimedia.LinearVolumeScale)
2021-11-10 04:17:00 +01:00
value: 1
anchors.fill: volumeSliderRect
2021-11-11 06:16:25 +01:00
anchors.horizontalCenter: orientation == Qt.Vertical ? volumeSliderRect.horizontalCenter : undefined
anchors.verticalCenter: orientation == Qt.Vertical ? undefined : volumeSliderRect.verticalCenter
orientation: Qt.Vertical
onDesiredVolumeChanged: {
2021-11-10 04:17:00 +01:00
volumeImage.muted = !(desiredVolume > 0);
}
}
// Used for resetting the timer on mouse moves on volumeSliderRect
2021-11-10 04:17:00 +01:00
MouseArea {
id: volumeSliderRectMouseArea
2021-11-10 04:17:00 +01:00
anchors.fill: parent
hoverEnabled: true
propagateComposedEvents: true
onExited: volumeSliderHideTimer.start()
onClicked: mouse.accepted = false
onPressed: mouse.accepted = false
onReleased: mouse.accepted = false
onPressAndHold: mouse.accepted = false
onPositionChanged: {
2021-11-10 04:17:00 +01:00
mouse.accepted = false;
volumeSliderHideTimer.start();
}
}
2021-11-10 04:17:00 +01:00
Behavior on opacity {
OpacityAnimator {
duration: 100
}
}
}
2021-11-10 04:17:00 +01:00
}