titlebar.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { createEffect, createMemo, Show, untrack } from "solid-js"
  2. import { createStore } from "solid-js/store"
  3. import { useLocation, useNavigate } from "@solidjs/router"
  4. import { IconButton } from "@opencode-ai/ui/icon-button"
  5. import { Icon } from "@opencode-ai/ui/icon"
  6. import { Button } from "@opencode-ai/ui/button"
  7. import { Tooltip, TooltipKeybind } from "@opencode-ai/ui/tooltip"
  8. import { useTheme } from "@opencode-ai/ui/theme"
  9. import { useLayout } from "@/context/layout"
  10. import { usePlatform } from "@/context/platform"
  11. import { useCommand } from "@/context/command"
  12. import { useLanguage } from "@/context/language"
  13. import { applyPath, backPath, forwardPath } from "./titlebar-history"
  14. export function Titlebar() {
  15. const layout = useLayout()
  16. const platform = usePlatform()
  17. const command = useCommand()
  18. const language = useLanguage()
  19. const theme = useTheme()
  20. const navigate = useNavigate()
  21. const location = useLocation()
  22. const mac = createMemo(() => platform.platform === "desktop" && platform.os === "macos")
  23. const windows = createMemo(() => platform.platform === "desktop" && platform.os === "windows")
  24. const web = createMemo(() => platform.platform === "web")
  25. const zoom = () => platform.webviewZoom?.() ?? 1
  26. const minHeight = () => (mac() ? `${40 / zoom()}px` : undefined)
  27. const [history, setHistory] = createStore({
  28. stack: [] as string[],
  29. index: 0,
  30. action: undefined as "back" | "forward" | undefined,
  31. })
  32. const path = () => `${location.pathname}${location.search}${location.hash}`
  33. createEffect(() => {
  34. const current = path()
  35. untrack(() => {
  36. const next = applyPath(history, current)
  37. if (next === history) return
  38. setHistory(next)
  39. })
  40. })
  41. const canBack = createMemo(() => history.index > 0)
  42. const canForward = createMemo(() => history.index < history.stack.length - 1)
  43. const back = () => {
  44. const next = backPath(history)
  45. if (!next) return
  46. setHistory(next.state)
  47. navigate(next.to)
  48. }
  49. const forward = () => {
  50. const next = forwardPath(history)
  51. if (!next) return
  52. setHistory(next.state)
  53. navigate(next.to)
  54. }
  55. command.register(() => [
  56. {
  57. id: "common.goBack",
  58. title: language.t("common.goBack"),
  59. category: language.t("command.category.view"),
  60. onSelect: back,
  61. },
  62. {
  63. id: "common.goForward",
  64. title: language.t("common.goForward"),
  65. category: language.t("command.category.view"),
  66. onSelect: forward,
  67. },
  68. ])
  69. const getWin = () => {
  70. if (platform.platform !== "desktop") return
  71. const tauri = (
  72. window as unknown as {
  73. __TAURI__?: {
  74. window?: {
  75. getCurrentWindow?: () => {
  76. startDragging?: () => Promise<void>
  77. toggleMaximize?: () => Promise<void>
  78. }
  79. }
  80. }
  81. }
  82. ).__TAURI__
  83. if (!tauri?.window?.getCurrentWindow) return
  84. return tauri.window.getCurrentWindow()
  85. }
  86. createEffect(() => {
  87. if (platform.platform !== "desktop") return
  88. const scheme = theme.colorScheme()
  89. const value = scheme === "system" ? null : scheme
  90. const tauri = (window as unknown as { __TAURI__?: { webviewWindow?: { getCurrentWebviewWindow?: () => unknown } } })
  91. .__TAURI__
  92. const get = tauri?.webviewWindow?.getCurrentWebviewWindow
  93. if (!get) return
  94. const win = get() as { setTheme?: (theme?: "light" | "dark" | null) => Promise<void> }
  95. if (!win.setTheme) return
  96. void win.setTheme(value).catch(() => undefined)
  97. })
  98. const interactive = (target: EventTarget | null) => {
  99. if (!(target instanceof Element)) return false
  100. const selector =
  101. "button, a, input, textarea, select, option, [role='button'], [role='menuitem'], [contenteditable='true'], [contenteditable='']"
  102. return !!target.closest(selector)
  103. }
  104. const drag = (e: MouseEvent) => {
  105. if (platform.platform !== "desktop") return
  106. if (e.buttons !== 1) return
  107. if (interactive(e.target)) return
  108. const win = getWin()
  109. if (!win?.startDragging) return
  110. e.preventDefault()
  111. void win.startDragging().catch(() => undefined)
  112. }
  113. const maximize = (e: MouseEvent) => {
  114. if (platform.platform !== "desktop") return
  115. if (interactive(e.target)) return
  116. if (e.target instanceof Element && e.target.closest("[data-tauri-decorum-tb]")) return
  117. const win = getWin()
  118. if (!win?.toggleMaximize) return
  119. e.preventDefault()
  120. void win.toggleMaximize().catch(() => undefined)
  121. }
  122. return (
  123. <header
  124. class="h-10 shrink-0 bg-background-base relative grid grid-cols-[auto_minmax(0,1fr)_auto] items-center"
  125. style={{ "min-height": minHeight() }}
  126. onMouseDown={drag}
  127. onDblClick={maximize}
  128. >
  129. <div
  130. classList={{
  131. "flex items-center min-w-0": true,
  132. "pl-2": !mac(),
  133. }}
  134. >
  135. <Show when={mac()}>
  136. <div class="h-full shrink-0" style={{ width: `${72 / zoom()}px` }} />
  137. <div class="xl:hidden w-10 shrink-0 flex items-center justify-center">
  138. <IconButton
  139. icon="menu"
  140. variant="ghost"
  141. class="size-8 rounded-md"
  142. onClick={layout.mobileSidebar.toggle}
  143. aria-label={language.t("sidebar.menu.toggle")}
  144. />
  145. </div>
  146. </Show>
  147. <Show when={!mac()}>
  148. <div class="xl:hidden w-[48px] shrink-0 flex items-center justify-center">
  149. <IconButton
  150. icon="menu"
  151. variant="ghost"
  152. class="size-8 rounded-md"
  153. onClick={layout.mobileSidebar.toggle}
  154. aria-label={language.t("sidebar.menu.toggle")}
  155. />
  156. </div>
  157. </Show>
  158. <div class="flex items-center gap-3 shrink-0">
  159. <TooltipKeybind
  160. class={web() ? "hidden xl:flex shrink-0 ml-14" : "hidden xl:flex shrink-0 ml-2"}
  161. placement="bottom"
  162. title={language.t("command.sidebar.toggle")}
  163. keybind={command.keybind("sidebar.toggle")}
  164. >
  165. <Button
  166. variant="ghost"
  167. class="group/sidebar-toggle size-6 p-0"
  168. onClick={layout.sidebar.toggle}
  169. aria-label={language.t("command.sidebar.toggle")}
  170. aria-expanded={layout.sidebar.opened()}
  171. >
  172. <div class="relative flex items-center justify-center size-4 [&>*]:absolute [&>*]:inset-0">
  173. <Icon
  174. size="small"
  175. name={layout.sidebar.opened() ? "layout-left-full" : "layout-left"}
  176. class="group-hover/sidebar-toggle:hidden"
  177. />
  178. <Icon size="small" name="layout-left-partial" class="hidden group-hover/sidebar-toggle:inline-block" />
  179. <Icon
  180. size="small"
  181. name={layout.sidebar.opened() ? "layout-left" : "layout-left-full"}
  182. class="hidden group-active/sidebar-toggle:inline-block"
  183. />
  184. </div>
  185. </Button>
  186. </TooltipKeybind>
  187. <div class="hidden xl:flex items-center gap-1 shrink-0">
  188. <Tooltip placement="bottom" value={language.t("common.goBack")} openDelay={2000}>
  189. <Button
  190. variant="ghost"
  191. icon="arrow-left"
  192. class="size-6 p-0"
  193. disabled={!canBack()}
  194. onClick={back}
  195. aria-label={language.t("common.goBack")}
  196. />
  197. </Tooltip>
  198. <Tooltip placement="bottom" value={language.t("common.goForward")} openDelay={2000}>
  199. <Button
  200. variant="ghost"
  201. icon="arrow-right"
  202. class="size-6 p-0"
  203. disabled={!canForward()}
  204. onClick={forward}
  205. aria-label={language.t("common.goForward")}
  206. />
  207. </Tooltip>
  208. </div>
  209. </div>
  210. <div id="opencode-titlebar-left" class="flex items-center gap-3 min-w-0 px-2" />
  211. </div>
  212. <div class="min-w-0 flex items-center justify-center pointer-events-none lg:absolute lg:inset-0 lg:flex lg:items-center lg:justify-center">
  213. <div id="opencode-titlebar-center" class="pointer-events-auto w-full min-w-0 flex justify-center lg:w-fit" />
  214. </div>
  215. <div
  216. classList={{
  217. "flex items-center min-w-0 justify-end": true,
  218. "pr-6": !windows(),
  219. }}
  220. onMouseDown={drag}
  221. >
  222. <div id="opencode-titlebar-right" class="flex items-center gap-3 shrink-0 justify-end" />
  223. <Show when={windows()}>
  224. <div class="w-6 shrink-0" />
  225. <div data-tauri-decorum-tb class="flex flex-row" />
  226. </Show>
  227. </div>
  228. </header>
  229. )
  230. }