icons: add regex substitutions (#487)

This commit is contained in:
end-4 2024-05-10 23:31:39 +07:00
parent 10d64ad6fe
commit f56f8c250c
4 changed files with 28 additions and 25 deletions

View file

@ -125,7 +125,13 @@ let configOptions = {
'wps': "wps-office2019-kprometheus",
'wpsoffice': "wps-office2019-kprometheus",
'': "image-missing",
}
},
regexSubstitutions: [
{
regex: /^steam_app_(\d+)$/,
replace: "steam_icon_$1",
}
]
},
'keybinds': {
// Format: Mod1+Mod2+key. CaSe SeNsItIvE!

View file

@ -6,8 +6,23 @@ export function iconExists(iconName) {
}
export function substitute(str) {
if(userOptions.icons.substitutions[str]) return userOptions.icons.substitutions[str];
// Normal substitutions
if (userOptions.icons.substitutions[str])
return userOptions.icons.substitutions[str];
if (!iconExists(str)) str = str.toLowerCase().replace(/\s+/g, '-'); // Turn into kebab-case
// Regex substitutions
for (let i = 0; i < userOptions.icons.regexSubstitutions.length; i++) {
const substitution = userOptions.icons.regexSubstitutions[i];
const replacedName = str.replace(
substitution.regex,
substitution.replace,
);
if (replacedName != str) return replacedName;
}
// Guess: convert to kebab case
if (!iconExists(str)) str = str.toLowerCase().replace(/\s+/g, "-");
// Original string
return str;
}
}

View file

@ -11,6 +11,7 @@ const { Box, Revealer } = Widget;
import { setupCursorHover } from '../.widgetutils/cursorhover.js';
import { getAllFiles, searchIcons } from './icons.js'
import { MaterialIcon } from '../.commonwidgets/materialicon.js';
import { substitute } from '../.miscutils/icons.js';
const icon_files = userOptions.icons.searchPaths.map(e => getAllFiles(e)).flat(1)
@ -24,25 +25,6 @@ function clearTimes() {
timers = []
}
function substitute(str) {
const subs = [
{ from: 'code-url-handler', to: 'visual-studio-code' },
{ from: 'Code', to: 'visual-studio-code' },
{ from: 'GitHub Desktop', to: 'github-desktop' },
{ from: 'wps', to: 'wps-office2019-kprometheus' },
{ from: 'gnome-tweaks', to: 'org.gnome.tweaks' },
{ from: 'Minecraft* 1.20.1', to: 'minecraft' },
{ from: '', to: 'image-missing' },
];
for (const { from, to } of subs) {
if (from === str)
return to;
}
return str;
}
function ExclusiveWindow(client) {
const fn = [
(client) => !(client !== null && client !== undefined),

View file

@ -67,7 +67,7 @@ export default (overviewMonitor = 0) => {
const iconName = substitute(c);
const appIcon = iconExists(iconName) ? Widget.Icon({
icon: substitute(c),
icon: iconName,
size: Math.min(w, h) * userOptions.overview.scale / 2.5,
}) : MaterialIcon('terminal', 'gigantic', {
css: `font-size: ${Math.min(w, h) * userOptions.overview.scale / 2.5}px`,
@ -424,4 +424,4 @@ export default (overviewMonitor = 0) => {
)
}),
});
}
}