-
Notifications
You must be signed in to change notification settings - Fork 1
/
dark_apps.js
95 lines (78 loc) · 2.49 KB
/
dark_apps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
const { exec } = require('child_process');
const { readFile } = require('fs');
const path = require('path');
const windowsWithDarkThemes = new Map();
const windowsClasses = new Map();
const wm_classes = [];
function getWindowIdsFromRaw(data) {
const stringData = data.toString();
const hexValueRegEx = /0x[a-fA-F0-9]+/g;
const windowIds = stringData.match(hexValueRegEx);
if (windowIds.length) {
return windowIds;
}
return [];
}
function loadConfig() {
const load = resolve => {
const configPath = path.resolve(process.env.HOME, '.dark');
readFile(configPath, (err, data) => {
if (err) {
console.error(err)
return
}
data.toString().split('\n')
.filter(s => !!s)
.forEach(s => wm_classes.push(s));
resolve();
});
}
return new Promise(load);
}
function checkWindowClass(windowId) {
const check = resolve => {
const process = exec(`xprop -id ${windowId} WM_CLASS`);
process.stdout.once('data', wmClass => {
windowsClasses.set(windowId, wmClass)
const hasDarkTheme = wm_classes.some(cls => wmClass.indexOf(cls) !== -1);
resolve(hasDarkTheme);
});
};
return new Promise(check);
}
function setDarkTheme(windowId) {
windowsWithDarkThemes.set(windowId, true);
const process = exec(`xprop -f _GTK_THEME_VARIANT 8u -set _GTK_THEME_VARIANT "dark" -id ${windowId}`);
process.stdout.once('end', () => {
const wmClass = windowsClasses.get(windowId);
console.info('Dark theme set for window with id', windowId, 'and class', wmClass);
});
}
function processData(data) {
const windowIds = getWindowIdsFromRaw(data)
for (const windowId of windowIds) {
checkWindowClass(windowId)
.then(hasDarkWindow => {
if (hasDarkWindow && !windowsWithDarkThemes.get(windowId)) {
setDarkTheme(windowId);
}
});
}
}
function initialCheck() {
const process = exec('xprop -root | grep "_NET_CLIENT_LIST(WINDOW)"');
process.stdout.once('data', data => processData(data));
}
function listenForWindows() {
const process = exec('xprop -spy -root _NET_ACTIVE_WINDOW');
process.stdout.on('data', data => processData(data));
}
function main() {
loadConfig()
.then(() => {
initialCheck();
listenForWindows();
});
}
main();