From 711192d1d155262ea1beb7912a6209e009d51898 Mon Sep 17 00:00:00 2001 From: kor Date: Mon, 6 Aug 2018 03:05:18 +1200 Subject: [PATCH] Add test for interprocess communication. --- docs/asset/style.css | 4 ++-- docs/index.html | 8 +++++++ docs/logic/add.js | 20 +++++++++++++++-- docs/logic/main.js | 3 +++ docs/logic/write.js | 23 +++++++++++++++++++ main.js | 53 +++++++++++++++++++++++++++++++++++++------- 6 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 docs/logic/write.js diff --git a/docs/asset/style.css b/docs/asset/style.css index 59b53ac..137ad87 100644 --- a/docs/asset/style.css +++ b/docs/asset/style.css @@ -173,8 +173,8 @@ body { /* visual */ background-color: var(--color-overlay-bg-i); opacity: 0; - -webkit-transition: opacity var(--animation-time) linear; - transition: opacity var(--animation-time) linear; + /*-webkit-transition: opacity var(--animation-time) linear; + transition: opacity var(--animation-time) linear;*/ width: 100%; height: 100%; /* position */ diff --git a/docs/index.html b/docs/index.html index 2578ec2..75fc2da 100644 --- a/docs/index.html +++ b/docs/index.html @@ -7,6 +7,13 @@ + + @@ -17,6 +24,7 @@ +
diff --git a/docs/logic/add.js b/docs/logic/add.js index 4f7c96c..8cbe0f7 100644 --- a/docs/logic/add.js +++ b/docs/logic/add.js @@ -1,3 +1,7 @@ +const { ipcRenderer } = nodeRequire('electron'); +// const mainProcess = remote.nodeRequire('./main.js'); +//const mainProcess = nodeRequire('./main.js'); + function Add() { this.overlay = null; @@ -83,12 +87,24 @@ function Add() content += ``; - - this.overlay.innerHTML += content; this.display = document.getElementById("display"); + document.getElementById("enter").addEventListener('click', + () => + { + console.log('test test'); + // ipcRenderer.send('write', 'test'); + let Data = + { + message: "Hi", + someData: "Let's go" + }; + + ipcRenderer.send('request-mainprocess-action', Data); + }); + for (var i = 0; i < this.keys.length; i++) { this.elementList[this.keys[i]].elem = document.getElementById(this.elementList[this.keys[i]].key); diff --git a/docs/logic/main.js b/docs/logic/main.js index 9b620f5..d508db0 100644 --- a/docs/logic/main.js +++ b/docs/logic/main.js @@ -3,6 +3,7 @@ function Main() this.db = null; this.view = null; this.add = null; + this.write = null; this.queryPrev = ''; this.queryCur = ''; @@ -14,6 +15,8 @@ function Main() this.view.install(); this.add = new Add(); this.add.install(); + this.write = new Write(); + this.write.install(); var escape = document.getElementById("escape"); escape.onclick = function() diff --git a/docs/logic/write.js b/docs/logic/write.js new file mode 100644 index 0000000..2c85da7 --- /dev/null +++ b/docs/logic/write.js @@ -0,0 +1,23 @@ + +function Write() +{ + + this.install = function() + { + } + + this.insert = function(data) + { + let position = 5; // get insert position + + let foo = fs.openSync('foo.txt','r+'); + let buf = new Buffer("hello"); + fs.writeSync(foo, buf, 0, buf.length, position); + fs.close(foo); + } +} + +// fs.appendFile('mynewfile1.txt', 'Hello content!', function (err) { +// if (err) throw err; +// console.log('Saved!'); +// }); \ No newline at end of file diff --git a/main.js b/main.js index 93e9c35..1846484 100644 --- a/main.js +++ b/main.js @@ -2,6 +2,7 @@ const {app, BrowserWindow, webFrame, Menu} = require('electron') const path = require('path') const url = require('url') const shell = require('electron').shell; +const fs = require('fs'); let is_shown = true; @@ -49,10 +50,28 @@ app.on('ready', () => { app.win = new BrowserWindow({ webPreferences: { - nodeIntegration: false + nodeIntegration: true }, width: 950, height: 950, backgroundColor:"#ddd", minWidth: 587, minHeight: 540, frame:true, autoHideMenuBar: true, icon: __dirname + '/icon.ico'}) - app.win.loadURL(`file://${__dirname}/docs/index.html`) + app.win.loadURL(`file://${__dirname}/docs/index.html`); + + // app.win.webContents.on('did-finish-load', () => { + // let code = ` + // console.log("3"); + // document.getElementById("enter").addEventListener("click", + // function (e) + // { + // let foo = fs.openSync('foo.txt','r+'); + // let buf = new Buffer("hello"); + // fs.writeSync(foo, buf, 0, buf.length, 5); + // fs.close(foo); + // console.log('WROTE!'); + + // console.log(document.getElementById("display").value); + // });`; + // app.win.webContents.executeJavaScript(code); + // }); + // app.win.toggleDevTools(); app.win.on('closed', () => { @@ -79,11 +98,6 @@ app.on('ready', () => app.win.webContents.on('new-window', this.handleRedirect) }) - - - - - app.on('window-all-closed', () => { app.quit() @@ -93,4 +107,27 @@ app.on('activate', () => { if (app.win === null) { createWindow() } -}) \ No newline at end of file +}) + +// write = function(data) +// { +// let foo = fs.openSync('foo.txt','r+'); +// let buf = new Buffer("hello"); +// fs.writeSync(foo, buf, 0, buf.length, 5); +// fs.close(foo); +// console.log('WROTE!'); +// } + +const {ipcMain} = require('electron'); + +// Attach listener in the main process with the given ID +ipcMain.on('request-mainprocess-action', (event, arg) => { + // Displays the object sent from the renderer process: + //{ + // message: "Hi", + // someData: "Let's go" + //} + console.log( + arg + ); +}); \ No newline at end of file