-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
150 lines (116 loc) · 3.3 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict'
require ('electron-reload')(__dirname, {
electron: require('${__dirname}/../../node_modules/electron')
});
const gkm = require('gkm');
const electron = require('electron');
const path = require('path');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const Tray = electron.Tray;
const iconPath = path.resolve(__dirname, './images/enter.ico');
const Menu = electron.Menu;
let tray = null;
const ipc = electron.ipcMain;
let mainWindow;
// read from db
const fs = require('fs');
// let rawdata = fs.readFileSync('db.json');
let rawdata = fs.readFileSync(path.resolve(__dirname, './db.json'));
let db = JSON.parse(rawdata);
const sounds = [
'sound_1.mp3',
'sound_2.mp3',
'sound_3.mp3'
];
function createWindow() {
mainWindow = new BrowserWindow({show: false});
mainWindow.loadURL(`file://${__dirname}/index.html`);
/* mainWindow.once('ready-to-show', () => {
mainWindow.show();
});
mainWindow.webContents.openDevTools();
mainWindow.on('closed', () => {
mainWindow = null;
tray.destroy();
}); */
}
// select previously selected sound to check/uncheck radio
function toggleRadio(menu) {
let menuItemIndex = db.selectedRadio;
menu.items[menuItemIndex].checked = true;
}
function changeSound(pos) {
db.selectedRadio = pos;
let selectedSound = sounds[pos];
mainWindow.webContents.send('sound-changed', path.resolve(__dirname, `./sounds/${selectedSound}`));
}
app.on('ready', () => {
createWindow();
tray = new Tray(iconPath);
let template1 = [];
for (let i=0; i<sounds.length; i++) {
let obj = {};
obj.label = `Sound ${i + 1}`,
obj.type = 'radio',
obj.checked = false,
obj.click = function(e) {
changeSound(i);
};
template1.push(obj);
}
let template2 = [
{
type: 'separator',
}, {
label: 'SOUND ON',
type: 'checkbox',
checked: db.isSoundOn,
click: function(e) {
db.isSoundOn = !db.isSoundOn;
}
}, {
type: 'separator',
}, {
role: 'quit'
}
]
let template = template1.concat(template2);
let ctxMenu = Menu.buildFromTemplate(template);
toggleRadio(ctxMenu);
tray.setContextMenu(ctxMenu);
tray.setToolTip('Clicky Keyboard - Play sound on key strokes');
gkm.events.on('key.pressed', (key) => {
// console.log(key);
if (!db.isSoundOn) return;
// play sound
mainWindow.webContents.send('key-pressed');
});
});
// change audio src in index.html to db's value
ipc.on('document-loaded', (e) => {
let selectedSound = sounds[db.selectedRadio];
e.sender.send('sound-changed', path.resolve(__dirname, `./sounds/${selectedSound}`));
});
app.setLoginItemSettings({openAtLogin: true});
app.on('window-all-closed', () => {
mainWindow = null;
tray.destroy();
app.quit();
});
app.on('before-quit', () => {
// update db
let data = JSON.stringify(db, null, 2);
fs.writeFileSync('db.json', data);
});
// for mac
app.on('activate', () => {
if (win === null) {
createWindow();
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
})