Files: 202db7a1462af5a1b1dbeb6b5c220468060f96ea / index.js
3678 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 | var windows = {} |
8 | var quitting = false |
9 | |
10 | electron.app.on('ready', () => { |
11 | var menu = defaultMenu(electron.app, electron.shell) |
12 | var view = menu.find(x => x.label === 'View') |
13 | view.submenu = [ |
14 | { role: 'reload' }, |
15 | { role: 'toggledevtools' }, |
16 | { type: 'separator' }, |
17 | { role: 'resetzoom' }, |
18 | { role: 'zoomin' }, |
19 | { role: 'zoomout' }, |
20 | { type: 'separator' }, |
21 | { role: 'togglefullscreen' } |
22 | ] |
23 | if (process.platform === 'darwin') { |
24 | var win = menu.find(x => x.label === 'Window') |
25 | win.submenu = [ |
26 | { role: 'minimize' }, |
27 | { role: 'zoom' }, |
28 | { role: 'close', label: 'Close' }, |
29 | { type: 'separator' }, |
30 | { role: 'front' } |
31 | ] |
32 | } |
33 | |
34 | Menu.setApplicationMenu(Menu.buildFromTemplate(menu)) |
35 | |
36 | startBackgroundProcess() |
37 | |
38 | // wait until server has started before opening main window |
39 | electron.ipcMain.once('server-started', function (ev, config) { |
40 | openMainWindow() |
41 | }) |
42 | |
43 | electron.app.on('before-quit', function () { |
44 | quitting = true |
45 | }) |
46 | |
47 | // allow inspecting of background process |
48 | electron.ipcMain.on('open-background-devtools', function (ev, config) { |
49 | if (windows.background) { |
50 | windows.background.webContents.openDevTools({detach: true}) |
51 | } |
52 | }) |
53 | }) |
54 | |
55 | function startBackgroundProcess () { |
56 | if (!windows.background) { |
57 | windows.background = openWindow(Path.join(__dirname, 'background-process.js'), { |
58 | connect: false, |
59 | center: true, |
60 | fullscreen: false, |
61 | fullscreenable: false, |
62 | height: 150, |
63 | maximizable: false, |
64 | minimizable: false, |
65 | resizable: false, |
66 | show: false, |
67 | skipTaskbar: true, |
68 | title: 'ticktack-server', |
69 | useContentSize: true, |
70 | width: 150 |
71 | }) |
72 | } |
73 | } |
74 | |
75 | function openMainWindow () { |
76 | if (!windows.main) { |
77 | var windowState = WindowState({ |
78 | defaultWidth: 1024, |
79 | defaultHeight: 768 |
80 | }) |
81 | windows.main = openWindow(Path.join(__dirname, 'main.js'), { |
82 | minWidth: 800, |
83 | x: windowState.x, |
84 | y: windowState.y, |
85 | width: windowState.width, |
86 | height: windowState.height, |
87 | autoHideMenuBar: true, |
88 | title: 'Ticktack', |
89 | frame: false, |
90 | titleBarStyle: 'hidden', |
91 | show: true, |
92 | backgroundColor: '#EEE', |
93 | icon: './assets/icon.png' |
94 | }) |
95 | windowState.manage(windows.main) |
96 | windows.main.setSheetOffset(40) |
97 | windows.main.on('close', function (e) { |
98 | if (!quitting && process.platform === 'darwin') { |
99 | e.preventDefault() |
100 | windows.main.hide() |
101 | } |
102 | }) |
103 | windows.main.on('closed', function () { |
104 | windows.main = null |
105 | if (process.platform !== 'darwin') electron.app.quit() |
106 | }) |
107 | } |
108 | } |
109 | |
110 | function openWindow (path, opts) { |
111 | var window = new electron.BrowserWindow(opts) |
112 | window.webContents.on('dom-ready', function () { |
113 | window.webContents.executeJavaScript(` |
114 | var electron = require('electron') |
115 | var h = require('mutant/h') |
116 | electron.webFrame.setZoomLevelLimits(1, 1) |
117 | var title = ${JSON.stringify(opts.title || 'Ticktack')} |
118 | document.documentElement.querySelector('head').appendChild( |
119 | h('title', title) |
120 | ) |
121 | require(${JSON.stringify(path)}) |
122 | `) |
123 | }) |
124 | |
125 | window.webContents.on('will-navigate', function (e, url) { |
126 | e.preventDefault() |
127 | electron.shell.openExternal(url) |
128 | }) |
129 | |
130 | window.webContents.on('new-window', function (e, url) { |
131 | e.preventDefault() |
132 | electron.shell.openExternal(url) |
133 | }) |
134 | |
135 | window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html')) |
136 | return window |
137 | } |
138 |
Built with git-ssb-web