Last active
January 7, 2016 05:21
-
-
Save takuan-osho/5f362327558bb9d69a52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| let nconf = require('nconf').file({file: getUserHome() + '/sound-machine-config.json'}); | |
| function saveSettings(settingKey, settingValue) { | |
| nconf.set(settingKey, settingValue); | |
| nconf.save(); | |
| } | |
| function readSettings(settingKey) { | |
| nconf.load(); | |
| return nconf.get(settingKey); | |
| } | |
| function getUserHome() { | |
| return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']; | |
| } | |
| module.exports = { | |
| saveSettings: saveSettings, | |
| readSettings: readSettings | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| let soundButtons = Array.from(document.querySelectorAll('.button-sound')); | |
| for (let soundButton of soundButtons) { | |
| let soundName = soundButton.attributes['data-sound'].value; | |
| prepareButton(soundButton, soundName); | |
| } | |
| function prepareButton(buttonEl, soundName) { | |
| buttonEl.querySelector('span').style.backgroundImage = `url("img/icons/${soundName}.png")`; | |
| let audio = new Audio(`${__dirname}/wav/${soundName}.wav`); | |
| buttonEl.addEventListener('click', () => { | |
| audio.currentTime = 0; | |
| audio.play(); | |
| }); | |
| } | |
| var ipc = require('ipc'); | |
| var closeEl = document.querySelector('.close'); | |
| closeEl.addEventListener('click', () => { | |
| ipc.send('close-main-window'); | |
| }); | |
| ipc.on('global-shortcut', (arg) => { | |
| var event = new MouseEvent('click'); | |
| soundButtons[arg].dispatchEvent(event); | |
| }); | |
| let settingsEl = document.querySelector('.settings'); | |
| settingsEl.addEventListener('click', () => { | |
| ipc.send('open-settings-window'); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| let electron = require('electron'); | |
| let app = electron.app; | |
| let BrowserWindow = electron.BrowserWindow; | |
| var mainWindow = null; | |
| let globalShortcut = electron.globalShortcut; | |
| var configuration = require('./configuration'); | |
| app.on('ready', () => { | |
| if (!configuration.readSettings('shortcutKeys')) { | |
| configuration.saveSettings('shortcutKeys', ['ctrl', 'shift']); | |
| } | |
| mainWindow = new BrowserWindow({ | |
| frame: false, | |
| height: 700, | |
| resizable: false, | |
| width: 368 | |
| }); | |
| mainWindow.loadURL(`file://${__dirname}/app/index.html`); | |
| setGlobalShortcuts(); | |
| }); | |
| function setGlobalShortcuts() { | |
| globalShortcut.unregisterAll(); | |
| let shortcutKeysSetting = configuration.readSettings('shortcutKeys'); | |
| let shortcutPrefix = shortcutKeysSetting.length === 0 ? '' : shortcutKeysSetting.join('+') + '+'; | |
| globalShortcut.register(shortcutPrefix + '1', () => { | |
| mainWindow.webContents.send('global-shortcut', 0); | |
| }); | |
| globalShortcut.register(shortcutPrefix + '2', () => { | |
| mainWindow.webContents.send('global-shortcut', 1); | |
| }); | |
| } | |
| var ipc = electron.ipcMain; | |
| ipc.on('close-main-window', () => { | |
| app.quit(); | |
| }); | |
| var settingsWindow = null; | |
| ipc.on('open-settings-window', () => { | |
| if (settingsWindow) { | |
| return; | |
| } | |
| settingsWindow = new BrowserWindow({ | |
| frame: false, | |
| height: 200, | |
| resizable: false, | |
| width: 200 | |
| }); | |
| settingsWindow.loadUrl('file://' + __dirname + '/app/settings.html'); | |
| settingsWindow.on('closed', () => { | |
| settingsWindow = null; | |
| }); | |
| }); | |
| ipc.on('close-settings-window', () => { | |
| if (settingsWindow) { | |
| settingsWindow.close(); | |
| } | |
| }); | |
| ipc.on('set-global-shortcuts', () => { | |
| setGlobalShortcuts(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| let electron = require('electron'); | |
| const ipc = electron.ipcRenderer; | |
| let closeEl = document.querySelector('.close'); | |
| closeEl.addEventListener('click', (e) => { | |
| ipc.send('close-settings-window'); | |
| }); | |
| let configuration = require('../configuration'); | |
| let modifierCheckboxes = document.querySelectorAll('.global-shortcut'); | |
| for (let modifierCheckbox of Array.from(modifierCheckboxes)) { | |
| let shortcutKeys = configuration.readSettings('shortcutKeys'); | |
| let modifierKey = modifierCheckbox.attributes['data-modifier-key'].value; | |
| modifierCheckbox.checked = shortcutKeys.indexOf(modifierKey) !== -1; | |
| modifierCheckbox.addEventListener('click', function (e) { | |
| bindModifierCheckboxes(e); | |
| }); | |
| } | |
| function bindModifierCheckboxes(e) { | |
| let shortcutKeys = configuration.readSettings('shortcutKeys'); | |
| let modifierKey = e.target.attributes['data-modifier-key'].value; | |
| if (shortcutKeys.indexOf(modifierKey) !== -1) { | |
| let shortcutKeyIndex = shortcutKeys.indexOf(modifierKey); | |
| shortcutKeys.splice(shortcutKeyIndex, 1); | |
| } | |
| else { | |
| shortcutKeys.push(modifierKey); | |
| } | |
| configuration.saveSettings('shortcutKeys', shortcutKeys); | |
| ipc.send('set-global-shortcuts'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment