Import apps/android/src/screens/chat/composer-gate.ts

This commit is contained in:
QQ 2026-11-17 08:28:44 +08:00
parent b43172793f
commit 250da02343
1 changed files with 79 additions and 0 deletions

View File

@ -46,3 +46,82 @@ export function submitIntent(input: SubmitGateInput): SubmitIntent {
if (!input.sendReady || input.permissionSelecting) return 'ignore'
return 'submit'
}
/**
* Blank sessions retain the data-policy note when vertical space allows,
* while active chats keep the bottom bar compact without extra footer lines.
*/
export function policyHintVisible(input: { showPolicyHint: boolean; compactComposer: boolean }): boolean {
return input.showPolicyHint && !input.compactComposer
}
export type QuickPromptActionMode = 'fill' | 'send'
export function isQuickPromptActionMode(value: unknown): value is QuickPromptActionMode {
return value === 'fill' || value === 'send'
}
/**
* Track whether the composer should promote from inline single-row mode
* (`[+] [Input] [⚡] [↑]`) to the two-row stacked layout (`[Input]` above
* `[+] ... [⚡] [↑]`). Uses overflow-length hysteresis so expanding to full
* pill width does not immediately shrink line height back to 1 line and flip-flop.
*/
export function nextStackedOverflowLength(input: {
draft: string
contentHeight: number
currentOverflowLength: number | null
}): number | null {
if (input.draft.length === 0) return null
if (input.contentHeight > 44) {
if (input.currentOverflowLength === null) {
return input.draft.length
}
return Math.min(input.currentOverflowLength, input.draft.length)
}
if (input.currentOverflowLength !== null) {
const exitThreshold = Math.max(1, input.currentOverflowLength - 2)
if (input.draft.length < exitThreshold) {
return null
}
}
return input.currentOverflowLength
}
export function shouldUseStackedComposer(input: {
draft: string
hasQuotes: boolean
overflowLength: number | null
}): boolean {
if (input.hasQuotes) return true
if (input.draft.length === 0) return false
if (input.draft.includes('\n')) return true
return input.overflowLength !== null && input.draft.length >= input.overflowLength
}
/**
* Show the top-right corner expand trigger only once a draft reaches 5+ lines
* inside the stacked composer so short 1–4 line pills remain uncluttered and
* the trigger stays cleanly separated above the bottom action bar.
*/
export function shouldShowExpandCorner(input: { draft: string; contentHeight: number }): boolean {
return input.draft.split('\n').length >= 5 || input.contentHeight > 110
}
/**
* Insert a quick prompt into an existing or empty draft and place the caret at the end.
*/
export function applyQuickPromptDraft(
currentDraft: string,
promptText: string,
): { draft: string; cursor: number } {
if (promptText.length === 0) {
return { draft: currentDraft, cursor: currentDraft.length }
}
const separator =
currentDraft.length === 0 || currentDraft.endsWith('\n') || currentDraft.endsWith(' ')
? ''
: (currentDraft.includes('\n') || promptText.includes('\n') ? '\n' : ' ')
const draft = `${currentDraft}${separator}${promptText}`
return { draft, cursor: draft.length }
}