feat: add electron app.

This commit is contained in:
jaywcjlove
2022-09-04 22:09:04 +08:00
parent a66e906eef
commit 76a6f48d0a
62 changed files with 483 additions and 98 deletions

1
electron/main/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
lib

View File

@@ -0,0 +1,16 @@
{
"name": "@wcj/wxmp-main",
"version": "2.1.0",
"main": "./lib/index.js",
"private": true,
"scripts": {
"build": "tsbb build --disable-babel --file-names src/index.ts",
"watch": "tsbb watch --disable-babel --file-names src/index.ts"
},
"files": [
"lib"
],
"devDependencies": {
"electron": "19.0.5"
}
}

56
electron/main/src/Menu.ts Normal file
View File

@@ -0,0 +1,56 @@
import { app, Menu, MenuItem, MenuItemConstructorOptions } from 'electron';
const isMac = process.platform === 'darwin';
const template = [
// { role: 'appMenu' }
...(isMac
? [
{
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideOthers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' },
],
},
]
: []),
{ role: 'editMenu' },
{
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
...(isMac
? [{ type: 'separator' }, { role: 'front' }, { type: 'separator' }, { role: 'window' }]
: [{ role: 'close' }]),
],
},
{
role: 'help',
submenu: [
{
label: 'Open Source for Github',
click: async () => {
const { shell } = require('electron');
await shell.openExternal('https://github.com/jaywcjlove/wxmp');
},
},
{
label: 'Online Website',
click: async () => {
const { shell } = require('electron');
await shell.openExternal('https://jaywcjlove.github.io/wxmp');
},
},
],
},
];
const menu = Menu.buildFromTemplate(template as Array<MenuItem | MenuItemConstructorOptions>);
Menu.setApplicationMenu(menu);

60
electron/main/src/app.ts Normal file
View File

@@ -0,0 +1,60 @@
import { app, shell, BrowserWindow } from 'electron';
import './Menu';
export interface Options extends Electron.BrowserWindowConstructorOptions {
preload?: string;
webpath?: string;
}
export class App {
app = app;
win?: BrowserWindow;
isLogin: boolean = false;
/** 创建主进程窗口 */
async createWindow(options: Options = {}, loadURL?: string) {
await app.whenReady();
const opts: Options = {
titleBarStyle: 'hidden', // 无标题栏
frame: false, // 创建无边窗口
width: 800,
height: 600,
minWidth: 850,
center: true,
maximizable: true,
minimizable: true,
resizable: true,
webPreferences: {
// 多线程
nodeIntegrationInWorker: true,
nodeIntegration: true,
contextIsolation: false,
},
...options,
};
if (options.preload) {
opts.webPreferences.preload = options.preload;
}
this.win = new BrowserWindow(opts);
if (process.env.NODE_ENV === 'development') {
this.win.loadURL(loadURL || 'http://localhost:3000/');
// 打开开发者工具,默认不打开
this.win.webContents.openDevTools();
} else {
this.win.loadFile(options.webpath);
}
this.win.webContents.setWindowOpenHandler(({ url }) => {
if (/^https?:\/\//.test(url)) {
shell.openExternal(url);
return { action: 'deny' };
}
return {
action: 'allow',
overrideBrowserWindowOptions: {
modal: true,
},
};
});
return this.win;
}
}

View File

@@ -0,0 +1 @@
export * from './app';

View File

@@ -0,0 +1,19 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"declaration": true,
"target": "es2017",
"noImplicitAny": true,
"resolveJsonModule": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": false,
"strict": false,
"skipLibCheck": true,
"outDir": "lib",
"baseUrl": "."
},
"include": ["src"]
}