From b7eb0462cb754bccde3afc53b151a9b6b61e4143 Mon Sep 17 00:00:00 2001
From: luoqing <luoqing@kylinsec.com.cn>
Date: Wed, 3 Jan 2024 13:49:14 +0800
Subject: [PATCH 05/17] fix(audio):listen to mute property change to fix
 shortcut key mute, volume icon unchanged issue
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- 监听mute静音属性变化,以修复快捷键静音,音量图标未变化的问题

Related #25508
---
 .../src/system-tray/audio-system-tray.cpp     | 12 ++++
 .../src/system-tray/volume-setting-page.cpp   | 69 ++++++++++++-------
 .../src/system-tray/volume-setting-page.h     |  5 +-
 3 files changed, 60 insertions(+), 26 deletions(-)

diff --git a/plugins/audio/src/system-tray/audio-system-tray.cpp b/plugins/audio/src/system-tray/audio-system-tray.cpp
index eb554b2..ed4412f 100644
--- a/plugins/audio/src/system-tray/audio-system-tray.cpp
+++ b/plugins/audio/src/system-tray/audio-system-tray.cpp
@@ -124,6 +124,18 @@ void AudioSystemTray::initConnect()
         setTrayIcon(currentVolume);
     });
 
+    connect(m_volumeSettingPage,&VolumeSettingPage::sinkMuteChanged,[this](bool mute,double currentVolume)
+    {
+        if(mute)
+        {
+            setTrayIcon(0);
+        }
+        else
+        {
+            setTrayIcon(currentVolume);
+        }
+    });
+
     connect(Kiran::StylePalette::instance(), &Kiran::StylePalette::themeChanged, [this](Kiran::PaletteType paletteType)
             {
                 //获取当前音量值重新设置TrayIcon
diff --git a/plugins/audio/src/system-tray/volume-setting-page.cpp b/plugins/audio/src/system-tray/volume-setting-page.cpp
index 03116c7..dbb1cd8 100644
--- a/plugins/audio/src/system-tray/volume-setting-page.cpp
+++ b/plugins/audio/src/system-tray/volume-setting-page.cpp
@@ -15,8 +15,8 @@
 #include "dbus/audio-device-interface.h"
 #include "dbus/audio-interface.h"
 #include "dbus/audio-stream-interface.h"
-#include "ui_volume-setting-page.h"
 #include "logging-category.h"
+#include "ui_volume-setting-page.h"
 
 #include <kiran-session-daemon/audio-i.h>
 #include <qt5-log-i.h>
@@ -41,7 +41,9 @@ VolumeSettingPage::VolumeSettingPage(enum AudioNode audio, const QString objectP
         m_sink = new AudioDeviceInterface(AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus(), this);
         initAudioDevice();
 
-        connect(m_sink, &AudioDeviceInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged);
+        connect(m_sink, &AudioDeviceInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged, Qt::UniqueConnection);
+        connect(m_sink, &AudioDeviceInterface::muteChanged, this, &VolumeSettingPage::changeSinkMute, Qt::UniqueConnection);
+
         connect(ui->volumeSetting, &QSlider::valueChanged, [this](int value)
                 {
                     double volumeValue = value / 100.0;
@@ -101,6 +103,7 @@ void VolumeSettingPage::initAudioStream()
     initSettings(m_sinkInput);
     ui->volumeName->setText(m_sinkInput->GetProperty("application.name"));
     connect(m_sinkInput, &AudioStreamInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged);
+    connect(m_sinkInput, &AudioStreamInterface::muteChanged, this, &VolumeSettingPage::changeSinkInputMute);
     connect(ui->volumeSetting, &QSlider::valueChanged, [this](int value)
             {
                 double volumeValue = value / 100.0;
@@ -123,10 +126,38 @@ void VolumeSettingPage::initSettings(Audio *audio)
     ui->volume->setText(QString::number(currentVolume) + "%");
 }
 
+void VolumeSettingPage::changeSinkMute(bool value)
+{
+    KLOG_DEBUG() << "change sink mute:" << value;
+    double currentVolume = m_sink->volume() * 100;
+    emit sinkMuteChanged(value, currentVolume);
+    if (value)
+    {
+        setVolumeIcon(0);
+    }
+    else
+    {
+        setVolumeIcon(currentVolume);
+    }
+}
+void VolumeSettingPage::changeSinkInputMute(bool value)
+{
+    KLOG_DEBUG() << "change sink input mute:" << value;
+    double currentVolume = m_sinkInput->volume() * 100;
+    if (value)
+    {
+        setVolumeIcon(0);
+    }
+    else
+    {
+        setVolumeIcon(currentVolume);
+    }
+}
+
 void VolumeSettingPage::handleVolumeChanged(double value)
 {
-    QSignalBlocker blocker(ui->volumeSetting); // 为了避免拖动的同时设置位置会出现问题
-    int currentVolume = round(value * 100);  // 表示数值的时候向上取整
+    QSignalBlocker blocker(ui->volumeSetting);  // 为了避免拖动的同时设置位置会出现问题
+    int currentVolume = round(value * 100);     // 表示数值的时候向上取整
     ui->volume->setText(QString::number(currentVolume) + "%");
     setVolumeIcon(currentVolume);
     ui->volumeSetting->setValue(currentVolume);
@@ -136,9 +167,9 @@ void VolumeSettingPage::handleVolumeChanged(double value)
 void VolumeSettingPage::handleMuteButtonClicked()
 {
     if (m_audioNode == AUDIO_DEVICE)
-        clickMuteButton(m_sink);
+        switchMute(m_sink);
     else
-        clickMuteButton(m_sinkInput);
+        switchMute(m_sinkInput);
 }
 
 void VolumeSettingPage::handleDefaultSinkChanged(int index)
@@ -154,7 +185,8 @@ void VolumeSettingPage::handleDefaultSinkChanged(int index)
     QDBusPendingReply<QString> defaultSinkPath = m_audioInterface->GetDefaultSink();
     m_sink = new AudioDeviceInterface(AUDIO_DBUS_NAME, defaultSinkPath, QDBusConnection::sessionBus(), this);
     initAudioDevice();
-    connect(m_sink, &AudioDeviceInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged);
+    connect(m_sink, &AudioDeviceInterface::volumeChanged, this, &VolumeSettingPage::handleVolumeChanged, Qt::UniqueConnection);
+    connect(m_sink, &AudioDeviceInterface::muteChanged, this, &VolumeSettingPage::changeSinkMute, Qt::UniqueConnection);
 }
 
 void VolumeSettingPage::handleSinkAdded(int index)
@@ -187,30 +219,17 @@ void VolumeSettingPage::handleSinkDelete(int index)
 }
 
 template <class Audio>
-void VolumeSettingPage::clickMuteButton(Audio *audio)
+void VolumeSettingPage::switchMute(Audio *audio)
 {
-    double currentVolumeDouble = audio->volume() * 100;
-    int currentVolume = round(currentVolumeDouble);
-
-    if (currentVolume != 0)
+    if (!audio->mute())
     {
-        m_volumeBeforeMute = currentVolume;
-        if (!audio->mute())
-        {
-            audio->SetMute(true);
-        }
-        audio->SetVolume(0);
-        KLOG_DEBUG(qLcAudio) << "current sink is mute :" << audio->mute();
+        audio->SetMute(true);
     }
     else
     {
-        if (m_volumeBeforeMute != 0)
-        {
-            //重新设置音量时，会自动解除静音状态
-            audio->SetVolume(m_volumeBeforeMute / 100.0);
-            m_volumeBeforeMute = 0;
-        }
+        audio->SetMute(false);
     }
+    KLOG_DEBUG() << "current defalut sink mute:" << audio->mute();
 }
 
 // XXX:频繁调用函数,需要优化
diff --git a/plugins/audio/src/system-tray/volume-setting-page.h b/plugins/audio/src/system-tray/volume-setting-page.h
index 80f0098..4a556fe 100644
--- a/plugins/audio/src/system-tray/volume-setting-page.h
+++ b/plugins/audio/src/system-tray/volume-setting-page.h
@@ -41,6 +41,8 @@ public:
     void disableSettings();
 
 public slots:
+    void changeSinkMute(bool value);
+    void changeSinkInputMute(bool value);
     void handleVolumeChanged(double value);
     void handleMuteButtonClicked();
     void handleDefaultSinkChanged(int index);
@@ -57,10 +59,11 @@ private:
     template <class Audio>
     void initSettings(Audio *audio);
     template <class Audio>
-    void clickMuteButton(Audio *audio);
+    void switchMute(Audio *audio);
 
 signals:
     void volumeChanged(double value);
+    void sinkMuteChanged(bool mute, double currentVolume);
 
 private:
     Ui::VolumeSettingPage *ui;
-- 
2.33.0

