Last active
June 3, 2021 15:26
-
-
Save ckerr/8c5fc0c6a5153d49b5a4a56d3ed9da8f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title></title> | |
</head> | |
<body> | |
Display is <span id="size"></span> | |
<script src="renderer.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules to control application life and create native browser window | |
const {app, crashReporter, ipcMain, BrowserWindow} = require('electron') | |
const path = require('path') | |
function createWindow () { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
webPreferences: { | |
contextIsolation: false, | |
enableRemoteModule: true, | |
nodeIntegration: true, | |
preload: path.join(__dirname, 'preload.js') | |
} | |
}) | |
mainWindow.loadFile('index.html'); | |
} | |
// This method will be called when Electron has finished | |
// initialization and is ready to create browser windows. | |
// Some APIs can only be used after this event occurs. | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
// On macOS it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
// Quit when all windows are closed, except on macOS. There, it's common | |
// for applications and their menu bar to stay active until the user quits | |
// explicitly with Cmd + Q. | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) | |
function testDone(success, ...logs) { | |
console.log(`test ${success ? 'passed' : 'failed'}`) | |
logs.forEach((i) => console.log(i)) | |
process.exit(success ? 0 : 1) | |
} | |
{ | |
crashReporter.start({ uploadToServer: false, submitURL: '' }) | |
ipcMain.on('test-done', (_, success, ...logs) => testDone(success, ...logs)) | |
const failIfBadExit = (details) => { | |
if (details.reason !== 'clean-exit') testDone(false, new Error('trace'), details) | |
} | |
app.on('child-process-gone', (_ev, details) => test.failIfBadExit(details)) | |
app.on('render-process-gone', (_ev, _, details) => test.failIfBadExit(details)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// All of the Node.js APIs are available in the preload process. | |
// It has the same sandbox as a Chrome extension. | |
// Test helpers | |
const test = { | |
assert: (ok, ...logs) => { | |
if (!ok) test.fail(...logs) | |
}, | |
fail: (...logs) => test.done(false, ...logs), | |
done: (success = true, ...logs) => { | |
if (!success) logs.unshift(new Error('test failed')) | |
require('electron').ipcRenderer.send('test-done', success, ...logs) | |
}, | |
}; | |
global.test = test; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { remote } = require("electron"); | |
// This file is required by the index.html file and will | |
// be executed in the renderer process for that window. | |
// No Node.js APIs are available in this process because | |
// `nodeIntegration` is turned off. Use `preload.js` to | |
// selectively enable features needed in the rendering | |
// process. | |
try { | |
const primaryDisplay = remote.screen.getPrimaryDisplay(); | |
const { width, height } = primaryDisplay.workAreaSize; | |
document.querySelector("#size").textContent = `${width}×${height}`; | |
test.done(); | |
} catch (e) { | |
console.log(e) | |
test.fail(e); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Empty */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment