Files: 3fa3e23c4b756580c6cf49253941a9e7d55b9023 / index.js
5203 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 | openMainWindow() |
60 | }) |
61 | |
62 | electron.app.on('before-quit', function () { |
63 | quitting = true |
64 | }) |
65 | |
66 | // allow inspecting of background process |
67 | electron.ipcMain.on('open-background-devtools', function (ev, config) { |
68 | if (windows.background) { |
69 | windows.background.webContents.openDevTools({ detach: true }) |
70 | } |
71 | }) |
72 | }) |
73 | |
74 | function startBackgroundProcess() { |
75 | if (!windows.background) { |
76 | windows.background = openWindow(Path.join(__dirname, 'background-process.js'), { |
77 | connect: false, |
78 | center: true, |
79 | fullscreen: false, |
80 | fullscreenable: false, |
81 | height: 150, |
82 | maximizable: false, |
83 | minimizable: false, |
84 | resizable: false, |
85 | show: false, |
86 | skipTaskbar: true, |
87 | title: 'ticktack-server', |
88 | useContentSize: true, |
89 | width: 150 |
90 | }) |
91 | } |
92 | } |
93 | |
94 | function openMainWindow() { |
95 | if (!windows.main) { |
96 | var windowState = WindowState({ |
97 | defaultWidth: 1024, |
98 | defaultHeight: 768 |
99 | }) |
100 | windows.main = openWindow(Path.join(__dirname, 'main.js'), { |
101 | minWidth: 800, |
102 | x: windowState.x, |
103 | y: windowState.y, |
104 | width: windowState.width, |
105 | height: windowState.height, |
106 | autoHideMenuBar: true, |
107 | title: 'Ticktack', |
108 | frame: false, |
109 | titleBarStyle: 'hidden', |
110 | show: true, |
111 | backgroundColor: '#EEE', |
112 | icon: './assets/icon.png' |
113 | }) |
114 | windowState.manage(windows.main) |
115 | windows.main.setSheetOffset(40) |
116 | windows.main.on('close', function (e) { |
117 | if (!quitting && process.platform === 'darwin') { |
118 | e.preventDefault() |
119 | windows.main.hide() |
120 | } |
121 | }) |
122 | windows.main.on('closed', function () { |
123 | windows.main = null |
124 | if (process.platform !== 'darwin') electron.app.quit() |
125 | }) |
126 | } |
127 | } |
128 | |
129 | function openFTUWindow() { |
130 | if (!windows.main) { |
131 | var windowState = WindowState({ |
132 | defaultWidth: 1024, |
133 | defaultHeight: 768 |
134 | }) |
135 | windows.main = openWindow(Path.join(__dirname, 'ftu', 'index.js'), { |
136 | minWidth: 800, |
137 | x: windowState.x, |
138 | y: windowState.y, |
139 | width: windowState.width, |
140 | height: windowState.height, |
141 | autoHideMenuBar: true, |
142 | title: 'Ticktack', |
143 | frame: false, |
144 | titleBarStyle: 'hidden', |
145 | show: true, |
146 | backgroundColor: '#EEE', |
147 | icon: './assets/icon.png' |
148 | }) |
149 | windowState.manage(windows.main) |
150 | windows.main.setSheetOffset(40) |
151 | windows.main.on('close', function (e) { |
152 | if (!quitting && process.platform === 'darwin') { |
153 | e.preventDefault() |
154 | windows.main.hide() |
155 | } |
156 | }) |
157 | windows.main.on('closed', function () { |
158 | windows.main = null |
159 | if (process.platform !== 'darwin') electron.app.quit() |
160 | }) |
161 | } |
162 | } |
163 | |
164 | function openWindow(path, opts) { |
165 | var window = new electron.BrowserWindow(opts) |
166 | window.webContents.on('dom-ready', function () { |
167 | window.webContents.executeJavaScript(` |
168 | var electron = require('electron') |
169 | var h = require('mutant/h') |
170 | electron.webFrame.setZoomLevelLimits(1, 1) |
171 | var title = ${JSON.stringify(opts.title || 'Ticktack')} |
172 | document.documentElement.querySelector('head').appendChild( |
173 | h('title', title) |
174 | ) |
175 | require(${JSON.stringify(path)}) |
176 | `) |
177 | }) |
178 | |
179 | window.webContents.on('will-navigate', function (e, url) { |
180 | e.preventDefault() |
181 | electron.shell.openExternal(url) |
182 | }) |
183 | |
184 | window.webContents.on('new-window', function (e, url) { |
185 | e.preventDefault() |
186 | electron.shell.openExternal(url) |
187 | }) |
188 | |
189 | window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html')) |
190 | return window |
191 | } |
192 |
Built with git-ssb-web