git ssb

2+

mixmix / ticktack



Tree: be227038215cfd41dc8aee4c1ece14657a72abce

Files: be227038215cfd41dc8aee4c1ece14657a72abce / index.js

6603 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
7// First-Time User Experience (FTU) needs the items below
8const fs = require('fs')
9const path = require('path')
10const os = require('os')
11const appName = process.env.SSB_APPNAME || 'ssb'
12const CONFIG_FOLDER = path.join(os.homedir(), `.${appName}`)
13const IMPORT_FILE = path.join(CONFIG_FOLDER, 'importing.json')
14
15var windows = {}
16var quitting = false
17
18electron.app.on('ready', () => {
19 var menu = defaultMenu(electron.app, electron.shell)
20 var view = menu.find(x => x.label === 'View')
21 view.submenu = [
22 { role: 'reload' },
23 { role: 'toggledevtools' },
24 { type: 'separator' },
25 { role: 'resetzoom' },
26 { role: 'zoomin' },
27 { role: 'zoomout' },
28 { type: 'separator' },
29 { role: 'togglefullscreen' }
30 ]
31 if (process.platform === 'darwin') {
32 var win = menu.find(x => x.label === 'Window')
33 win.submenu = [
34 { role: 'minimize' },
35 { role: 'zoom' },
36 { role: 'close', label: 'Close' },
37 { type: 'separator' },
38 { role: 'front' }
39 ]
40 }
41
42 Menu.setApplicationMenu(Menu.buildFromTemplate(menu))
43
44 if (!fs.existsSync(Path.join(CONFIG_FOLDER, 'secret'))) {
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 setImportRunningFlag(false)
55 startBackgroundProcess()
56 })
57
58 // FTU told app to import some identity, need to start sbot and keep FTU running
59 electron.ipcMain.once('import-identity', function (ev) {
60 console.log('import identity')
61 setImportRunningFlag(true)
62 startBackgroundProcess()
63 })
64
65 // FTU import finished, ready to start main window
66 electron.ipcMain.once('import-completed', function (ev) {
67 console.log('> import finished, opening main window')
68 setImportRunningFlag(false)
69 openMainWindow()
70 })
71
72 // wait until server has started before opening main window
73 electron.ipcMain.once('server-started', function (ev, config) {
74 let keepFTURunning = getImportRunningFlag(false)
75 if (!keepFTURunning) {
76 console.log('> Opening main window')
77 openMainWindow()
78 } else {
79 // sbot started but we're importing an older identity, need
80 // to tell FTU to wait for sync.
81 openFTUWindow()
82 windows.ftu.webContents.send('import-resumed')
83 }
84 })
85
86 electron.app.on('before-quit', function () {
87 quitting = true
88 })
89
90 // allow inspecting of background process
91 electron.ipcMain.on('open-background-devtools', function (ev, config) {
92 if (windows.background) {
93 windows.background.webContents.openDevTools({ detach: true })
94 }
95 })
96})
97
98function startBackgroundProcess () {
99 if (!windows.background) {
100 windows.background = openWindow(Path.join(__dirname, 'background-process.js'), {
101 connect: false,
102 center: true,
103 fullscreen: false,
104 fullscreenable: false,
105 height: 150,
106 maximizable: false,
107 minimizable: false,
108 resizable: false,
109 show: false,
110 skipTaskbar: true,
111 title: 'ticktack-server',
112 useContentSize: true,
113 width: 150
114 })
115 }
116}
117
118function openMainWindow () {
119 if (!windows.main) {
120 var windowState = WindowState({
121 defaultWidth: 1024,
122 defaultHeight: 768
123 })
124 windows.main = openWindow(Path.join(__dirname, 'main.js'), {
125 minWidth: 800,
126 x: windowState.x,
127 y: windowState.y,
128 width: windowState.width,
129 height: windowState.height,
130 autoHideMenuBar: true,
131 title: 'Ticktack',
132 frame: false,
133 titleBarStyle: 'hidden',
134 show: true,
135 backgroundColor: '#EEE',
136 icon: './assets/icon.png'
137 })
138 windowState.manage(windows.main)
139 windows.main.setSheetOffset(40)
140 windows.main.on('close', function (e) {
141 if (!quitting && process.platform === 'darwin') {
142 e.preventDefault()
143 windows.main.hide()
144 }
145 })
146 windows.main.on('closed', function () {
147 windows.main = null
148 if (process.platform !== 'darwin') electron.app.quit()
149 })
150
151 if (windows.ftu) {
152 windows.ftu.hide()
153 }
154 }
155}
156
157function openFTUWindow () {
158 if (!windows.ftu) {
159 var windowState = WindowState({
160 defaultWidth: 1024,
161 defaultHeight: 768
162 })
163 windows.ftu = openWindow(Path.join(__dirname, 'ftu', 'index.js'), {
164 minWidth: 800,
165 x: windowState.x,
166 y: windowState.y,
167 width: windowState.width,
168 height: windowState.height,
169 autoHideMenuBar: true,
170 title: 'Ticktack',
171 frame: false,
172 titleBarStyle: 'hidden',
173 show: true,
174 backgroundColor: '#EEE',
175 icon: './assets/icon.png'
176 })
177 windowState.manage(windows.ftu)
178 windows.ftu.setSheetOffset(40)
179 windows.ftu.on('close', function (e) {
180 if (!quitting && process.platform === 'darwin') {
181 e.preventDefault()
182 windows.ftu.hide()
183 }
184 })
185 windows.ftu.on('closed', function () {
186 windows.ftu = null
187 if (process.platform !== 'darwin') electron.app.quit()
188 })
189 }
190}
191
192function openWindow (path, opts) {
193 var window = new electron.BrowserWindow(opts)
194 window.webContents.on('dom-ready', function () {
195 window.webContents.executeJavaScript(`
196 var electron = require('electron')
197 var h = require('mutant/h')
198 electron.webFrame.setZoomLevelLimits(1, 1)
199 var title = ${JSON.stringify(opts.title || 'Ticktack')}
200 document.documentElement.querySelector('head').appendChild(
201 h('title', title)
202 )
203 require(${JSON.stringify(path)})
204 `)
205 })
206
207 window.webContents.on('will-navigate', function (e, url) {
208 e.preventDefault()
209 electron.shell.openExternal(url)
210 })
211
212 window.webContents.on('new-window', function (e, url) {
213 e.preventDefault()
214 electron.shell.openExternal(url)
215 })
216
217 window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html'))
218 return window
219}
220
221function getImportRunningFlag (defaultValue) {
222 if (fs.existsSync(IMPORT_FILE)) {
223 let data = JSON.parse(fs.readFileSync(IMPORT_FILE))
224 return data.importing || defaultValue
225 } else {
226 return defaultValue
227 }
228}
229
230function setImportRunningFlag (v) {
231 let data = {}
232 if (fs.existsSync(IMPORT_FILE)) {
233 data = JSON.parse(fs.readFileSync(IMPORT_FILE))
234 }
235
236 data.importing = v
237
238 fs.writeFileSync(IMPORT_FILE, JSON.stringify(data))
239}
240

Built with git-ssb-web