Last active
November 5, 2022 00:47
-
-
Save alielmajdaoui/2f793493247a86801bdbb593a4ebc595 to your computer and use it in GitHub Desktop.
./src/settings-main/settings-main.ts
This file contains 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
/* eslint global-require: off, no-console: off, promise/always-return: off, import/prefer-default-export: off */ | |
import path from 'path'; | |
import { app, BrowserWindow, shell } from 'electron'; | |
import { resolveHtmlPath } from '../main/util'; | |
let settingsWindow: BrowserWindow | null = null; | |
const isDebug = | |
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true'; | |
const installExtensions = async () => { | |
const installer = require('electron-devtools-installer'); | |
const forceDownload = !!process.env.UPGRADE_EXTENSIONS; | |
const extensions = ['REACT_DEVELOPER_TOOLS']; | |
return installer | |
.default( | |
extensions.map((name) => installer[name]), | |
forceDownload | |
) | |
.catch(console.log); | |
}; | |
export const createWindow = async () => { | |
if (isDebug) { | |
await installExtensions(); | |
} | |
const RESOURCES_PATH = app.isPackaged | |
? path.join(process.resourcesPath, 'assets') | |
: path.join(__dirname, '../../assets'); | |
const getAssetPath = (...paths: string[]): string => { | |
return path.join(RESOURCES_PATH, ...paths); | |
}; | |
settingsWindow = new BrowserWindow({ | |
show: false, | |
width: 1024, | |
height: 728, | |
icon: getAssetPath('icon.png'), | |
webPreferences: { | |
preload: app.isPackaged | |
? path.join(__dirname, 'preload.js') | |
: path.join(__dirname, '../../.erb/dll/preload.js'), | |
}, | |
}); | |
settingsWindow.loadURL(resolveHtmlPath('settings.html')); | |
settingsWindow.on('ready-to-show', () => { | |
if (!settingsWindow) { | |
throw new Error('"settingsWindow" is not defined'); | |
} | |
settingsWindow.show(); | |
}); | |
settingsWindow.on('closed', () => { | |
settingsWindow = null; | |
}); | |
// Open urls in the user's browser | |
settingsWindow.webContents.setWindowOpenHandler((edata) => { | |
shell.openExternal(edata.url); | |
return { action: 'deny' }; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment