nx-browse
Version:
tel13520521413
161 lines (159 loc) • 5.49 kB
text/typescript
import box_electron, {
app,
dialog,// 对话框:可以绑定事件的本机系统对话框
Notification,// 通知
autoUpdater,
Tray,// 托盘图标
globalShortcut,// globalShortcut 模块可以在操作系统中注册/注销全局快捷键
BrowserView,
BrowserWindow,
BrowserWindowConstructorOptions, WebContents
} from "electron"
// import 'regenerator-runtime/runtime'; // facebook的regenerator模块,
// 生成器函数、async、await函数经babel编译后,regenerator-runtime模块用于提供功能实现
// if (!app.requestSingleInstanceLock()) {
// return pcTips('whenReady is ing .exit this.show app');
// app.releaseSingleInstanceLock()
// const sourceMapSupport = require('source-map-support');
// sourceMapSupport.install();
// require('electron-debug')()
// }
const cache: {
tray?: Tray,
tipicon?: string,
trayicon?: string,
routerCache: { [k: string]: BrowserWindow }
} = {
routerCache: {}
}
interface BrowserRetrun {
browserObj: BrowserWindow,
webContents: WebContents
lockTitle: () => void,
lockWin: () => void,
createChildBrowser: (browserObj: BrowserWindowConstructorOptions) => Promise<BrowserRetrun>,
}
const nx_browser = (
browserOption: BrowserWindowConstructorOptions = {},
cacheKey?: string
): Promise<BrowserRetrun> => new Promise((ok) => {
if (!app.isPackaged) {
browserOption.x = 3000;
browserOption.y = 0;
}
// show: false
let browserObj: BrowserWindow;
const newBrowser = () => new BrowserWindow({
minimizable: false,
maximizable: false,
...browserOption
});
if (cacheKey && cache.routerCache[cacheKey]) {
browserObj =cache.routerCache[cacheKey]
} else if (cacheKey) {
browserObj = cache.routerCache[cacheKey] = newBrowser()
} else {
browserObj = newBrowser()
}
if (!app.isPackaged) {
browserObj.webContents.openDevTools();
// https://www.electronjs.org/docs/tutorial/accessibility#spectron
}
const webContents = browserObj.webContents;
const res: BrowserRetrun = {
browserObj,
webContents,
lockTitle() {
browserObj.on('page-title-updated', (e) => e.preventDefault())
webContents.on('page-title-updated', (e) => e.preventDefault())
},
lockWin() {
webContents.on("new-window", (e, u) => {
e.preventDefault();
webContents.loadURL(u);
})
webContents.on("will-navigate", (e, u) => {
e.preventDefault();
webContents.loadURL(u);
});
},
createChildBrowser(option: BrowserWindowConstructorOptions) {
return nx_browser({ ...option, parent: browserObj })
}
}
ok(res)
})
export const electron = {
...box_electron,
nx_appUpdata(url: string) {
return new Promise((ok) => {
// if (!app.isPackaged) return ok()
autoUpdater.setFeedURL({ url })
setInterval(() => {
autoUpdater.checkForUpdates()
}, 600000)
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
const dialogOpts = {
type: 'info',
buttons: ['现在更新', '以后再说'],
title: '应用有更新',
message: releaseNotes + releaseName,
detail: 'A new version has been downloaded. Restart the application to apply the updates.'
}
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall()
})
})
autoUpdater.on('error', message => {
const c = new Notification({
title: '出现错误',
body: '老师电话:xxxxxxxxxx'
});
c.show();
c.closeButtonText = '确定'
c.on('click', () => c.close())
})
ok()
})
},
nx_appexit: () => {
BrowserWindow.getAllWindows().forEach(v => v.close());
app.quit();
app.exit(0)
},
nx_init: {
setAppUserModelId: app.setAppUserModelId,
tray: (icon: string) => new Promise(ok => {
cache.trayicon = icon;
cache.tray = new Tray(icon)
ok(cache.tray)
})
},
nx_tray: (): Promise<Tray> => new Promise((ok, err) => {
return cache.tray ? ok(cache.tray) : err('please run nx_init.tray')
}),
nx_tips(
title: string,
body?: string | object,
icon?: string
) {
return new Promise((ok) => {
if (typeof body === 'object') {
body = body.toString()
}
if (icon) {
cache.tipicon = icon;
}
const tip = new Notification({
title,
body: body,
timeoutType: 'default',
icon: cache['tipicon'],
})
tip.show()
ok(tip)
// notification.on('click', () => notification.show())
})
},
nx_browser
}