2024-07-05 15:10:53 +02:00
|
|
|
import { Applications } from "resource:///com/github/Aylur/ags/service/applications.js";
|
2024-07-05 15:50:08 +02:00
|
|
|
import GLib from "gi://GLib?version=2.0";
|
2024-07-05 15:10:53 +02:00
|
|
|
|
|
|
|
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: {} },
|
|
|
|
);
|
|
|
|
|
2024-07-05 15:50:08 +02:00
|
|
|
function fileExists(path) {
|
|
|
|
return GLib.file_test(path, GLib.FileTest.EXISTS);
|
|
|
|
}
|
|
|
|
|
2024-07-05 15:10:53 +02:00
|
|
|
export function getIconName(client) {
|
|
|
|
if (!client) {
|
2024-07-05 15:50:08 +02:00
|
|
|
return "";
|
2024-07-05 15:10:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-07-05 15:50:08 +02:00
|
|
|
app_icons.classOrNames[client.class] = "";
|
2024-07-05 15:10:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
}
|