Files: 347ecc7597fd3babb873a1d28f4713bd560424ec / index.js
3694 bytesRaw
1 | var defaultMenu = require('electron-default-menu') |
2 | var WindowState = require('electron-window-state') |
3 | var electron = require('electron') |
4 | var Menu = electron.Menu |
5 | var Path = require('path') |
6 | |
7 | var windows = {} |
8 | var quitting = false |
9 | |
10 | console.log('STARTING electron') |
11 | |
12 | electron.app.on('ready', () => { |
13 | var menu = defaultMenu(electron.app, electron.shell) |
14 | var view = menu.find(x => x.label === 'View') |
15 | view.submenu = [ |
16 | { role: 'reload' }, |
17 | { role: 'toggledevtools' }, |
18 | { type: 'separator' }, |
19 | { role: 'resetzoom' }, |
20 | { role: 'zoomin' }, |
21 | { role: 'zoomout' }, |
22 | { type: 'separator' }, |
23 | { role: 'togglefullscreen' } |
24 | ] |
25 | if (process.platform === 'darwin') { |
26 | var win = menu.find(x => x.label === 'Window') |
27 | win.submenu = [ |
28 | { role: 'minimize' }, |
29 | { role: 'zoom' }, |
30 | { role: 'close', label: 'Close' }, |
31 | { type: 'separator' }, |
32 | { role: 'front' } |
33 | ] |
34 | } |
35 | |
36 | Menu.setApplicationMenu(Menu.buildFromTemplate(menu)) |
37 | |
38 | startBackgroundProcess() |
39 | |
40 | // wait until server has started before opening main window |
41 | electron.ipcMain.once('server-started', function (ev, config) { |
42 | openMainWindow() |
43 | }) |
44 | |
45 | electron.app.on('before-quit', function () { |
46 | quitting = true |
47 | }) |
48 | |
49 | // allow inspecting of background process |
50 | electron.ipcMain.on('open-background-devtools', function (ev, config) { |
51 | if (windows.background) { |
52 | windows.background.webContents.openDevTools({detach: true}) |
53 | } |
54 | }) |
55 | }) |
56 | |
57 | function startBackgroundProcess () { |
58 | if (!windows.background) { |
59 | windows.background = openWindow(Path.join(__dirname, 'background-process.js'), { |
60 | connect: false, |
61 | center: true, |
62 | fullscreen: false, |
63 | fullscreenable: false, |
64 | height: 150, |
65 | maximizable: false, |
66 | minimizable: false, |
67 | resizable: false, |
68 | show: false, |
69 | skipTaskbar: true, |
70 | title: 'patchbay-server', |
71 | useContentSize: true, |
72 | width: 150 |
73 | }) |
74 | } |
75 | } |
76 | |
77 | function openMainWindow () { |
78 | if (!windows.main) { |
79 | var windowState = WindowState({ |
80 | defaultWidth: 1024, |
81 | defaultHeight: 768 |
82 | }) |
83 | windows.main = openWindow(Path.join(__dirname, 'main.js'), { |
84 | minWidth: 800, |
85 | x: windowState.x, |
86 | y: windowState.y, |
87 | width: windowState.width, |
88 | height: windowState.height, |
89 | autoHideMenuBar: true, |
90 | title: 'Patchbay', |
91 | show: true, |
92 | backgroundColor: '#FFF', |
93 | icon: './assets/icon.png' |
94 | }) |
95 | windowState.manage(windows.main) |
96 | windows.main.setSheetOffset(40) |
97 | windows.main.on('close', function (e) { |
98 | if (!quitting && process.platform === 'darwin') { |
99 | e.preventDefault() |
100 | windows.main.hide() |
101 | } |
102 | }) |
103 | windows.main.on('closed', function () { |
104 | windows.main = null |
105 | if (process.platform !== 'darwin') electron.app.quit() |
106 | }) |
107 | } |
108 | } |
109 | |
110 | function openWindow (path, opts) { |
111 | var window = new electron.BrowserWindow(opts) |
112 | window.webContents.on('dom-ready', function () { |
113 | window.webContents.executeJavaScript(` |
114 | var electron = require('electron') |
115 | var h = require('mutant/h') |
116 | electron.webFrame.setZoomLevelLimits(1, 1) |
117 | var title = ${JSON.stringify(opts.title || 'Patchbay')} |
118 | document.documentElement.querySelector('head').appendChild( |
119 | h('title', title) |
120 | ) |
121 | require(${JSON.stringify(path)}) |
122 | `) // NOTE tried process(electron) |
123 | }) |
124 | |
125 | window.webContents.on('will-navigate', function (e, url) { |
126 | e.preventDefault() |
127 | electron.shell.openExternal(url) |
128 | }) |
129 | |
130 | window.webContents.on('new-window', function (e, url) { |
131 | e.preventDefault() |
132 | electron.shell.openExternal(url) |
133 | }) |
134 | |
135 | window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html')) |
136 | return window |
137 | } |
138 | |
139 |
Built with git-ssb-web