git ssb

16+

Dominic / patchbay



Tree: 03ce3474ce130d4761d364412f79db838997ddb9

Files: 03ce3474ce130d4761d364412f79db838997ddb9 / index.js

4357 bytesRaw
1var defaultMenu = require('electron-default-menu')
2var WindowState = require('electron-window-state')
3var electron = require('electron')
4var Menu = electron.Menu
5var Path = require('path')
6
7var windows = {}
8var quitting = false
9
10console.log('STARTING electron')
11electron.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({ mode: 'detach' })
35 }
36 })
37})
38
39function 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
59function 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
70 x: windowState.x,
71 y: windowState.y,
72 minWidth: 800,
73 width: windowState.width,
74 height: windowState.height,
75 autoHideMenuBar: true,
76 frame: !process.env.FRAME,
77 // titleBarStyle: 'hidden',
78 backgroundColor: '#FFF',
79 icon: './assets/icon.png'
80 })
81 windowState.manage(windows.main)
82 windows.main.setSheetOffset(40)
83 windows.main.on('close', function (e) {
84 if (!quitting && process.platform === 'darwin') {
85 e.preventDefault()
86 windows.main.hide()
87 }
88 })
89 windows.main.on('closed', function () {
90 windows.main = null
91 if (process.platform !== 'darwin') electron.app.quit()
92 })
93}
94
95function openWindow (path, opts) {
96 var window = new electron.BrowserWindow(opts)
97
98 window.webContents.on('dom-ready', function () {
99 window.webContents.executeJavaScript(`
100 var electron = require('electron')
101 var h = require('mutant/h')
102 electron.webFrame.setVisualZoomLevelLimits(1, 1)
103 var title = ${JSON.stringify(opts.title || 'Patchbay')}
104 document.documentElement.querySelector('head').appendChild(
105 h('title', title)
106 )
107 require(${JSON.stringify(path)})
108 `) // NOTE tried process(electron)
109 })
110
111 window.webContents.on('will-navigate', function (e, url) {
112 e.preventDefault()
113 electron.shell.openExternal(url)
114 })
115
116 window.webContents.on('new-window', function (e, url) {
117 e.preventDefault()
118 electron.shell.openExternal(url)
119 })
120
121 window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html'))
122 return window
123}
124
125function startMenus () {
126 var menu = defaultMenu(electron.app, electron.shell)
127 var view = menu.find(x => x.label === 'View')
128 view.submenu = [
129 { role: 'reload' },
130 { role: 'toggledevtools' },
131 { type: 'separator' },
132 { role: 'resetzoom' },
133 { role: 'zoomin' },
134 { role: 'zoomout' },
135 { type: 'separator' },
136 { role: 'togglefullscreen' }
137 ]
138 var win = menu.find(x => x.label === 'Window')
139 win.submenu = [
140 { role: 'minimize' },
141 { role: 'zoom' },
142 { role: 'close', label: 'Close Window', accelerator: 'CmdOrCtrl+Shift+W' },
143 { type: 'separator' },
144 {
145 label: 'Close Tab',
146 accelerator: 'CmdOrCtrl+W',
147 click () {
148 windows.main.webContents.send('closeTab')
149 }
150 },
151 {
152 label: 'Select Next Tab',
153 accelerator: 'CmdOrCtrl+Shift+]',
154 click () {
155 windows.main.webContents.send('nextTab')
156 }
157 },
158 {
159 label: 'Select Previous Tab',
160 accelerator: 'CmdOrCtrl+Shift+[',
161 click () {
162 windows.main.webContents.send('previousTab')
163 }
164 },
165 { type: 'separator' },
166 { role: 'front' }
167 ]
168
169 Menu.setApplicationMenu(Menu.buildFromTemplate(menu))
170}
171

Built with git-ssb-web