|
| 1 | +const nwSplasher = { |
| 2 | + º: '%c', |
| 3 | + consoleNormal: 'font-family: sans-serif', |
| 4 | + consoleBold: '' + |
| 5 | + 'font-family: sans-serif' + |
| 6 | + 'font-weight: bold', |
| 7 | + consoleCode: '' + |
| 8 | + 'background: #EEEEF6;' + |
| 9 | + 'border: 1px solid #B2B0C1;' + |
| 10 | + 'border-radius: 7px;' + |
| 11 | + 'padding: 2px 8px 3px;' + |
| 12 | + 'color: #5F5F5F;' + |
| 13 | + 'line-height: 22px;' + |
| 14 | + 'box-shadow: 0px 0px 1px 1px rgba(178,176,193,0.3)', |
| 15 | + validateUrl: function (url) { |
| 16 | + if (typeof(url) === 'string') { |
| 17 | + return url; |
| 18 | + } |
| 19 | + let fs = require('fs'); |
| 20 | + files = fs.readdirSync('.'); |
| 21 | + files = files.filter(function (file) { |
| 22 | + return file.endsWith('.html') |
| 23 | + }); |
| 24 | + |
| 25 | + if (files.length) { |
| 26 | + url = files[0]; |
| 27 | + } |
| 28 | + |
| 29 | + const defaults = [ |
| 30 | + 'app.html', |
| 31 | + 'main.html', |
| 32 | + 'default.html', |
| 33 | + 'index.html', |
| 34 | + ]; |
| 35 | + |
| 36 | + defaults.forEach(function (possibleFile) { |
| 37 | + if (files.includes(possibleFile)) { |
| 38 | + url = possibleFile; |
| 39 | + } |
| 40 | + }); |
| 41 | + if (url) { |
| 42 | + return url; |
| 43 | + } |
| 44 | + return false; |
| 45 | + }, |
| 46 | + validateNewWindowOptions: function (newWindowOptions) { |
| 47 | + if (!newWindowOptions || typeof(newWindowOptions) !== 'object' || Array.isArray(newWindowOptions)) { |
| 48 | + newWindowOptions = {}; |
| 49 | + } |
| 50 | + |
| 51 | + // Needs to be a new instance so splash screen animations play smoothly |
| 52 | + newWindowOptions.new_instance = true; |
| 53 | + // hide the app window until it signals it is done loading |
| 54 | + newWindowOptions.show = false; |
| 55 | + |
| 56 | + return newWindowOptions; |
| 57 | + }, |
| 58 | + validatePort: function (port) { |
| 59 | + if (typeof(port) !== 'number') { |
| 60 | + port = 4443; |
| 61 | + } |
| 62 | + return port; |
| 63 | + }, |
| 64 | + /** |
| 65 | + * Used by your Splash screen window. This creates a websocket and spawns |
| 66 | + * your main app in a hidden window. Then waits for the app to send a signal |
| 67 | + * to the websocket to close the splash screen. |
| 68 | + * |
| 69 | + * All params are optional. |
| 70 | + * |
| 71 | + * @param {string} url URL to load in the App window. Defaults to index.html, default.html, main.html, or app.html if those files exist, or the first html file it finds. |
| 72 | + * @param {object} newWindowOptions Object with the NW.js Window fields (height, width, frameless, etc) |
| 73 | + * @param {number} port Defaults to 1337, must match the same port number used in the app window |
| 74 | + * @return {undefined} Returns nothing, just executes |
| 75 | + */ |
| 76 | + loadAppWindowInBackground: function (url, newWindowOptions, port) { |
| 77 | + url = this.validateUrl(url); |
| 78 | + newWindowOptions = this.validateNewWindowOptions(newWindowOptions); |
| 79 | + port = this.validatePort(port); |
| 80 | + |
| 81 | + if (!url) { |
| 82 | + console.log(this.º + 'NW-Splasher: Could not find a valid path to load your app window.\n' + |
| 83 | + 'Pass in url or filename to load in the app Window.', this.consoleNormal); |
| 84 | + console.log(this.º + 'Example:', this.consoleBold); |
| 85 | + console.log(this.º + 'nwSplasher.loadAppInBackground(\'index.html\');', this.consoleCode); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + const net = require('net'); |
| 90 | + const server = net.createServer(function (socket) { |
| 91 | + socket.write('Echo server'); |
| 92 | + socket.pipe(socket); |
| 93 | + |
| 94 | + // Handle incoming messages app window |
| 95 | + socket.on('data', function (data) { |
| 96 | + if (data.toString() === 'loaded') { |
| 97 | + server.close(); |
| 98 | + nw.Window.get().close(true); |
| 99 | + } |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + server.listen(port, 'localhost'); |
| 104 | + |
| 105 | + // Launch hidden app window and wait for it to signal to close the splasher window |
| 106 | + nw.Window.open(url, newWindowOptions); |
| 107 | + }, |
| 108 | + /** |
| 109 | + * Call this from your App window when it is ready to be shown. |
| 110 | + * This will also trigger closing the Splash screen window. |
| 111 | + * |
| 112 | + * @param {number} port Optional port number, defaults to 1337. Must match port number used in Splash window |
| 113 | + * @return {undefined} Nothing is returned, this just runs a command. |
| 114 | + */ |
| 115 | + closeSplashAndShowApp: function (port) { |
| 116 | + port = this.validatePort(port); |
| 117 | + |
| 118 | + const net = require('net'); |
| 119 | + const client = new net.Socket(); |
| 120 | + client.connect(port, 'localhost'); |
| 121 | + |
| 122 | + client.write('loaded'); |
| 123 | + nw.Window.get().show(); |
| 124 | + } |
| 125 | +}; |
0 commit comments