Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
import { handleDragMove, repositionOverlay, stopEdgeScroll } from './selection-overlay'
import { cancelSelect } from './selection-mode'
import { applyXtermSelection } from './selection-range'
import { notify } from './host-notify'
import { getMouseTrackingMode, isSafeSgrMouseCoordinate } from './mouse-input-encoding'
import { viewportToCell } from './viewport-cell'
import type { TerminalDocumentScope } from './document-scope'
import { ESC } from './escape-introducers'
import { notifyTerminalSurfaceTap } from './surface-tap'
import { viewportToMouseReportCell } from './mouse-report-cell'
import { dispatcherShouldBlockSurface, TAP_SLOP } from './tap-dispatch'
export type TerminalMouseGesture = {
startX: number
startY: number
lastX: number
lastY: number
lastCellKey: string | null
moved: boolean
mode: string
dismissedSelection: boolean
}
export function buildMouseButtonReport(
scope: TerminalDocumentScope,
kind: string,
clientX: number,
clientY: number
) {
const mouseTrackingMode = getMouseTrackingMode(scope)
if (mouseTrackingMode === 'none' || !scope.mouseEncodingKnown) {
return ''
}
if (kind === 'motion' && mouseTrackingMode !== 'drag' && mouseTrackingMode !== 'any') {
return ''
}
if (kind === 'release' && mouseTrackingMode === 'x10') {
return ''
}
const cell = viewportToMouseReportCell(scope, clientX, clientY)
if (!cell) {
return ''
}
const sgrButton = kind === 'motion' ? 32 : 0
const sgrFinal = kind === 'release' ? 'm' : 'M'
if (scope.sgrMousePixelsMode) {
if (!isSafeSgrMouseCoordinate(cell.x) || !isSafeSgrMouseCoordinate(cell.y)) {
return ''
}
return ESC + '[<' + sgrButton + ';' + cell.x + ';' + cell.y + sgrFinal
}
if (scope.sgrMouseMode) {
const sgrCol = cell.col + 1
const sgrRow = cell.row + 1
if (!isSafeSgrMouseCoordinate(sgrCol) || !isSafeSgrMouseCoordinate(sgrRow)) {
return ''
}
return ESC + '[<' + sgrButton + ';' + sgrCol + ';' + sgrRow + sgrFinal
}
const button = kind === 'motion' ? 64 : kind === 'release' ? 35 : 32
const col = cell.col + 1 + 32
const row = cell.row + 1 + 32
// Non-SGR bytes above ASCII are unsafe through JSON/RPC; drop unencodable input.
if (col > 126 || row > 126) {
return ''
}
return (
ESC + '[M' + String.fromCharCode(button) + String.fromCharCode(col) + String.fromCharCode(row)
)
}
export function mouseReportCellKey(scope: TerminalDocumentScope, clientX: number, clientY: number) {
const cell = viewportToMouseReportCell(scope, clientX, clientY)
return cell ? cell.col + ',' + cell.row : null
}
export function abandonMouseGesture(scope: TerminalDocumentScope) {
const gesture = scope.mouseGesture
scope.mouseGesture = null
if (!gesture) {
return
}
if (gesture.mode === 'tracking') {
// The TUI already received press; lost pointers must release its latched button.
const release = buildMouseButtonReport(scope, 'release', gesture.lastX, gesture.lastY)
if (release) {
notify(scope, { type: 'terminal-input', bytes: release })
}
} else if (gesture.mode === 'selecting') {
if (scope.sel) {
scope.sel.activeHandle = null
}
stopEdgeScroll(scope)
}
}
export function beginMouseDrag(scope: TerminalDocumentScope, gesture: TerminalMouseGesture) {
gesture.moved = true
// Without proven encoding, select locally instead of sending guessed mouse bytes.
if (getMouseTrackingMode(scope) !== 'none' && scope.mouseEncodingKnown) {
gesture.mode = 'tracking'
gesture.lastCellKey = mouseReportCellKey(scope, gesture.startX, gesture.startY)
const press = buildMouseButtonReport(scope, 'press', gesture.startX, gesture.startY)
if (press) {
notify(scope, { type: 'terminal-input', bytes: press })
}
return
}
const anchor = viewportToCell(scope, gesture.startX, gesture.startY)
if (!anchor) {
gesture.mode = 'cancelled'
return
}
// Mouse selects character anchors; reuse handle-drag edge scrolling without touch’s word seeding.
gesture.mode = 'selecting'
scope.selMode = 'select'
scope.sel = { anchor: anchor, focus: anchor, activeHandle: 'end' }
scope.selectionOverlay!.classList.add('active')
notify(scope, { type: 'set-select-mode', enabled: true })
applyXtermSelection(scope)
repositionOverlay(scope)
}
export function attachSurfaceMouseClickDragHandler(
scope: TerminalDocumentScope,
targetSurface: HTMLElement
) {
targetSurface.addEventListener(
'pointerdown',
function (e) {
if (e.pointerType !== 'mouse' || e.button !== 0) {
return
}
if (dispatcherShouldBlockSurface(scope) || !scope.term) {
return
}
// Clear stale gestures when outside-WebView pointerup was lost.
if (scope.mouseGesture) {
abandonMouseGesture(scope)
}
// Capture mouse pointers so leaving the surface cannot strand drag state without pointerup.
try {
if (targetSurface.setPointerCapture) {
targetSurface.setPointerCapture(e.pointerId)
}
} catch {}
scope.mouseGesture = {
startX: e.clientX,
startY: e.clientY,
lastX: e.clientX,
lastY: e.clientY,
lastCellKey: null,
moved: false,
mode: 'pending',
dismissedSelection: false
}
if (scope.selMode === 'select') {
cancelSelect(scope)
scope.mouseGesture.dismissedSelection = true
}
},
true
)
targetSurface.addEventListener(
'pointermove',
function (e) {
const gesture = scope.mouseGesture
if (e.pointerType !== 'mouse' || !gesture || gesture.mode === 'cancelled') {
return
}
if (!scope.term) {
return
}
gesture.lastX = e.clientX
gesture.lastY = e.clientY
if ((e.buttons & 1) === 0) {
// On re-entry release the stale TUI press at the new pointer coordinates when capture was unavailable.
abandonMouseGesture(scope)
return
}
if (!gesture.moved) {
const dx = Math.abs(e.clientX - gesture.startX)
const dy = Math.abs(e.clientY - gesture.startY)
if (dx + dy <= TAP_SLOP) {
return
}
beginMouseDrag(scope, gesture)
}
if (gesture.mode === 'tracking') {
// Report once per cell to bound traffic by grid size instead of pointermove cadence.
const cellKey = mouseReportCellKey(scope, e.clientX, e.clientY)
if (cellKey && cellKey !== gesture.lastCellKey) {
gesture.lastCellKey = cellKey
const motion = buildMouseButtonReport(scope, 'motion', e.clientX, e.clientY)
if (motion) {
notify(scope, { type: 'terminal-input', bytes: motion })
}
}
} else if (gesture.mode === 'selecting') {
handleDragMove(scope, 'end', e.clientX, e.clientY)
}
},
true
)
targetSurface.addEventListener(
'pointerup',
function (e) {
const gesture = scope.mouseGesture
if (e.pointerType !== 'mouse' || !gesture || e.button !== 0) {
return
}
scope.mouseGesture = null
if (gesture.mode === 'cancelled' || !scope.term) {
return
}
if (gesture.mode === 'tracking') {
const release = buildMouseButtonReport(scope, 'release', e.clientX, e.clientY)
if (release) {
notify(scope, { type: 'terminal-input', bytes: release })
}
return
}
if (gesture.mode === 'selecting') {
if (scope.sel) {
scope.sel.activeHandle = null
}
stopEdgeScroll(scope)
repositionOverlay(scope)
return
}
if (dispatcherShouldBlockSurface(scope)) {
return
}
// Dismissing selection must not also activate a link or focus the keyboard.
if (gesture.dismissedSelection) {
return
}
notifyTerminalSurfaceTap(scope, e.clientX, e.clientY, false)
},
true
)
targetSurface.addEventListener(
'pointercancel',
function (e) {
if (e.pointerType !== 'mouse') {
return
}
abandonMouseGesture(scope)
},
true
)
// Android can pair mouse-flavored pointer events with real touches; touch dispatch takes ownership when they arrive.
targetSurface.addEventListener(
'touchstart',
function () {
if (scope.mouseGesture) {
abandonMouseGesture(scope)
}
},
true
)
}