docs: New fiddle example 'Create a new window' (#20480)
* add: New fiddle for Manage Windows section example * Delete package.json Not needed * Address issue with .gitignore file, load new window, removing unwanted css class * Delete package.json * Pushing change regarding the use of shell.OpenExternal API with an event listener for the href tag on the link
This commit is contained in:
parent
b31084493e
commit
20c910f98e
3 changed files with 100 additions and 0 deletions
25
docs/fiddles/windows/manage-windows/new-window/index.html
Normal file
25
docs/fiddles/windows/manage-windows/new-window/index.html
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>Create a new window</h2>
|
||||||
|
<h3>Supports: Win, MacOS, Linux <span>|</span> Process: Main</h3>
|
||||||
|
<button id="new-window">View Demo</button>
|
||||||
|
<p>The <code>BrowserWindow</code> module gives you the ability to create new windows in your app. This main process module can be used from the renderer process with the <code>remote</code> module, as is shown in this demo.</p>
|
||||||
|
<p>There are a lot of options when creating a new window. A few are in this demo, but visit the <a id="browser-window-link" href="">documentation<span>(opens in new window)</span></a>
|
||||||
|
<div>
|
||||||
|
<h2>ProTip</h2>
|
||||||
|
<strong>Use an invisible browser window to run background tasks.</strong>
|
||||||
|
<p>You can set a new browser window to not be shown (be invisible) in order to use that additional renderer process as a kind of new thread in which to run JavaScript in the background of your app. You do this by setting the <code>show</code> property to <code>false</code> when defining the new window.</p>
|
||||||
|
<pre><code><span>var</span> win = <span>new</span> BrowserWindow({
|
||||||
|
<span>width</span>: <span>400</span>, <span>height</span>: <span>225</span>, <span>show</span>: <span>false</span>
|
||||||
|
})</code></pre>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
// You can also require other files to run in this process
|
||||||
|
require('./renderer.js')
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
56
docs/fiddles/windows/manage-windows/new-window/main.js
Normal file
56
docs/fiddles/windows/manage-windows/new-window/main.js
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
// Modules to control application life and create native browser window
|
||||||
|
const { app, BrowserWindow } = require('electron')
|
||||||
|
|
||||||
|
// Keep a global reference of the window object, if you don't, the window will
|
||||||
|
// be closed automatically when the JavaScript object is garbage collected.
|
||||||
|
let mainWindow
|
||||||
|
|
||||||
|
function createWindow () {
|
||||||
|
// Create the browser window.
|
||||||
|
mainWindow = new BrowserWindow({
|
||||||
|
width: 800,
|
||||||
|
height: 600,
|
||||||
|
webPreferences: {
|
||||||
|
nodeIntegration: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// and load the index.html of the app.
|
||||||
|
mainWindow.loadFile('index.html')
|
||||||
|
|
||||||
|
// Open the DevTools.
|
||||||
|
// mainWindow.webContents.openDevTools()
|
||||||
|
|
||||||
|
// Emitted when the window is closed.
|
||||||
|
mainWindow.on('closed', function () {
|
||||||
|
// Dereference the window object, usually you would store windows
|
||||||
|
// in an array if your app supports multi windows, this is the time
|
||||||
|
// when you should delete the corresponding element.
|
||||||
|
mainWindow = null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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', createWindow)
|
||||||
|
|
||||||
|
// 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 (mainWindow === null) {
|
||||||
|
createWindow()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// In this file you can include the rest of your app's specific main process
|
||||||
|
// code. You can also put them in separate files and require them here.
|
19
docs/fiddles/windows/manage-windows/new-window/renderer.js
Normal file
19
docs/fiddles/windows/manage-windows/new-window/renderer.js
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
const { BrowserWindow } = require('electron').remote
|
||||||
|
const { shell } = require('electron').remote
|
||||||
|
|
||||||
|
const newWindowBtn = document.getElementById('new-window')
|
||||||
|
const link = document.getElementById('browser-window-link')
|
||||||
|
|
||||||
|
newWindowBtn.addEventListener('click', (event) => {
|
||||||
|
|
||||||
|
let win = new BrowserWindow({ width: 400, height: 320 })
|
||||||
|
|
||||||
|
win.on('close', () => { win = null })
|
||||||
|
win.loadURL('https://electronjs.org')
|
||||||
|
win.show()
|
||||||
|
})
|
||||||
|
|
||||||
|
link.addEventListener('click', (e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
shell.openExternal("http://electron.atom.io/docs/api/browser-window")
|
||||||
|
})
|
Loading…
Reference in a new issue