index.js
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { app, shell, BrowserWindow, screen, ipcMain} from 'electron'
import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'
function createWindow(width, height) {
// Create the browser window.
const mainWindow = new BrowserWindow({
icon:icon,
width,
height: height,
center: true,
show: false,
transparent:true,
autoHideMenuBar: true,
resizable: false,
titleBarStyle:'hidden',
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false,
contextIsolation:false,
webSecurity: false,
}
})
// 点击最小化
ipcMain.on('minimizing',(event,args)=>{
event.preventDefault(); // 阻止默认最小化行为
mainWindow.minimize(); // 最小化到任务栏
})
// 点击全屏显示
ipcMain.on('toggle-fullscreen', (event) => {
if (mainWindow.isFullScreen()) {
mainWindow.setFullScreen(false)
} else {
mainWindow.setFullScreen(true)
}
});
// 在主进程中
mainWindow.webContents.on('before-input-event', (event, input) => {
if (input.key === 'Escape' && mainWindow.isFullScreen()) {
mainWindow.setFullScreen(false)
event.preventDefault() // 阻止默认行为
}
})
mainWindow.on('ready-to-show', () => {
mainWindow.show()
mainWindow.setFullScreen(true)
})
mainWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url)
return { action: 'deny' }
})
if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
// 窗口调试 mainWindow.webContents.openDevTools()
mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
} else {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}
}
app.whenReady().then(() => {
// 主进程获取工作区宽高
const primaryDisplay = screen.getPrimaryDisplay()
const { width, height } = primaryDisplay.bounds
electronApp.setAppUserModelId('com.electron')
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
createWindow(width, height)
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow(width, height)
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})