2026-02-08 16:21:04 +09:00
|
|
|
import type { SplitDirection, TmuxPaneInfo, WindowState } from "./types"
|
|
|
|
|
import { computeGridPlan, mapPaneToSlot } from "./grid-planning"
|
|
|
|
|
import { canSplitPane, getBestSplitDirection } from "./pane-split-availability"
|
2026-02-17 03:40:37 +09:00
|
|
|
import { MIN_PANE_WIDTH } from "./types"
|
2026-02-08 16:21:04 +09:00
|
|
|
|
|
|
|
|
export interface SpawnTarget {
|
|
|
|
|
targetPaneId: string
|
|
|
|
|
splitDirection: SplitDirection
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildOccupancy(
|
|
|
|
|
agentPanes: TmuxPaneInfo[],
|
|
|
|
|
plan: ReturnType<typeof computeGridPlan>,
|
|
|
|
|
mainPaneWidth: number,
|
|
|
|
|
): Map<string, TmuxPaneInfo> {
|
|
|
|
|
const occupancy = new Map<string, TmuxPaneInfo>()
|
|
|
|
|
for (const pane of agentPanes) {
|
|
|
|
|
const slot = mapPaneToSlot(pane, plan, mainPaneWidth)
|
|
|
|
|
occupancy.set(`${slot.row}:${slot.col}`, pane)
|
|
|
|
|
}
|
|
|
|
|
return occupancy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findFirstEmptySlot(
|
|
|
|
|
occupancy: Map<string, TmuxPaneInfo>,
|
|
|
|
|
plan: ReturnType<typeof computeGridPlan>,
|
|
|
|
|
): { row: number; col: number } {
|
|
|
|
|
for (let row = 0; row < plan.rows; row++) {
|
|
|
|
|
for (let col = 0; col < plan.cols; col++) {
|
|
|
|
|
if (!occupancy.has(`${row}:${col}`)) {
|
|
|
|
|
return { row, col }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { row: plan.rows - 1, col: plan.cols - 1 }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findSplittableTarget(
|
|
|
|
|
state: WindowState,
|
2026-02-17 03:40:37 +09:00
|
|
|
minPaneWidth: number,
|
2026-02-08 16:21:04 +09:00
|
|
|
_preferredDirection?: SplitDirection,
|
|
|
|
|
): SpawnTarget | null {
|
|
|
|
|
if (!state.mainPane) return null
|
|
|
|
|
const existingCount = state.agentPanes.length
|
|
|
|
|
|
|
|
|
|
if (existingCount === 0) {
|
|
|
|
|
const virtualMainPane: TmuxPaneInfo = { ...state.mainPane, width: state.windowWidth }
|
2026-02-17 03:40:37 +09:00
|
|
|
if (canSplitPane(virtualMainPane, "-h", minPaneWidth)) {
|
2026-02-08 16:21:04 +09:00
|
|
|
return { targetPaneId: state.mainPane.paneId, splitDirection: "-h" }
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 04:10:27 +09:00
|
|
|
const plan = computeGridPlan(
|
|
|
|
|
state.windowWidth,
|
|
|
|
|
state.windowHeight,
|
|
|
|
|
existingCount + 1,
|
|
|
|
|
state.mainPane.width,
|
|
|
|
|
minPaneWidth,
|
|
|
|
|
)
|
|
|
|
|
const mainPaneWidth = state.mainPane.width
|
2026-02-08 16:21:04 +09:00
|
|
|
const occupancy = buildOccupancy(state.agentPanes, plan, mainPaneWidth)
|
|
|
|
|
const targetSlot = findFirstEmptySlot(occupancy, plan)
|
|
|
|
|
|
|
|
|
|
const leftPane = occupancy.get(`${targetSlot.row}:${targetSlot.col - 1}`)
|
2026-02-17 03:40:37 +09:00
|
|
|
if (leftPane && canSplitPane(leftPane, "-h", minPaneWidth)) {
|
2026-02-08 16:21:04 +09:00
|
|
|
return { targetPaneId: leftPane.paneId, splitDirection: "-h" }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const abovePane = occupancy.get(`${targetSlot.row - 1}:${targetSlot.col}`)
|
2026-02-17 03:40:37 +09:00
|
|
|
if (abovePane && canSplitPane(abovePane, "-v", minPaneWidth)) {
|
2026-02-08 16:21:04 +09:00
|
|
|
return { targetPaneId: abovePane.paneId, splitDirection: "-v" }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const splittablePanes = state.agentPanes
|
2026-02-17 03:40:37 +09:00
|
|
|
.map((pane) => ({ pane, direction: getBestSplitDirection(pane, minPaneWidth) }))
|
2026-02-08 16:21:04 +09:00
|
|
|
.filter(
|
|
|
|
|
(item): item is { pane: TmuxPaneInfo; direction: SplitDirection } =>
|
|
|
|
|
item.direction !== null,
|
|
|
|
|
)
|
|
|
|
|
.sort((a, b) => b.pane.width * b.pane.height - a.pane.width * a.pane.height)
|
|
|
|
|
|
|
|
|
|
const best = splittablePanes[0]
|
|
|
|
|
if (best) {
|
|
|
|
|
return { targetPaneId: best.pane.paneId, splitDirection: best.direction }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-17 03:40:37 +09:00
|
|
|
export function findSpawnTarget(
|
|
|
|
|
state: WindowState,
|
|
|
|
|
minPaneWidth: number = MIN_PANE_WIDTH,
|
|
|
|
|
): SpawnTarget | null {
|
|
|
|
|
return findSplittableTarget(state, minPaneWidth)
|
2026-02-08 16:21:04 +09:00
|
|
|
}
|