main.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. import { app, BrowserWindow, Event, Input, ipcMain, Menu, WebContentsView } from 'electron'
  2. import { fileURLToPath } from 'node:url'
  3. import path from 'node:path'
  4. import fs from 'node:fs'
  5. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  6. // The built directory structure
  7. //
  8. // ├─┬─┬ dist
  9. // │ │ └── index.html
  10. // │ │
  11. // │ ├─┬ dist-electron
  12. // │ │ ├── main.js
  13. // │ │ └── preload.mjs
  14. // │
  15. process.env.APP_ROOT = path.join(__dirname, '..')
  16. // 🚧 Use ['ENV_NAME'] avoid vite:define plugin - Vite@2.x
  17. export const VITE_DEV_SERVER_URL = process.env['VITE_DEV_SERVER_URL']
  18. export const MAIN_DIST = path.join(process.env.APP_ROOT, 'dist-electron')
  19. export const RENDERER_DIST = path.join(process.env.APP_ROOT, 'dist')
  20. process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL ? path.join(process.env.APP_ROOT, 'public') : RENDERER_DIST
  21. let mainWindow: BrowserWindow | null
  22. let childView: WebContentsView | null
  23. let isSideOpen = true
  24. let childViewAspectRatio = 0;
  25. const SIDE_WIDTH = 250
  26. const EXPAND_VIEW_SIZE = 40
  27. const LOADING_VIEW_WIDTH = 150
  28. const LOADING_VIEW_HEIGHT = 100
  29. function loadWindowPage(window: BrowserWindow, subPath: string) {
  30. if (VITE_DEV_SERVER_URL) {
  31. window.loadURL(VITE_DEV_SERVER_URL + "#" + subPath)
  32. } else {
  33. window.loadFile(path.join(RENDERER_DIST, 'index.html') + "#" + subPath)
  34. }
  35. }
  36. function loadViewUrl(view: WebContentsView, subPath: string) {
  37. if (!mainWindow) {
  38. return
  39. }
  40. if (VITE_DEV_SERVER_URL) {
  41. view.webContents.loadURL(VITE_DEV_SERVER_URL + "#" + subPath)
  42. } else {
  43. view.webContents.loadFile(path.join(RENDERER_DIST, 'index.html') + "#" + subPath)
  44. }
  45. }
  46. Menu.setApplicationMenu(null)
  47. function createWindow() {
  48. mainWindow = new BrowserWindow({
  49. icon: path.join(process.env.VITE_PUBLIC, 'icon.ico'),
  50. webPreferences: {
  51. preload: path.join(__dirname, 'preload.mjs'),
  52. contextIsolation: true,
  53. allowRunningInsecureContent: true,
  54. partition: 'persist:minnan-demo-app',
  55. },
  56. width: 1200,
  57. height: 800,
  58. })
  59. childView = new WebContentsView({
  60. webPreferences: {
  61. partition: 'persist:minnan-demo-app',
  62. allowRunningInsecureContent: true,
  63. enableBlinkFeatures: 'PasswordManager',
  64. },
  65. })
  66. const expandButtonView = new WebContentsView({
  67. webPreferences: {
  68. preload: path.join(__dirname, 'preload.mjs'),
  69. contextIsolation: true,
  70. },
  71. })
  72. const loadingView = new BrowserWindow({
  73. skipTaskbar: true,
  74. width: LOADING_VIEW_WIDTH,
  75. height: LOADING_VIEW_HEIGHT,
  76. parent: mainWindow,
  77. thickFrame: true,
  78. titleBarStyle: 'hidden',
  79. webPreferences: {
  80. preload: path.join(__dirname, 'preload.mjs'),
  81. contextIsolation: true,
  82. },
  83. })
  84. mainWindow.contentView.addChildView(childView)
  85. mainWindow.contentView.addChildView(expandButtonView)
  86. expandButtonView.setVisible(false)
  87. childView.webContents.on('did-start-loading', () => {
  88. loadingView.show()
  89. });
  90. childView.webContents.on('did-stop-loading', () => {
  91. loadingView.hide()
  92. });
  93. childView.webContents.on('did-fail-load', (_, errorCode, errorDescription) => {
  94. loadingView.hide()
  95. loadWindowPage(loadingView, '/error?code=' + errorCode + '&message=' + errorDescription);
  96. });
  97. function updateChildWindowBounds() {
  98. const bounds = mainWindow!.getBounds();
  99. expandButtonView.setBounds({
  100. x: 0,
  101. y: bounds.height - 100,
  102. width: EXPAND_VIEW_SIZE,
  103. height: EXPAND_VIEW_SIZE
  104. })
  105. loadingView.setBounds({
  106. x: bounds.x + (bounds.width - LOADING_VIEW_WIDTH) / 2,
  107. y: bounds.y + (bounds.height - LOADING_VIEW_HEIGHT) / 2,
  108. width: LOADING_VIEW_WIDTH,
  109. height: LOADING_VIEW_HEIGHT
  110. })
  111. if (childViewAspectRatio) {
  112. // 保持子页纵横比
  113. const rect = {
  114. x: isSideOpen ? SIDE_WIDTH : 0,
  115. y: 0,
  116. width: bounds.width - (isSideOpen ? SIDE_WIDTH : 0),
  117. height: bounds.height
  118. };
  119. // 计算子窗口的最佳尺寸(contain模式)
  120. const availableRatio = rect.width / rect.height;
  121. let childWidth, childHeight;
  122. if (availableRatio > childViewAspectRatio) {
  123. // 高度受限制
  124. childHeight = rect.height;
  125. childWidth = childHeight * childViewAspectRatio;
  126. } else {
  127. // 宽度受限制
  128. childWidth = rect.width;
  129. childHeight = childWidth / childViewAspectRatio;
  130. }
  131. // 计算居中位置
  132. const childX = rect.x + (rect.width - childWidth) / 2;
  133. const childY = rect.y + (rect.height - childHeight) / 2;
  134. // 应用到子窗口
  135. childView!.setBounds({
  136. x: Math.round(childX),
  137. y: Math.round(childY),
  138. width: Math.round(childWidth),
  139. height: Math.round(childHeight)
  140. });
  141. } else {
  142. childView!.setBounds({
  143. x: isSideOpen ? SIDE_WIDTH : 0,
  144. y: 0,
  145. width: bounds.width - (isSideOpen ? SIDE_WIDTH : 0),
  146. height: bounds.height
  147. });
  148. }
  149. }
  150. loadWindowPage(mainWindow, '/');
  151. loadWindowPage(loadingView, '/loading');
  152. loadViewUrl(childView, '/hello');
  153. loadViewUrl(expandButtonView, '/expand');
  154. updateChildWindowBounds();
  155. mainWindow.on('resize', () => {
  156. updateChildWindowBounds()
  157. })
  158. mainWindow.webContents.on('did-finish-load', () => {
  159. mainWindow?.webContents.send('main-process-message', (new Date).toLocaleString())
  160. })
  161. function handleWindowFullScreenKeys(event: Event, input: Input) {
  162. if (input.key === 'F11' && input.type === 'keyDown') {
  163. event.preventDefault();
  164. mainWindow?.setFullScreen(!mainWindow?.isFullScreen())
  165. } else if (input.key === 'F12' && input.type === 'keyDown') {
  166. event.preventDefault();
  167. mainWindow?.webContents.toggleDevTools();
  168. }
  169. }
  170. // 添加F11全屏切换功能
  171. mainWindow.webContents.on('before-input-event', (event, input) => {
  172. handleWindowFullScreenKeys(event, input)
  173. })
  174. childView.webContents.on('before-input-event', (event, input) => {
  175. handleWindowFullScreenKeys(event, input)
  176. })
  177. // 处理退出应用事件
  178. ipcMain.on('exit-app', () => {
  179. app.quit()
  180. })
  181. // 处理全屏切换事件
  182. ipcMain.on('toggle-fullscreen', (_event, isFullScreen: boolean) => {
  183. if (mainWindow) {
  184. if (isFullScreen) {
  185. mainWindow.setFullScreen(true)
  186. } else {
  187. mainWindow.setFullScreen(false)
  188. }
  189. }
  190. })
  191. // 加载子页URL
  192. ipcMain.on('load-child-url', (_event, url: string, aspectRatio: number) => {
  193. if (childView)
  194. childView.webContents.loadURL(url)
  195. childViewAspectRatio = aspectRatio
  196. updateChildWindowBounds()
  197. })
  198. // 子页侧边栏开关
  199. ipcMain.on('toggle-child-side', (_event, value: boolean) => {
  200. isSideOpen = value
  201. expandButtonView.setVisible(!isSideOpen)
  202. mainWindow?.webContents.send('main-side-state-changed', isSideOpen)
  203. updateChildWindowBounds()
  204. })
  205. // 处理获取应用路径事件
  206. ipcMain.handle('get-app-path', () => {
  207. return app.getAppPath()
  208. })
  209. // 处理打开窗口事件
  210. ipcMain.on('open-window', (_event, url: string) => {
  211. const newWin = new BrowserWindow({
  212. icon: path.join(process.env.VITE_PUBLIC, 'icon.ico'),
  213. webPreferences: {
  214. preload: path.join(__dirname, 'preload.mjs'),
  215. contextIsolation: true,
  216. allowRunningInsecureContent: true,
  217. },
  218. fullscreenable: true,
  219. maximizable: true,
  220. width: 1200,
  221. height: 800,
  222. })
  223. newWin.loadURL(url)
  224. newWin.maximize();
  225. // 添加F11全屏切换功能
  226. newWin.webContents.on('before-input-event', (event, input) => {
  227. if (input.key === 'F11' && input.type === 'keyDown') {
  228. event.preventDefault();
  229. newWin?.setFullScreen(!newWin?.fullScreen)
  230. } else if (input.key === 'F12' && input.type === 'keyDown') {
  231. event.preventDefault();
  232. newWin?.webContents.toggleDevTools();
  233. }
  234. })
  235. })
  236. // 处理加载apps.json事件
  237. ipcMain.handle('load-apps-json', async () => {
  238. const appPath = process.cwd()
  239. const appsJsonPath = path.join(appPath, 'apps.json')
  240. try {
  241. if (fs.existsSync(appsJsonPath)) {
  242. const data = fs.readFileSync(appsJsonPath, 'utf8')
  243. return JSON.parse(data)
  244. } else {
  245. // 开发环境下回退到public目录
  246. const devAppsJsonPath = path.join(process.env.VITE_PUBLIC || '', 'apps.json')
  247. if (fs.existsSync(devAppsJsonPath)) {
  248. const data = fs.readFileSync(devAppsJsonPath, 'utf8')
  249. return JSON.parse(data)
  250. }
  251. throw new Error('apps.json not found')
  252. }
  253. } catch (error) {
  254. console.error('Error loading apps.json:', error)
  255. throw error
  256. }
  257. })
  258. // 处理加载默认apps.json事件
  259. ipcMain.handle('load-default-apps-json', async () => {
  260. const devAppsJsonPath = path.join(process.env.VITE_PUBLIC || '', 'apps.json')
  261. if (fs.existsSync(devAppsJsonPath)) {
  262. const data = fs.readFileSync(devAppsJsonPath, 'utf8')
  263. return JSON.parse(data)
  264. }
  265. throw new Error('apps.json not found')
  266. })
  267. // 处理显示配置窗口事件
  268. ipcMain.on('show-config', () => {
  269. const configWindow = new BrowserWindow({
  270. icon: path.join(process.env.VITE_PUBLIC, 'icon.ico'),
  271. webPreferences: {
  272. preload: path.join(__dirname, 'preload.mjs'),
  273. contextIsolation: true,
  274. allowRunningInsecureContent: true,
  275. },
  276. title: '列表配置',
  277. parent: mainWindow || undefined,
  278. skipTaskbar: true,
  279. minimizable: false,
  280. maximizable: false,
  281. modal: true,
  282. width: 800,
  283. height: 600,
  284. })
  285. loadWindowPage(configWindow, '/config')
  286. });
  287. // 处理保存apps.json事件
  288. ipcMain.on('save-apps-json', (_event, appsJson: string) => {
  289. const appPath = process.cwd()
  290. const appsJsonPath = path.join(appPath, 'apps.json')
  291. try {
  292. fs.writeFileSync(appsJsonPath, appsJson)
  293. mainWindow?.webContents.send('main-config-changed')
  294. } catch (error) {
  295. console.error('Error saving apps.json:', error)
  296. throw error
  297. }
  298. })
  299. // 处理显示关于窗口事件
  300. ipcMain.on('show-about', () => {
  301. const aboutWindow = new BrowserWindow({
  302. icon: path.join(process.env.VITE_PUBLIC, 'icon.ico'),
  303. webPreferences: {
  304. preload: path.join(__dirname, 'preload.mjs'),
  305. contextIsolation: true,
  306. allowRunningInsecureContent: true,
  307. },
  308. parent: mainWindow || undefined,
  309. title: '关于程序',
  310. skipTaskbar: true,
  311. maximizable: false,
  312. minimizable: false,
  313. modal: true,
  314. width: 450,
  315. height: 470,
  316. })
  317. loadWindowPage(aboutWindow, '/about')
  318. });
  319. }
  320. // Quit when all mainWindowdows are closed, except on macOS. There, it's common
  321. // for applications and their menu bar to stay active until the user quits
  322. // explicitly with Cmd + Q.
  323. app.on('window-all-closed', () => {
  324. if (process.platform !== 'darwin') {
  325. app.quit()
  326. mainWindow = null
  327. }
  328. })
  329. app.on('activate', () => {
  330. // On OS X it's common to re-create a mainWindowdow in the app when the
  331. // dock icon is clicked and there are no other mainWindowdows open.
  332. if (BrowserWindow.getAllWindows().length === 0) {
  333. createWindow()
  334. }
  335. })
  336. app.whenReady().then(createWindow)