terminal-split.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import { For, Show, createMemo, createSignal, onCleanup } from "solid-js"
  2. import { Terminal } from "./terminal"
  3. import { useTerminal, type Panel } from "@/context/terminal"
  4. import { IconButton } from "@opencode-ai/ui/icon-button"
  5. export interface TerminalSplitProps {
  6. tabId: string
  7. }
  8. function computeLayout(
  9. panels: Record<string, Panel>,
  10. panelId: string,
  11. bounds: { top: number; left: number; width: number; height: number },
  12. ): Map<string, { top: number; left: number; width: number; height: number }> {
  13. const result = new Map<string, { top: number; left: number; width: number; height: number }>()
  14. const panel = panels[panelId]
  15. if (!panel) return result
  16. if (panel.ptyId) {
  17. result.set(panel.ptyId, bounds)
  18. } else if (panel.children && panel.children.length === 2) {
  19. const [leftId, rightId] = panel.children
  20. const sizes = panel.sizes ?? [50, 50]
  21. if (panel.direction === "horizontal") {
  22. const topHeight = (bounds.height * sizes[0]) / 100
  23. const topBounds = { ...bounds, height: topHeight }
  24. const bottomBounds = { ...bounds, top: bounds.top + topHeight, height: bounds.height - topHeight }
  25. for (const [k, v] of computeLayout(panels, leftId, topBounds)) result.set(k, v)
  26. for (const [k, v] of computeLayout(panels, rightId, bottomBounds)) result.set(k, v)
  27. } else {
  28. const leftWidth = (bounds.width * sizes[0]) / 100
  29. const leftBounds = { ...bounds, width: leftWidth }
  30. const rightBounds = { ...bounds, left: bounds.left + leftWidth, width: bounds.width - leftWidth }
  31. for (const [k, v] of computeLayout(panels, leftId, leftBounds)) result.set(k, v)
  32. for (const [k, v] of computeLayout(panels, rightId, rightBounds)) result.set(k, v)
  33. }
  34. }
  35. return result
  36. }
  37. function findPanelForPty(panels: Record<string, Panel>, ptyId: string): string | undefined {
  38. for (const [id, panel] of Object.entries(panels)) {
  39. if (panel.ptyId === ptyId) return id
  40. }
  41. }
  42. export function TerminalSplit(props: TerminalSplitProps) {
  43. const terminal = useTerminal()
  44. const pane = createMemo(() => terminal.pane(props.tabId))
  45. const terminals = createMemo(() => terminal.all().filter((t) => t.tabId === props.tabId))
  46. const [containerFocused, setContainerFocused] = createSignal(true)
  47. const layout = createMemo(() => {
  48. const p = pane()
  49. if (!p) {
  50. const single = terminals()[0]
  51. if (!single) return new Map()
  52. return new Map([[single.id, { top: 0, left: 0, width: 100, height: 100 }]])
  53. }
  54. return computeLayout(p.panels, p.root, { top: 0, left: 0, width: 100, height: 100 })
  55. })
  56. const focused = createMemo(() => {
  57. const p = pane()
  58. if (!p) return props.tabId
  59. const focusedPanel = p.panels[p.focused ?? ""]
  60. return focusedPanel?.ptyId ?? props.tabId
  61. })
  62. const handleFocus = (ptyId: string) => {
  63. const p = pane()
  64. if (!p) return
  65. const panelId = findPanelForPty(p.panels, ptyId)
  66. if (panelId) terminal.focus(props.tabId, panelId)
  67. }
  68. const handleClose = (ptyId: string) => {
  69. const pty = terminal.all().find((t) => t.id === ptyId)
  70. if (!pty) return
  71. const p = pane()
  72. if (!p) {
  73. if (pty.tabId === props.tabId) {
  74. terminal.closeTab(props.tabId)
  75. }
  76. return
  77. }
  78. const panelId = findPanelForPty(p.panels, ptyId)
  79. if (panelId) terminal.closeSplit(props.tabId, panelId)
  80. }
  81. return (
  82. <div
  83. class="relative size-full"
  84. data-terminal-split-container
  85. onFocusIn={() => setContainerFocused(true)}
  86. onFocusOut={(e) => {
  87. const related = e.relatedTarget as Node | null
  88. if (!related || !e.currentTarget.contains(related)) {
  89. setContainerFocused(false)
  90. }
  91. }}
  92. >
  93. <For each={terminals()}>
  94. {(pty) => {
  95. const bounds = createMemo(() => layout().get(pty.id) ?? { top: 0, left: 0, width: 100, height: 100 })
  96. const isFocused = createMemo(() => focused() === pty.id)
  97. const hasSplits = createMemo(() => !!pane())
  98. return (
  99. <div
  100. class="absolute flex flex-col min-h-0"
  101. classList={{
  102. "ring-1 ring-inset ring-border-strong-base": containerFocused() && isFocused(),
  103. "border-l border-border-weak-base": bounds().left > 0,
  104. "border-t border-border-weak-base": bounds().top > 0,
  105. }}
  106. style={{
  107. top: `${bounds().top}%`,
  108. left: `${bounds().left}%`,
  109. width: `${bounds().width}%`,
  110. height: `${bounds().height}%`,
  111. }}
  112. onClick={() => handleFocus(pty.id)}
  113. >
  114. <Show when={pane()}>
  115. <div class="absolute top-1 right-1 z-10 opacity-0 hover:opacity-100 transition-opacity">
  116. <IconButton
  117. icon="close"
  118. variant="ghost"
  119. onClick={(e) => {
  120. e.stopPropagation()
  121. handleClose(pty.id)
  122. }}
  123. />
  124. </div>
  125. </Show>
  126. <div
  127. class="flex-1 min-h-0"
  128. classList={{ "opacity-50": !containerFocused() || (hasSplits() && !isFocused()) }}
  129. >
  130. <Terminal
  131. pty={pty}
  132. focused={isFocused()}
  133. onCleanup={terminal.update}
  134. onConnectError={() => terminal.clone(pty.id)}
  135. onExit={() => handleClose(pty.id)}
  136. class="size-full"
  137. />
  138. </div>
  139. </div>
  140. )
  141. }}
  142. </For>
  143. <ResizeHandles tabId={props.tabId} />
  144. </div>
  145. )
  146. }
  147. function ResizeHandles(props: { tabId: string }) {
  148. const terminal = useTerminal()
  149. const pane = createMemo(() => terminal.pane(props.tabId))
  150. const splits = createMemo(() => {
  151. const p = pane()
  152. if (!p) return []
  153. return Object.values(p.panels).filter((panel) => panel.children && panel.children.length === 2)
  154. })
  155. return <For each={splits()}>{(panel) => <ResizeHandle tabId={props.tabId} panelId={panel.id} />}</For>
  156. }
  157. function ResizeHandle(props: { tabId: string; panelId: string }) {
  158. const terminal = useTerminal()
  159. const pane = createMemo(() => terminal.pane(props.tabId))
  160. const panel = createMemo(() => pane()?.panels[props.panelId])
  161. let cleanup: VoidFunction | undefined
  162. onCleanup(() => cleanup?.())
  163. const position = createMemo(() => {
  164. const p = pane()
  165. if (!p) return null
  166. const pan = panel()
  167. if (!pan?.children || pan.children.length !== 2) return null
  168. const bounds = computePanelBounds(p.panels, p.root, props.panelId, {
  169. top: 0,
  170. left: 0,
  171. width: 100,
  172. height: 100,
  173. })
  174. if (!bounds) return null
  175. const sizes = pan.sizes ?? [50, 50]
  176. if (pan.direction === "horizontal") {
  177. return {
  178. horizontal: true,
  179. top: bounds.top + (bounds.height * sizes[0]) / 100,
  180. left: bounds.left,
  181. size: bounds.width,
  182. }
  183. }
  184. return {
  185. horizontal: false,
  186. top: bounds.top,
  187. left: bounds.left + (bounds.width * sizes[0]) / 100,
  188. size: bounds.height,
  189. }
  190. })
  191. const handleMouseDown = (e: MouseEvent) => {
  192. e.preventDefault()
  193. const pos = position()
  194. if (!pos) return
  195. const container = (e.target as HTMLElement).closest("[data-terminal-split-container]") as HTMLElement
  196. if (!container) return
  197. const rect = container.getBoundingClientRect()
  198. const pan = panel()
  199. if (!pan) return
  200. const p = pane()
  201. if (!p) return
  202. const panelBounds = computePanelBounds(p.panels, p.root, props.panelId, {
  203. top: 0,
  204. left: 0,
  205. width: 100,
  206. height: 100,
  207. })
  208. if (!panelBounds) return
  209. const handleMouseMove = (e: MouseEvent) => {
  210. if (pan.direction === "horizontal") {
  211. const totalPx = (rect.height * panelBounds.height) / 100
  212. const topPx = (rect.height * panelBounds.top) / 100
  213. const posPx = e.clientY - rect.top - topPx
  214. const percent = Math.max(10, Math.min(90, (posPx / totalPx) * 100))
  215. terminal.resizeSplit(props.tabId, props.panelId, [percent, 100 - percent])
  216. } else {
  217. const totalPx = (rect.width * panelBounds.width) / 100
  218. const leftPx = (rect.width * panelBounds.left) / 100
  219. const posPx = e.clientX - rect.left - leftPx
  220. const percent = Math.max(10, Math.min(90, (posPx / totalPx) * 100))
  221. terminal.resizeSplit(props.tabId, props.panelId, [percent, 100 - percent])
  222. }
  223. }
  224. const handleMouseUp = () => {
  225. document.removeEventListener("mousemove", handleMouseMove)
  226. document.removeEventListener("mouseup", handleMouseUp)
  227. cleanup = undefined
  228. }
  229. cleanup = handleMouseUp
  230. document.addEventListener("mousemove", handleMouseMove)
  231. document.addEventListener("mouseup", handleMouseUp)
  232. }
  233. return (
  234. <Show when={position()}>
  235. {(pos) => (
  236. <div
  237. data-component="resize-handle"
  238. data-direction={pos().horizontal ? "vertical" : "horizontal"}
  239. class="absolute"
  240. style={{
  241. top: `${pos().top}%`,
  242. left: `${pos().left}%`,
  243. width: pos().horizontal ? `${pos().size}%` : "8px",
  244. height: pos().horizontal ? "8px" : `${pos().size}%`,
  245. transform: pos().horizontal ? "translateY(-50%)" : "translateX(-50%)",
  246. cursor: pos().horizontal ? "row-resize" : "col-resize",
  247. }}
  248. onMouseDown={handleMouseDown}
  249. />
  250. )}
  251. </Show>
  252. )
  253. }
  254. function computePanelBounds(
  255. panels: Record<string, Panel>,
  256. currentId: string,
  257. targetId: string,
  258. bounds: { top: number; left: number; width: number; height: number },
  259. ): { top: number; left: number; width: number; height: number } | null {
  260. if (currentId === targetId) return bounds
  261. const panel = panels[currentId]
  262. if (!panel?.children || panel.children.length !== 2) return null
  263. const [leftId, rightId] = panel.children
  264. const sizes = panel.sizes ?? [50, 50]
  265. const horizontal = panel.direction === "horizontal"
  266. if (horizontal) {
  267. const topHeight = (bounds.height * sizes[0]) / 100
  268. const bottomHeight = bounds.height - topHeight
  269. const topBounds = { ...bounds, height: topHeight }
  270. const bottomBounds = { ...bounds, top: bounds.top + topHeight, height: bottomHeight }
  271. return (
  272. computePanelBounds(panels, leftId, targetId, topBounds) ??
  273. computePanelBounds(panels, rightId, targetId, bottomBounds)
  274. )
  275. }
  276. const leftWidth = (bounds.width * sizes[0]) / 100
  277. const rightWidth = bounds.width - leftWidth
  278. const leftBounds = { ...bounds, width: leftWidth }
  279. const rightBounds = { ...bounds, left: bounds.left + leftWidth, width: rightWidth }
  280. return (
  281. computePanelBounds(panels, leftId, targetId, leftBounds) ??
  282. computePanelBounds(panels, rightId, targetId, rightBounds)
  283. )
  284. }