main.js 3.7 KB
Newer Older
E
.  
Eugene Pankov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
const Config = require('electron-config')
const electron = require('electron')
const platform = require('os').platform()
require('electron-debug')({enabled: true, showDevTools: process.argv.indexOf('--debug') != -1})

let app = electron.app
let windowConfig = new Config({name: 'window'})


setupWindowManagement = () => {
    let windowCloseable

    app.window.on('close', (e) => {
        windowConfig.set('windowBoundaries', app.window.getBounds())
        if (!windowCloseable) {
E
.  
Eugene Pankov 已提交
16
            app.window.minimize()
E
.  
Eugene Pankov 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
            e.preventDefault()
        }
    })

    app.window.on('closed', () => {
        app.window = null
    })

    electron.ipcMain.on('window-closeable', (event, flag) => {
        windowCloseable = flag
    })

    electron.ipcMain.on('window-focus', () => {
        app.window.show()
        app.window.focus()
    })

E
.  
Eugene Pankov 已提交
34 35 36 37 38 39 40 41 42 43 44 45
    electron.ipcMain.on('window-maximize', () => {
        if (app.window.isMaximized()) {
            app.window.unmaximize()
        } else {
            app.window.maximize()
        }
    })

    electron.ipcMain.on('window-minimize', () => {
        app.window.minimize()
    })

E
.  
Eugene Pankov 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    app.on('before-quit', () => windowCloseable = true)
}


setupMenu = () => {
    var template = [{
        label: "Application",
        submenu: [
            { type: "separator" },
            { label: "Quit", accelerator: "CmdOrCtrl+Q", click: () => {
                app.window.webContents.send('host:quit-request')
            }}
        ]}, {
            label: "Edit",
            submenu: [
                { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
                { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
                { type: "separator" },
                { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
                { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
                { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
                { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
            ]
        }]

    electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate(template))
}


start = () => {
    let t0 = Date.now()

    let secondInstance = app.makeSingleInstance((argv) => {
        app.window.focus()
    })

    if (secondInstance) {
        app.quit()
        return
    }

    let options = {
        width: 800,
        height: 400,
E
.  
Eugene Pankov 已提交
90
        //icon: `${app.getAppPath()}/assets/img/icon.png`,
E
.  
Eugene Pankov 已提交
91
        title: 'ELEMENTS Benchmark',
E
.  
Eugene Pankov 已提交
92 93
        minWidth: 300,
        minHeight: 100,
E
.  
Eugene Pankov 已提交
94 95 96
        'web-preferences': {'web-security': false},
        //- background to avoid the flash of unstyled window
        backgroundColor: '#1D272D',
E
.  
Eugene Pankov 已提交
97
        frame: false,
E
.  
Eugene Pankov 已提交
98 99 100 101 102 103 104
    }
    Object.assign(options, windowConfig.get('windowBoundaries'))

    if (platform == 'darwin') {
        options.titleBarStyle = 'hidden'
    }

E
.  
Eugene Pankov 已提交
105
    app.commandLine.appendSwitch('disable-http-cache')
E
.  
Eugene Pankov 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    app.window = new electron.BrowserWindow(options)
    app.window.loadURL(`file://${app.getAppPath()}/assets/webpack/index.html`, {extraHeaders: "pragma: no-cache\n"})

    if (platform != 'darwin') {
        app.window.setMenu(null)
    }

    app.window.show()
    app.window.focus()

    setupWindowManagement()
    setupMenu()

    console.info(`Host startup: ${Date.now() - t0}ms`)
    t0 = Date.now()
    electron.ipcMain.on('app:ready', () => {
        console.info(`App startup: ${Date.now() - t0}ms`)
    })
}

app.on('ready', start)

app.on('activate', () => {
    if (!app.window)
        start()
    else {
        app.window.show()
        app.window.focus()
    }
})

process.on('uncaughtException', function(err) {
    console.log(err)
    app.window.webContents.send('uncaughtException', err)
})