Skip to content

Commit

Permalink
feat: Mouse wheel click to play/pause and scroll to adjust volume
Browse files Browse the repository at this point in the history
Also go previous/next with back/forward mouse buttons
Allow to scroll the volume bar to adjust volume

Closes: ccatterina#115
  • Loading branch information
luisbocanegra committed Jul 22, 2024
1 parent 4fd8bae commit e1bf6ac
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/contents/ui/Player.qml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ QtObject {
mpris2Model.currentPlayer.volume = volume
}

function changeVolume(delta, showOSD) {
mpris2Model.currentPlayer.changeVolume(delta, showOSD);
}

function setShuffle(shuffle) {
mpris2Model.currentPlayer.shuffle = shuffle
}
Expand Down
16 changes: 14 additions & 2 deletions src/contents/ui/VolumeBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,23 @@ Item {
height: parent.height + 8
width: parent.width
cursorShape: Qt.PointingHandCursor
property int wheelDelta: 0
onClicked: ({x}) => {
container.changeVolume(x/parent.width)
player.setVolume(x/parent.width)
}
onPositionChanged: (mouse) => {
if (pressed) container.changeVolume(mouse.x/parent.width)
if (pressed) player.setVolume(mouse.x/parent.width)
}
onWheel: (wheel) => {
wheelDelta += (wheel.inverted ? -1 : 1) * (wheel.angleDelta.y ? wheel.angleDelta.y : -wheel.angleDelta.x)
while (wheelDelta >= 120) {
wheelDelta -= 120;
player.changeVolume(volumeStep / 100, false);
}
while (wheelDelta <= -120) {
wheelDelta += 120;
player.changeVolume(-volumeStep / 100, false);
}
}
}

Expand Down
50 changes: 45 additions & 5 deletions src/contents/ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.kde.plasma.plasmoid
import org.kde.plasma.components as PlasmaComponents3
import org.kde.kirigami as Kirigami
import org.kde.plasma.private.mpris as Mpris
import org.kde.plasma.private.mediacontroller 1.0

PlasmoidItem {
id: widget
Expand All @@ -15,8 +16,14 @@ PlasmoidItem {
return plasmoid.configuration.useCustomFont ? plasmoid.configuration.customFont : Kirigami.Theme.defaultFont
}
readonly property font boldTextFont: Qt.font(Object.assign({}, textFont, {weight: Font.Bold}))
readonly property int volumeStep: config.volumeStep

toolTipSubText: player.canRaise ? i18n("Ctrl+Click to bring player to the front") : i18n("This player can't be raised")
toolTipSubText: {
let text = player.playbackStatus === Mpris.PlaybackStatus.Playing ? i18n("Middle-click to pause") : i18n("Middle-click to play")
text += "\n" + i18n("Scroll to adjust volume")
text += "\n" + (player.canRaise ? i18n("Ctrl+Click to bring player to the front") : i18n("This player can't be raised"))
return text
}

Player {
id: player
Expand All @@ -27,6 +34,10 @@ PlasmoidItem {
}
}

GlobalConfig {
id: config
}

compactRepresentation: Item {
id: compact

Expand All @@ -44,12 +55,41 @@ PlasmoidItem {

MouseArea {
anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.BackButton | Qt.ForwardButton
propagateComposedEvents: true
property int wheelDelta: 0
onClicked: (mouse) => {
if (mouse.modifiers & Qt.ControlModifier) {
if (player.canRaise) player.raise()
} else {
mouse.accepted = false
switch (mouse.button) {
case Qt.MiddleButton:
player.playPause()
break
case Qt.BackButton:
if (player.canGoPrevious) {
player.previous();
}
break
case Qt.ForwardButton:
if (player.canGoNext) {
player.next();
}
break
default:
if (mouse.modifiers & Qt.ControlModifier) {
if (player.canRaise) player.raise()
} else {
mouse.accepted = false
}
}
}
onWheel: (wheel) => {
wheelDelta += (wheel.inverted ? -1 : 1) * (wheel.angleDelta.y ? wheel.angleDelta.y : -wheel.angleDelta.x)
while (wheelDelta >= 120) {
wheelDelta -= 120;
player.changeVolume(volumeStep / 100, true);
}
while (wheelDelta <= -120) {
wheelDelta += 120;
player.changeVolume(-volumeStep / 100, true);
}
}
z: 999
Expand Down

0 comments on commit e1bf6ac

Please sign in to comment.