Files: 0e154203de9454a9f8f6a7ecc08aa6f661819f6d / index.js
3628 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 | show: true, |
90 | backgroundColor: '#EEE', |
91 | icon: './assets/icon.png' |
92 | }) |
93 | windowState.manage(windows.main) |
94 | windows.main.setSheetOffset(40) |
95 | windows.main.on('close', function (e) { |
96 | if (!quitting && process.platform === 'darwin') { |
97 | e.preventDefault() |
98 | windows.main.hide() |
99 | } |
100 | }) |
101 | windows.main.on('closed', function () { |
102 | windows.main = null |
103 | if (process.platform !== 'darwin') electron.app.quit() |
104 | }) |
105 | } |
106 | } |
107 | |
108 | function openWindow (path, opts) { |
109 | var window = new electron.BrowserWindow(opts) |
110 | window.webContents.on('dom-ready', function () { |
111 | window.webContents.executeJavaScript(` |
112 | var electron = require('electron') |
113 | var h = require('mutant/h') |
114 | electron.webFrame.setZoomLevelLimits(1, 1) |
115 | var title = ${JSON.stringify(opts.title || 'Ticktack')} |
116 | document.documentElement.querySelector('head').appendChild( |
117 | h('title', title) |
118 | ) |
119 | require(${JSON.stringify(path)}) |
120 | `) |
121 | }) |
122 | |
123 | window.webContents.on('will-navigate', function (e, url) { |
124 | e.preventDefault() |
125 | electron.shell.openExternal(url) |
126 | }) |
127 | |
128 | window.webContents.on('new-window', function (e, url) { |
129 | e.preventDefault() |
130 | electron.shell.openExternal(url) |
131 | }) |
132 | |
133 | window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html')) |
134 | return window |
135 | } |
136 | |
137 |
Built with git-ssb-web