diff --git a/README.md b/README.md index 01cce4c..e000263 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,11 @@ So far the following have been implemented * `crash-reporter.js`: Reporting side of the crash reporting * `drag-drop-main.js`: Handling of drag-and-drop events onto the dock or launcher * `drag-drop-render.js`: Handling of drag-and-drop events onto the window +* `startup-main.js`: Enabling and disabling auto-startup feature + +**Disclaimer**: This repository has been structured to separate each feature as much +as possible. As such not all of the communication is set up in the same way it should +be in more complete and complicated applications. ## Missing The following have not yet been implemented in this demo diff --git a/index.html b/index.html index 1eaf9e3..d5a69c6 100644 --- a/index.html +++ b/index.html @@ -18,6 +18,15 @@ +
+

Auto-Start

+ +
+ +
diff --git a/index.js b/index.js index 1082c57..ceae948 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,7 @@ function createWindow () { require('./menu').init(win); require('./progress').init(win); require('./drag-drop-main').init(win); + require('./startup-main').init(win); // // Open the DevTools. // win.webContents.openDevTools(); diff --git a/package.json b/package.json index 67d5aff..53ae05d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "electron-demo", "productName": "ElectronDemo", - "version": "0.0.4", + "version": "0.0.5", "description": "Demo Electron app to use all the 'sexy' features", "author": { "name": "Daplie Labs", @@ -17,5 +17,8 @@ "electron": "^1.6.5", "electron-installer-debian": "^0.5.1", "electron-packager": "^8.6.0" + }, + "dependencies": { + "auto-launch": "^5.0.1" } } diff --git a/startup-main.js b/startup-main.js new file mode 100644 index 0000000..560be59 --- /dev/null +++ b/startup-main.js @@ -0,0 +1,67 @@ +var AutoLaunch = require('auto-launch'); +var electron = require('electron'); +var app = electron.app; +var ipc = electron.ipcMain; + +// On Mac, work around a bug in auto-launch where it opens a Terminal window +// See https://github.com/Teamwork/node-auto-launch/issues/28#issuecomment-222194437 +var appPath; +if (process.platform === 'darwin') { + appPath = app.getPath('exe').replace(/\.app\/Content.*/, '.app'); +} else { + appPath = undefined; // Use the default +} + +var appLauncher = new AutoLaunch({ + name: 'ElectronDemo', + path: appPath, + isHidden: true +}); + +var win; +function init(window) { + if (win) { + console.error("can't initiliaze startup multiple times"); + return; + } + win = window; + + ipc.on('startupChange', function (ev, state) { + if (state) { + install(); + } else { + uninstall(); + } + }); + ipc.on('reqStartupState', function () { + appLauncher + .isEnabled() + .then(function (enabled) { + win.webContents.send('startupState', enabled); + }); + }); +} + +function install () { + return appLauncher + .isEnabled() + .then(function (enabled) { + if (!enabled) { + return appLauncher.enable(); + } + }); +} + +function uninstall () { + return appLauncher + .isEnabled() + .then(function (enabled) { + if (enabled) { + return appLauncher.disable(); + } + }); +} + +module.exports = { + init: init, +}; diff --git a/startup-render.js b/startup-render.js new file mode 100644 index 0000000..1ec7dcc --- /dev/null +++ b/startup-render.js @@ -0,0 +1,17 @@ +var electron = require('electron'); +var ipc = electron.ipcRenderer; + +ipc.on('startupState', function (e, state) { + var checkbox = document.getElementsByClassName('auto-start')[0]; + checkbox.checked = state; +}); + +setTimeout(function () { + ipc.send('reqStartupState'); +}, 1000); + +document.body.addEventListener('change', function (ev) { + if (ev.target.classList.contains('auto-start')) { + ipc.send('startupChange', ev.target.checked); + } +});