From ac869537e71ed03fa24a47f742c9fc9b95f9837f Mon Sep 17 00:00:00 2001
From: matt1432 <matt@nelim.org>
Date: Mon, 4 Sep 2023 22:27:34 -0400
Subject: [PATCH] refactor(ags): rearrange bar js

---
 config/ags/config.js             |   4 +-
 config/ags/js/bar/bar.js         |  65 ++++++++++++++
 config/ags/js/bar/heart.js       |  31 +++++++
 config/ags/js/bar/osk-toggle.js  |  37 ++++++++
 config/ags/js/bar/traybuttons.js | 146 -------------------------------
 config/ags/js/common.js          |  17 ++++
 6 files changed, 152 insertions(+), 148 deletions(-)
 create mode 100644 config/ags/js/bar/bar.js
 create mode 100644 config/ags/js/bar/heart.js
 create mode 100644 config/ags/js/bar/osk-toggle.js
 delete mode 100644 config/ags/js/bar/traybuttons.js

diff --git a/config/ags/config.js b/config/ags/config.js
index da692cb1..095e213a 100644
--- a/config/ags/config.js
+++ b/config/ags/config.js
@@ -1,6 +1,6 @@
 import { exec } from 'resource:///com/github/Aylur/ags/utils.js';
 import { Powermenu } from './js/powermenu.js';
-import { LeftBar } from './js/bar/traybuttons.js';
+import { Bar } from './js/bar/bar.js';
 import { Closer } from './js/common.js';
 
 const scss = ags.App.configDir + '/scss/main.scss';
@@ -13,7 +13,7 @@ export default {
     style: css,
     windows: [
       Powermenu,
-      LeftBar,
+      Bar,
       Closer,
     ]
 }
diff --git a/config/ags/js/bar/bar.js b/config/ags/js/bar/bar.js
new file mode 100644
index 00000000..04491b98
--- /dev/null
+++ b/config/ags/js/bar/bar.js
@@ -0,0 +1,65 @@
+const { Window, CenterBox, Box } = ags.Widget;
+
+import { Separator, EventBox }  from '../common.js';
+import { CurrentWindow }        from './current-window.js';
+import { Workspaces }           from './workspaces.js';
+import { OskToggle }            from './osk-toggle.js'
+import { Heart }                from './heart.js'
+
+export const Bar = Window({
+  name: 'left-bar',
+  layer: 'overlay',
+  anchor: 'top left right',
+  exclusive: true,
+
+  child: CenterBox({
+    className: 'transparent',
+    halign: 'fill',
+    style: 'margin-top: 5px; margin-left: 5px;',
+    vertical: false,
+    
+    children: [
+
+      // Left
+      Box({
+        halign: 'start',
+        children: [
+
+          OskToggle,
+  
+          Separator(12),
+
+          EventBox({
+            className: 'toggle-off',
+            onPrimaryClickRelease: '',
+            child: ags.Widget.Box({
+              className: 'tablet-toggle',
+              vertical: false,
+
+              child: ags.Widget.Label({
+                label: " 󰦧 ",
+              }),
+            }),
+          }),
+      
+          Separator(12),
+
+          Heart,
+
+          Separator(12),
+
+          Workspaces(),
+
+        ],
+      }),
+
+      // Center
+      CurrentWindow,
+
+      // Right
+      Box({
+        halign: 'end',
+      }),
+    ],
+  }),
+});
diff --git a/config/ags/js/bar/heart.js b/config/ags/js/bar/heart.js
new file mode 100644
index 00000000..9b895a53
--- /dev/null
+++ b/config/ags/js/bar/heart.js
@@ -0,0 +1,31 @@
+
+
+import { EventBox } from '../common.js';
+
+ags.Utils.subprocess(
+  ['bash', '-c', 'tail -f /home/matt/.config/.heart'],
+  (output) => {
+    Heart.child.children[0].label = ' ' + output;
+
+    if (output == '󰣐') {
+      Heart.toggleClassName('toggle-on', true);
+    } else {
+      Heart.toggleClassName('toggle-on', false);
+    }
+  },
+);
+export const Heart = EventBox({
+  className: 'toggle-off',
+  halign: 'center',
+  onPrimaryClickRelease: function() {
+    ags.Utils.exec('/home/matt/.nix/config/ags/bin/heart.sh toggle');
+  },
+  child: ags.Widget.Box({
+    className: 'heart-toggle',
+    vertical: false,
+
+    child: ags.Widget.Label({
+      label: '',
+    }),
+  }),
+});
diff --git a/config/ags/js/bar/osk-toggle.js b/config/ags/js/bar/osk-toggle.js
new file mode 100644
index 00000000..2004a4c2
--- /dev/null
+++ b/config/ags/js/bar/osk-toggle.js
@@ -0,0 +1,37 @@
+import { Separator, EventBox } from '../common.js';
+
+ags.Utils.subprocess(
+  ['bash', '-c', '/home/matt/.nix/config/ags/scripts/osk-toggle.sh getState'],
+  (output) => {
+    if (output == 'Running') {
+      OskToggle.toggleClassName('toggle-on', true);
+    } else {
+      OskToggle.toggleClassName('toggle-on', false);
+    }
+  },
+);
+export const OskToggle = EventBox({
+  className: 'toggle-off',
+  onPrimaryClickRelease: function() {
+    ags.Utils.subprocess(
+      ['bash', '-c', '/home/matt/.nix/config/ags/bin/osk-toggle.sh toggle'],
+      (output) => {
+        if (output == 'Running') {
+          OskToggle.toggleClassName('toggle-on', false);
+        } else {
+          OskToggle.toggleClassName('toggle-on', true);
+        }
+      },
+    );
+  },
+  child: ags.Widget.Box({
+    className: 'osk-toggle',
+    vertical: false,
+
+    children: [
+      ags.Widget.Label({
+        label: " 󰌌 ",
+      }),
+    ],
+  }),
+});
diff --git a/config/ags/js/bar/traybuttons.js b/config/ags/js/bar/traybuttons.js
deleted file mode 100644
index 25456e20..00000000
--- a/config/ags/js/bar/traybuttons.js
+++ /dev/null
@@ -1,146 +0,0 @@
-import Gdk from 'gi://Gdk';
-const display = Gdk.Display.get_default();
-import { CurrentWindow } from './current-window.js';
-import { Workspaces } from './workspaces.js';
-
-const Separator = width => ags.Widget.Box({
-  style: `min-width: ${width}px;`,
-});
-
-ags.Utils.subprocess(
-  ['bash', '-c', '/home/matt/.nix/config/ags/scripts/osk-toggle.sh getState'],
-  (output) => {
-    if (output == 'Running') {
-      OskToggle.toggleClassName('toggle-on', true);
-    } else {
-      OskToggle.toggleClassName('toggle-on', false);
-    }
-  },
-);
-const OskToggle = ags.Widget.EventBox({
-  className: 'toggle-off',
-  onPrimaryClickRelease: function() {
-    ags.Utils.subprocess(
-      ['bash', '-c', '/home/matt/.nix/config/ags/scripts/osk-toggle.sh toggle'],
-      (output) => {
-        if (output == 'Running') {
-          OskToggle.toggleClassName('toggle-on', false);
-        } else {
-          OskToggle.toggleClassName('toggle-on', true);
-        }
-      },
-    );
-  },
-  onHover: box => {
-    box.window.set_cursor(Gdk.Cursor.new_from_name(display, 'pointer'));
-  },
-  onHoverLost: box => {
-    box.window.set_cursor(null);
-  },
-  child: ags.Widget.Box({
-    className: 'osk-toggle',
-    vertical: false,
-
-    children: [
-      ags.Widget.Label({
-        label: " 󰌌 ",
-      }),
-    ],
-  }),
-});
-
-ags.Utils.subprocess(
-  ['bash', '-c', 'tail -f /home/matt/.config/.heart'],
-  (output) => {
-    HeartToggle.child.children[0].label = ' ' + output;
-
-    if (output == '󰣐') {
-      HeartToggle.toggleClassName('toggle-on', true);
-    } else {
-      HeartToggle.toggleClassName('toggle-on', false);
-    }
-  },
-);
-const HeartToggle = ags.Widget.EventBox({
-  className: 'toggle-off',
-  halign: 'center',
-  onPrimaryClickRelease: function() {
-    ags.Utils.exec('/home/matt/.nix/config/ags/scripts/heart.sh toggle');
-  },
-  onHover: box => {
-    box.window.set_cursor(Gdk.Cursor.new_from_name(display, 'pointer'));
-  },
-  onHoverLost: box => {
-    box.window.set_cursor(null);
-  },
-  child: ags.Widget.Box({
-    className: 'heart-toggle',
-    vertical: false,
-
-    child: ags.Widget.Label({
-      label: '',
-    }),
-  }),
-});
-
-export const LeftBar = ags.Widget.Window({
-  name: 'left-bar',
-  layer: 'overlay',
-  anchor: 'top left right',
-  exclusive: true,
-
-  child: ags.Widget.CenterBox({
-    className: 'transparent',
-    halign: 'fill',
-    style: 'margin-top: 5px; margin-left: 5px;',
-    vertical: false,
-    
-    children: [
-
-      ags.Widget.Box({
-        halign: 'start',
-        children: [
-
-          OskToggle,
-  
-          Separator(12),
-
-          ags.Widget.EventBox({
-            className: 'toggle-off',
-            onPrimaryClickRelease: '',
-            onHover: box => {
-              box.window.set_cursor(Gdk.Cursor.new_from_name(display, 'pointer'));
-            },
-            onHoverLost: box => {
-              box.window.set_cursor(null);
-            },
-            child: ags.Widget.Box({
-              className: 'tablet-toggle',
-              vertical: false,
-
-              child: ags.Widget.Label({
-                label: " 󰦧 ",
-              }),
-            }),
-          }),
-      
-          Separator(12),
-
-          HeartToggle,
-
-          Separator(12),
-
-          Workspaces(),
-
-        ],
-      }),
-
-      CurrentWindow,
-
-      ags.Widget.Box({
-        halign: 'end',
-        style: 'background: red; min-width: 100px; min-height: 40px',
-      }),
-    ],
-  }),
-});
diff --git a/config/ags/js/common.js b/config/ags/js/common.js
index a9708057..e35c1c14 100644
--- a/config/ags/js/common.js
+++ b/config/ags/js/common.js
@@ -11,3 +11,20 @@ export const Closer = ags.Widget.Window({
     },
   }),
 });
+
+export const Separator = width => ags.Widget.Box({
+  style: `min-width: ${width}px;`,
+});
+
+import Gdk from 'gi://Gdk';
+const display = Gdk.Display.get_default();
+
+export const EventBox = ({ ...params }) => ags.Widget.EventBox({
+  ...params,
+  onHover: box => {
+    box.window.set_cursor(Gdk.Cursor.new_from_name(display, 'pointer'));
+  },
+  onHoverLost: box => {
+    box.window.set_cursor(null);
+  },
+});