Skip to content

Instantly share code, notes, and snippets.

@VerteDinde
Created September 14, 2021 23:46
Show Gist options
  • Save VerteDinde/caf59c61ecbc65333a2e67b36d9a31cc to your computer and use it in GitHub Desktop.
Save VerteDinde/caf59c61ecbc65333a2e67b36d9a31cc to your computer and use it in GitHub Desktop.
transparent-child-open
<!DOCTYPE html>
<html lang="en">
<head>
<title>BrowserWindow</title>
</head>
<body>
<h1>Hello from your BrowserWindow!</h1>
Parent window.
<button onClick="window.open('', '', 'top=500,left=200,transparent=true,nodeIntegration=true');">Open child</button>
<main class="app">
<button onclick="window.app.openChildWindow()">Open Child Window</button>
</main>
</body>
</html>
// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain} = require('electron')
const path = require('path')
function createMainWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
allowRunningInsecureContent: false,
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
sandbox: true,
preload: path.join(app.getAppPath(), 'preload.js'),
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
ipcMain.handle('setFullscreen', (event, flag) => {
if (mainWindow) {
mainWindow.setFullScreen(flag)
}
})
ipcMain.handle('openChildWindow', (event) => {
const childWindow = new BrowserWindow({
show: false,
frame: false,
transparent: true,
alwaysOnTop: true,
autoHideMenuBar: true,
resizable: false,
useContentSize: true,
fullscreenable: false,
enableLargerThanScreen: true,
hasShadow: true,
skipTaskbar: true,
});
})
}
// 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.on('ready', createMainWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X 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()
}
})
function createNewWindow(options) {
// Create the browser window.
let newWindow = null;
if (options) {
newWindow = new BrowserWindow({options});
} else {
newWindow = new BrowserWindow({
show: false,
frame: false,
transparent: true,
alwaysOnTop: true,
autoHideMenuBar: true,
resizable: false,
useContentSize: true,
fullscreenable: false,
enableLargerThanScreen: true,
hasShadow: false,
skipTaskbar: true,
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
preload: path.join(app.getAppPath(), 'preload.js'),
}
})
}
// and load the index.html of the app.
newWindow.loadFile('index.html')
}
{
"name": "transparent-modal",
"productName": "transparent-modal",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "khammond",
"scripts": {
"start": "electron ."
},
"dependencies": {},
"devDependencies": {
"electron": "15.0.0-beta.5"
}
}
const { ipcRenderer, contextBridge } = require('electron')
contextBridge.exposeInMainWorld(
'app',
{
setFullscreen: (flag) => ipcRenderer.invoke('setFullscreen', flag),
openChildWindow: () => ipcRenderer.invoke('openChildWindow'),
openNativeChildWindow: () => ipcRenderer.invoke('openNativeChildWindow'),
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment