Add IPC logging and echo button
This commit is contained in:
parent
5723119001
commit
961857ff23
@ -16,5 +16,12 @@
|
||||
<body>
|
||||
<h1>Hello from Electron renderer!</h1>
|
||||
<p>👋</p>
|
||||
<button class="echoButton">
|
||||
ECHO
|
||||
</button>
|
||||
<button class="echoButton">
|
||||
ECHO 2
|
||||
</button>
|
||||
</body>
|
||||
<script src="pagejs/echo_button.js"></script>
|
||||
</html>
|
||||
|
||||
29
main.js
29
main.js
@ -1,17 +1,24 @@
|
||||
console.log('Hello world')
|
||||
|
||||
const { app, BrowserWindow } = require('electron')
|
||||
const { app, BrowserWindow, ipcMain } = require('electron')
|
||||
const path = require('node:path')
|
||||
|
||||
const createWindow = () => {
|
||||
app.on('window-all-closed', windowsClosed)
|
||||
app.whenReady().then(appReady)
|
||||
|
||||
function createWindow() {
|
||||
const win = new BrowserWindow({
|
||||
width: 800,
|
||||
height: 600
|
||||
width: 400,
|
||||
height: 600,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'preload.js')
|
||||
}
|
||||
})
|
||||
|
||||
win.loadFile('index.html')
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
function appReady() {
|
||||
createWindow()
|
||||
|
||||
// MacOS window activation stuffs (??) (idk I don't use Apple products)
|
||||
@ -20,11 +27,17 @@ app.whenReady().then(() => {
|
||||
createWindow()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
ipcMain.on('log', ipcLog)
|
||||
}
|
||||
|
||||
function windowsClosed() {
|
||||
// Windows and Linux need to call app.quit manually
|
||||
// otherwise process will not exit
|
||||
if (process.platform !== 'darwin') app.quit()
|
||||
})
|
||||
}
|
||||
|
||||
function ipcLog(event, msg) {
|
||||
console.log(msg)
|
||||
}
|
||||
|
||||
|
||||
18
pagejs/echo_button.js
Normal file
18
pagejs/echo_button.js
Normal file
@ -0,0 +1,18 @@
|
||||
window.upbp.log('Loading echo button events')
|
||||
|
||||
var echoButtons = document.querySelectorAll('.echoButton')
|
||||
|
||||
if (echoButtons.length === 0) {
|
||||
window.upbp.log('WARN: No echo buttons (.echoButton)')
|
||||
}
|
||||
|
||||
window.upbp.log(echoButtons)
|
||||
|
||||
for (btn of echoButtons) {
|
||||
btn.addEventListener('click', echoButtonClick)
|
||||
}
|
||||
|
||||
function echoButtonClick() {
|
||||
window.upbp.log('Echo button clicked')
|
||||
}
|
||||
|
||||
8
preload.js
Normal file
8
preload.js
Normal file
@ -0,0 +1,8 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron')
|
||||
|
||||
contextBridge.exposeInMainWorld('upbp', {
|
||||
log: (msg) => {
|
||||
ipcRenderer.send('log', msg)
|
||||
}
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user