dotfiles/home/ags/files/utils.js

78 lines
2.1 KiB
JavaScript

import { Applications } from "resource:///com/github/Aylur/ags/service/applications.js";
import GLib from "gi://GLib?version=2.0";
const app_icons = new Applications().list.reduce(
(acc, app) => {
if (app.icon_name) {
acc.classOrNames[app.wm_class ?? app.name] = app.icon_name;
acc.executables[app.executable] = app.icon_name;
}
return acc;
},
{ classOrNames: {}, executables: {} },
);
function fileExists(path) {
return GLib.file_test(path, GLib.FileTest.EXISTS);
}
export function getIconName(client) {
if (!client) {
return "";
}
let icon = app_icons.classOrNames[client.class];
if (!icon) {
// TODO cache?
const filePath = `${App.configDir}/assets/${client.class}.png`;
if (fileExists(filePath)) {
icon = filePath;
app_icons.classOrNames[client.class] = icon;
}
}
if (!icon) {
// TODO cache?
const filePath = `${App.configDir}/assets/${client.class}.svg`;
if (fileExists(filePath)) {
icon = filePath;
app_icons.classOrNames[client.class] = icon;
}
}
if (!icon) {
const binaryName = Utils.exec(`ps -p ${client.pid} -o comm=`);
icon = app_icons.executables[binaryName];
if (!icon) {
let key = Object.keys(app_icons.executables).find((key) => key.startsWith(binaryName));
if (key) {
icon = app_icons.executables[key];
}
}
if (icon) {
app_icons[client.class] = icon;
}
}
if (!icon) {
const icon_key = Object.keys(app_icons.classOrNames).find(
(key) =>
key.includes(client.title) ||
key.includes(client.initialTitle) ||
key.includes(client.initialClass) ||
key.includes(client.class),
);
if (icon_key) {
icon = app_icons.classOrNames[icon_key];
app_icons.classOrNames[client.class] = icon;
}
}
if (!icon) {
app_icons.classOrNames[client.class] = "";
}
return icon;
}