From 2c6ae961ff33c45c9d9d7758715af3b52d2db052 Mon Sep 17 00:00:00 2001
From: luoqing <luoqing@kylinsec.com.cn>
Date: Thu, 7 Sep 2023 19:21:39 +0800
Subject: [PATCH] fix(audio-tray):Fixed an issue where the volume tray icon did
 not change with the volume after the default Sink was changed
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 修复默认Sink变更后，音量托盘图标不随音量大小改变的问题

Closed #14124
---
 .../src/system-tray/audio-system-tray.cpp     | 27 +++++++++++--------
 .../audio/src/system-tray/audio-system-tray.h |  1 -
 .../src/system-tray/volume-setting-page.cpp   |  2 ++
 .../src/system-tray/volume-setting-page.h     | 22 ++++++++-------
 4 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/plugins/audio/src/system-tray/audio-system-tray.cpp b/plugins/audio/src/system-tray/audio-system-tray.cpp
index 00e90f7..0c6aca6 100644
--- a/plugins/audio/src/system-tray/audio-system-tray.cpp
+++ b/plugins/audio/src/system-tray/audio-system-tray.cpp
@@ -41,7 +41,6 @@ AudioSystemTray::AudioSystemTray(QWidget *parent) : QWidget(parent)
     initVolumeSettingPage(defaultSinkPath);
     initMixedSettingPage();
 
-    m_sink = new AudioDeviceInterface(AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus());
     m_statusNotifierManager = new StatusNotifierManagerInterface(STATUS_NOTIFIER_MANAGER, STATUS_NOTIFIER_MANAGER_OBJECT_NAME, QDBusConnection::sessionBus(), this);
     m_systemTray = new QSystemTrayIcon();
 
@@ -82,8 +81,11 @@ void AudioSystemTray::initMixedSettingPage()
 void AudioSystemTray::initTrayIcon()
 {
     getTrayIconStyle();
-    double currentVolumeDouble = m_sink->volume() * 100;
-    KLOG_INFO() << "currentVolumeDouble" << round(currentVolumeDouble);
+    
+    QDBusPendingReply<QString> defaultSinkPath = m_audioInterface->GetDefaultSink();
+    AudioDeviceInterface defaultSink (AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus());
+    double currentVolumeDouble = defaultSink.volume() * 100;
+    KLOG_INFO() << "current Volume Double" << round(currentVolumeDouble);
     setTrayIcon(round(currentVolumeDouble));
 }
 
@@ -117,11 +119,12 @@ void AudioSystemTray::initConnect()
 {
     connect(m_systemTray, &QSystemTrayIcon::activated, this, &AudioSystemTray::handleAudioTrayClicked);
 
-    connect(m_sink, &AudioDeviceInterface::volumeChanged, [=](double value)
-            {
-                int currentVolume = round(value * 100);  //表示数值的时候向上取整
-                KLOG_DEBUG() << "m_sink volumeChanged :" << currentVolume;
-                setTrayIcon(currentVolume); });
+    connect(m_volumeSettingPage,&VolumeSettingPage::volumeChanged,[=](double value)
+    {
+        int currentVolume = round(value * 100);  //表示数值的时候向上取整
+        KLOG_DEBUG() << "m_sink volumeChanged :" << currentVolume;
+        setTrayIcon(currentVolume);
+    });
 
     connect(m_statusNotifierManager, &StatusNotifierManagerInterface::StyleChanged, [=](const QString &style)
             {
@@ -129,9 +132,11 @@ void AudioSystemTray::initConnect()
                 //重新获取style
                 getTrayIconStyle();
                 //获取当前音量值重新设置TrayIcon
-                m_sink->volume();
-                double currentVolumeDouble = m_sink->volume() * 100;
-                setTrayIcon(round(currentVolumeDouble)); });
+                QDBusPendingReply<QString> defaultSinkPath = m_audioInterface->GetDefaultSink();
+                AudioDeviceInterface defaultSink (AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus());
+                double currentVolumeDouble = defaultSink.volume() * 100;
+                setTrayIcon(round(currentVolumeDouble)); 
+            });
 }
 
 void AudioSystemTray::handleAudioTrayClicked(QSystemTrayIcon::ActivationReason reason)
diff --git a/plugins/audio/src/system-tray/audio-system-tray.h b/plugins/audio/src/system-tray/audio-system-tray.h
index 16846e6..a61284e 100644
--- a/plugins/audio/src/system-tray/audio-system-tray.h
+++ b/plugins/audio/src/system-tray/audio-system-tray.h
@@ -68,7 +68,6 @@ private:
 
     StatusNotifierManagerInterface* m_statusNotifierManager;
     AudioInterface* m_audioInterface;
-    AudioDeviceInterface* m_sink;
 
     QString m_colorTheme;
     int xTray, yTray, heightTray, widthTray;
diff --git a/plugins/audio/src/system-tray/volume-setting-page.cpp b/plugins/audio/src/system-tray/volume-setting-page.cpp
index fac69a6..54b8f10 100644
--- a/plugins/audio/src/system-tray/volume-setting-page.cpp
+++ b/plugins/audio/src/system-tray/volume-setting-page.cpp
@@ -132,6 +132,7 @@ void VolumeSettingPage::handleVolumeChanged(double value)
     setVolumeIcon(currentVolume);
     ui->volumeSetting->setValue(currentVolume);
     ui->volumeSetting->blockSignals(false);
+    emit volumeChanged(value);
 }
 
 void VolumeSettingPage::handleMuteButtonClicked()
@@ -144,6 +145,7 @@ void VolumeSettingPage::handleMuteButtonClicked()
 
 void VolumeSettingPage::handleDefaultSinkChanged(int index)
 {
+    KLOG_DEBUG() << "Default Sink Changed";
     // delete and restart init defaultSink
     if (m_sink != nullptr)
     {
diff --git a/plugins/audio/src/system-tray/volume-setting-page.h b/plugins/audio/src/system-tray/volume-setting-page.h
index 4719021..80f0098 100644
--- a/plugins/audio/src/system-tray/volume-setting-page.h
+++ b/plugins/audio/src/system-tray/volume-setting-page.h
@@ -40,16 +40,6 @@ public:
     QPixmap trayIconColorSwitch(const QString &iconPath);
     void disableSettings();
 
-private:
-    void initDbusServiceWatcher();
-    void initAudioDevice();
-    void initAudioStream();
-    template <class Audio>
-    void initSettings(Audio *audio);
-    template <class Audio>
-    void clickMuteButton(Audio *audio);
-   
-
 public slots:
     void handleVolumeChanged(double value);
     void handleMuteButtonClicked();
@@ -60,6 +50,18 @@ public slots:
     void setVolumeIcon(int value);
     void hideLine();
 
+private:
+    void initDbusServiceWatcher();
+    void initAudioDevice();
+    void initAudioStream();
+    template <class Audio>
+    void initSettings(Audio *audio);
+    template <class Audio>
+    void clickMuteButton(Audio *audio);
+
+signals:
+    void volumeChanged(double value);
+
 private:
     Ui::VolumeSettingPage *ui;
     AudioInterface *m_audioInterface;
-- 
2.33.0

