-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
90 lines (72 loc) · 2.18 KB
/
main.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
const { app, Menu, Tray } = require('electron');
const os = require('os');
const path = require('path');
const glob = require('glob-fs')();
const mv = require('mv');
const fontsDirectory = 'Library/Fonts/';
const stashDirectory = 'Library/LoseTheFonts/';
let tray = null;
let fonts_enabled = true;
function start() {
process.chdir(os.homedir());
app.dock.hide();
setupTray();
}
function setupTray() {
tray = new Tray(getIcon(true));
tray.setHighlightMode('never');
tray.on('click', event => {
toggleFonts();
});
tray.on('right-click', _ => {
tray.popUpContextMenu(getContextMenu(fonts_enabled));
});
}
function getIcon(is_active) {
const icon = {
active: path.join(__dirname, 'iconTemplate.png'),
inactive: path.join(__dirname, 'iconInactiveTemplate.png'),
};
return is_active ? icon.active : icon.inactive;
}
function setIcon(is_active) {
tray.setImage(getIcon(is_active));
}
function getContextMenu(all_fonts = true) {
const contextMenu = Menu.buildFromTemplate([
{ label: 'All fonts', type: 'radio', click: () => toggleFonts(true), checked: all_fonts},
{ label: 'System fonts', type: 'radio', click: () => toggleFonts(false), checked: !all_fonts },
{ label: 'Quit', role: 'quit' },
]);
return contextMenu;
}
function toggleFonts(are_enabled = !fonts_enabled) {
fonts_enabled = are_enabled;
setIcon(are_enabled);
if (!are_enabled) {
stashFonts();
} else {
unStashFonts();
}
}
function stashFonts() {
glob.readdirStream(fontsDirectory + '*')
.on('data', function (file) {
if (!file.isFile()) return;
mv(file.path, stashDirectory + file.basename, { mkdirp: true }, (err) => {
if (err) throw err;
});
})
.on('error', console.error);
}
function unStashFonts() {
glob.readdirStream(stashDirectory + '*')
.on('data', function(file) {
if (!file.isFile()) return;
mv(file.path, fontsDirectory + file.basename, (err) => {
if (err) throw err;
});
})
.on('error', console.error);
}
app.on('ready', start);