GlobalConfig.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * 全局配置
  3. */
  4. import NodePath from "path"
  5. import { app, screen, BrowserWindow } from "electron"
  6. import PKG from "../../package.json"
  7. class GlobalConfig {
  8. //#region 只读属性
  9. /** 是否为 Windows 平台 */
  10. static readonly IS_WIN32 = process.platform === "win32"
  11. /** 是否为 macOS 平台 */
  12. static readonly IS_MACOS = process.platform === "darwin"
  13. /** 是否为 Linux 平台 */
  14. static readonly IS_LINUX = process.platform === "linux"
  15. /** 是否为开发模式 */
  16. static readonly IS_DEV_MODE = !app.isPackaged
  17. /** 项目名称 */
  18. static readonly PROJECT_NAME = PKG.name
  19. /**
  20. * 源码目录
  21. * - dev : {project directory}/
  22. * - prod :
  23. * 1. on macOS : /Applications/{app name}.app/Contents/Resources/app?.asar
  24. * 2. on Linux : {installation directory}/resources/app?.asar
  25. * 3. on Windows : {installation directory}/resources/app?.asar
  26. */
  27. static readonly DIR_APP = app.getAppPath()
  28. /** 静态资源目录 */
  29. static readonly DIR_STATIC = NodePath.resolve(this.DIR_APP, "static")
  30. /** 存放资源文件的目录 */
  31. static readonly DIR_RESOURCES = NodePath.resolve(this.DIR_APP, this.IS_DEV_MODE ? "" : "..")
  32. /** 根目录/安装目录 */
  33. static readonly DIR_ROOT = this.IS_DEV_MODE ? this.DIR_APP : NodePath.resolve(this.DIR_RESOURCES, "..")
  34. /** 版本号 */
  35. static readonly APP_VERSION = PKG.version
  36. //#endregion
  37. /** 程序名称 */
  38. static getAppTitle() {
  39. const suffix = this.IS_DEV_MODE ? ` | v${this.APP_VERSION}` : ""
  40. return `${import.meta.env.VITE_APP_TITLE || this.PROJECT_NAME}${suffix}`
  41. }
  42. /** 程序图标 */
  43. static getAppLogo(isDocker?: boolean) {
  44. const logoList = {
  45. win32: "icons/logo_256x256.ico",
  46. darwin: "icons/logo_256x256.icns",
  47. linux: "icons/logo_256x256.png"
  48. }
  49. const type = isDocker ? "linux" : process.platform
  50. return NodePath.join(this.DIR_STATIC, logoList[type])
  51. }
  52. /** 运行地址/路径 */
  53. static getWinUrl() {
  54. if (this.IS_DEV_MODE) return `http://${PKG.env.host}:${PKG.env.port}`
  55. return NodePath.join(this.DIR_APP, "dist/index.html")
  56. }
  57. /** 本地日志路径 */
  58. static getLocalLogsPath() {
  59. return app.getPath("logs")
  60. }
  61. /** 挂载全局变量 */
  62. static mountGlobalVariables() {
  63. // 静态资源路径
  64. global.StaticPath = this.DIR_STATIC
  65. // 客户端图标
  66. global.ClientLogo = this.getAppLogo()
  67. // 客户端版本号
  68. global.ClientVersion = this.APP_VERSION
  69. }
  70. /** 根据分辨率适配窗口大小 */
  71. static adaptByScreen(dto: WinStateDTO, win: BrowserWindow | null) {
  72. const devSize = { width: 1920, height: 1080 }
  73. const areaSize = screen.getPrimaryDisplay().workAreaSize
  74. const zoomFactor = Math.min(1, areaSize.width / devSize.width, areaSize.height / devSize.height)
  75. const realSize = { width: 0, height: 0 }
  76. realSize.width = Math.round(dto.width * zoomFactor)
  77. realSize.height = Math.round(dto.height * zoomFactor)
  78. win?.webContents.setZoomFactor(zoomFactor)
  79. return realSize
  80. }
  81. }
  82. export default GlobalConfig