windows.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import windowState from "electron-window-state"
  2. import { app, BrowserWindow, nativeImage, nativeTheme } from "electron"
  3. import { dirname, join } from "node:path"
  4. import { fileURLToPath } from "node:url"
  5. import type { TitlebarTheme } from "../preload/types"
  6. type Globals = {
  7. updaterEnabled: boolean
  8. deepLinks?: string[]
  9. }
  10. const root = dirname(fileURLToPath(import.meta.url))
  11. let backgroundColor: string | undefined
  12. export function setBackgroundColor(color: string) {
  13. backgroundColor = color
  14. }
  15. export function getBackgroundColor(): string | undefined {
  16. return backgroundColor
  17. }
  18. function iconsDir() {
  19. return app.isPackaged ? join(process.resourcesPath, "icons") : join(root, "../../resources/icons")
  20. }
  21. function iconPath() {
  22. const ext = process.platform === "win32" ? "ico" : "png"
  23. return join(iconsDir(), `icon.${ext}`)
  24. }
  25. function tone() {
  26. return nativeTheme.shouldUseDarkColors ? "dark" : "light"
  27. }
  28. function overlay(theme: Partial<TitlebarTheme> = {}) {
  29. const mode = theme.mode ?? tone()
  30. return {
  31. color: "#00000000",
  32. symbolColor: mode === "dark" ? "white" : "black",
  33. height: 40,
  34. }
  35. }
  36. export function setTitlebar(win: BrowserWindow, theme: Partial<TitlebarTheme> = {}) {
  37. if (process.platform !== "win32") return
  38. win.setTitleBarOverlay(overlay(theme))
  39. }
  40. export function setDockIcon() {
  41. if (process.platform !== "darwin") return
  42. const icon = nativeImage.createFromPath(join(iconsDir(), "dock.png"))
  43. if (!icon.isEmpty()) app.dock?.setIcon(icon)
  44. }
  45. export function createMainWindow(globals: Globals) {
  46. const state = windowState({
  47. defaultWidth: 1280,
  48. defaultHeight: 800,
  49. })
  50. const mode = tone()
  51. const win = new BrowserWindow({
  52. x: state.x,
  53. y: state.y,
  54. width: state.width,
  55. height: state.height,
  56. show: true,
  57. title: "OpenCode",
  58. icon: iconPath(),
  59. backgroundColor,
  60. ...(process.platform === "darwin"
  61. ? {
  62. titleBarStyle: "hidden" as const,
  63. trafficLightPosition: { x: 12, y: 14 },
  64. }
  65. : {}),
  66. ...(process.platform === "win32"
  67. ? {
  68. frame: false,
  69. titleBarStyle: "hidden" as const,
  70. titleBarOverlay: overlay({ mode }),
  71. }
  72. : {}),
  73. webPreferences: {
  74. preload: join(root, "../preload/index.mjs"),
  75. sandbox: false,
  76. },
  77. })
  78. state.manage(win)
  79. loadWindow(win, "index.html")
  80. wireZoom(win)
  81. injectGlobals(win, globals)
  82. return win
  83. }
  84. export function createLoadingWindow(globals: Globals) {
  85. const mode = tone()
  86. const win = new BrowserWindow({
  87. width: 640,
  88. height: 480,
  89. resizable: false,
  90. center: true,
  91. show: true,
  92. icon: iconPath(),
  93. backgroundColor,
  94. ...(process.platform === "darwin" ? { titleBarStyle: "hidden" as const } : {}),
  95. ...(process.platform === "win32"
  96. ? {
  97. frame: false,
  98. titleBarStyle: "hidden" as const,
  99. titleBarOverlay: overlay({ mode }),
  100. }
  101. : {}),
  102. webPreferences: {
  103. preload: join(root, "../preload/index.mjs"),
  104. sandbox: false,
  105. },
  106. })
  107. loadWindow(win, "loading.html")
  108. injectGlobals(win, globals)
  109. return win
  110. }
  111. function loadWindow(win: BrowserWindow, html: string) {
  112. const devUrl = process.env.ELECTRON_RENDERER_URL
  113. if (devUrl) {
  114. const url = new URL(html, devUrl)
  115. void win.loadURL(url.toString())
  116. return
  117. }
  118. void win.loadFile(join(root, `../renderer/${html}`))
  119. }
  120. function injectGlobals(win: BrowserWindow, globals: Globals) {
  121. win.webContents.on("dom-ready", () => {
  122. const deepLinks = globals.deepLinks ?? []
  123. const data = {
  124. updaterEnabled: globals.updaterEnabled,
  125. deepLinks: Array.isArray(deepLinks) ? deepLinks.splice(0) : deepLinks,
  126. }
  127. void win.webContents.executeJavaScript(
  128. `window.__OPENCODE__ = Object.assign(window.__OPENCODE__ ?? {}, ${JSON.stringify(data)})`,
  129. )
  130. })
  131. }
  132. function wireZoom(win: BrowserWindow) {
  133. win.webContents.setZoomFactor(1)
  134. win.webContents.on("zoom-changed", () => {
  135. win.webContents.setZoomFactor(1)
  136. })
  137. }