implemented auto-start
This commit is contained in:
		
							parent
							
								
									af3adf660e
								
							
						
					
					
						commit
						e1039de5fd
					
				@ -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
 | 
			
		||||
 | 
			
		||||
@ -18,6 +18,15 @@
 | 
			
		||||
    </div>
 | 
			
		||||
    <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>
 | 
			
		||||
      <button class="crasher">Crash Program</button>
 | 
			
		||||
    </div>
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										1
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								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();
 | 
			
		||||
 | 
			
		||||
@ -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"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										67
									
								
								startup-main.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								startup-main.js
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										17
									
								
								startup-render.js
									
									
									
									
									
										Normal 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);
 | 
			
		||||
  }
 | 
			
		||||
});
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user