volumeosd: Listen to osd service for opening the popup

Fixes: https://invent.kde.org/plasma/plasma-mobile/-/issues/423

We currently listen to when the volume changes, which wouldn't happen if
the user reaches 0% or 100% and continues trying to press the volume
button. Instead, listen to OSD events for determining when to open the
popup.
This commit is contained in:
Devin Lin 2025-09-13 08:54:11 -04:00
parent c3e1bd607b
commit 447049d570
5 changed files with 80 additions and 12 deletions

View file

@ -6,6 +6,7 @@ pragma Singleton
import QtQuick
import org.kde.plasma.private.volume
import org.kde.plasma.private.mobileshell as MobileShell
QtObject {
id: root
@ -100,21 +101,16 @@ QtObject {
return icon;
}
// emitted when the volume changed, but not due to sink switching
signal volumeChanged()
property var updateVolume: Connections {
target: root.paSinkModel ? (PreferredDevice.sink ? PreferredDevice.sink : null) : null
enabled: target !== null
function onVolumeChanged() {
root.volumeValue = root.volumePercent(PreferredDevice.sink.volume, root.maxVolumeValue);
root.volumeChanged();
}
function onMutedChanged() {
root.volumeValue = PreferredDevice.sink.muted ? 0 : root.volumePercent(PreferredDevice.sink.volume, root.maxVolumeValue);
root.volumeChanged();
}
}

View file

@ -19,14 +19,14 @@ import org.kde.plasma.private.mobileshell as MobileShell
QtObject {
id: component
function showVolumeOverlay() {
property var osdListener: Connections {
target: MobileShellState.VolumeOSDListener
function onShowOSD(icon, volume, maxVolume) {
if (!osd.visible) {
vcp.showOverlay();
}
}
Component.onCompleted: {
MobileShell.AudioInfo.volumeChanged.connect(showVolumeOverlay);
}
property var apiListener: Connections {

View file

@ -7,6 +7,7 @@ set(mobileshellstateplugin_SRCS
lockscreendbusclient.cpp
startupfeedbackmodel.cpp
windowlistener.cpp
volumeosdlistener.cpp
)
qt_generate_dbus_interface(

View file

@ -0,0 +1,45 @@
// SPDX-FileCopyrightText: 2025 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "volumeosdlistener.h"
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusServiceWatcher>
#include <QDebug>
#include <QString>
using namespace Qt::StringLiterals;
VolumeOSDListener::VolumeOSDListener(QObject *parent)
: QObject{parent}
{
QDBusServiceWatcher *watcher =
new QDBusServiceWatcher(QStringLiteral("org.kde.plasmashell"), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, [this]() -> void {
connectDBus();
});
connectDBus();
}
void VolumeOSDListener::connectDBus()
{
bool success = QDBusConnection::sessionBus().connect(QStringLiteral("org.kde.plasmashell"),
QStringLiteral("/org/kde/osdService"),
QStringLiteral("org.kde.osdService"),
QStringLiteral("osdProgress"),
this,
SLOT(onOSDProgress(QString, int, int, QString)));
}
void VolumeOSDListener::onOSDProgress(const QString &icon, int volume, int maxVolume, const QString &text)
{
Q_UNUSED(text)
if (icon == "audio-volume-muted"_L1 || icon == "audio-volume-low"_L1 || icon == "audio-volume-medium"_L1 || icon == "audio-volume-high"_L1) {
Q_EMIT showOSD(icon, volume, maxVolume);
}
}

View file

@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: 2025 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <QObject>
#include <qqmlregistration.h>
class VolumeOSDListener : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
public:
VolumeOSDListener(QObject *parent = nullptr);
Q_SIGNALS:
void showOSD(const QString &icon, int volume, int maxVolume);
private Q_SLOTS:
void onOSDProgress(const QString &icon, int volume, int maxVolume, const QString &text);
private:
void connectDBus();
};