testing ags

This commit is contained in:
cnst
2024-07-15 14:39:56 +02:00
parent 91e09c461b
commit 2f32e46601
55 changed files with 2218 additions and 22 deletions

View File

@@ -0,0 +1,30 @@
import { Audio, Icons } from "../imports.js";
export const audioIcon = () => {
if (Audio.speaker?.stream.isMuted) return Icons.volume.muted;
const vol = Audio.speaker?.volume * 100;
const icon = [
[101, "overamplified"],
[67, "high"],
[34, "medium"],
[1, "low"],
[0, "muted"],
].find(([threshold]) => threshold <= vol)[1];
return Icons.volume[icon];
};
export const micIcon = () => {
if (Audio.microphone?.stream.isMuted) return Icons.microphone.muted;
const vol = Audio.microphone?.volume * 100;
const icon = [
[67, "high"],
[34, "medium"],
[1, "low"],
[0, "muted"],
].find(([threshold]) => threshold <= vol)[1];
return Icons.microphone[icon];
};

View File

@@ -0,0 +1,22 @@
import { Battery } from "../imports.js";
export const toTime = (time) => {
const MINUTE = 60;
const HOUR = MINUTE * 60;
if (time > 24 * HOUR) return "";
const hours = Math.round(time / HOUR);
const minutes = Math.round((time - hours * HOUR) / MINUTE);
const hoursDisplay = hours > 0 ? `${hours}h ` : "";
const minutesDisplay = minutes > 0 ? `${minutes}m ` : "";
return `${hoursDisplay}${minutesDisplay}`;
};
export const batteryTime = () => {
return Battery.timeRemaining > 0 && toTime(Battery.timeRemaining) != ""
? `${toTime(Battery.timeRemaining)}remaining`
: "";
};

View File

@@ -0,0 +1,27 @@
import { Bluetooth, Icons } from "../imports.js";
export const getBluetoothDevice = (addr) =>
Bluetooth.getDevice(addr).alias ?? Bluetooth.getDevice(addr).name;
export const getBluetoothIcon = (connected) => {
if (!Bluetooth.enabled) return Icons.bluetooth.disabled;
if (connected.length > 0) return Icons.bluetooth.active;
return Icons.bluetooth.disconnected;
};
export const getBluetoothText = (connected) => {
if (!Bluetooth.enabled) return "Bluetooth off";
if (connected.length > 0) {
const dev = Bluetooth.getDevice(connected[0].address);
let battery_str = "";
if (dev.battery_percentage > 0) {
battery_str += ` ${dev.battery_percentage}%`;
}
return dev.name + battery_str;
}
return "Bluetooth on";
};

View File

@@ -0,0 +1,107 @@
import { Hyprland } from "../imports.js";
export let DEFAULT_MONITOR;
const connID = Hyprland.connect("notify::workspaces", () => {
Hyprland.disconnect(connID);
DEFAULT_MONITOR = {
name: Hyprland.monitors[0].name,
id: Hyprland.monitors[0].id,
};
});
export const changeWorkspace = (ws) =>
Hyprland.messageAsync(`dispatch workspace ${ws}`);
export const focusedSwitch = (self) => {
const id = Hyprland.active.workspace.id;
if (self.lastFocused == id) return;
self.children[self.lastFocused - 1].toggleClassName("focused", false);
self.children[id - 1].toggleClassName("focused", true);
self.lastFocused = id;
};
export const added = (self, name) => {
if (!name) return;
const ws = Hyprland.workspaces.find((e) => e.name == name);
const id = ws?.id ?? Number(name);
const child = self.children[id - 1];
child.monitor = {
name: ws?.monitor ?? DEFAULT_MONITOR.name,
id: ws?.monitorID ?? DEFAULT_MONITOR.id,
};
child.active = true;
child.toggleClassName(`monitor${child.monitor.id}`, true);
// if this id is bigger than the last biggest id, visibilise all other ws before it
if (id > self.biggestId) {
for (let i = self.biggestId; i <= id; i++) {
self.children[i - 1].visible = true;
}
self.biggestId = id;
}
};
const makeInvisible = (self, id) => {
if (id <= 1) return;
const child = self.children[id - 1];
if (child.active) {
self.biggestId = id;
return;
}
child.visible = false;
makeInvisible(self, id - 1);
};
export const removed = (self, name) => {
if (!name) return;
const id = Number(name);
const child = self.children[id - 1];
child.toggleClassName(`monitor${child.monitor.id}`, false);
child.active = false;
// if this id is the biggest id, unvisibilise it and all other inactives until the next active before it
if (id == self.biggestId) {
makeInvisible(self, id);
}
};
export const moveWorkspace = (self, data) => {
const [id, name] = data.split(",");
const child = self.children[id - 1];
// remove previous monitor class
child.toggleClassName(`monitor${child.monitor.id}`, false);
// add new monitor and class
const monitor = Hyprland.monitors.find((e) => e.name == name);
child.monitor = {
name,
id: monitor?.id ?? DEFAULT_MONITOR.id,
};
print(`child ${id}: monitor ${name} ${child.monitor.id}`);
child.toggleClassName(`monitor${child.monitor.id}`, true);
};
export const sortWorkspaces = () => {
return Hyprland.workspaces
.sort((x, y) => {
return x.id - y.id;
})
.filter((x) => {
return x.name.indexOf("special") == -1;
});
};
export const getLastWorkspaceId = () => sortWorkspaces().slice(-1)[0].id;
export const workspaceActive = (id) => sortWorkspaces().some((e) => e.id == id);

View File

@@ -0,0 +1,35 @@
export default {
bluetooth: {
active: "bluetooth-active-symbolic",
disabled: "bluetooth-disabled-symbolic",
disconnected: "bluetooth-disconnected-symbolic",
},
brightness: "display-brightness-symbolic",
media: {
play: "media-playback-start-symbolic",
pause: "media-playback-pause-symbolic",
next: "media-skip-forward-symbolic",
previous: "media-skip-backward-symbolic",
player: "multimedia-player-symbolic",
},
volume: {
muted: "audio-volume-muted-symbolic",
low: "audio-volume-low-symbolic",
medium: "audio-volume-medium-symbolic",
high: "audio-volume-high-symbolic",
overamplified: "audio-volume-overamplified-symbolic",
},
microphone: {
muted: "microphone-sensitivity-muted-symbolic",
low: "microphone-sensitivity-low-symbolic",
medium: "microphone-sensitivity-medium-symbolic",
high: "microphone-sensitivity-high-symbolic",
},
powerButton: "system-shutdown-symbolic",
};

View File

@@ -0,0 +1,46 @@
import { Icons, Utils } from "../imports.js";
import GLib from "gi://GLib";
export const findPlayer = (players) => {
// try to get the first active player
const activePlayer = players.find((p) => p.playBackStatus == "Playing");
if (activePlayer != null) return activePlayer;
// otherwise get the first "working" player
for (const p of players) {
if (p.title != "undefined") return p;
}
};
export const mprisStateIcon = (status) => {
const state = status == "Playing" ? "pause" : "play";
return Icons.media[state];
};
export const MEDIA_CACHE_PATH = Utils.CACHE_DIR + "/media";
export const blurredPath = MEDIA_CACHE_PATH + "/blurred";
export const generateBackground = (cover_path) => {
const url = cover_path;
if (!url) return "";
const makeBg = (bg) => `background: center/cover url('${bg}')`;
const blurred = blurredPath + url.substring(MEDIA_CACHE_PATH.length);
if (GLib.file_test(blurred, GLib.FileTest.EXISTS)) {
return makeBg(blurred);
}
Utils.ensureDirectory(blurredPath);
Utils.exec(`convert ${url} -blur 0x22 ${blurred}`);
return makeBg(blurred);
};
export function lengthStr(length) {
const min = Math.floor(length / 60);
const sec = Math.floor(length % 60);
const sec0 = sec < 10 ? "0" : "";
return `${min}:${sec0}${sec}`;
}

View File

@@ -0,0 +1,27 @@
import { Network } from "../imports.js";
export const getNetIcon = () => {
if (Network.connectivity == "none") return "";
if (Network.primary == "wired") return "network-wired";
return Network.wifi.icon_name;
};
export const getNetText = () => {
// no connection
if (Network.connectivity == "none") return "No connection";
// wired
if (Network.primary == "wired") return "Wired";
// wifi
const wifi = Network.wifi;
switch (wifi.internet) {
case "connected":
return wifi.ssid;
case "connecting":
return "Connecting";
case "disconnected":
return "Disconnected";
}
};

View File

@@ -0,0 +1,46 @@
import App from "resource:///com/github/Aylur/ags/app.js";
import { Widget } from "../imports.js";
const { Box, Revealer, Window } = Widget;
export default ({
name,
child,
revealerSetup = null,
transition = "crossfade",
transitionDuration = 200,
...props
}) => {
const window = Window({
name,
popup: false,
focusable: false,
visible: false,
...props,
setup: (self) => (self.getChild = () => child),
child: Box({
css: `
min-height: 1px;
min-width: 1px;
padding: 1px;
`,
child: Revealer({
transition,
transitionDuration,
child: child,
setup:
revealerSetup ??
((self) =>
self.hook(App, (self, currentName, visible) => {
if (currentName === name) {
self.reveal_child = visible;
}
})),
}),
}),
});
return window;
};