Socialify

Folder ..

Viewing VolumeController.qml
148 lines (122 loc) • 5.2 KB

  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
import QtQuick 2.0

import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0

import org.kde.kquickcontrolsaddons 2.0
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.extras 2.0 as PlasmaExtras

MouseArea {
    id: root
    property bool muted: false
    property var pulseObject
    property string iconName: ""
    property bool shadeIconWhileMuted: false

    height: layout.implicitHeight

    ColumnLayout {
        id: layout

        anchors {
            left: parent.left
            right: parent.right
        }

        RowLayout {
            id: controler
            Layout.fillWidth: true
            spacing: 8

            ColumnLayout {
                id: column

                RowLayout {
                    id: contorller
                    Layout.fillWidth: true

                    QIconItem {
                        id: clientIcon

                        Layout.preferredHeight: slider.height
                        Layout.preferredWidth: slider.height

                        icon:  iconName !== "" ? iconName : "undefined"
                        visible: iconName !== ""

                        state: {
                            if (shadeIconWhileMuted)
                                if (pulseObject)
                                    return pulseObject.muted ? QIconItem.DisabledState : QIconItem.DefaultState

                            return QIconItem.DefaultState ;
                        }
                        MouseArea {
                            anchors.fill: parent
                            onPressed: pulseObject.muted = !pulseObject.muted
                        }
                    }


                    PlasmaComponents.Slider {
                        id: slider

                        // Helper properties to allow async slider updates.
                        // While we are sliding we must not react to value updates
                        // as otherwise we can easily end up in a loop where value
                        // changes trigger volume changes trigger value changes.
                        property int volume: pulseObject? pulseObject.volume : -1
                        property bool ignoreValueChange: false

                        Layout.fillWidth: true
                        minimumValue: 0
                        // FIXME: I do wonder if exposing max through the model would be useful at all
                        maximumValue: 65536
                        stepSize: maximumValue / 100
                        visible: pulseObject? pulseObject.hasVolume : false
                        enabled: {
                            if (pulseObject && typeof pulseObject.volumeWritable === 'undefined') {
                                return !pulseObject.muted
                            }
                            return pulseObject ?  (pulseObject.volumeWritable && !pulseObject.muted) : false
                        }

                        onVolumeChanged: {
                            ignoreValueChange = true
                            value = pulseObject ? pulseObject.volume : -1
                            ignoreValueChange = false
                        }

                        onValueChanged: {
                            if (!ignoreValueChange) {
                                setVolume(value)

                                if (!pressed) {
                                    updateTimer.restart()
                                }
                            }
                        }

                        onPressedChanged: {
                            if (!pressed) {
                                // Make sure to sync the volume once the button was
                                // released.
                                // Otherwise it might be that the slider is at v10
                                // whereas PA rejected the volume change and is
                                // still at v15 (e.g.).
                                updateTimer.restart()
                            }
                        }

                        Timer {
                            id: updateTimer
                            interval: 200
                            onTriggered: slider.value = pulseObject.volume
                        }
                    }

                    PlasmaComponents.Label {
                        id: percentText
                        text: i18nc(
                                  "volume percentage", "%1%", Math.floor(
                                      slider.value / slider.maximumValue * 100.0))
                    }
                }
            }
        }
    }

    function setVolume(volume) {
        var device = pulseObject
        if (volume > 0 && muted) {
            var toMute = !device.Muted
            if (toMute) {
                osd.show(0)
            } else {
                osd.show(volumePercent(volume))
            }
            device.Muted = toMute
        }
        device.volume = volume
    }

    function volumePercent(volume) {
        return 100 * volume / slider.maximumValue
    }
}