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