main.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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'), {
  34. hash: subPath,
  35. })
  36. }
  37. }
  38. function loadViewUrl(view: WebContentsView, subPath: string) {
  39. if (!mainWindow) {
  40. return
  41. }
  42. if (VITE_DEV_SERVER_URL) {
  43. view.webContents.loadURL(VITE_DEV_SERVER_URL + "#" + subPath)
  44. } else {
  45. view.webContents.loadFile(path.join(RENDERER_DIST, 'index.html'), {
  46. hash: subPath,
  47. })
  48. }
  49. }
  50. Menu.setApplicationMenu(null)
  51. function createWindow() {
  52. mainWindow = new BrowserWindow({
  53. icon: path.join(process.env.VITE_PUBLIC, 'icon.ico'),
  54. webPreferences: {
  55. preload: path.join(__dirname, 'preload.mjs'),
  56. contextIsolation: true,
  57. devTools: true,
  58. allowRunningInsecureContent: true,
  59. },
  60. width: 1200,
  61. height: 800,
  62. })
  63. childView = new WebContentsView({
  64. webPreferences: {
  65. allowRunningInsecureContent: true,
  66. },
  67. })
  68. const expandButtonView = new WebContentsView({
  69. webPreferences: {
  70. preload: path.join(__dirname, 'preload.mjs'),
  71. contextIsolation: true,
  72. },
  73. })
  74. const loadingView = new BrowserWindow({
  75. skipTaskbar: true,
  76. width: LOADING_VIEW_WIDTH,
  77. height: LOADING_VIEW_HEIGHT,
  78. parent: mainWindow,
  79. thickFrame: true,
  80. titleBarStyle: 'hidden',
  81. webPreferences: {
  82. preload: path.join(__dirname, 'preload.mjs'),
  83. contextIsolation: true,
  84. },
  85. })
  86. mainWindow.contentView.addChildView(childView)
  87. mainWindow.contentView.addChildView(expandButtonView)
  88. expandButtonView.setVisible(false)
  89. childView.webContents.on('did-start-loading', () => {
  90. loadingView.show()
  91. });
  92. childView.webContents.on('did-stop-loading', () => {
  93. loadingView.hide()
  94. });
  95. childView.webContents.on('did-fail-load', (_, errorCode, errorDescription) => {
  96. loadingView.hide()
  97. loadWindowPage(loadingView, '/error?code=' + errorCode + '&message=' + errorDescription);
  98. });
  99. function updateChildWindowBounds() {
  100. const bounds = mainWindow!.getBounds();
  101. expandButtonView.setBounds({
  102. x: 0,
  103. y: bounds.height - 100,
  104. width: EXPAND_VIEW_SIZE,
  105. height: EXPAND_VIEW_SIZE
  106. })
  107. loadingView.setBounds({
  108. x: bounds.x + (bounds.width - LOADING_VIEW_WIDTH) / 2,
  109. y: bounds.y + (bounds.height - LOADING_VIEW_HEIGHT) / 2,
  110. width: LOADING_VIEW_WIDTH,
  111. height: LOADING_VIEW_HEIGHT
  112. })
  113. if (childViewAspectRatio) {
  114. // 保持子页纵横比
  115. const rect = {
  116. x: isSideOpen ? SIDE_WIDTH : 0,
  117. y: 0,
  118. width: bounds.width - (isSideOpen ? SIDE_WIDTH : 0),
  119. height: bounds.height
  120. };
  121. // 计算子窗口的最佳尺寸(contain模式)
  122. const availableRatio = rect.width / rect.height;
  123. let childWidth, childHeight;
  124. if (availableRatio > childViewAspectRatio) {
  125. // 高度受限制
  126. childHeight = rect.height;
  127. childWidth = childHeight * childViewAspectRatio;
  128. } else {
  129. // 宽度受限制
  130. childWidth = rect.width;
  131. childHeight = childWidth / childViewAspectRatio;
  132. }
  133. // 计算居中位置
  134. const childX = rect.x + (rect.width - childWidth) / 2;
  135. const childY = rect.y + (rect.height - childHeight) / 2;
  136. // 应用到子窗口
  137. childView!.setBounds({
  138. x: Math.round(childX),
  139. y: Math.round(childY),
  140. width: Math.round(childWidth),
  141. height: Math.round(childHeight)
  142. });
  143. } else {
  144. childView!.setBounds({
  145. x: isSideOpen ? SIDE_WIDTH : 0,
  146. y: 0,
  147. width: bounds.width - (isSideOpen ? SIDE_WIDTH : 0),
  148. height: bounds.height
  149. });
  150. }
  151. }
  152. loadWindowPage(mainWindow, '/');
  153. loadWindowPage(loadingView, '/loading');
  154. loadViewUrl(childView, '/hello');
  155. loadViewUrl(expandButtonView, '/expand');
  156. updateChildWindowBounds();
  157. mainWindow.on('resize', () => {
  158. updateChildWindowBounds()
  159. })
  160. mainWindow.webContents.on('did-finish-load', () => {
  161. mainWindow?.webContents.send('main-process-message', (new Date).toLocaleString())
  162. })
  163. function handleWindowFullScreenKeys(window: BrowserWindow | WebContentsView, event: Event, input: Input) {
  164. if (input.key === 'F11' && input.type === 'keyDown') {
  165. event.preventDefault();
  166. mainWindow?.setFullScreen(!mainWindow?.isFullScreen())
  167. } else if (input.key === 'F12' && input.type === 'keyDown') {
  168. event.preventDefault();
  169. if (window === mainWindow) {
  170. toggleDevTools()
  171. } else if (window === childView) {
  172. childView?.webContents.toggleDevTools()
  173. }
  174. } else if (input.key === 'F5' && input.type === 'keyDown') {
  175. event.preventDefault();
  176. if (window === mainWindow) {
  177. mainWindow?.webContents.reload()
  178. } else if (window === childView) {
  179. childView?.webContents.reload()
  180. }
  181. }
  182. }
  183. function toggleDevTools() {
  184. if (mainWindow) {
  185. if (mainWindow.webContents.isDevToolsOpened()) {
  186. mainWindow.webContents.closeDevTools()
  187. } else {
  188. mainWindow.webContents.openDevTools({
  189. mode: 'detach',
  190. })
  191. }
  192. }
  193. }
  194. // 添加F11全屏切换功能
  195. mainWindow.webContents.on('before-input-event', (event, input) => {
  196. handleWindowFullScreenKeys(mainWindow!, event, input)
  197. })
  198. childView.webContents.on('before-input-event', (event, input) => {
  199. handleWindowFullScreenKeys(childView!, event, input)
  200. })
  201. // 处理退出应用事件
  202. ipcMain.on('exit-app', () => {
  203. app.quit()
  204. })
  205. // 处理全屏切换事件
  206. ipcMain.on('toggle-fullscreen', (_event, isFullScreen: boolean) => {
  207. if (mainWindow) {
  208. if (isFullScreen) {
  209. mainWindow.setFullScreen(true)
  210. } else {
  211. mainWindow.setFullScreen(false)
  212. }
  213. }
  214. })
  215. ipcMain.on('toggle-dev-tools', toggleDevTools)
  216. // 加载子页URL
  217. ipcMain.on('load-child-url', (_event, url: string, aspectRatio: number, inputPassword?: { username: string; password: string }) => {
  218. if (childView) {
  219. console.log('load-child-url', url, inputPassword)
  220. childView.webContents.loadURL(url)
  221. if (inputPassword) {
  222. childView.webContents.once('did-finish-load', () => {
  223. console.log('did-finish-load', inputPassword)
  224. const script = `
  225. (function() {
  226. function tryFill() {
  227. const passwordInputs = document.querySelectorAll('input[type="password"]');
  228. if (passwordInputs.length === 0) return false;
  229. const allInputs = Array.from(document.querySelectorAll('input'));
  230. const usernameInput = allInputs.find(el => {
  231. const type = el.getAttribute('type');
  232. return !type || type === 'text' || type === 'email';
  233. });
  234. const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
  235. if (usernameInput) {
  236. nativeInputValueSetter.call(usernameInput, ${JSON.stringify(inputPassword.username)});
  237. usernameInput.dispatchEvent(new Event('input', { bubbles: true }));
  238. usernameInput.dispatchEvent(new Event('change', { bubbles: true }));
  239. }
  240. const pwdInput = passwordInputs[0];
  241. nativeInputValueSetter.call(pwdInput, ${JSON.stringify(inputPassword.password)});
  242. pwdInput.dispatchEvent(new Event('input', { bubbles: true }));
  243. pwdInput.dispatchEvent(new Event('change', { bubbles: true }));
  244. return true;
  245. }
  246. let attempts = 0;
  247. const timer = setInterval(() => {
  248. if (tryFill() || ++attempts >= 10) clearInterval(timer);
  249. }, 500);
  250. })();
  251. `;
  252. childView!.webContents.executeJavaScript(script).catch(() => {});
  253. });
  254. }
  255. }
  256. childViewAspectRatio = aspectRatio
  257. updateChildWindowBounds()
  258. })
  259. // 子页侧边栏开关
  260. ipcMain.on('toggle-child-side', (_event, value: boolean) => {
  261. isSideOpen = value
  262. expandButtonView.setVisible(!isSideOpen)
  263. mainWindow?.webContents.send('main-side-state-changed', isSideOpen)
  264. updateChildWindowBounds()
  265. })
  266. // 处理获取应用路径事件
  267. ipcMain.handle('get-app-path', () => {
  268. return app.getAppPath()
  269. })
  270. // 处理打开窗口事件
  271. ipcMain.on('open-window', (_event, url: string) => {
  272. const newWin = new BrowserWindow({
  273. icon: path.join(process.env.VITE_PUBLIC, 'icon.ico'),
  274. webPreferences: {
  275. preload: path.join(__dirname, 'preload.mjs'),
  276. contextIsolation: true,
  277. allowRunningInsecureContent: true,
  278. },
  279. fullscreenable: true,
  280. maximizable: true,
  281. width: 1200,
  282. height: 800,
  283. })
  284. newWin.loadURL(url)
  285. newWin.maximize();
  286. // 添加F11全屏切换功能
  287. newWin.webContents.on('before-input-event', (event, input) => {
  288. if (input.key === 'F11' && input.type === 'keyDown') {
  289. event.preventDefault();
  290. newWin?.setFullScreen(!newWin?.fullScreen)
  291. } else if (input.key === 'F12' && input.type === 'keyDown') {
  292. event.preventDefault();
  293. newWin?.webContents.toggleDevTools();
  294. }
  295. })
  296. })
  297. // 处理加载apps.json事件
  298. ipcMain.handle('load-apps-json', async () => {
  299. const appPath = process.cwd()
  300. const appsJsonPath = path.join(appPath, 'apps.json')
  301. try {
  302. if (fs.existsSync(appsJsonPath)) {
  303. const data = fs.readFileSync(appsJsonPath, 'utf8')
  304. return JSON.parse(data)
  305. } else {
  306. // 开发环境下回退到public目录
  307. const devAppsJsonPath = path.join(process.env.VITE_PUBLIC || '', 'apps.json')
  308. if (fs.existsSync(devAppsJsonPath)) {
  309. const data = fs.readFileSync(devAppsJsonPath, 'utf8')
  310. return JSON.parse(data)
  311. }
  312. throw new Error('apps.json not found')
  313. }
  314. } catch (error) {
  315. console.error('Error loading apps.json:', error)
  316. throw error
  317. }
  318. })
  319. // 处理加载默认apps.json事件
  320. ipcMain.handle('load-default-apps-json', async () => {
  321. const devAppsJsonPath = path.join(process.env.VITE_PUBLIC || '', 'apps.json')
  322. if (fs.existsSync(devAppsJsonPath)) {
  323. const data = fs.readFileSync(devAppsJsonPath, 'utf8')
  324. return JSON.parse(data)
  325. }
  326. throw new Error('apps.json not found')
  327. })
  328. // 处理显示配置窗口事件
  329. ipcMain.on('show-config', () => {
  330. const configWindow = new BrowserWindow({
  331. icon: path.join(process.env.VITE_PUBLIC, 'icon.ico'),
  332. webPreferences: {
  333. preload: path.join(__dirname, 'preload.mjs'),
  334. contextIsolation: true,
  335. allowRunningInsecureContent: true,
  336. },
  337. title: '列表配置',
  338. parent: mainWindow || undefined,
  339. skipTaskbar: true,
  340. minimizable: false,
  341. maximizable: false,
  342. modal: true,
  343. width: 800,
  344. height: 600,
  345. })
  346. loadWindowPage(configWindow, '/config')
  347. });
  348. // 处理保存apps.json事件
  349. ipcMain.on('save-apps-json', (_event, appsJson: string) => {
  350. const appPath = process.cwd()
  351. const appsJsonPath = path.join(appPath, 'apps.json')
  352. try {
  353. fs.writeFileSync(appsJsonPath, appsJson)
  354. mainWindow?.webContents.send('main-config-changed')
  355. } catch (error) {
  356. console.error('Error saving apps.json:', error)
  357. throw error
  358. }
  359. })
  360. // 处理显示关于窗口事件
  361. ipcMain.on('show-about', () => {
  362. const aboutWindow = new BrowserWindow({
  363. icon: path.join(process.env.VITE_PUBLIC, 'icon.ico'),
  364. webPreferences: {
  365. preload: path.join(__dirname, 'preload.mjs'),
  366. contextIsolation: true,
  367. allowRunningInsecureContent: true,
  368. },
  369. parent: mainWindow || undefined,
  370. title: '关于程序',
  371. skipTaskbar: true,
  372. maximizable: false,
  373. minimizable: false,
  374. modal: true,
  375. width: 450,
  376. height: 470,
  377. })
  378. loadWindowPage(aboutWindow, '/about')
  379. });
  380. }
  381. // Quit when all mainWindowdows are closed, except on macOS. There, it's common
  382. // for applications and their menu bar to stay active until the user quits
  383. // explicitly with Cmd + Q.
  384. app.on('window-all-closed', () => {
  385. if (process.platform !== 'darwin') {
  386. app.quit()
  387. mainWindow = null
  388. }
  389. })
  390. app.on('activate', () => {
  391. // On OS X it's common to re-create a mainWindowdow in the app when the
  392. // dock icon is clicked and there are no other mainWindowdows open.
  393. if (BrowserWindow.getAllWindows().length === 0) {
  394. createWindow()
  395. }
  396. })
  397. app.whenReady().then(createWindow)