Created
July 16, 2016 09:03
-
-
Save Chovin/dd29a9f8f89d2e00387db0787328efe3 to your computer and use it in GitHub Desktop.
updated code from tutorial: https://medium.com/developers-writing/building-a-desktop-application-with-electron-204203eeb658#.ix43bncqp
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'; | |
var 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'; | |
var soundButtons = document.querySelectorAll('.button-sound'); | |
for (var i = 0; i < soundButtons.length; i++) { | |
var soundButton = soundButtons[i]; | |
var soundName = soundButton.attributes['data-sound'].value; | |
prepareButton(soundButton, soundName); | |
} | |
function prepareButton(buttonEl, soundName) { | |
buttonEl.querySelector('span').style.backgroundImage = 'url("img/icons/' + soundName + '.png")'; | |
var audio = new Audio(__dirname + '/wav/' + soundName + '.wav'); | |
buttonEl.addEventListener('click', function () { | |
audio.currentTime = 0; | |
audio.play(); | |
}); | |
} | |
var ipc = require('electron').ipcRenderer; | |
var closeEl = document.querySelector('.close'); | |
closeEl.addEventListener('click', function () { | |
ipc.send('close-main-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'; | |
var soundButtons = document.querySelectorAll('.button-sound'); | |
for (var i = 0; i < soundButtons.length; i++) { | |
var soundButton = soundButtons[i]; | |
var soundName = soundButton.attributes['data-sound'].value; | |
prepareButton(soundButton, soundName); | |
} | |
function prepareButton(buttonEl, soundName) { | |
buttonEl.querySelector('span').style.backgroundImage = 'url("img/icons/' + soundName + '.png")'; | |
var audio = new Audio(__dirname + '/wav/' + soundName + '.wav'); | |
buttonEl.addEventListener('click', function () { | |
audio.currentTime = 0; | |
audio.play(); | |
}); | |
} | |
var ipc = require('electron').ipcRenderer; | |
var closeEl = document.querySelector('.close'); | |
closeEl.addEventListener('click', function () { | |
ipc.send('close-main-window'); | |
}); | |
ipc.on('global-shortcut', function (evt, arg) { | |
var event = new MouseEvent('click'); | |
soundButtons[arg].dispatchEvent(event); | |
}); |
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'; | |
var soundButtons = document.querySelectorAll('.button-sound'); | |
for (var i = 0; i < soundButtons.length; i++) { | |
var soundButton = soundButtons[i]; | |
var soundName = soundButton.attributes['data-sound'].value; | |
prepareButton(soundButton, soundName); | |
} | |
function prepareButton(buttonEl, soundName) { | |
buttonEl.querySelector('span').style.backgroundImage = 'url("img/icons/' + soundName + '.png")'; | |
var audio = new Audio(__dirname + '/wav/' + soundName + '.wav'); | |
buttonEl.addEventListener('click', function () { | |
audio.currentTime = 0; | |
audio.play(); | |
}); | |
} | |
var ipc = require('electron').ipcRenderer; | |
var closeEl = document.querySelector('.close'); | |
closeEl.addEventListener('click', function () { | |
ipc.send('close-main-window'); | |
}); | |
ipc.on('global-shortcut', function (evt, arg) { | |
var event = new MouseEvent('click'); | |
soundButtons[arg].dispatchEvent(event); | |
}); | |
var settingsEl = document.querySelector('.settings'); | |
settingsEl.addEventListener('click', function () { | |
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'; | |
var soundButtons = document.querySelectorAll('.button-sound'); | |
for (var i = 0; i < soundButtons.length; i++) { | |
var soundButton = soundButtons[i]; | |
var soundName = soundButton.attributes['data-sound'].value; | |
prepareButton(soundButton, soundName); | |
} | |
function prepareButton(buttonEl, soundName) { | |
buttonEl.querySelector('span').style.backgroundImage = 'url("img/icons/' + soundName + '.png")'; | |
var audio = new Audio(__dirname + '/wav/' + soundName + '.wav'); | |
buttonEl.addEventListener('click', function () { | |
audio.currentTime = 0; | |
audio.play(); | |
}); | |
} | |
var ipc = require('electron').ipcRenderer; | |
var closeEl = document.querySelector('.close'); | |
closeEl.addEventListener('click', function () { | |
ipc.send('close-main-window'); | |
}); | |
ipc.on('global-shortcut', function (evt, arg) { | |
var event = new MouseEvent('click'); | |
soundButtons[arg].dispatchEvent(event); | |
}); | |
var settingsEl = document.querySelector('.settings'); | |
settingsEl.addEventListener('click', function () { | |
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'; | |
var app = require('electron').app; | |
var BrowserWindow = require('electron').BrowserWindow; | |
var mainWindow = null; | |
app.on('ready', function() { | |
mainWindow = new BrowserWindow({ | |
frame: false, | |
height: 700, | |
resizable: false, | |
width: 368 | |
}); | |
mainWindow.loadURL('file://' + __dirname + '/app/index.html'); | |
}); | |
var ipc = require('electron').ipcMain; | |
ipc.on('close-main-window', function () { | |
app.quit(); | |
}); |
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'; | |
var app = require('electron').app; | |
var BrowserWindow = require('electron').BrowserWindow; | |
var mainWindow = null; | |
app.on('ready', function() { | |
mainWindow = new BrowserWindow({ | |
frame: false, | |
height: 700, | |
resizable: false, | |
width: 368 | |
}); | |
mainWindow.loadURL('file://' + __dirname + '/app/index.html'); | |
}); | |
var ipc = require('electron').ipcMain; | |
ipc.on('close-main-window', function () { | |
app.quit(); | |
}); | |
var globalShortcut = require('electron').globalShortcut; | |
app.on('ready', function() { | |
//... // existing code from earlier | |
globalShortcut.register('ctrl+shift+1', function () { | |
mainWindow.webContents.send('global-shortcut', 0); | |
}); | |
globalShortcut.register('ctrl+shift+2', function () { | |
mainWindow.webContents.send('global-shortcut', 1); | |
}); | |
}); |
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'; | |
var app = require('electron').app; | |
var BrowserWindow = require('electron').BrowserWindow; | |
var mainWindow = null; | |
var globalShortcut = require('electron').globalShortcut; | |
app.on('ready', function() { | |
mainWindow = new BrowserWindow({ | |
frame: false, | |
height: 700, | |
resizable: false, | |
width: 368 | |
}); | |
mainWindow.loadURL('file://' + __dirname + '/app/index.html'); | |
globalShortcut.register('ctrl+shift+1', function () { | |
mainWindow.webContents.send('global-shortcut', 0); | |
}); | |
globalShortcut.register('ctrl+shift+2', function () { | |
mainWindow.webContents.send('global-shortcut', 1); | |
}); | |
}); | |
var ipc = require('electron').ipcMain; | |
ipc.on('close-main-window', function () { | |
app.quit(); | |
}); | |
var settingsWindow = null; | |
ipc.on('open-settings-window', function () { | |
if (settingsWindow) { | |
return; | |
} | |
settingsWindow = new BrowserWindow({ | |
frame: false, | |
height: 200, | |
resizable: false, | |
width: 200 | |
}); | |
settingsWindow.loadURL('file://' + __dirname + '/app/settings.html'); | |
settingsWindow.on('closed', function () { | |
settingsWindow = null; | |
}); | |
}); | |
ipc.on('close-settings-window', function () { | |
if (settingsWindow) { | |
settingsWindow.close(); | |
} | |
}); |
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'; | |
var app = require('electron').app; | |
var BrowserWindow = require('electron').BrowserWindow; | |
var mainWindow = null; | |
var globalShortcut = require('electron').globalShortcut; | |
var configuration = require('./configuration.js'); | |
app.on('ready', function() { | |
mainWindow = new BrowserWindow({ | |
frame: false, | |
height: 700, | |
resizable: false, | |
width: 368 | |
}); | |
mainWindow.loadURL('file://' + __dirname + '/app/index.html'); | |
if (!configuration.readSettings('shortcutKeys')) { | |
configuration.saveSettings('shortcutKeys', ['ctrl', 'shift']); | |
} | |
setGlobalShortcuts(); | |
}); | |
function setGlobalShortcuts() { | |
globalShortcut.unregisterAll(); | |
var shortcutKeysSetting = configuration.readSettings('shortcutKeys'); | |
var shortcutPrefix = shortcutKeysSetting.length === 0 ? '' : shortcutKeysSetting.join('+') + '+'; | |
globalShortcut.register(shortcutPrefix + '1', function () { | |
mainWindow.webContents.send('global-shortcut', 0); | |
}); | |
globalShortcut.register(shortcutPrefix + '2', function () { | |
mainWindow.webContents.send('global-shortcut', 1); | |
}); | |
} | |
var ipc = require('electron').ipcMain; | |
ipc.on('close-main-window', function () { | |
app.quit(); | |
}); | |
var settingsWindow = null; | |
ipc.on('open-settings-window', function () { | |
if (settingsWindow) { | |
return; | |
} | |
settingsWindow = new BrowserWindow({ | |
frame: false, | |
height: 200, | |
resizable: false, | |
width: 200 | |
}); | |
settingsWindow.loadURL('file://' + __dirname + '/app/settings.html'); | |
settingsWindow.on('closed', function () { | |
settingsWindow = null; | |
}); | |
}); | |
ipc.on('close-settings-window', function () { | |
if (settingsWindow) { | |
settingsWindow.close(); | |
} | |
}); | |
ipc.on('set-global-shortcuts', function () { | |
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'; | |
var app = require('electron').app; | |
var BrowserWindow = require('electron').BrowserWindow; | |
var mainWindow = null; | |
app.on('ready', function() { | |
mainWindow = new BrowserWindow({ | |
height: 600, | |
width: 800 | |
}); | |
console.log('----------\n'+__dirname+'\n----------') | |
mainWindow.loadURL('file://' + __dirname + '/app/index.html'); | |
}); |
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'; | |
var ipc = require('electron').ipcRenderer; | |
var closeEl = document.querySelector('.close'); | |
closeEl.addEventListener('click', function (e) { | |
ipc.send('close-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'; | |
var ipc = require('electron').ipcRenderer; | |
var closeEl = document.querySelector('.close'); | |
closeEl.addEventListener('click', function (e) { | |
ipc.send('close-settings-window'); | |
}); | |
var configuration = require('../configuration.js'); | |
var modifierCheckboxes = document.querySelectorAll('.global-shortcut'); | |
for (var i = 0; i < modifierCheckboxes.length; i++) { | |
var shortcutKeys = configuration.readSettings('shortcutKeys'); | |
var modifierKey = modifierCheckboxes[i].attributes['data-modifier-key'].value; | |
modifierCheckboxes[i].checked = shortcutKeys.indexOf(modifierKey) !== -1; | |
modifierCheckboxes[i].addEventListener('click', function (e) { | |
bindModifierCheckboxes(e); | |
}); | |
} | |
function bindModifierCheckboxes(e) { | |
var shortcutKeys = configuration.readSettings('shortcutKeys'); | |
var modifierKey = e.target.attributes['data-modifier-key'].value; | |
if (shortcutKeys.indexOf(modifierKey) !== -1) { | |
var 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