mirror of
https://github.com/jaywcjlove/wxmp.git
synced 2026-01-11 07:48:48 +08:00
feat: add electron app.
This commit is contained in:
1
electron/main/.gitignore
vendored
Normal file
1
electron/main/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
lib
|
||||
16
electron/main/package.json
Normal file
16
electron/main/package.json
Normal 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
56
electron/main/src/Menu.ts
Normal 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
60
electron/main/src/app.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
1
electron/main/src/index.ts
Normal file
1
electron/main/src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './app';
|
||||
19
electron/main/tsconfig.json
Normal file
19
electron/main/tsconfig.json
Normal 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"]
|
||||
}
|
||||
Reference in New Issue
Block a user