git ssb

1+

Daan Patchwork / patchwork



Tree: d3826fa5570d6a70af4cd9401fe6f9284ec0ef5c

Files: d3826fa5570d6a70af4cd9401fe6f9284ec0ef5c / lib / window.js

1875 bytesRaw
1const Path = require('path')
2const electron = require('electron')
3const extend = require('xtend/mutable')
4
5module.exports = function Window (config, path, opts) {
6 const window = new electron.BrowserWindow(extend({
7 show: false,
8 webPreferences: {
9 nodeIntegration: true // XXX: Maybe not always necessary (?)
10 }
11 }, opts))
12
13 electron.ipcMain.on('ready-to-show', handleReadyToShow)
14
15 window.webContents.on('dom-ready', function () {
16 window.webContents.executeJavaScript(`
17 var electron = require('electron')
18 var rootView = require(${JSON.stringify(path)})
19 var h = require('mutant/h')
20
21 electron.webFrame.setVisualZoomLevelLimits(1, 1)
22
23 var config = ${JSON.stringify(config)}
24 var data = ${JSON.stringify(opts.data)}
25 var title = ${JSON.stringify(opts.title || 'Patchwork')}
26
27 document.documentElement.querySelector('head').appendChild(
28 h('title', title)
29 )
30
31 var shouldShow = ${opts.show !== false}
32 var shouldConnect = ${opts.connect !== false}
33
34 document.documentElement.replaceChild(h('body', [
35 rootView(config, data)
36 ]), document.body)
37 `)
38 })
39
40 // setTimeout(function () {
41 // window.show()
42 // }, 3000)
43
44 window.webContents.on('will-navigate', function (e, url) {
45 e.preventDefault()
46 electron.shell.openExternal(url)
47 })
48
49 window.webContents.on('new-window', function (e, url) {
50 e.preventDefault()
51 electron.shell.openExternal(url)
52 })
53
54 window.on('closed', function () {
55 electron.ipcMain.removeListener('ready-to-show', handleReadyToShow)
56 })
57
58 window.loadURL('file://' + Path.join(__dirname, '..', 'assets', 'base.html'))
59 return window
60
61 // scoped
62
63 function handleReadyToShow (ev) {
64 if (ev.sender === window) {
65 window.show()
66 electron.ipcMain.removeListener('ready-to-show', handleReadyToShow)
67 }
68 }
69}
70

Built with git-ssb-web