diff --git a/applet/contents/code/icon.js b/applet/contents/code/icon.js --- a/applet/contents/code/icon.js +++ b/applet/contents/code/icon.js @@ -20,13 +20,13 @@ function name(volume, muted) { // FIXME: hardcoded max value - var split_base = 65536/3.0; var icon = null; - if ((volume / split_base <= 0) || muted) { + var percent = volume / 65536; + if (percent <= 0.0 || muted) { icon = "audio-volume-muted"; - } else if (volume / split_base <= 1) { + } else if (percent <= 0.25) { icon = "audio-volume-low"; - } else if (volume / split_base <= 2) { + } else if (percent <= 0.75) { icon = "audio-volume-medium"; } else { icon = "audio-volume-high"; diff --git a/applet/contents/ui/ListItemBase.qml b/applet/contents/ui/ListItemBase.qml --- a/applet/contents/ui/ListItemBase.qml +++ b/applet/contents/ui/ListItemBase.qml @@ -39,48 +39,13 @@ enabled: subComponent - function volumePercent(volume) { - return 100 * volume / slider.maximumValue; - } - function setVolume(volume) { if (volume > 0 && Muted) { - toggleMute(); + Muted = false; } PulseObject.volume = volume } - function bound(value, min, max) { - return Math.max(min, Math.min(value, max)); - } - - // FIXME: increase/decrease are also present on app streams as they derive - // from this, they are not used there though. - // seems naughty. - function increaseVolume() { - var step = slider.maximumValue / 15; - var volume = bound(PulseObject.volume + step, 0, slider.maximumValue); - setVolume(volume); - osd.show(volumePercent(volume)); - } - - function decreaseVolume() { - var step = slider.maximumValue / 15; - var volume = bound(PulseObject.volume - step, 0, slider.maximumValue); - setVolume(volume); - osd.show(volumePercent(volume)); - } - - function toggleMute() { - var toMute = !Muted; - if (toMute) { - osd.show(0); - } else { - osd.show(volumePercent(Volume)); - } - Muted = toMute; - } - anchors { left: parent.left; right: parent.right; diff --git a/applet/contents/ui/main.qml b/applet/contents/ui/main.qml --- a/applet/contents/ui/main.qml +++ b/applet/contents/ui/main.qml @@ -32,41 +32,60 @@ Item { id: main + + property int volumeStep: 65536 / 15 + property string displayName: i18n("Audio Volume") + Layout.minimumHeight: units.gridUnit * 12 Layout.minimumWidth: units.gridUnit * 12 Layout.preferredHeight: units.gridUnit * 20 Layout.preferredWidth: units.gridUnit * 20 - property string displayName: i18n("Audio Volume") - Plasmoid.icon: sinkModel.sinks.length > 0 ? Icon.name(sinkModel.sinks[0].volume, sinkModel.sinks[0].muted) : Icon.name(0, true) + Plasmoid.icon: sinkModel.defaultSink ? Icon.name(sinkModel.defaultSink.volume, sinkModel.defaultSink.muted) : Icon.name(0, true) Plasmoid.switchWidth: units.gridUnit * 12 Plasmoid.switchHeight: units.gridUnit * 12 Plasmoid.toolTipMainText: displayName - // FIXME: Plasmoid.toolTipSubText: sinkModel.volumeText + Plasmoid.toolTipSubText: sinkModel.defaultSink ? i18n("Volume at %1%\n%2", volumePercent(sinkModel.defaultSink.volume), sinkModel.defaultSink.description) : "" - function runOnAllSinks(func) { - if (typeof(sinkView) === "undefined") { - print("This case we need to handle."); - return; - } else if (sinkView.count < 0) { + function bound(value, min, max) { + return Math.max(min, Math.min(value, max)); + } + + function volumePercent(volume) { + return Math.round(100 * volume / 65536); + } + + function increaseVolume(showOsd) { + if (!sinkModel.defaultSink) { return; } - for (var i = 0; i < sinkView.count; ++i) { - sinkView.currentIndex = i; - sinkView.currentItem[func](); + var volume = bound(sinkModel.defaultSink.volume + volumeStep, 0, 65536); + sinkModel.defaultSink.volume = volume; + if (showOsd) { + osd.show(volumePercent(volume)); } } - function increaseVolume() { - runOnAllSinks("increaseVolume"); - } - - function decreaseVolume() { - runOnAllSinks("decreaseVolume"); + function decreaseVolume(showOsd) { + if (!sinkModel.defaultSink) { + return; + } + var volume = bound(sinkModel.defaultSink.volume - volumeStep, 0, 65536); + sinkModel.defaultSink.volume = volume; + if (showOsd) { + osd.show(volumePercent(volume)); + } } - function muteVolume() { - runOnAllSinks("toggleMute"); + function muteVolume(showOsd) { + if (!sinkModel.defaultSink) { + return; + } + var toMute = !sinkModel.defaultSink.muted; + sinkModel.defaultSink.muted = toMute; + if (showOsd) { + osd.show(toMute ? 0 : volumePercent(sinkModel.defaultSink.volume)); + } } Plasmoid.compactRepresentation: PlasmaCore.IconItem { @@ -128,19 +147,19 @@ objectName: "increase_volume" text: i18n("Increase Volume") shortcut: Qt.Key_VolumeUp - onTriggered: increaseVolume() + onTriggered: increaseVolume(true) } GlobalAction { objectName: "decrease_volume" text: i18n("Decrease Volume") shortcut: Qt.Key_VolumeDown - onTriggered: decreaseVolume() + onTriggered: decreaseVolume(true) } GlobalAction { objectName: "mute" text: i18n("Mute") shortcut: Qt.Key_VolumeMute - onTriggered: muteVolume() + onTriggered: muteVolume(true) } } diff --git a/src/pulseaudio.h b/src/pulseaudio.h --- a/src/pulseaudio.h +++ b/src/pulseaudio.h @@ -64,7 +64,7 @@ class Q_DECL_EXPORT SinkModel : public AbstractModel { Q_OBJECT - Q_PROPERTY(QList sinks READ sinks NOTIFY sinksChanged) + Q_PROPERTY(QPulseAudio::Sink *defaultSink READ defaultSink NOTIFY defaultSinkChanged) public: enum ItemRole { IndexRole = Qt::UserRole + 1, @@ -74,14 +74,15 @@ SinkModel(QObject *parent = nullptr); - QList sinks() const; + Sink *defaultSink() const; int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; bool setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE; signals: void sinksChanged(); + void defaultSinkChanged(); protected: virtual void onDataAdded(quint32 index) Q_DECL_OVERRIDE Q_DECL_FINAL; @@ -97,6 +98,7 @@ class Q_DECL_EXPORT SourceModel : public AbstractModel { Q_OBJECT + Q_PROPERTY(QPulseAudio::Source *defaultSource READ defaultSource NOTIFY defaultSourceChanged) public: enum ItemRole { IndexRole = Qt::UserRole + 1, @@ -106,12 +108,15 @@ SourceModel(QObject *parent = nullptr); + Source *defaultSource() const; + int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE; bool setData(const QModelIndex &index, const QVariant &value, int role) Q_DECL_OVERRIDE; signals: void sourcesChanged(); + void defaultSourceChanged(); protected: virtual void onDataAdded(quint32 index) Q_DECL_OVERRIDE Q_DECL_FINAL; diff --git a/src/pulseaudio.cpp b/src/pulseaudio.cpp --- a/src/pulseaudio.cpp +++ b/src/pulseaudio.cpp @@ -29,6 +29,7 @@ #include "sinkinput.h" #include "source.h" #include "sourceoutput.h" +#include "server.h" namespace QPulseAudio { @@ -111,20 +112,14 @@ connect(&context()->sinks(), &SinkMap::added, this, &SinkModel::sinksChanged); connect(&context()->sinks(), &SinkMap::updated, this, &SinkModel::sinksChanged); connect(&context()->sinks(), &SinkMap::removed, this, &SinkModel::sinksChanged); + connect(context()->server(), &Server::defaultSinkChanged, this, &SinkModel::defaultSinkChanged); emit sinksChanged(); } -#warning very naughty, used by main.qml to set volume on all sinks -QList SinkModel::sinks() const +Sink *SinkModel::defaultSink() const { - QList ret; - if (!context()) - return ret; - for (Sink *sink : context()->sinks().data().values()) { - ret << sink; - } - return ret; + return context()->server()->defaultSink(); } int SinkModel::rowCount(const QModelIndex &parent) const @@ -301,10 +296,16 @@ connect(&context()->sources(), &SourceMap::added, this, &SourceModel::sourcesChanged); connect(&context()->sources(), &SourceMap::updated, this, &SourceModel::sourcesChanged); connect(&context()->sources(), &SourceMap::removed, this, &SourceModel::sourcesChanged); + connect(context()->server(), &Server::defaultSourceChanged, this, &SourceModel::defaultSourceChanged); emit sourcesChanged(); } +Source *SourceModel::defaultSource() const +{ + return context()->server()->defaultSource(); +} + int SourceModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); diff --git a/src/qml/plugin.cpp b/src/qml/plugin.cpp --- a/src/qml/plugin.cpp +++ b/src/qml/plugin.cpp @@ -24,13 +24,14 @@ #include "pulseaudio.h" #include "client.h" +#include "sink.h" +#include "source.h" #include "globalactioncollection.h" #include "volumeosd.h" void Plugin::registerTypes(const char* uri) { - qmlRegisterType(); qmlRegisterType(uri, 0, 1, "CardModel"); qmlRegisterType(uri, 0, 1, "ClientModel"); qmlRegisterType(uri, 0, 1, "SinkModel"); @@ -41,5 +42,8 @@ qmlRegisterType(uri, 0, 1, "GlobalAction"); qmlRegisterType(uri, 0, 1, "GlobalActionCollection"); qmlRegisterType(uri, 0, 1, "VolumeOSD"); + qmlRegisterType(); + qmlRegisterType(); + qmlRegisterType(); }