-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
468 additions
and
290 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
|
||
|
||
|
||
node_modules | ||
.DS_Store | ||
dist | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type {ModuleContext} from './ModuleContext.js'; | ||
|
||
export interface AppModule { | ||
enable(context: ModuleContext): Promise<void>|void; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ModuleContext = { | ||
readonly app: Electron.App; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {AppModule} from './AppModule.js'; | ||
import {ModuleContext} from './ModuleContext.js'; | ||
import {app} from 'electron'; | ||
|
||
class ModuleRunner implements PromiseLike<void> { | ||
#promise: Promise<void>; | ||
|
||
constructor() { | ||
this.#promise = Promise.resolve(); | ||
} | ||
|
||
then<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): PromiseLike<TResult1 | TResult2> { | ||
return this.#promise.then(onfulfilled, onrejected); | ||
} | ||
|
||
enable(module: AppModule) { | ||
const p = module.enable(this.#createModuleContext()); | ||
|
||
if (p instanceof Promise) { | ||
this.#promise = this.#promise.then(() => p); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
#createModuleContext(): ModuleContext { | ||
return { | ||
app, | ||
}; | ||
} | ||
} | ||
|
||
export function createModuleRunner() { | ||
return new ModuleRunner(); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,56 @@ | ||
import {app} from 'electron'; | ||
import './security-restrictions'; | ||
import {platform} from 'node:process'; | ||
import type {AppInitConfig} from './AppInitConfig.js'; | ||
import {createWindowManager} from './createWindowManager.js'; | ||
import {runAutoUpdater} from './auto-updater.js'; | ||
import {createModuleRunner} from './ModuleRunner.js'; | ||
import {createSingleInstanceApp} from './modules/SingleInstanceApp.js'; | ||
import {createWindowManagerModule} from './modules/WindowManager.js'; | ||
import {createApplicationTerminatorOnLastWindowCloseModule} from './modules/ApplicationTerminatorOnLastWindowClose.js'; | ||
import {createHardwareAccelerationModule} from './modules/HarfwareAccelerationModule.js'; | ||
import {createAutoUpdaterModule} from './modules/AutoUpdater.js'; | ||
import {createChromeDevToolsExtensionModule} from './modules/ChromeDevToolsExtension.js'; | ||
import {createBlockNotAllowedOrigins} from './modules/security/BlockNotAllowdOrigins.js'; | ||
import {createPermissionsForOrigin} from './modules/security/PermissionsForOrigin.js'; | ||
import {createExternalUrlsModule} from './modules/ExternalUrls.js'; | ||
|
||
// Used in packages/entry-point.js | ||
export function initApp(initConfig: AppInitConfig) { | ||
const {restoreOrCreateWindow} = createWindowManager({ | ||
preload: initConfig.preload, | ||
renderer: initConfig.renderer, | ||
}); | ||
|
||
/** | ||
* Prevent electron from running multiple instances. | ||
*/ | ||
const isSingleInstance = app.requestSingleInstanceLock(); | ||
if (!isSingleInstance) { | ||
app.quit(); | ||
process.exit(0); | ||
} | ||
app.on('second-instance', restoreOrCreateWindow); | ||
|
||
/** | ||
* Disable Hardware Acceleration to save more system resources. | ||
*/ | ||
app.disableHardwareAcceleration(); | ||
|
||
/** | ||
* Shout down background process if all windows was closed | ||
*/ | ||
app.on('window-all-closed', () => { | ||
if (platform !== 'darwin') { | ||
app.quit(); | ||
} | ||
}); | ||
// Used in packages/entry-point.js | ||
export async function initApp(initConfig: AppInitConfig) { | ||
const moduleRunner = createModuleRunner() | ||
.enable(createWindowManagerModule({initConfig})) | ||
.enable(createSingleInstanceApp()) | ||
.enable(createApplicationTerminatorOnLastWindowCloseModule()) | ||
.enable(createHardwareAccelerationModule({enable: false})) | ||
.enable(createAutoUpdaterModule()) | ||
.enable(createChromeDevToolsExtensionModule({extension: 'VUEJS3_DEVTOOLS'})) | ||
|
||
/** | ||
* @see https://www.electronjs.org/docs/latest/api/app#event-activate-macos Event: 'activate'. | ||
*/ | ||
app.on('activate', restoreOrCreateWindow); | ||
// Security | ||
.enable(createBlockNotAllowedOrigins( | ||
new Set(initConfig.renderer instanceof URL ? [initConfig.renderer.origin] : []), | ||
)); | ||
|
||
/** | ||
* Create the application window when the background process is ready. | ||
*/ | ||
app | ||
.whenReady() | ||
.then(restoreOrCreateWindow) | ||
.catch(e => console.error('Failed create window:', e)); | ||
|
||
/** | ||
* Install any extension in development mode only. | ||
* Note: You must install `electron-devtools-installer` manually | ||
* Special rules for development mode | ||
*/ | ||
// if (import.meta.env.DEV) { | ||
// app | ||
// .whenReady() | ||
// .then(() => import('electron-devtools-installer')) | ||
// .then(module => { | ||
// const {default: installExtension, VUEJS_DEVTOOLS} = | ||
// //@ts-expect-error Hotfix for https://github.com/cawa-93/vite-electron-builder/issues/915 | ||
// typeof module.default === 'function' ? module : (module.default as typeof module); | ||
// | ||
// return installExtension(VUEJS_DEVTOOLS, { | ||
// loadExtensionOptions: { | ||
// allowFileAccess: true, | ||
// }, | ||
// }); | ||
// }) | ||
// .catch(e => console.error('Failed install extension:', e)); | ||
// } | ||
|
||
runAutoUpdater(); | ||
if (initConfig.renderer instanceof URL) { | ||
moduleRunner.enable( | ||
createPermissionsForOrigin( | ||
initConfig.renderer.origin, | ||
new Set(['openExternal']), | ||
), | ||
) | ||
.enable( | ||
createExternalUrlsModule( | ||
new Set([ | ||
'https://vite.dev', | ||
'https://developer.mozilla.org', | ||
'https://solidjs.com', | ||
'https://qwik.dev', | ||
'https://lit.dev', | ||
'https://react.dev', | ||
'https://preactjs.com', | ||
'https://www.typescriptlang.org', | ||
'https://vuejs.org', | ||
]), | ||
), | ||
); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/main/src/modules/ApplicationTerminatorOnLastWindowClose.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {AppModule} from '../AppModule.js'; | ||
import {ModuleContext} from '../ModuleContext.js'; | ||
|
||
class ApplicationTerminatorOnLastWindowClose implements AppModule { | ||
enable({app}: ModuleContext): Promise<void> | void { | ||
app.on('window-all-closed', () => app.quit()); | ||
} | ||
} | ||
|
||
|
||
export function createApplicationTerminatorOnLastWindowCloseModule(...args: ConstructorParameters<typeof ApplicationTerminatorOnLastWindowClose>) { | ||
return new ApplicationTerminatorOnLastWindowClose(...args); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {AppModule} from '../AppModule.js'; | ||
import electronUpdater, {type AppUpdater, type Logger} from 'electron-updater'; | ||
|
||
type DownloadNotification = Parameters<AppUpdater['checkForUpdatesAndNotify']>[0]; | ||
|
||
export class AutoUpdater implements AppModule { | ||
|
||
readonly #logger: Logger | null; | ||
readonly #notification: DownloadNotification; | ||
|
||
constructor( | ||
{ | ||
logger = null, | ||
downloadNotification = undefined, | ||
}: | ||
{ | ||
logger?: Logger | null | undefined, | ||
downloadNotification?: DownloadNotification | ||
} = {}, | ||
) { | ||
this.#logger = logger; | ||
this.#notification = downloadNotification; | ||
} | ||
|
||
async enable(): Promise<void> { | ||
await this.runAutoUpdater(); | ||
} | ||
|
||
getAutoUpdater(): AppUpdater { | ||
// Using destructuring to access autoUpdater due to the CommonJS module of 'electron-updater'. | ||
// It is a workaround for ESM compatibility issues, see https://github.com/electron-userland/electron-builder/issues/7976. | ||
const {autoUpdater} = electronUpdater; | ||
return autoUpdater; | ||
} | ||
|
||
async runAutoUpdater() { | ||
const updater = this.getAutoUpdater(); | ||
try { | ||
updater.logger = this.#logger || null; | ||
updater.fullChangelog = true; | ||
|
||
return await updater.checkForUpdatesAndNotify(this.#notification); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
if (error.message.includes('No published versions')) { | ||
return null; | ||
} | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
} | ||
|
||
|
||
export function createAutoUpdaterModule(...args: ConstructorParameters<typeof AutoUpdater>) { | ||
return new AutoUpdater(...args); | ||
} |
Oops, something went wrong.