Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 105 additions & 19 deletions panels/notification/osd/audio/audioapplet.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,65 @@
// 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

#include "audioapplet.h"

#include "pluginfactory.h"

Check warning on line 7 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "pluginfactory.h" not found.

#include <QDBusConnection>

Check warning on line 9 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusConnection> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusMessage>

Check warning on line 10 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusMessage> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusPendingCall>

Check warning on line 11 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusPendingCall> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusReply>

Check warning on line 12 in panels/notification/osd/audio/audioapplet.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusReply> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <DDBusSender>

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<QString> fetchDefaultSinkPath()
{
QDBusReply<QVariant> 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<QDBusObjectPath>(defaultSink);
return DDBusSender().service("org.deepin.dde.Audio1")
.path(path.path())
.interface("org.deepin.dde.Audio1.Sink");
return qdbus_cast<QDBusObjectPath>(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
Expand All @@ -55,21 +77,85 @@

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<QVariant> reply = sink.property("Mute").get();
if (reply.isValid()) {
m_muted = qdbus_cast<bool>(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<QDBusObjectPath>(*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<QVariant> 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<bool>(muteReply)) {

if (*m_muted) {
return "osd_volume_mute";
}

Expand All @@ -88,11 +174,11 @@
return QString("osd_volume_%1").arg(level);
}

double AudioApplet::fetchVolume() const
double AudioApplet::fetchVolume(DDBusSender &sink) const
{
QDBusReply<QVariant> volumeReply = audioSinkInter().property("Volume").get();
QDBusReply<QVariant> 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;
}

Expand Down
19 changes: 17 additions & 2 deletions panels/notification/osd/audio/audioapplet.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// 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

#pragma once

#include "applet.h"

Check warning on line 7 in panels/notification/osd/audio/audioapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "applet.h" not found.

#include <QVariantMap>

Check warning on line 9 in panels/notification/osd/audio/audioapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QVariantMap> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <optional>

Check warning on line 10 in panels/notification/osd/audio/audioapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <optional> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class QDBusMessage;
class DDBusSender;

namespace osd {

class AudioApplet : public DS_NAMESPACE::DApplet
Expand All @@ -16,6 +22,7 @@
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);
Expand All @@ -37,13 +44,21 @@
void volumeRatioChanged();
void increaseVolumeChanged();

private Q_SLOTS:

Check warning on line 47 in panels/notification/osd/audio/audioapplet.h

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.
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<bool> m_muted;
QString m_iconName;
double m_volumeRatio;
};
Expand Down
48 changes: 25 additions & 23 deletions panels/notification/osd/displaymode/displaymodeapplet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,32 @@ QQmlListProperty<DPItem> 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(state);
setState(fetchState());
}

void DisPlayModeApplet::refreshDisplayState()
{
m_planItemsValid = fetchPlanItems();
setCurrentPlanItem(m_planItemsValid ? fetchCurrentPlanItem() : nullptr);
Q_EMIT planItemsChanged();
}

void DisPlayModeApplet::next()
{
auto it = std::find_if(m_planItems.begin(), m_planItems.end(), [this](const DPItem *item) {
return m_currentPlanItem && item->key() == m_currentPlanItem->key();
});
if (it != m_planItems.end()) {
auto offset = it - m_planItems.begin();
auto nextIndex = (offset + 1) % m_planItems.count();
const auto item = m_planItems.at(nextIndex);
Q_ASSERT(item);
setCurrentPlanItem(item);
qCInfo(osdDPLog) << "next display mode" << item->mode() << item->name();
m_actionTimer->start();
}
if (!m_planItemsValid)
refreshDisplayState();

if (!m_planItemsValid || m_planItems.isEmpty())
return;

const auto nextIndex = (m_planItems.indexOf(m_currentPlanItem) + 1) % m_planItems.size();
const auto item = m_planItems.at(nextIndex);
setCurrentPlanItem(item);
qCInfo(osdDPLog) << "next display mode" << item->mode() << item->name();
m_actionTimer->start();
}

void DisPlayModeApplet::doAction()
Expand All @@ -152,18 +153,18 @@ void DisPlayModeApplet::setCurrentPlanItem(DPItem *newCurrentPlanItem)
emit currentPlanItemChanged();
}

void DisPlayModeApplet::fetchPlanItems()
bool DisPlayModeApplet::fetchPlanItems()
{
QDBusReply<QStringList> listOutputNames = displayInter().method("ListOutputNames").call();
if (!listOutputNames.isValid()) {
qCWarning(osdDPLog) << "Failed to fetch ListOutputNames" << listOutputNames.error();
return;
return false;
}
auto outputNames = listOutputNames.value();

setCurrentPlanItem(nullptr);
qDeleteAll(m_planItems);
m_planItems.clear();
m_currentPlanItem = nullptr;

m_planItems << new DPItem(tr("Duplicate"), "osd_multi_screen_copy", DPItem::Merge, this);
m_planItems << new DPItem(tr("Extend"), "osd_multi_screen_extension", DPItem::Extend, this);
Expand All @@ -175,6 +176,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
Expand Down
4 changes: 3 additions & 1 deletion panels/notification/osd/displaymode/displaymodeapplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,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<DPItem *> m_planItems;
DPItem *m_currentPlanItem = nullptr;
bool m_planItemsValid = false;
int m_state;
QTimer *m_actionTimer = nullptr;
};
Expand Down
23 changes: 10 additions & 13 deletions panels/notification/osd/displaymode/package/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +31 to +33

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Unwanted display-mode OSD appears

When a DirectSwitchLayout request is dispatched, update() now returns true for DirectSwitchLayout, so the parent OSD package activates this applet even though this path only syncs and optionally applies the mode; users see the display-mode selection overlay for a direct-switch request.

Preserve the false return for DirectSwitchLayout if that request should apply the mode without activating the display-mode OSD.

Prompt for AI agents
In `panels/notification/osd/displaymode/package/main.qml` at lines 31-33:

**Unwanted display-mode OSD appears**

When a `DirectSwitchLayout` request is dispatched, `update()` now returns `true` for `DirectSwitchLayout`, so the parent OSD package activates this applet even though this path only syncs and optionally applies the mode; users see the display-mode selection overlay for a direct-switch request.

Preserve the false return for `DirectSwitchLayout` if that request should apply the mode without activating the display-mode OSD.

}
function match(osdType)
{
Expand Down
Loading
Loading