| 1 |
/* |
| 2 |
* Audacity: A Digital Audio Editor |
| 3 |
*/ |
| 4 |
import QtQuick 2.15 |
| 5 |
import QtQuick.Controls 2.15 |
| 6 |
|
| 7 |
import Muse.Ui 1.0 |
| 8 |
import Muse.UiComponents 1.0 |
| 9 |
|
| 10 |
Slider { |
| 11 |
id: root |
| 12 |
|
| 13 |
property real volumeLevel: 0.0 |
| 14 |
property real readableVolumeLevel: Math.round(root.volumeLevel * 10) / 10 |
| 15 |
|
| 16 |
property alias navigation: navCtrl |
| 17 |
|
| 18 |
readonly property real handleWidth: 16 |
| 19 |
readonly property real handleHeight: handleWidth |
| 20 |
|
| 21 |
signal volumeLevelMoved(var level) |
| 22 |
|
| 23 |
from: -60 |
| 24 |
to: 0 |
| 25 |
value: root.volumeLevel |
| 26 |
stepSize: 0.1 |
| 27 |
orientation: Qt.Horizontal |
| 28 |
wheelEnabled: true |
| 29 |
|
| 30 |
width: parent.width |
| 31 |
height: handleWidth |
| 32 |
|
| 33 |
opacity: enabled ? 1.0 : ui.theme.itemOpacityDisabled |
| 34 |
|
| 35 |
signal increaseRequested() |
| 36 |
signal decreaseRequested() |
| 37 |
|
| 38 |
QtObject { |
| 39 |
id: prv |
| 40 |
|
| 41 |
readonly property real rulerLineWidth: root.width - root.handleWidth |
| 42 |
|
| 43 |
readonly property int rulerYPos: root.height / 2 |
| 44 |
|
| 45 |
property real dragStartOffset: 0.0 |
| 46 |
} |
| 47 |
|
| 48 |
NavigationControl { |
| 49 |
id: navCtrl |
| 50 |
name: root.objectName !== "" ? root.objectName : "VolumeSlider" |
| 51 |
enabled: root.enabled && root.visible |
| 52 |
|
| 53 |
accessible.role: MUAccessible.Range |
| 54 |
accessible.visualItem: root |
| 55 |
|
| 56 |
accessible.value: root.readableVolumeLevel |
| 57 |
accessible.minimumValue: root.from |
| 58 |
accessible.maximumValue: root.to |
| 59 |
accessible.stepSize: root.stepSize |
| 60 |
|
| 61 |
onNavigationEvent: function(event) { |
| 62 |
switch(event.type) { |
| 63 |
case NavigationEvent.Left: |
| 64 |
root.decreaseRequested() |
| 65 |
event.accepted = true |
| 66 |
break |
| 67 |
case NavigationEvent.Right: |
| 68 |
root.increaseRequested() |
| 69 |
event.accepted = true |
| 70 |
break |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
background: Item { |
| 76 |
id: bgCanvas |
| 77 |
|
| 78 |
height: root.height |
| 79 |
width: root.width |
| 80 |
|
| 81 |
NavigationFocusBorder { |
| 82 |
navigationCtrl: navCtrl |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
handle: Item { |
| 87 |
x: root.position * prv.rulerLineWidth |
| 88 |
y: prv.rulerYPos - root.handleHeight / 2 |
| 89 |
implicitWidth: root.handleWidth |
| 90 |
implicitHeight: root.handleHeight |
| 91 |
|
| 92 |
MouseArea { |
| 93 |
anchors.fill: parent |
| 94 |
|
| 95 |
onDoubleClicked: { |
| 96 |
// Double click resets the volume |
| 97 |
root.volumeLevelMoved(0.0) |
| 98 |
} |
| 99 |
|
| 100 |
// The MouseArea steals mouse press events from the slider. |
| 101 |
// There is really no way to prevent that. |
| 102 |
// (if you set mouse.accepted to false in the onPressed handler, |
| 103 |
// the MouseArea won't receive doubleClick events). |
| 104 |
// So we use this workaround. |
| 105 |
|
| 106 |
preventStealing: true // Don't let a Flickable steal the mouse |
| 107 |
|
| 108 |
onPressed: function(mouse) { |
| 109 |
prv.dragStartOffset = mouse.x |
| 110 |
} |
| 111 |
|
| 112 |
onPositionChanged: function(mouse) { |
| 113 |
let mousePosInRoot = mapToItem(root, mouse.x - prv.dragStartOffset, 0).x |
| 114 |
let newPosZeroToOne = mousePosInRoot / prv.rulerLineWidth |
| 115 |
|
| 116 |
let newPosClamped = Math.max(0.0, Math.min(newPosZeroToOne, 1.0)) |
| 117 |
let localNewValue = root.valueAt(newPosClamped) |
| 118 |
root.volumeLevelMoved(localNewValue) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
Rectangle { |
| 123 |
id: handleRect |
| 124 |
anchors.fill: parent |
| 125 |
radius: width / 2 |
| 126 |
color: "transparent" |
| 127 |
border.color: ui.theme.fontPrimaryColor |
| 128 |
|
| 129 |
Rectangle { |
| 130 |
anchors.fill: parent |
| 131 |
anchors.margins: 1 |
| 132 |
radius: width / 2 |
| 133 |
color: ui.theme.backgroundPrimaryColor |
| 134 |
opacity: 0.3 |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
onMoved: { |
| 141 |
navigation.requestActiveByInteraction() |
| 142 |
root.volumeLevelMoved(value) |
| 143 |
} |
| 144 |
} |