Commit bd5619e0 authored by Radek Hušek's avatar Radek Hušek
Browse files

[Media controller] Add simple volume control

Summary:
Add simple volume control to Media Controller applet. Volume can by changed by:

- mouse wheel on applet icon (3 % step), and
- new global shortcuts (5 % step, unbound by default).

There is no mute support currently (mpris interface does not have a mute method so we would have to keep
track of old volume).

BUG: 386588

Test Plan: Tested in daily use with Cantata and SMPlayer. There is no UI change.

Reviewers: #plasma, broulik

Reviewed By: #plasma, broulik

Subscribers: broulik, nicolasfella, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D11021
parent 7c9099da
......@@ -155,6 +155,15 @@ Item {
anchors.fill: parent
hoverEnabled: true
acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.BackButton | Qt.ForwardButton
onWheel: {
var service = mpris2Source.serviceForSource(mpris2Source.current)
var operation = service.operationDescription("ChangeVolume")
operation.delta = (wheel.angleDelta.y / 120) * 0.03
operation.showOSD = true
service.startOperationCall(operation)
}
onClicked: {
switch (mouse.button) {
case Qt.MiddleButton:
......
......@@ -47,4 +47,12 @@
<label>The required volume level, as a double between 0 and 1.</label>
</entry>
</group>
<group name="ChangeVolume">
<entry name="delta" type="Double">
<label>The required volume level change, as a double between -1 and 1.</label>
</entry>
<entry name="showOSD" type="Bool">
<label>True is OSD should be shown.</label>
</entry>
</group>
</kcfg>
......@@ -146,5 +146,27 @@ void MultiplexedService::enableGlobalShortcuts()
}
}
);
QAction *volumeupAction = m_actionCollection->addAction(QStringLiteral("mediavolumeup"));
volumeupAction->setText(i18n("Media volume up"));
KGlobalAccel::setGlobalShortcut(volumeupAction, QKeySequence());
connect(volumeupAction, &QAction::triggered, this,
[this] {
if (m_control && m_control->playerInterface()->canControl()) {
m_control->changeVolume(0.05, true);
}
}
);
QAction *volumedownAction = m_actionCollection->addAction(QStringLiteral("mediavolumedown"));
volumedownAction->setText(i18n("Media volume down"));
KGlobalAccel::setGlobalShortcut(volumedownAction, QKeySequence());
connect(volumedownAction, &QAction::triggered, this,
[this] {
if (m_control && m_control->playerInterface()->canControl()) {
m_control->changeVolume(-0.05, true);
}
}
);
}
......@@ -131,6 +131,26 @@ void PlayerActionJob::start()
setError(MissingArgument);
emitResult();
}
} else if (operation == QLatin1String("ChangeVolume")) {
if (parameters().value(QStringLiteral("delta")).type() != QVariant::Double) {
setErrorText(QStringLiteral("delta"));
setError(MissingArgument);
emitResult();
return;
}
if (parameters().value(QStringLiteral("showOSD")).type() != QVariant::Bool) {
setErrorText(QStringLiteral("showOSD"));
setError(MissingArgument);
emitResult();
return;
}
m_controller->changeVolume(
parameters()[QStringLiteral("delta")].toDouble(),
parameters()[QStringLiteral("showOSD")].toBool()
);
setError(NoError);
emitResult();
} else if (operation == QLatin1String("GetPosition")) {
m_controller->updatePosition();
} else {
......
......@@ -63,6 +63,7 @@ void PlayerControl::updateEnabledOperations()
setOperationEnabled(QStringLiteral("SetPosition"), caps & PlayerContainer::CanSeek);
setOperationEnabled(QStringLiteral("OpenUri"), caps & PlayerContainer::CanControl);
setOperationEnabled(QStringLiteral("SetVolume"), caps & PlayerContainer::CanControl);
setOperationEnabled(QStringLiteral("ChangeVolume"), caps & PlayerContainer::CanControl);
setOperationEnabled(QStringLiteral("SetLoopStatus"), caps & PlayerContainer::CanControl);
setOperationEnabled(QStringLiteral("SetRate"), caps & PlayerContainer::CanControl);
setOperationEnabled(QStringLiteral("SetShuffle"), caps & PlayerContainer::CanControl);
......@@ -89,6 +90,31 @@ void PlayerControl::containerDestroyed()
m_container = nullptr;
}
void PlayerControl::changeVolume(double delta, bool showOSD) {
const double volume = playerInterface()->volume();
const double newVolume = qBound(0.0, volume + delta, qMax(volume, 1.0));
playerInterface()->setVolume(newVolume);
if (showOSD) {
const auto& data = m_container->data();
QDBusMessage msg = QDBusMessage::createMethodCall(
QStringLiteral("org.kde.plasmashell"),
QStringLiteral("/org/kde/osdService"),
QStringLiteral("org.kde.osdService"),
QStringLiteral("mediaPlayerVolumeChanged")
);
msg.setArguments({
(int)(100 * newVolume),
data.value("Identity", ""),
data.value("Desktop Icon Name", "")
});
QDBusConnection::sessionBus().asyncCall(msg);
}
}
Plasma::ServiceJob* PlayerControl::createJob(const QString& operation,
QMap<QString,QVariant>& parameters)
{
......
......@@ -49,6 +49,8 @@ public:
Plasma::ServiceJob* createJob(const QString& operation,
QMap<QString,QVariant>& parameters) override;
void changeVolume(double delta, bool showOSD);
Q_SIGNALS:
void enabledOperationsChanged();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment