|
| 1 | +import { CliRenderEvents, RGBA, TextAttributes, type BoxRenderable, type Renderable } from "@opentui/core" |
| 2 | +import { useRenderer } from "@opentui/solid" |
| 3 | +import { createResource, createSignal, Match, onCleanup, Show, Switch, type JSX } from "solid-js" |
| 4 | +import { usePaneLayout } from "../context/pane-layout" |
| 5 | +import type { PaneLayoutNode } from "../context/pane-layout-model" |
| 6 | +import { useData } from "../context/data" |
| 7 | +import { usePromptRef } from "../context/prompt" |
| 8 | +import { useTheme } from "../context/theme" |
| 9 | +import { Session } from "../routes/session" |
| 10 | +import { PersistentTerminalPane } from "./persistent-terminal-pane" |
| 11 | + |
| 12 | +export function PaneWorkspace(props: { sessionID: string; verticalTabsWidth: number }) { |
| 13 | + const panes = usePaneLayout() |
| 14 | + createResource( |
| 15 | + () => props.sessionID, |
| 16 | + (sessionID) => panes.load(sessionID).catch(() => undefined), |
| 17 | + ) |
| 18 | + const workspace = () => panes.get(props.sessionID) |
| 19 | + return ( |
| 20 | + <Show when={workspace()} fallback={<Session verticalTabsWidth={props.verticalTabsWidth} />}> |
| 21 | + {(value) => ( |
| 22 | + <PaneNode node={value().layout} rootSessionID={props.sessionID} verticalTabsWidth={props.verticalTabsWidth} /> |
| 23 | + )} |
| 24 | + </Show> |
| 25 | + ) |
| 26 | +} |
| 27 | + |
| 28 | +function PaneNode(props: { node: PaneLayoutNode; rootSessionID?: string; verticalTabsWidth: number }) { |
| 29 | + const panes = usePaneLayout() |
| 30 | + const prompt = usePromptRef() |
| 31 | + const theme = useTheme() |
| 32 | + const data = useData() |
| 33 | + return ( |
| 34 | + <Switch> |
| 35 | + <Match when={props.node.type === "item" ? props.node.item : undefined}> |
| 36 | + {(item) => { |
| 37 | + let focusTerminal: (() => void) | undefined |
| 38 | + const focus = () => { |
| 39 | + if (item().type === "session" && item().id === props.rootSessionID) { |
| 40 | + prompt.current?.focus() |
| 41 | + return |
| 42 | + } |
| 43 | + focusTerminal?.() |
| 44 | + } |
| 45 | + return ( |
| 46 | + <PaneSurface |
| 47 | + focus={focus} |
| 48 | + title={ |
| 49 | + item().type === "terminal" |
| 50 | + ? "Terminal" |
| 51 | + : `Session · ${data.session.get(item().id)?.title ?? "Untitled"}` |
| 52 | + } |
| 53 | + > |
| 54 | + <Switch> |
| 55 | + <Match when={item().type === "session" && item().id === props.rootSessionID}> |
| 56 | + <Session verticalTabsWidth={props.verticalTabsWidth} /> |
| 57 | + </Match> |
| 58 | + <Match when={item().type === "session"}> |
| 59 | + <UnavailablePane label={`Session ${item().id}`} /> |
| 60 | + </Match> |
| 61 | + <Match when={item().type === "terminal"}> |
| 62 | + <PersistentTerminalPane |
| 63 | + ptyID={item().id} |
| 64 | + autoFocus={!props.rootSessionID || panes.shouldFocus(item().id)} |
| 65 | + onAutoFocus={() => panes.clearFocus(item().id)} |
| 66 | + onFocusRequest={(value) => (focusTerminal = value)} |
| 67 | + /> |
| 68 | + </Match> |
| 69 | + </Switch> |
| 70 | + </PaneSurface> |
| 71 | + ) |
| 72 | + }} |
| 73 | + </Match> |
| 74 | + <Match when={props.node.type === "split" ? props.node : undefined}> |
| 75 | + {(node) => ( |
| 76 | + <box |
| 77 | + flexGrow={1} |
| 78 | + minWidth={0} |
| 79 | + minHeight={0} |
| 80 | + flexDirection={node().direction === "horizontal" ? "row" : "column"} |
| 81 | + > |
| 82 | + <box flexGrow={node().ratio} flexBasis={0} minWidth={0} minHeight={0}> |
| 83 | + <PaneNode |
| 84 | + node={node().first} |
| 85 | + rootSessionID={props.rootSessionID} |
| 86 | + verticalTabsWidth={props.verticalTabsWidth} |
| 87 | + /> |
| 88 | + </box> |
| 89 | + <box flexGrow={1 - node().ratio} flexBasis={0} minWidth={0} minHeight={0}> |
| 90 | + <PaneNode |
| 91 | + node={node().second} |
| 92 | + rootSessionID={props.rootSessionID} |
| 93 | + verticalTabsWidth={props.verticalTabsWidth} |
| 94 | + /> |
| 95 | + </box> |
| 96 | + </box> |
| 97 | + )} |
| 98 | + </Match> |
| 99 | + </Switch> |
| 100 | + ) |
| 101 | +} |
| 102 | + |
| 103 | +function PaneSurface(props: { focus: () => void; title: string; children: JSX.Element }) { |
| 104 | + const renderer = useRenderer() |
| 105 | + const theme = useTheme() |
| 106 | + const [focused, setFocused] = createSignal(false) |
| 107 | + let pane: BoxRenderable | undefined |
| 108 | + const contains = (current: Renderable | null) => { |
| 109 | + while (current) { |
| 110 | + if (current === pane) return true |
| 111 | + current = current.parent |
| 112 | + } |
| 113 | + return false |
| 114 | + } |
| 115 | + const onFocused = (current: Renderable | null) => setFocused(contains(current)) |
| 116 | + renderer.on(CliRenderEvents.FOCUSED_RENDERABLE, onFocused) |
| 117 | + onCleanup(() => renderer.off(CliRenderEvents.FOCUSED_RENDERABLE, onFocused)) |
| 118 | + return ( |
| 119 | + <box |
| 120 | + ref={(value) => { |
| 121 | + pane = value |
| 122 | + setFocused(contains(renderer.currentFocusedRenderable)) |
| 123 | + }} |
| 124 | + flexGrow={1} |
| 125 | + minWidth={0} |
| 126 | + minHeight={0} |
| 127 | + position="relative" |
| 128 | + flexDirection="column" |
| 129 | + > |
| 130 | + <box |
| 131 | + height={1} |
| 132 | + flexShrink={0} |
| 133 | + paddingLeft={1} |
| 134 | + paddingRight={1} |
| 135 | + backgroundColor={ |
| 136 | + focused() ? theme.raise(theme.raise(theme.background.surface.offset)) : theme.background.surface.offset |
| 137 | + } |
| 138 | + > |
| 139 | + <text |
| 140 | + fg={focused() ? theme.text.default : theme.text.subdued} |
| 141 | + attributes={focused() ? TextAttributes.BOLD : undefined} |
| 142 | + wrapMode="none" |
| 143 | + truncate |
| 144 | + > |
| 145 | + {props.title} |
| 146 | + </text> |
| 147 | + </box> |
| 148 | + <box flexGrow={1} minWidth={0} minHeight={0} position="relative"> |
| 149 | + {props.children} |
| 150 | + </box> |
| 151 | + <Show when={!focused()}> |
| 152 | + <box |
| 153 | + position="absolute" |
| 154 | + left={0} |
| 155 | + top={0} |
| 156 | + width="100%" |
| 157 | + height="100%" |
| 158 | + zIndex={1} |
| 159 | + backgroundColor={RGBA.fromInts(0, 0, 0)} |
| 160 | + opacity={0.3} |
| 161 | + onMouseDown={props.focus} |
| 162 | + /> |
| 163 | + </Show> |
| 164 | + </box> |
| 165 | + ) |
| 166 | +} |
| 167 | + |
| 168 | +function UnavailablePane(props: { label: string }) { |
| 169 | + const theme = useTheme() |
| 170 | + return ( |
| 171 | + <box flexGrow={1} alignItems="center" justifyContent="center"> |
| 172 | + <text fg={theme.text.subdued}>{props.label} is unavailable in this prototype.</text> |
| 173 | + </box> |
| 174 | + ) |
| 175 | +} |
0 commit comments