From 9af85fe32ccecd148256b54686cbfca147168b6a Mon Sep 17 00:00:00 2001 From: chenyuanbo Date: Tue, 24 Nov 2026 14:24:16 +0800 Subject: [PATCH] fix(osd): speed up Super+P display-mode checkmarks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Dispatch OSD requests immediately by removing the fixed 100 ms delay. 2. Read display state on open. Advance the checkmark from the cached list on repeat presses and apply the final selection after two seconds. 3. Retry failed list reads. Start from the first item if the active mode or primary output is unknown. 4. Update audio OSD from Volume and Mute notifications. Refresh volume, mute and the volume boost setting when the default output changes. Log: Improve Super+P checkmark response and refresh audio OSD on changes Influence: 1. Verify the first Super+P press shows the active display mode. 2. Hold Super and tap P repeatedly. Verify the checkmark advances and only the final selection is applied after two seconds. 3. Verify recovery after a failed display list or mode read. 4. Test volume up from zero, mute and switching the default audio output while the OSD is visible. Check the progress bar and 100% mark. fix(osd): 加快 Super+P 切换显示模式时的勾选更新 1. 移除 OSD 固定的 100 ms 延时,收到请求后立即派发。 2. 首次打开读取显示状态,连按时用缓存列表推进勾选, 停按两秒后应用最后选中的模式。 3. 候选列表读取失败时重试,当前模式或主屏未知时从首项开始轮选。 4. 根据音量和静音通知补刷音量 OSD,默认输出切换时同时刷新 音量、静音和音量增强设置。 Influence: 1. 首按 Super+P,确认面板勾选当前模式。 2. 按住 Super 连按 P,确认勾选逐次推进,停按两秒只应用最后一项。 3. 验证候选列表或实际模式读取失败后仍能恢复轮选。 4. 验证音量从零回升、静音和面板可见时切换默认输出, 检查进度条和 100% 刻度是否正确。 Log: 加快 Super+P 勾选更新,并在音量状态变化后补刷面板 PMS: BUG-383567 --- panels/notification/osd/audio/audioapplet.cpp | 124 +++++++++++++++--- panels/notification/osd/audio/audioapplet.h | 19 ++- .../osd/displaymode/displaymodeapplet.cpp | 28 ++-- .../osd/displaymode/displaymodeapplet.h | 4 +- .../osd/displaymode/package/main.qml | 23 ++-- panels/notification/osd/osdpanel.cpp | 22 ++-- panels/notification/osd/osdpanel.h | 3 +- 7 files changed, 167 insertions(+), 56 deletions(-) diff --git a/panels/notification/osd/audio/audioapplet.cpp b/panels/notification/osd/audio/audioapplet.cpp index 4105e2709..10f06d140 100644 --- a/panels/notification/osd/audio/audioapplet.cpp +++ b/panels/notification/osd/audio/audioapplet.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -7,6 +7,7 @@ #include "pluginfactory.h" #include +#include #include #include @@ -14,30 +15,51 @@ namespace osd { +const QString audioService = QStringLiteral("org.deepin.dde.Audio1"); +const QString audioPath = QStringLiteral("/org/deepin/dde/Audio1"); +const QString audioSinkInterface = QStringLiteral("org.deepin.dde.Audio1.Sink"); + static DDBusSender audioInter() { - return DDBusSender().service("org.deepin.dde.Audio1") - .path("/org/deepin/dde/Audio1") - .interface("org.deepin.dde.Audio1"); + return DDBusSender().service(audioService) + .path(audioPath) + .interface(audioService); } -static DDBusSender audioSinkInter() +static DDBusSender audioSinkInter(const QString &sinkPath) +{ + return DDBusSender().service(audioService) + .path(sinkPath) + .interface(audioSinkInterface); +} + +static std::optional fetchDefaultSinkPath() { QDBusReply defaultSink = audioInter().property("DefaultSink").get(); if (!defaultSink.isValid()) { qWarning() << "Failed to fetch DefaultSink" << defaultSink.error(); - return DDBusSender(); + return std::nullopt; } - const auto path = qdbus_cast(defaultSink); - return DDBusSender().service("org.deepin.dde.Audio1") - .path(path.path()) - .interface("org.deepin.dde.Audio1.Sink"); + return qdbus_cast(defaultSink).path(); } AudioApplet::AudioApplet(QObject *parent) : DApplet(parent) { +} + +bool AudioApplet::load() +{ + if (!DApplet::load()) + return false; + + // Subscribe before reading the initial state so changes during sync are delivered as well. + QDBusConnection::sessionBus().connect( + audioService, QString(), + QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("PropertiesChanged"), + this, SLOT(onAudioPropertiesChanged(QString,QVariantMap,QStringList,QDBusMessage))); + return true; } bool AudioApplet::increaseVolume() const @@ -55,21 +77,85 @@ void AudioApplet::setIncreaseVolume(bool newIncreaseVolume) void AudioApplet::sync() { - setVolumeValue(fetchVolume()); + if (const auto sinkPath = fetchDefaultSinkPath()) + updateDefaultSink(*sinkPath); +} +void AudioApplet::updateDefaultSink(const QString &sinkPath) +{ + m_sinkPath = sinkPath; + readSinkState(true, true); setIncreaseVolume(fetchIncreaseVolume()); + setIconName(fetchIconName()); +} +void AudioApplet::readSinkState(bool readVolume, bool readMute) +{ + if (!readVolume && !readMute) + return; + + if (m_sinkPath.isEmpty() || m_sinkPath == QStringLiteral("/")) { + m_muted.reset(); + setVolumeValue(0.0); + return; + } + + auto sink = audioSinkInter(m_sinkPath); + if (readMute) { + const QDBusReply reply = sink.property("Mute").get(); + if (reply.isValid()) { + m_muted = qdbus_cast(reply); + } else { + qWarning() << "Failed to fetch Mute" << reply.error(); + m_muted.reset(); + } + } + if (readVolume) + setVolumeValue(fetchVolume(sink)); +} + +void AudioApplet::onAudioPropertiesChanged(const QString &interface, const QVariantMap &changed, + const QStringList &invalidated, const QDBusMessage &message) +{ + if (interface == audioService && message.path() == audioPath) { + const auto defaultSink = changed.constFind(QStringLiteral("DefaultSink")); + if (defaultSink != changed.cend()) { + updateDefaultSink(qdbus_cast(*defaultSink).path()); + } else if (invalidated.contains(QStringLiteral("DefaultSink"))) { + sync(); + } + return; + } + + if (interface != audioSinkInterface || message.path() != m_sinkPath) + return; + + const auto volume = changed.constFind(QStringLiteral("Volume")); + const auto mute = changed.constFind(QStringLiteral("Mute")); + const bool hasVolume = volume != changed.cend(); + const bool hasMute = mute != changed.cend(); + const bool readVolume = !hasVolume && invalidated.contains(QStringLiteral("Volume")); + const bool readMute = !hasMute && invalidated.contains(QStringLiteral("Mute")); + if (!hasVolume && !hasMute && !readVolume && !readMute) + return; + + if (hasMute) + m_muted = mute->toBool(); + + if (hasVolume) + setVolumeValue(volume->toDouble()); + + readSinkState(readVolume, readMute); setIconName(fetchIconName()); } QString AudioApplet::fetchIconName() const { - QDBusReply muteReply = audioSinkInter().property("Mute").get(); - if (!muteReply.isValid()) { - qWarning() << "Failed to fetch Mute" << muteReply.error(); - return QString(); + if (!m_muted.has_value()) { + return {}; } - if (qdbus_cast(muteReply)) { + + if (*m_muted) { return "osd_volume_mute"; } @@ -88,11 +174,11 @@ QString AudioApplet::fetchIconName() const return QString("osd_volume_%1").arg(level); } -double AudioApplet::fetchVolume() const +double AudioApplet::fetchVolume(DDBusSender &sink) const { - QDBusReply volumeReply = audioSinkInter().property("Volume").get(); + QDBusReply volumeReply = sink.property("Volume").get(); if (!volumeReply.isValid()) { - qWarning() << "Failed to Volume Volume" << volumeReply.error(); + qWarning() << "Failed to fetch Volume" << volumeReply.error(); return 0.0; } diff --git a/panels/notification/osd/audio/audioapplet.h b/panels/notification/osd/audio/audioapplet.h index 7e4e5ae85..700029c38 100644 --- a/panels/notification/osd/audio/audioapplet.h +++ b/panels/notification/osd/audio/audioapplet.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -6,6 +6,12 @@ #include "applet.h" +#include +#include + +class QDBusMessage; +class DDBusSender; + namespace osd { class AudioApplet : public DS_NAMESPACE::DApplet @@ -16,6 +22,7 @@ class AudioApplet : public DS_NAMESPACE::DApplet Q_PROPERTY(bool increaseVolume READ increaseVolume WRITE setIncreaseVolume NOTIFY increaseVolumeChanged FINAL) public: explicit AudioApplet(QObject *parent = nullptr); + bool load() override; double volumeValue() const; void setVolumeValue(double newVolumeValue); @@ -37,13 +44,21 @@ class AudioApplet : public DS_NAMESPACE::DApplet void volumeRatioChanged(); void increaseVolumeChanged(); +private Q_SLOTS: + void onAudioPropertiesChanged(const QString &interface, const QVariantMap &changed, + const QStringList &invalidated, const QDBusMessage &message); + private: + void updateDefaultSink(const QString &sinkPath); + void readSinkState(bool readVolume, bool readMute); QString fetchIconName() const; - double fetchVolume() const; + double fetchVolume(DDBusSender &sink) const; bool fetchIncreaseVolume() const; private: double m_increaseVolume = 1.0; double m_volumeValue = 1.0; + QString m_sinkPath; + std::optional m_muted; QString m_iconName; double m_volumeRatio; }; diff --git a/panels/notification/osd/displaymode/displaymodeapplet.cpp b/panels/notification/osd/displaymode/displaymodeapplet.cpp index ea52653fe..ca661eae6 100644 --- a/panels/notification/osd/displaymode/displaymodeapplet.cpp +++ b/panels/notification/osd/displaymode/displaymodeapplet.cpp @@ -113,20 +113,25 @@ QQmlListProperty DisPlayModeApplet::planItems() void DisPlayModeApplet::sync() { - if (!m_actionTimer->isActive()) { - fetchPlanItems(); - auto current = fetchCurrentPlanItem(); - setCurrentPlanItem(current); - Q_EMIT planItemsChanged(); - } - auto state = fetchState(); + if (!m_actionTimer->isActive()) + refreshDisplayState(); + + setState(fetchState()); +} - setState(state); +void DisPlayModeApplet::refreshDisplayState() +{ + m_planItemsValid = fetchPlanItems(); + setCurrentPlanItem(m_planItemsValid ? fetchCurrentPlanItem() : nullptr); + Q_EMIT planItemsChanged(); } void DisPlayModeApplet::next() { - if (m_planItems.isEmpty()) + if (!m_planItemsValid) + refreshDisplayState(); + + if (!m_planItemsValid || m_planItems.isEmpty()) return; const auto nextIndex = (m_planItems.indexOf(m_currentPlanItem) + 1) % m_planItems.size(); @@ -159,7 +164,7 @@ void DisPlayModeApplet::setCurrentPlanItem(DPItem *newCurrentPlanItem) emit currentPlanItemChanged(); } -void DisPlayModeApplet::fetchPlanItems() +bool DisPlayModeApplet::fetchPlanItems() { setCurrentPlanItem(nullptr); qDeleteAll(m_planItems); @@ -168,7 +173,7 @@ void DisPlayModeApplet::fetchPlanItems() QDBusReply listOutputNames = displayInter().method("ListOutputNames").call(); if (!listOutputNames.isValid()) { qCWarning(osdDPLog) << "Failed to fetch ListOutputNames" << listOutputNames.error(); - return; + return false; } auto outputNames = listOutputNames.value(); @@ -184,6 +189,7 @@ void DisPlayModeApplet::fetchPlanItems() m_planItems << new DPItem(tr("Only on %1").arg(item), item, iconName, DPItem::Single, this); } } + return true; } DPItem *DisPlayModeApplet::fetchCurrentPlanItem() const diff --git a/panels/notification/osd/displaymode/displaymodeapplet.h b/panels/notification/osd/displaymode/displaymodeapplet.h index c99986891..b5e613d3a 100644 --- a/panels/notification/osd/displaymode/displaymodeapplet.h +++ b/panels/notification/osd/displaymode/displaymodeapplet.h @@ -74,13 +74,15 @@ class DisPlayModeApplet : public DS_NAMESPACE::DApplet private: void setState(int newState); void setCurrentPlanItem(DPItem *newCurrentPlanItem); - void fetchPlanItems(); + void refreshDisplayState(); + bool fetchPlanItems(); DPItem *fetchCurrentPlanItem() const; int fetchState() const; private: QList m_planItems; DPItem *m_currentPlanItem = nullptr; + bool m_planItemsValid = false; int m_state; QTimer *m_actionTimer = nullptr; Dtk::Core::DConfig *m_osdConfig = nullptr; diff --git a/panels/notification/osd/displaymode/package/main.qml b/panels/notification/osd/displaymode/package/main.qml index df1d933f9..1f1e6a20a 100644 --- a/panels/notification/osd/displaymode/package/main.qml +++ b/panels/notification/osd/displaymode/package/main.qml @@ -18,22 +18,19 @@ AppletItem { function update(osdType) { - Applet.sync() - if (match(osdType)) { - - if (osdType === "DirectSwitchLayout") { - if (Applet.state !== 2) { - Applet.doAction() - } - } else if (osdType === "SwitchMonitors") { - if (Panel.lastOsdType() === osdType) { - Applet.next() - } - } + if (!match(osdType)) + return false + if (osdType === "SwitchMonitors" && Panel.lastOsdType() === osdType) { + Applet.next() return true } - return false + + Applet.sync() + if (osdType === "DirectSwitchLayout" && Applet.state !== 2) + Applet.doAction() + + return true } function match(osdType) { diff --git a/panels/notification/osd/osdpanel.cpp b/panels/notification/osd/osdpanel.cpp index 6d47be03d..3ded1b8c7 100644 --- a/panels/notification/osd/osdpanel.cpp +++ b/panels/notification/osd/osdpanel.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -48,6 +48,10 @@ bool OsdPanel::init() m_osdTimer->setInterval(m_interval); m_osdTimer->setSingleShot(true); QObject::connect(m_osdTimer, &QTimer::timeout, this, &OsdPanel::doneSetting); + + m_hideTimer = new QTimer(this); + m_hideTimer->setSingleShot(true); + QObject::connect(m_hideTimer, &QTimer::timeout, this, &OsdPanel::hideOsd); return DPanel::init(); } @@ -65,13 +69,13 @@ bool OsdPanel::visible() const void OsdPanel::ShowOSD(const QString &text) { + // A new request extends the visible period; a hide already scheduled by + // doneSetting() must not cut this request short. + m_hideTimer->stop(); qCInfo(osdLog()) << "show text" << text; m_osdTimer->setInterval(text == "SwitchWM3D" ? 2000 : 1000); - - QTimer::singleShot(100, this, [this, text]() { - setOsdType(text); - showOsd(); - }); + setOsdType(text); + showOsd(); } void OsdPanel::doneSetting() @@ -83,13 +87,12 @@ void OsdPanel::doneSetting() int delay = m_osdTimer->interval() - m_osdTimer->remainingTime(); if (delay < 0) delay = 0; - QTimer::singleShot(delay,this, [this](){ - hideOsd(); - }); + m_hideTimer->start(delay); } void OsdPanel::hideOsd() { + m_hideTimer->stop(); m_osdTimer->stop(); setVisible(false); updateLastOsdType({}); @@ -97,6 +100,7 @@ void OsdPanel::hideOsd() void OsdPanel::showOsd() { + m_hideTimer->stop(); m_osdTimer->stop(); m_osdTimer->start(); diff --git a/panels/notification/osd/osdpanel.h b/panels/notification/osd/osdpanel.h index d01334189..e0d28ff8d 100644 --- a/panels/notification/osd/osdpanel.h +++ b/panels/notification/osd/osdpanel.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -46,6 +46,7 @@ private Q_SLOTS: private: bool m_visible = false; QTimer *m_osdTimer = nullptr; + QTimer *m_hideTimer = nullptr; QString m_osdType; QString m_lastOsdType; int m_interval {2000};