Skip to content

Instantly share code, notes, and snippets.

@ada-lovecraft
Last active October 16, 2017 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ada-lovecraft/310f6bf953a809cf5b07ee6e131e0019 to your computer and use it in GitHub Desktop.
Save ada-lovecraft/310f6bf953a809cf5b07ee6e131e0019 to your computer and use it in GitHub Desktop.
Electron Window Manager
const WindowManager = require('./WindowManager()')
const {
app,
globalShortcut,
//ipcMain allows us to recieve requests from index.js using the ipc rendering (kinda sets a listener)
ipcMain
}, electron = require('electron');
const path = require('path');
const url = require('url');
//This configuration tool allows us to save general config settings to a JS file in the app data folder
const configuration = require('./configuration');
// utilities and composables
const because = (test, cb) => test ? cb() : void(0)
const formatURL = filename => url.format({
pathname: path.join(__dirname, filename),
protocol: 'file:',
slashes: true
})
const createSplash = () => {
const win = WindowManager.createWindow('splash')
win.once('ready-to-show', () => {
win.show()
})
return win
}
const createMain = () => {
const win = WindowManager.createWindow('main')
win.once('ready-to-show', () => {
WindowManager.grab('splash').hide()
win.show()
})
return win
}
app.on('ready', function() {
// Create the splash Window
const splash = createSplash()
let fptr = 'splashWindow.html'
if (!configuration.readSettings('firstRun')) {
fptr = 'firstRun.html'
}
WindowManager.loadFileForWindow(formatURL(fptr), splash)
WindowManager.activate(splash)
// Create The Main Window
// though this might need to be its own function
const main = createMain();
const ret = globalShortcut.register('CommandOrControl+X', () => {
console.log('CommandOrControl+X is pressed');
splash.close();
main.close();
});
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (!WindowManager.checkWindowExists('main')) {
createMain()
}
});
app.on('window-all-closed', app.quit);
app.on('before-quit', () => {
const win = WindowManager.grab('main')
win.removeAllListeners('close');
win.close();
});
const {BrowserWindow} = require('electron')
const {lebab, keys} = require('./utils')
const debug = require('debug')
const log = debug('WindowManager:log')
var windowID = 0
class WindowManager {
constructor() {
this.windows = {}
this.active = null
}
checkWindowExists(name) {
return !!this.windows[name]
}
/**
* returns a window's id from either an id or a BrowserWindow Object
* helper method
* @method getID
* @param {String | BrowserWindow} winRef - areference to a BrowserWindow object
* @return {String} the window's id
*/
getID(winRef) {
if(typeof winRef == 'String') {
const ref = lebab(winRef)
if(this.checkWindowExists(ref)) {
return ref
} else {
throw new ReferenceError('No Window Named ${winRef} exists')
}
} else {
const id = winRef.__name
if(!id) {
throw new ReferenceError('The given window is not managed by the window Manager. Please add() it first.')
}
}
}
grab(name) {
const id = lebab(name)
const win = this.windows[id]
if(!this.checkWindowExists(id)) {
throw new ReferenceError(`No Window Named ${name} exists`)
}
return this.windows[id]
}
loadFileForWindow(fptr, winRef) {
const id = getID(windRef)
const win = this.grab(id)
win.loadURL(fptr)
}
addWindow(name, win) {
const id = lebab(name)
if(typeof wind !== BrowserWindow) {
throw new TypeError(`Error: Can not add object named ${name}. Only BrowserWindow instances are allowed`)
}
if(this.checkWindowExists(id)) {
throw new ReferenceError(`Cannot add a new window named ${name} as it already exists`)
}
win.__id = windowID++
win.__name = id
this.windows[id] = win
win.on('closed', () => this.destroy(win))
log('added window named:', name)
return win
}
createWindow(name) {
const id = lebab(name)
let win = this.windows[id]
if(win) {
log('destroying old window named:', name)
this.destroy(win)
}
const win = new BrowserWindow({
frame: false,
kiosk: false,
show: false,
width: 1920,
height: 1080,
resizable: false,
backgroundColor: '#000000'
});
// cleanup
win.on('closed', () => this.destroy(win))
win.__name = name
win.__id = windowID++
log('created new window named:', name)
}
destroy(winRef) {
const id = this.getID(winRef)
try {
this.windows[id].destroy()
} catch(err) {
log('failed to destory window:', id)
throw err
}
}
activate(winRef) {
const id = this.getID(winRef)
const win = this.grab(id)
this.active = win
}
sendCommand(cmd, data) {
this.active.webContents.send(cmd, data)
}
}
module.exports = new WindowManager()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment