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