git ssb

1+

Daan Patchwork / patchwork



Tree: fe4e56b2dea67a6b17e1545cc56ea259702342b1

Files: fe4e56b2dea67a6b17e1545cc56ea259702342b1 / lib / window.js

1809 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 enableRemoteModule: true,
11 }
12 }, opts))
13
14 electron.ipcMain.on('ready-to-show', handleReadyToShow)
15
16 window.webContents.on('dom-ready', function () {
17 window.webContents.executeJavaScript(`
18 var electron = require('electron')
19 var rootView = require(${JSON.stringify(path)})
20 var h = require('mutant/h')
21
22 electron.webFrame.setVisualZoomLevelLimits(1, 1)
23
24 var config = ${JSON.stringify(config)}
25 var data = ${JSON.stringify(opts.data)}
26 var title = ${JSON.stringify(opts.title || 'Patchwork')}
27
28 document.documentElement.querySelector('head').appendChild(
29 h('title', title)
30 )
31
32 document.documentElement.replaceChild(h('body', [
33 rootView(config, data)
34 ]), document.body)
35 `)
36 })
37
38 // setTimeout(function () {
39 // window.show()
40 // }, 3000)
41
42 window.webContents.on('will-navigate', function (e, url) {
43 e.preventDefault()
44 electron.shell.openExternal(url)
45 })
46
47 window.webContents.on('new-window', function (e, url) {
48 e.preventDefault()
49 electron.shell.openExternal(url)
50 })
51
52 window.on('closed', function () {
53 electron.ipcMain.removeListener('ready-to-show', handleReadyToShow)
54 })
55
56 window.loadURL('file://' + Path.join(__dirname, '..', 'assets', 'base.html'))
57 return window
58
59 // scoped
60
61 function handleReadyToShow (ev) {
62 if (ev.sender === window) {
63 window.show()
64 electron.ipcMain.removeListener('ready-to-show', handleReadyToShow)
65 }
66 }
67}
68

Built with git-ssb-web