platform.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { createSimpleContext } from "@opencode-ai/ui/context"
  2. import type { AsyncStorage, SyncStorage } from "@solid-primitives/storage"
  3. import type { Accessor } from "solid-js"
  4. import type { DesktopMenuAction } from "../desktop-menu"
  5. import { ServerConnection } from "./server"
  6. import type { WslServersPlatform } from "../wsl/types"
  7. import type { UpdaterPlatform } from "../updater"
  8. type PickerPaths = string | string[] | null
  9. type OpenDirectoryPickerOptions = { title?: string; multiple?: boolean }
  10. type OpenAttachmentPickerOptions = {
  11. title?: string
  12. multiple?: boolean
  13. accept?: string[]
  14. extensions?: string[]
  15. defaultPath?: string
  16. }
  17. type SaveFilePickerOptions = { title?: string; defaultPath?: string }
  18. type PlatformName = "web" | "desktop"
  19. type DesktopOS = "macos" | "windows" | "linux"
  20. export type FatalRendererErrorLog = {
  21. error: string
  22. url: string
  23. version?: string
  24. platform: PlatformName
  25. os?: DesktopOS
  26. }
  27. type PlatformBase = {
  28. /** App version */
  29. version?: string
  30. /** Open a URL in the default browser */
  31. openLink(url: string): void
  32. /** Open a local path in a local app (desktop only) */
  33. openPath?(path: string, app?: string): Promise<void>
  34. /** Reveal a local path in the system file manager; false when the path does not exist (desktop only) */
  35. revealPath?(path: string): Promise<boolean>
  36. /** Restart the app */
  37. restart(): Promise<void>
  38. /** Navigate back in history */
  39. back(): void
  40. /** Navigate forward in history */
  41. forward(): void
  42. /** Send a system notification (optional deep link) */
  43. notify(title: string, description?: string, href?: string): Promise<void>
  44. /** Open a native attachment picker and read selected files sequentially (desktop only) */
  45. openAttachmentPickerDialog?(
  46. opts: OpenAttachmentPickerOptions,
  47. onFile: (file: File) => Promise<unknown>,
  48. ): Promise<void>
  49. /** Resolve the native source path for a desktop File. */
  50. getPathForFile?(file: File): string
  51. /** Open a native save file picker dialog (desktop only) */
  52. saveFilePickerDialog?(opts?: SaveFilePickerOptions): Promise<string | null>
  53. /** Storage mechanism, defaults to localStorage */
  54. storage?: (name?: string) => SyncStorage | AsyncStorage
  55. /** Stable platform window identity for window-scoped persistence */
  56. windowID?: string
  57. /** Application-global desktop updater */
  58. updater?: UpdaterPlatform
  59. /** Fetch override */
  60. fetch?: typeof fetch
  61. /** Get the configured default server URL (platform-specific) */
  62. getDefaultServer?(): Promise<ServerConnection.Key | null>
  63. /** Set the default server URL to use on app startup (platform-specific) */
  64. setDefaultServer?(url: ServerConnection.Key | null): Promise<void> | void
  65. /** Manage WSL sidecar servers (Electron on Windows only) */
  66. wslServers?: WslServersPlatform
  67. /** Get the preferred display backend (desktop only) */
  68. getDisplayBackend?(): Promise<DisplayBackend | null> | DisplayBackend | null
  69. /** Set the preferred display backend (desktop only) */
  70. setDisplayBackend?(backend: DisplayBackend): Promise<void>
  71. /** Parse markdown to HTML using native parser (desktop only, returns unprocessed code blocks) */
  72. parseMarkdown?(markdown: string): Promise<string>
  73. /** Webview zoom level (desktop only) */
  74. webviewZoom?: Accessor<number>
  75. /** Get whether native pinch/Ctrl-scroll zoom gestures are enabled (desktop only) */
  76. getPinchZoomEnabled?(): Promise<boolean> | boolean
  77. /** Allow native pinch/Ctrl-scroll zoom gestures (desktop only) */
  78. setPinchZoomEnabled?(enabled: boolean): Promise<void> | void
  79. /** Run a desktop-only menu action from the app chrome */
  80. runDesktopMenuAction?(action: DesktopMenuAction): Promise<void> | void
  81. /** Check if an editor app exists (desktop only) */
  82. checkAppExists?(appName: string): Promise<boolean>
  83. /** Read image from clipboard (desktop only) */
  84. readClipboardImage?(): Promise<File | null>
  85. /** Export collected diagnostic logs (desktop only) */
  86. exportDebugLogs?(): Promise<string>
  87. /** Record a fatal renderer error in platform logs (desktop only) */
  88. recordFatalRendererError?(error: FatalRendererErrorLog): Promise<void>
  89. }
  90. export type Platform = PlatformBase &
  91. (
  92. | { platform: "web"; os?: never }
  93. | {
  94. platform: "desktop"
  95. os?: DesktopOS
  96. openDirectoryPickerDialog(opts?: OpenDirectoryPickerOptions): Promise<PickerPaths>
  97. }
  98. )
  99. export type DisplayBackend = "auto" | "wayland"
  100. export const { use: usePlatform, provider: PlatformProvider } = createSimpleContext({
  101. name: "Platform",
  102. init: (props: { value: Platform }) => {
  103. return props.value
  104. },
  105. })