Skip to content

Commit a256305

Browse files
committed
feat!: make visualizer fit widget fixed/custom width/height
preparation for #4 BREAKING CHANGE: Number of bars option has been disabled for the current visualizer styles (bars, wave), it's now is automatically calculated based on the widget width or height
1 parent d68df5b commit a256305

8 files changed

Lines changed: 140 additions & 68 deletions

File tree

package/contents/config/main.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@
8686
<entry name="barGap" type="Int">
8787
<default>5</default>
8888
</entry>
89-
<!-- ./package/contents/ui/code/globals.js -->
89+
<!-- ./package/contents/ui/code/globals.js[baseBarColors] -->
9090
<entry name="barColors" type="String">
9191
<default>{}</default>
9292
</entry>
93-
<!-- ./package/contents/ui/code/globals.js -->
93+
<!-- ./package/contents/ui/code/globals.js[baseWaveFillColors] -->
9494
<entry name="waveFillColors" type="String">
9595
<default>{}</default>
9696
</entry>
@@ -103,8 +103,16 @@
103103
<entry name="roundedBars" type="Bool">
104104
<default>false</default>
105105
</entry>
106+
<!-- fill panel thickness -->
106107
<entry name="fillPanel" type="Bool">
107108
<default>false</default>
108109
</entry>
110+
<!-- take all the available space in the panel -->
111+
<entry name="expanding" type="Bool">
112+
<default>false</default>
113+
</entry>
114+
<entry name="length" type="Int">
115+
<default>200</default>
116+
</entry>
109117
</group>
110118
</kcfg>

package/contents/ui/CompactRepresentation.qml

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import QtQuick
22
import QtQuick.Layouts
33
import org.kde.kirigami as Kirigami
44
import org.kde.plasma.plasmoid
5-
import org.kde.plasma.components as PlasmaComponents
65
import "./components"
76
import "code/enum.js" as Enum
87
import "code/globals.js" as Globals
@@ -11,19 +10,25 @@ import "code/utils.js" as Utils
1110
Item {
1211
id: root
1312

14-
Layout.preferredWidth: main.onDesktop ? content.implicitWidth : (main.horizontal ? content.implicitWidth : parent.height)
15-
Layout.preferredHeight: main.onDesktop ? 0 : (main.horizontal ? parent.height : content.implicitHeight)
16-
Layout.minimumWidth: Layout.preferredWidth
17-
Layout.minimumHeight: Layout.preferredHeight
13+
Layout.preferredWidth: {
14+
if (main.onDesktop || main.horizontal && Plasmoid.configuration.expanding) {
15+
return 0;
16+
}
17+
return main.horizontal ? Plasmoid.configuration.length : parent.width;
18+
}
1819

19-
property int framerate: Plasmoid.configuration.framerate
20-
property int barGap: Plasmoid.configuration.barGap
21-
property int barCount: {
22-
if (visualizerStyle === Enum.VisualizerStyles.Wave) {
23-
return Math.max(2, Plasmoid.configuration.barCount);
20+
Layout.preferredHeight: {
21+
if (main.onDesktop || !main.horizontal && Plasmoid.configuration.expanding) {
22+
return 0;
2423
}
25-
return Plasmoid.configuration.barCount;
24+
return main.horizontal ? parent.height : Plasmoid.configuration.length;
2625
}
26+
Layout.fillWidth: main.horizontal && Plasmoid.configuration.expanding && !main.onDesktop
27+
Layout.fillHeight: !main.horizontal && Plasmoid.configuration.expanding && !main.onDesktop
28+
29+
property int framerate: Plasmoid.configuration.framerate
30+
property int barGap: Plasmoid.configuration.barGap
31+
property int barCount: main.barCount
2732
property int barWidth: Plasmoid.configuration.barWidth
2833
property int noiseReduction: Plasmoid.configuration.noiseReduction
2934
property int monstercat: Plasmoid.configuration.monstercat
@@ -67,34 +72,31 @@ Item {
6772
return config;
6873
}
6974

70-
RowLayout {
71-
id: content
72-
height: parent.height
73-
anchors.horizontalCenter: parent.horizontalCenter
74-
Visualizer {
75-
id: visualizer
76-
visualizerStyle: root.visualizerStyle
77-
barWidth: root.barWidth
78-
barGap: root.barGap
79-
barCount: root.barCount
80-
centeredBars: root.centeredBars
81-
roundedBars: root.roundedBars
82-
fillWave: root.fillWave
83-
barColorsCfg: root.barColorsCfg
84-
waveFillColorsCfg: root.waveFillColorsCfg
85-
values: cava.values
86-
debugMode: Plasmoid.configuration.debugMode
87-
visible: !cava.hasError
88-
}
89-
Kirigami.Icon {
90-
Layout.preferredWidth: Kirigami.Units.iconSizes.roundedIconSize(Math.min(main.height, main.width))
91-
Layout.preferredHeight: Layout.preferredWidth
92-
source: Qt.resolvedUrl("./icons/error.svg").toString().replace("file://", "")
93-
active: mouseArea.containsMouse
94-
isMask: true
95-
color: Kirigami.Theme.negativeTextColor
96-
visible: cava.hasError
97-
}
75+
Visualizer {
76+
id: visualizer
77+
anchors.fill: parent
78+
visualizerStyle: root.visualizerStyle
79+
barWidth: root.barWidth
80+
barGap: root.barGap
81+
barCount: root.barCount
82+
centeredBars: root.centeredBars
83+
roundedBars: root.roundedBars
84+
fillWave: root.fillWave
85+
barColorsCfg: root.barColorsCfg
86+
waveFillColorsCfg: root.waveFillColorsCfg
87+
values: cava.values
88+
debugMode: Plasmoid.configuration.debugMode
89+
visible: !cava.hasError && !cava.idle
90+
}
91+
Kirigami.Icon {
92+
anchors.centerIn: parent
93+
width: Kirigami.Units.iconSizes.roundedIconSize(Math.min(main.height, main.width))
94+
height: width
95+
source: Qt.resolvedUrl("./icons/error.svg").toString().replace("file://", "")
96+
active: mouseArea.containsMouse
97+
isMask: true
98+
color: Kirigami.Theme.negativeTextColor
99+
visible: cava.hasError
98100
}
99101

100102
MouseArea {

package/contents/ui/code/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,7 @@ function delay(interval, callback, parentItem) {
134134
});
135135
timer.start();
136136
}
137+
138+
function makeEven(n) {
139+
return n - (n % 2);
140+
}

package/contents/ui/components/Visualizer.qml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ Item {
1818
required property var waveFillColorsCfg
1919
property list<int> values
2020
property bool debugMode: false
21-
Layout.preferredWidth: visualizer.width
22-
Layout.preferredHeight: parent.height
21+
2322
Rectangle {
2423
id: kirigamiColorItem
2524
opacity: 0
@@ -35,15 +34,29 @@ Item {
3534
width: height
3635
Kirigami.Theme.colorSet: Kirigami.Theme[root.waveFillColorsCfg.systemColorSet]
3736
}
37+
Rectangle {
38+
color: Kirigami.Theme.highlightColor
39+
opacity: 0.2
40+
visible: root.debugMode
41+
width: canvas.width
42+
height: canvas.height
43+
anchors.centerIn: parent
44+
}
3845
Canvas {
39-
id: visualizer
46+
id: canvas
47+
anchors.centerIn: parent
4048
property int visualizerStyle: root.visualizerStyle
4149
property int barWidth: root.barWidth
4250
property int spacing: root.barGap
4351
property int barCount: root.values.length
4452
property bool centeredBars: root.centeredBars
4553
property bool roundedBars: root.roundedBars
46-
property var values: root.values
54+
property var values: {
55+
if (root.debugMode) {
56+
root.values[0] = 100;
57+
}
58+
return root.values;
59+
}
4760
property bool fillWave: root.fillWave
4861

4962
property real radiusOffset: barWidth / 2
@@ -62,7 +75,7 @@ Item {
6275
height: parent.height
6376
property bool fixAlign: barWidth % 2 === 0 && centeredBars && visualizerStyle === Enum.VisualizerStyles.Wave
6477

65-
onValuesChanged: visualizer.requestPaint()
78+
onValuesChanged: canvas.requestPaint()
6679

6780
onPaint: {
6881
const ctx = getContext("2d");
@@ -179,8 +192,8 @@ Item {
179192
Shape {
180193
id: shape
181194
visible: root.debugMode
182-
width: parent.width
183-
height: parent.height
195+
width: canvas.width
196+
height: canvas.height
184197
anchors.centerIn: parent
185198
ShapePath {
186199
fillColor: "transparent"

package/contents/ui/configCava.qml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,27 @@ KCM.SimpleKCM {
5454
to: 144
5555
}
5656

57-
SpinBox {
58-
id: barCountSpinbox
57+
RowLayout {
58+
id: barsRow
59+
Kirigami.FormData.buddyFor: barCountSpinbox
5960
Kirigami.FormData.label: i18n("Number of bars:")
60-
from: 1
61-
to: 512
61+
SpinBox {
62+
id: barCountSpinbox
63+
enabled: false //TODO enable when we have a visualization style that can use it
64+
from: 1
65+
to: 512
66+
Layout.alignment: Qt.AlignTop
67+
}
68+
Label {
69+
visible: !barCountSpinbox.enabled
70+
text: i18n("Automatically calculated for the current visualizer style.")
71+
font: Kirigami.Theme.smallFont
72+
Layout.maximumWidth: 200
73+
wrapMode: Label.Wrap
74+
enabled: false
75+
Layout.alignment: Qt.AlignTop
76+
}
6277
}
63-
6478
RowLayout {
6579
Kirigami.FormData.label: i18n("Automatic sensitivity:")
6680
CheckBox {

package/contents/ui/configGeneral.qml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import org.kde.plasma.core as PlasmaCore
88
KCM.SimpleKCM {
99
id: root
1010
property alias cfg_desktopWidgetBg: desktopWidgetBackgroundRadio.value
11-
property alias cfg_fillPanel: fillPanelCheckbox.checked
1211
property alias cfg_hideWhenIdle: hideWhenIdleCheckbox.checked
1312
property int cfg_visualizerStyle
1413
property string cfg_barColors
@@ -44,11 +43,6 @@ KCM.SimpleKCM {
4443
}
4544
}
4645

47-
CheckBox {
48-
id: fillPanelCheckbox
49-
Kirigami.FormData.label: i18n("Fill panel thickness:")
50-
}
51-
5246
RadioButton {
5347
Kirigami.FormData.label: i18n("Desktop background:")
5448
text: i18n("Default")

package/contents/ui/configVisualizer.qml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,35 @@ import QtQuick.Controls
33
import QtQuick.Layouts
44
import org.kde.kirigami as Kirigami
55
import org.kde.kcmutils as KCM
6+
import org.kde.plasma.core as PlasmaCore
7+
import org.kde.plasma.plasmoid
68
import "components" as Components
79
import "code/enum.js" as Enum
10+
import "code/utils.js" as Utils
811

912
KCM.SimpleKCM {
1013
id: root
1114
property alias cfg_barGap: barGapSpinbox.value
1215
property alias cfg_barWidth: barWidthSpinbox.value
1316
property alias cfg_centeredBars: centeredBarsCheckbox.checked
1417
property alias cfg_roundedBars: roundedBarsCheckbox.checked
18+
// fill panel thickness
1519
property alias cfg_fillPanel: fillPanelCheckbox.checked
16-
property alias cfg_hideWhenIdle: hideWhenIdleCheckbox.checked
1720
property int cfg_visualizerStyle
1821
property string cfg_barColors
1922
property string cfg_waveFillColors
2023
property alias cfg_fillWave: fillWaveCheckbox.checked
24+
// take all the available space in the panel
25+
property alias cfg_expanding: expandingCheckbox.checked
26+
property alias cfg_length: lengthSpinbox.value
27+
28+
readonly property bool vertical: {
29+
if (Plasmoid.formFactor == PlasmaCore.Types.Vertical) {
30+
return true;
31+
}
32+
return false;
33+
}
34+
readonly property string dimensionStr: vertical ? i18n("height") : i18n("width")
2135

2236
ColumnLayout {
2337

@@ -56,12 +70,22 @@ KCM.SimpleKCM {
5670
}
5771

5872
CheckBox {
59-
id: hideWhenIdleCheckbox
60-
Kirigami.FormData.label: i18n("Auto-hide when idle:")
73+
id: expandingCheckbox
74+
visible: !(Plasmoid.location === PlasmaCore.Types.Floating)
75+
Kirigami.FormData.label: i18n("Fill panel %1:", root.dimensionStr)
76+
}
77+
SpinBox {
78+
id: lengthSpinbox
79+
visible: !(Plasmoid.location === PlasmaCore.Types.Floating)
80+
enabled: !expandingCheckbox.checked
81+
Kirigami.FormData.label: i18n("Fixed %1:", root.dimensionStr)
82+
from: 1
83+
to: 9999
6184
}
6285

6386
CheckBox {
6487
id: fillPanelCheckbox
88+
visible: !(Plasmoid.location === PlasmaCore.Types.Floating)
6589
Kirigami.FormData.label: i18n("Fill panel thickness:")
6690
}
6791

package/contents/ui/main.qml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ PlasmoidItem {
1717
property bool onDesktop: Plasmoid.location === PlasmaCore.Types.Floating
1818
property bool horizontal: Plasmoid.formFactor !== PlasmaCore.Types.Vertical
1919

20+
property int barCount: {
21+
let bars = 1;
22+
const width = main.width;
23+
bars = Math.floor((width + Plasmoid.configuration.barGap) / (Plasmoid.configuration.barWidth + Plasmoid.configuration.barGap));
24+
if (Plasmoid.configuration.outputChannels === "stereo") {
25+
bars = Utils.makeEven(bars);
26+
}
27+
if (Plasmoid.configuration.visualizerStyle === Enum.VisualizerStyles.Wave) {
28+
bars = Math.max(2, bars);
29+
}
30+
return bars;
31+
}
32+
2033
property bool hideWhenIdle: Plasmoid.configuration.hideWhenIdle
2134

2235
Plasmoid.status: PlasmaCore.Types.ActiveStatus
@@ -30,16 +43,16 @@ PlasmoidItem {
3043
onExpandedChanged: {
3144
Utils.delay(1000, updateStatus, main);
3245
}
46+
onBarCountChanged: {
47+
if (editMode && !cava.running) {
48+
return;
49+
}
50+
cava.barCount = barCount;
51+
}
3352

3453
Cava {
3554
id: cava
3655
framerate: Plasmoid.configuration.framerate
37-
barCount: {
38-
if (Plasmoid.configuration.visualizerStyle === Enum.VisualizerStyles.Wave) {
39-
return Math.max(2, Plasmoid.configuration.barCount);
40-
}
41-
return Plasmoid.configuration.barCount;
42-
}
4356
noiseReduction: Plasmoid.configuration.noiseReduction
4457
monstercat: Plasmoid.configuration.monstercat
4558
waves: Plasmoid.configuration.waves

0 commit comments

Comments
 (0)