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