Files: 6d8b19c2ddca9ab63af6775ccb64d0942a23d808 / index.js
4354 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 | electron.app.on('ready', () => { |
12 | startMenus() |
13 | |
14 | startBackgroundProcess() |
15 | // wait until server has started before opening main window |
16 | electron.ipcMain.once('server-started', function (ev, config) { |
17 | openMainWindow() |
18 | }) |
19 | |
20 | electron.app.on('before-quit', function () { |
21 | quitting = true |
22 | }) |
23 | |
24 | electron.app.on('activate', function (e) { |
25 | // reopen the app when dock icon clicked on macOS |
26 | if (windows.main) { |
27 | windows.main.show() |
28 | } |
29 | }) |
30 | |
31 | // allow inspecting of background process |
32 | electron.ipcMain.on('open-background-devtools', function (ev, config) { |
33 | if (windows.background) { |
34 | windows.background.webContents.openDevTools({ detach: true }) |
35 | } |
36 | }) |
37 | }) |
38 | |
39 | function startBackgroundProcess () { |
40 | if (windows.background) return |
41 | |
42 | windows.background = openWindow(Path.join(__dirname, 'server.js'), { |
43 | title: 'patchbay-server', |
44 | show: false, |
45 | connect: false, |
46 | width: 150, |
47 | height: 150, |
48 | center: true, |
49 | fullscreen: false, |
50 | fullscreenable: false, |
51 | maximizable: false, |
52 | minimizable: false, |
53 | resizable: false, |
54 | skipTaskbar: true, |
55 | useContentSize: true |
56 | }) |
57 | } |
58 | |
59 | function openMainWindow () { |
60 | if (windows.main) return |
61 | |
62 | var windowState = WindowState({ |
63 | defaultWidth: 1024, |
64 | defaultHeight: 768 |
65 | }) |
66 | windows.main = openWindow(Path.join(__dirname, 'main.js'), { |
67 | title: 'Patchbay', |
68 | show: true, |
69 | x: windowState.x, |
70 | y: windowState.y, |
71 | minWidth: 800, |
72 | width: windowState.width, |
73 | height: windowState.height, |
74 | autoHideMenuBar: true, |
75 | frame: !process.env.FRAME, |
76 | // titleBarStyle: 'hidden', |
77 | backgroundColor: '#FFF', |
78 | icon: './assets/icon.png' |
79 | }) |
80 | windowState.manage(windows.main) |
81 | windows.main.setSheetOffset(40) |
82 | windows.main.on('close', function (e) { |
83 | if (!quitting && process.platform === 'darwin') { |
84 | e.preventDefault() |
85 | windows.main.hide() |
86 | } |
87 | }) |
88 | windows.main.on('closed', function () { |
89 | windows.main = null |
90 | if (process.platform !== 'darwin') electron.app.quit() |
91 | }) |
92 | } |
93 | |
94 | function openWindow (path, opts) { |
95 | var window = new electron.BrowserWindow(opts) |
96 | |
97 | window.webContents.on('dom-ready', function () { |
98 | window.webContents.executeJavaScript(` |
99 | var electron = require('electron') |
100 | var h = require('mutant/h') |
101 | electron.webFrame.setVisualZoomLevelLimits(1, 1) |
102 | var title = ${JSON.stringify(opts.title || 'Patchbay')} |
103 | document.documentElement.querySelector('head').appendChild( |
104 | h('title', title) |
105 | ) |
106 | require(${JSON.stringify(path)}) |
107 | `) // NOTE tried process(electron) |
108 | }) |
109 | |
110 | window.webContents.on('will-navigate', function (e, url) { |
111 | e.preventDefault() |
112 | electron.shell.openExternal(url) |
113 | }) |
114 | |
115 | window.webContents.on('new-window', function (e, url) { |
116 | e.preventDefault() |
117 | electron.shell.openExternal(url) |
118 | }) |
119 | |
120 | window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html')) |
121 | return window |
122 | } |
123 | |
124 | function startMenus () { |
125 | var menu = defaultMenu(electron.app, electron.shell) |
126 | var view = menu.find(x => x.label === 'View') |
127 | view.submenu = [ |
128 | { role: 'reload' }, |
129 | { role: 'toggledevtools' }, |
130 | { type: 'separator' }, |
131 | { role: 'resetzoom' }, |
132 | { role: 'zoomin' }, |
133 | { role: 'zoomout' }, |
134 | { type: 'separator' }, |
135 | { role: 'togglefullscreen' } |
136 | ] |
137 | var win = menu.find(x => x.label === 'Window') |
138 | win.submenu = [ |
139 | { role: 'minimize' }, |
140 | { role: 'zoom' }, |
141 | { role: 'close', label: 'Close Window', accelerator: 'CmdOrCtrl+Shift+W' }, |
142 | { type: 'separator' }, |
143 | { |
144 | label: 'Close Tab', |
145 | accelerator: 'CmdOrCtrl+W', |
146 | click () { |
147 | windows.main.webContents.send('closeTab') |
148 | } |
149 | }, |
150 | { |
151 | label: 'Select Next Tab', |
152 | accelerator: 'CmdOrCtrl+Shift+]', |
153 | click () { |
154 | windows.main.webContents.send('nextTab') |
155 | } |
156 | }, |
157 | { |
158 | label: 'Select Previous Tab', |
159 | accelerator: 'CmdOrCtrl+Shift+[', |
160 | click () { |
161 | windows.main.webContents.send('previousTab') |
162 | } |
163 | }, |
164 | { type: 'separator' }, |
165 | { role: 'front' } |
166 | ] |
167 | |
168 | Menu.setApplicationMenu(Menu.buildFromTemplate(menu)) |
169 | } |
170 |
Built with git-ssb-web