electron-vite-app/src/main/index.ts

193 lines
5.8 KiB
TypeScript

import { app, shell, BrowserWindow, ipcMain, Notification } from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
import { autoUpdater } from 'electron-updater'
import Store from './lib/Store'
import { registerToastTarget, showErrorToast, showSuccessToast, showToast } from './lib/Toast'
autoUpdater.autoDownload = false;
autoUpdater.autoInstallOnAppQuit = true;
const store = new Store({
configName: 'user-preferences',
defaults: {
showSetupWindow: true
}
});
let mainWindow: BrowserWindow;
let splashWindow: BrowserWindow;
let setupWindow: BrowserWindow;
function createSplashWindow(): void {
splashWindow = new BrowserWindow({
width: 350,
height: 350,
transparent: true,
frame: false,
alwaysOnTop: true
});
splashWindow.loadFile('src/renderer/splash.html');
splashWindow.center();
splashWindow.setSkipTaskbar(true);
splashWindow.show();
}
function createSetupWindow(): void {
setupWindow = new BrowserWindow({
width: 380,
height: 390,
show: false,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false
},
});
setupWindow.loadFile('src/renderer/setup.html');
}
function createMainWindow(): void {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 900,
height: 670,
show: false,
autoHideMenuBar: true,
...(process.platform === 'linux' ? { icon } : {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false
}
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
// HMR for renderer base on electron-vite cli.
// Load the remote URL for development or the local html file for production.
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}
}
function finishSetup(): void {
store.set('showSetupWindow', false);
createMainWindow();
mainWindow.on('ready-to-show', () => {
registerToastTarget(mainWindow);
mainWindow.show();
setupWindow.close();
})
}
function createWindow(): void {
createSplashWindow();
let showSetupWindow = store.get('showSetupWindow');
if (showSetupWindow) {
createSetupWindow();
setupWindow.on('ready-to-show', () => {
splashWindow.close();
setupWindow.show();
})
} else {
createMainWindow();
mainWindow.on('ready-to-show', () => {
registerToastTarget(mainWindow);
checkForUpdates();
splashWindow.close();
mainWindow.show();
})
}
}
// 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(() => {
// Set app user model id for windows
electronApp.setAppUserModelId('com.electron')
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
// IPC test
ipcMain.on('ping', () => console.log('pong'))
ipcMain.on('finish-setup', () => finishSetup())
ipcMain.handle('get-version', () => app.getVersion())
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', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
// 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.
function showNotification(value: any) {
new Notification({
title: value,
}).show()
}
function checkForUpdates() {
autoUpdater.checkForUpdates();
console.log(`Checking for updates. Current version ${app.getVersion()}`);
showToast(`Checking for updates. Current version ${app.getVersion()}`);
showNotification(`Checking for updates. Current version ${app.getVersion()}`);
}
/*New Update Available*/
autoUpdater.on("update-available", () => {
console.log(`Update available. Current version ${app.getVersion()}`);
showToast(`Update available. Current version ${app.getVersion()}`);
showNotification(`Update available. Current version ${app.getVersion()}`);
let pth = autoUpdater.downloadUpdate();
console.log(pth);
showToast(`Download update path : ${pth}`);
showNotification(`Download update path : ${pth}`);
});
autoUpdater.on("update-not-available", () => {
console.log(`No update available. Current version ${app.getVersion()}`);
showToast(`No update available. Current version ${app.getVersion()}`);
showNotification(`No update available. Current version ${app.getVersion()}`);
});
/*Download Completion Message*/
autoUpdater.on("update-downloaded", () => {
console.log(`Update downloaded. Current version ${app.getVersion()}`);
showSuccessToast(`Update downloaded. Current version ${app.getVersion()}`);
showNotification(`Update downloaded. Current version ${app.getVersion()}`);
});
autoUpdater.on("error", (info) => {
console.log(info);
showErrorToast(`Error: ${info}`);
showNotification(`Error: ${info}`);
});