implemented auto-start

This commit is contained in:
tigerbot 2017-04-24 17:21:00 -06:00
parent af3adf660e
commit e1039de5fd
6 changed files with 103 additions and 1 deletions

View File

@ -11,6 +11,11 @@ So far the following have been implemented
* `crash-reporter.js`: Reporting side of the crash reporting * `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-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 * `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 ## Missing
The following have not yet been implemented in this demo The following have not yet been implemented in this demo

View File

@ -18,6 +18,15 @@
</div> </div>
<script>require("./drag-drop-render")</script> <script>require("./drag-drop-render")</script>
<div>
<h3>Auto-Start</h3>
<label>
<input type="checkbox" class="auto-start"/>
Start Electron Demo on startup
</label>
</div>
<script>require("./startup-render")</script>
<div> <div>
<button class="crasher">Crash Program</button> <button class="crasher">Crash Program</button>
</div> </div>

View File

@ -27,6 +27,7 @@ function createWindow () {
require('./menu').init(win); require('./menu').init(win);
require('./progress').init(win); require('./progress').init(win);
require('./drag-drop-main').init(win); require('./drag-drop-main').init(win);
require('./startup-main').init(win);
// // Open the DevTools. // // Open the DevTools.
// win.webContents.openDevTools(); // win.webContents.openDevTools();

View File

@ -1,7 +1,7 @@
{ {
"name": "electron-demo", "name": "electron-demo",
"productName": "ElectronDemo", "productName": "ElectronDemo",
"version": "0.0.4", "version": "0.0.5",
"description": "Demo Electron app to use all the 'sexy' features", "description": "Demo Electron app to use all the 'sexy' features",
"author": { "author": {
"name": "Daplie Labs", "name": "Daplie Labs",
@ -17,5 +17,8 @@
"electron": "^1.6.5", "electron": "^1.6.5",
"electron-installer-debian": "^0.5.1", "electron-installer-debian": "^0.5.1",
"electron-packager": "^8.6.0" "electron-packager": "^8.6.0"
},
"dependencies": {
"auto-launch": "^5.0.1"
} }
} }

67
startup-main.js Normal file
View File

@ -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,
};

17
startup-render.js Normal file
View File

@ -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);
}
});