git ssb

16+

Dominic / patchbay



Tree: 1c2926f12ff8e365e1d469f23180b50d50b9c749

Files: 1c2926f12ff8e365e1d469f23180b50d50b9c749 / index.js

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

Built with git-ssb-web