Files: b42fccbaa67e2711ebbeb26092b463df381ba13a / index.js
5291 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 | // FTU needs |
8 | const fs = require('fs') |
9 | const Config = require('ssb-config/inject') |
10 | const appName = process.env.ssb_appname || 'ssb' |
11 | const config = Config(appName) |
12 | const isInstalled = fs.existsSync(Path.join(config.path, 'secret')) |
13 | |
14 | var windows = {} |
15 | var quitting = false |
16 | |
17 | electron.app.on('ready', () => { |
18 | var menu = defaultMenu(electron.app, electron.shell) |
19 | var view = menu.find(x => x.label === 'View') |
20 | view.submenu = [ |
21 | { role: 'reload' }, |
22 | { role: 'toggledevtools' }, |
23 | { type: 'separator' }, |
24 | { role: 'resetzoom' }, |
25 | { role: 'zoomin' }, |
26 | { role: 'zoomout' }, |
27 | { type: 'separator' }, |
28 | { role: 'togglefullscreen' } |
29 | ] |
30 | if (process.platform === 'darwin') { |
31 | var win = menu.find(x => x.label === 'Window') |
32 | win.submenu = [ |
33 | { role: 'minimize' }, |
34 | { role: 'zoom' }, |
35 | { role: 'close', label: 'Close' }, |
36 | { type: 'separator' }, |
37 | { role: 'front' } |
38 | ] |
39 | } |
40 | |
41 | Menu.setApplicationMenu(Menu.buildFromTemplate(menu)) |
42 | |
43 | // TODO: FTU must happen before this part. |
44 | if (!isInstalled) { |
45 | console.log('Ticktack or SSB not installed, run FTU') |
46 | openFTUWindow() |
47 | } else { |
48 | startBackgroundProcess() |
49 | } |
50 | |
51 | // FTU told app to create new identity, so proceed as normal |
52 | electron.ipcMain.once('create-new-identity', function (ev) { |
53 | console.log('create new identity') |
54 | startBackgroundProcess() |
55 | }) |
56 | |
57 | // wait until server has started before opening main window |
58 | electron.ipcMain.once('server-started', function (ev, config) { |
59 | console.log("> Opening main window") |
60 | openMainWindow() |
61 | }) |
62 | |
63 | electron.app.on('before-quit', function () { |
64 | quitting = true |
65 | }) |
66 | |
67 | // allow inspecting of background process |
68 | electron.ipcMain.on('open-background-devtools', function (ev, config) { |
69 | if (windows.background) { |
70 | windows.background.webContents.openDevTools({ detach: true }) |
71 | } |
72 | }) |
73 | }) |
74 | |
75 | function startBackgroundProcess() { |
76 | if (!windows.background) { |
77 | windows.background = openWindow(Path.join(__dirname, 'background-process.js'), { |
78 | connect: false, |
79 | center: true, |
80 | fullscreen: false, |
81 | fullscreenable: false, |
82 | height: 150, |
83 | maximizable: false, |
84 | minimizable: false, |
85 | resizable: false, |
86 | show: false, |
87 | skipTaskbar: true, |
88 | title: 'ticktack-server', |
89 | useContentSize: true, |
90 | width: 150 |
91 | }) |
92 | } |
93 | } |
94 | |
95 | function openMainWindow() { |
96 | if (!windows.main) { |
97 | var windowState = WindowState({ |
98 | defaultWidth: 1024, |
99 | defaultHeight: 768 |
100 | }) |
101 | windows.main = openWindow(Path.join(__dirname, 'main.js'), { |
102 | minWidth: 800, |
103 | x: windowState.x, |
104 | y: windowState.y, |
105 | width: windowState.width, |
106 | height: windowState.height, |
107 | autoHideMenuBar: true, |
108 | title: 'Ticktack', |
109 | frame: false, |
110 | titleBarStyle: 'hidden', |
111 | show: true, |
112 | backgroundColor: '#EEE', |
113 | icon: './assets/icon.png' |
114 | }) |
115 | windowState.manage(windows.main) |
116 | windows.main.setSheetOffset(40) |
117 | windows.main.on('close', function (e) { |
118 | if (!quitting && process.platform === 'darwin') { |
119 | e.preventDefault() |
120 | windows.main.hide() |
121 | } |
122 | }) |
123 | windows.main.on('closed', function () { |
124 | windows.main = null |
125 | if (process.platform !== 'darwin') electron.app.quit() |
126 | }) |
127 | |
128 | if (windows.ftu) { |
129 | windows.ftu.hide() |
130 | } |
131 | } |
132 | } |
133 | |
134 | function openFTUWindow() { |
135 | if (!windows.ftu) { |
136 | var windowState = WindowState({ |
137 | defaultWidth: 1024, |
138 | defaultHeight: 768 |
139 | }) |
140 | windows.ftu = openWindow(Path.join(__dirname, 'ftu', 'index.js'), { |
141 | minWidth: 800, |
142 | x: windowState.x, |
143 | y: windowState.y, |
144 | width: windowState.width, |
145 | height: windowState.height, |
146 | autoHideMenuBar: true, |
147 | title: 'Ticktack', |
148 | frame: false, |
149 | titleBarStyle: 'hidden', |
150 | show: true, |
151 | backgroundColor: '#EEE', |
152 | icon: './assets/icon.png' |
153 | }) |
154 | windowState.manage(windows.ftu) |
155 | windows.ftu.setSheetOffset(40) |
156 | windows.ftu.on('close', function (e) { |
157 | if (!quitting && process.platform === 'darwin') { |
158 | e.preventDefault() |
159 | windows.ftu.hide() |
160 | } |
161 | }) |
162 | windows.ftu.on('closed', function () { |
163 | windows.ftu = null |
164 | if (process.platform !== 'darwin') electron.app.quit() |
165 | }) |
166 | } |
167 | } |
168 | |
169 | function openWindow(path, opts) { |
170 | var window = new electron.BrowserWindow(opts) |
171 | window.webContents.on('dom-ready', function () { |
172 | window.webContents.executeJavaScript(` |
173 | var electron = require('electron') |
174 | var h = require('mutant/h') |
175 | electron.webFrame.setZoomLevelLimits(1, 1) |
176 | var title = ${JSON.stringify(opts.title || 'Ticktack')} |
177 | document.documentElement.querySelector('head').appendChild( |
178 | h('title', title) |
179 | ) |
180 | require(${JSON.stringify(path)}) |
181 | `) |
182 | }) |
183 | |
184 | window.webContents.on('will-navigate', function (e, url) { |
185 | e.preventDefault() |
186 | electron.shell.openExternal(url) |
187 | }) |
188 | |
189 | window.webContents.on('new-window', function (e, url) { |
190 | e.preventDefault() |
191 | electron.shell.openExternal(url) |
192 | }) |
193 | |
194 | window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html')) |
195 | return window |
196 | } |
197 |
Built with git-ssb-web