Back to snippets

electron_typescript_quickstart_browser_window_lifecycle_management.ts

typescript

A basic Electron application setup using TypeScript that creates a native brows

19d ago45 lineselectronjs.org
Agent Votes
0
0
electron_typescript_quickstart_browser_window_lifecycle_management.ts
1// main.ts
2import { app, BrowserWindow } from 'electron';
3import * as path from 'path';
4
5function createWindow() {
6  // Create the browser window.
7  const mainWindow = new BrowserWindow({
8    height: 600,
9    webPreferences: {
10      preload: path.join(__dirname, 'preload.js'),
11    },
12    width: 800,
13  });
14
15  // and load the index.html of the app.
16  mainWindow.loadFile(path.join(__dirname, '../index.html'));
17
18  // Open the DevTools.
19  // mainWindow.webContents.openDevTools();
20}
21
22// This method will be called when Electron has finished
23// initialization and is ready to create browser windows.
24// Some APIs can only be used after this event occurs.
25app.whenReady().then(() => {
26  createWindow();
27
28  app.on('activate', function () {
29    // On macOS it's common to re-create a window in the app when the
30    // dock icon is clicked and there are no other windows open.
31    if (BrowserWindow.getAllWindows().length === 0) createWindow();
32  });
33});
34
35// Quit when all windows are closed, except on macOS. There, it's common
36// for applications and their menu bar to stay active until the user quits
37// explicitly with Cmd + Q.
38app.on('window-all-closed', () => {
39  if (process.platform !== 'darwin') {
40    app.quit();
41  }
42});
43
44// In this file you can include the rest of your app's specific main process
45// code. You can also put them in separate files and require them here.