diff --git a/components/mobileshell/qml/dataproviders/AudioInfo.qml b/components/mobileshell/qml/dataproviders/AudioInfo.qml index ed73e2cf..08e7c336 100644 --- a/components/mobileshell/qml/dataproviders/AudioInfo.qml +++ b/components/mobileshell/qml/dataproviders/AudioInfo.qml @@ -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(); } } diff --git a/components/mobileshell/qml/popups/volumeosd/VolumeOSDProvider.qml b/components/mobileshell/qml/popups/volumeosd/VolumeOSDProvider.qml index 1f77d3e0..78a22775 100644 --- a/components/mobileshell/qml/popups/volumeosd/VolumeOSDProvider.qml +++ b/components/mobileshell/qml/popups/volumeosd/VolumeOSDProvider.qml @@ -19,14 +19,14 @@ import org.kde.plasma.private.mobileshell as MobileShell QtObject { id: component - function showVolumeOverlay() { - if (!osd.visible) { - vcp.showOverlay(); - } - } + property var osdListener: Connections { + target: MobileShellState.VolumeOSDListener - Component.onCompleted: { - MobileShell.AudioInfo.volumeChanged.connect(showVolumeOverlay); + function onShowOSD(icon, volume, maxVolume) { + if (!osd.visible) { + vcp.showOverlay(); + } + } } property var apiListener: Connections { diff --git a/components/mobileshellstate/CMakeLists.txt b/components/mobileshellstate/CMakeLists.txt index bd113006..32bd87a0 100644 --- a/components/mobileshellstate/CMakeLists.txt +++ b/components/mobileshellstate/CMakeLists.txt @@ -7,6 +7,7 @@ set(mobileshellstateplugin_SRCS lockscreendbusclient.cpp startupfeedbackmodel.cpp windowlistener.cpp + volumeosdlistener.cpp ) qt_generate_dbus_interface( diff --git a/components/mobileshellstate/volumeosdlistener.cpp b/components/mobileshellstate/volumeosdlistener.cpp new file mode 100644 index 00000000..30ebe759 --- /dev/null +++ b/components/mobileshellstate/volumeosdlistener.cpp @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2025 Devin Lin +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "volumeosdlistener.h" + +#include +#include +#include +#include +#include +#include + +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); + } +} diff --git a/components/mobileshellstate/volumeosdlistener.h b/components/mobileshellstate/volumeosdlistener.h new file mode 100644 index 00000000..1a294f5e --- /dev/null +++ b/components/mobileshellstate/volumeosdlistener.h @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2025 Devin Lin +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include + +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(); +};