From d4cb4b3c3e27e33c12e8d3b4e8a3a1ad4f339a54 Mon Sep 17 00:00:00 2001
From: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>
Date: Tue, 15 Apr 2025 22:06:15 +1000
Subject: mediadisplay: fix visualiser symmetry

Ensure even number of bars so visualiser is symmetrical
If odd number, need to use average for the center bar, which looks bad
---
 src/modules/mediadisplay/visualiser.tsx | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

(limited to 'src/modules/mediadisplay/visualiser.tsx')

diff --git a/src/modules/mediadisplay/visualiser.tsx b/src/modules/mediadisplay/visualiser.tsx
index 9eacd44..d788e7b 100644
--- a/src/modules/mediadisplay/visualiser.tsx
+++ b/src/modules/mediadisplay/visualiser.tsx
@@ -22,7 +22,7 @@ export default () => (
                         .get_property("min-width", Gtk.StateFlags.NORMAL) as number;
                     const gaps = self.get_style_context().get_margin(Gtk.StateFlags.NORMAL).right;
                     const bars = Math.floor((width - gaps) / (barWidth + gaps));
-                    if (bars > 0) cava.set_bars(bars);
+                    if (bars > 0) cava.set_bars(bars % 2 ? bars : bars - 1);
                 });
             }
 
@@ -52,24 +52,19 @@ export default () => (
                 const radius = barWidth / 2;
                 const xOff = (width - len * (barWidth + gaps) - gaps) / 2 - radius;
                 const center = height / 2;
-                const half = Math.floor(len / 2);
+                const half = len / 2;
 
-                // Render channels facing each other
-                for (let i = half - 1; i >= 0; i--) {
-                    const x = (half - i) * (barWidth + gaps) + xOff;
-                    const value = center * values[i];
+                const renderPill = (x: number, value: number) => {
+                    x = x * (barWidth + gaps) + xOff;
+                    value *= center;
                     cr.arc(x, center + value, radius, 0, Math.PI);
                     cr.arc(x, center - value, radius, Math.PI, Math.PI * 2);
                     cr.fill();
-                }
+                };
 
-                for (let i = half; i < len; i++) {
-                    const x = (i + 1) * (barWidth + gaps) + xOff;
-                    const value = center * values[i];
-                    cr.arc(x, center + value, radius, 0, Math.PI);
-                    cr.arc(x, center - value, radius, Math.PI, Math.PI * 2);
-                    cr.fill();
-                }
+                // Render channels facing each other
+                for (let i = half - 1; i >= 0; i--) renderPill(half - i, values[i]);
+                for (let i = half; i < len; i++) renderPill(i + 1, values[i]);
             });
         }}
     />
-- 
cgit v1.2.3-freya

