| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- const { app, BrowserWindow, Menu, ipcMain, globalShortcut, screen, Tray } = require('electron');
- const { join } = require('path');
- const { platform } = require('process');
- class MainSerivce {
- constructor() {
- Menu.setApplicationMenu(null) // 去掉菜单栏
- app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
- app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
- this.loadingWin = null
- this.mainWin = null
- this.contrlEvent = null
- if (!app.requestSingleInstanceLock({ key: 'contrl' })) {
- app.quit()
- } else {
- app.on('ready', this.onRead.bind(this))
- app.on('activate', (e, isVisible) => {
- if (!isVisible && this.mainWin) {
- // 兼容Mac dock 栏点击
- this.mainWin.show()
- }
- })
- app.on('window-all-closed', () => app.quit())
- }
- }
- createLoading() {
- const { size: { width, height } } = screen.getPrimaryDisplay()
- this.loadingWin = new BrowserWindow({
- frame: false, // 无边框(窗口、工具栏等),只包含网页内容
- width,
- height,
- resizable: false,
- center: true,
- alwaysOnTop: true,
- transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
- })
- if (app.isPackaged) {
- this.loadingWin.loadFile(join(__dirname, './loading.html'))
- } else {
- this.loadingWin.loadFile('loading.html')
- }
- this.loadingWin.on('close', () => {
- this.loadingWin = null
- })
- }
- createWindow() {
- this.mainWin = new BrowserWindow({
- minWidth: 900,
- minHeight: 760,
- width: 900,
- height: 760,
- titleBarStyle:'hidden',
- titleBarOverlay:{
- color:'rgba(0,0,0,0)',
- height:40
- },
- webPreferences: {
- contextIsolation: true,
- nodeIntegration: true,
- webSecurity: true,
- nodeIntegrationInWorker: true,
- preload: join(__dirname, './preload.js')
- },
- show: false
- })// 创建一个窗口
- this.mainWin.setMenu(null)
- // 不同环境加载不同文件
- if (app.isPackaged) {
- this.mainWin.loadFile(join(__dirname, './index.html'))
- } else {
- this.mainWin.loadURL('http://localhost:6547/')
- }
- // 事件监听
- this.mainWin.on('close', () => { this.mainWin = null })
- }
- onRead() {
- this.createLoading()
- this.createWindow()
- // 注册调试模式
- globalShortcut.register('Ctrl+F12', () => {
- this.mainWin.webContents.toggleDevTools()
- })
- // 系统环境
- if (platform === 'win32') {
- // 右键
- tray.setContextMenu(contextMenu)
- // 禁用右键
- this.mainWin.hookWindowMessage(278, () => {
- this.mainWin.setEnabled(false);//窗口禁用
- setTimeout(() => {
- this.mainWin.setEnabled(true);
- }, 100) //延时太快会立刻启动,太慢会妨碍窗口其他操作,可自行测试最佳时间
- return true
- })
- }
- // 通信
- ipcMain.on('signal', (_, evt, data) => {
- if (evt === 'close-loading') {
- if (this.loadingWin) this.loadingWin.close()
- this.mainWin.show()
- } else if (evt === 'minWin') {
- this.mainWin.minimize()
- } else if (evt === 'closeWin') {
- this.mainWin.close()
- } else if (evt === 'maxWin') {
- const { width, height } = screen.getPrimaryDisplay().size
- if (data) { this.mainWin.setBounds({ x: 0, y: 0, width, height }) } else {
- this.mainWin.setBounds({ width: 1300, height: 760 })
- this.mainWin.center()
- }
- }
- })
- }
- };
- new MainSerivce()
|