Skip to content

Commit

Permalink
ags: move globalThis variables to imports.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fufexan committed Dec 10, 2023
1 parent 7c0119e commit 96bc436
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
9 changes: 5 additions & 4 deletions home/programs/ags/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// start date of Ags, used for OSD
globalThis.startDate = Date.now();

import { App, Utils } from "./imports.js";
import Bar from "./windows/bar/main.js";
import Osd from "./windows/osd/main.js";
Expand All @@ -13,7 +10,11 @@ Utils.exec(`sassc ${scss} ${css}`);

export default {
style: css,
windows: [Bar, Osd(0), Music],
windows: [
Bar,
Osd(0),
Music,
],

closeWindowDelay: {
"osd": 500,
Expand Down
26 changes: 26 additions & 0 deletions home/programs/ags/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,29 @@ export const Bluetooth = await service("bluetooth");
export const Hyprland = await service("hyprland");
export const Mpris = await service("mpris");
export const Network = await service("network");

// Variables
export const startDate = Date.now();
export const osdVars = Variable({
reveal: Variable(false),
debounceTimer: Date.now(),
timePassed: 0,
timeout: null,
container: null,
});

export const player = Variable(Mpris.getPlayer(""));
export const musicVisible = Variable(false);

// Functions
globalThis.getPlayer = () => {
console.log(`player: ${JSON.stringify(player.value)}`);
};

globalThis.setPlayer = () => {
const player = Mpris.getPlayer("");
const players = Mpris.players;
console.log(`player: ${JSON.stringify(player)}`);
console.log(`players: ${JSON.stringify(players)}`);
player.value = player;
};
12 changes: 9 additions & 3 deletions home/programs/ags/windows/bar/modules/music.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Mpris, Variable, Widget } from "../../../imports.js";
import {
Mpris,
musicVisible,
player,
Variable,
Widget,
} from "../../../imports.js";

const revealControls = Variable(false);
globalThis.player = Variable(Mpris.getPlayer(""));
globalThis.musicVisible = Variable(false);

setPlayer();

const Cover = Widget.Box({
className: "cover",
Expand Down
13 changes: 3 additions & 10 deletions home/programs/ags/windows/osd/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { Variable, Widget } from "../../imports.js";
import { osdVars, Widget } from "../../imports.js";
import { brightnessIndicator, volumeIndicator } from "./parts.js";

globalThis.osdVars = {
reveal: Variable(false),
debounceTimer: Date.now(),
timePassed: 0,
timeout: null,
};

const OsdContainer = Widget.Box({
className: "osd-container",
visible: false,
Expand All @@ -17,7 +10,7 @@ const OsdContainer = Widget.Box({
],
});

globalThis.osdcontainer = OsdContainer;
osdVars.value.osdcontainer = OsdContainer;

export const Osd = (monitor = 0) =>
Widget.Window({
Expand All @@ -27,7 +20,7 @@ export const Osd = (monitor = 0) =>
visible: false,

child: OsdContainer,
binds: [["visible", osdVars.reveal]],
binds: [["visible", osdVars.value.reveal]],
});

export default Osd;
20 changes: 10 additions & 10 deletions home/programs/ags/windows/osd/util.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Utils, Widget } from "../../imports.js";
import { osdVars, startDate, Utils, Widget } from "../../imports.js";
import GLib from "gi://GLib";

// create a debouncer for burst events (like Audio changes)
export const debounce = (self) => {
const date = Date.now();
if (date - osdVars.debounceTimer < 50) {
osdVars.debounceTimer = date;
if (date - osdVars.value.debounceTimer < 50) {
osdVars.value.debounceTimer = date;
return;
}
osdVars.debounceTimer = date;
osdVars.value.debounceTimer = date;
toggleOsd(self);
};

Expand All @@ -19,20 +19,20 @@ export const toggleOsd = (self) => {
if (Date.now() - startDate < 100) return;

// make all other osds invisible
osdcontainer.children.forEach((e) => {
osdVars.value.osdcontainer.children.forEach((e) => {
if (e != self) e.visible = false;
});

// set self to visible and make sure window is revealed
self.visible = true;
osdVars.reveal.value = true;
osdVars.value.reveal.value = true;

// after 1.5s, make self and window invisible
osdVars.timePassed = 1500;
if (osdVars.timeout) GLib.source_remove(osdVars.timeout);
osdVars.timeout = Utils.timeout(osdVars.timePassed, () => {
osdVars.value.timePassed = 1500;
if (osdVars.value.timeout) GLib.source_remove(osdVars.value.timeout);
osdVars.value.timeout = Utils.timeout(osdVars.value.timePassed, () => {
self.visible = false;
osdVars.reveal.value = false;
osdVars.value.reveal.value = false;
});
};

Expand Down

0 comments on commit 96bc436

Please sign in to comment.