Skip to content

Commit b1eb3b1

Browse files
radroidclaude
andcommitted
feat(mobile): queue/send from the keyboard Return key
Fixes #8. Closes #7. Mobile queueing already worked end-to-end (every send enqueues to the thread outbox, which sends-when-idle and holds-when-busy; the primary button already relabels to "Queue" while busy and a queueCount indicator exists). The only gap was the keyboard: the soft-keyboard Return inserted a newline, and submit was only wired to hardware Command-Return on iOS (and not at all on Android). - iOS: intercept a bare "\n" in the shouldChangeText delegate and fire onComposerSubmit (soft keyboard or hardware Return). Exact single "\n" only, so a multi-line paste ("a\nb") is unaffected; Command-Return keyCommand intact. - Android: wire onComposerSubmit for the first time — an onComposerSubmit event, plus a plain-Enter interception on both the key-event path (onKeyDown) and the IME commitText path, with Shift+Enter still inserting a newline. Multi-line input type preserved. - RN: forward onSubmit -> onComposerSubmit through the Android wrapper (was dropped); iOS wrapper already forwarded it. - Composer: Return routes to the existing handleSend (queue-when-busy / send-when-idle), so queueCount updates the same way as the button. Add a dedicated "Insert line break" toolbar button (inserts "\n" at the caret via the same replaceTextRange helper used for @file / skill tokens) as the cross-platform newline affordance now that Return submits. - Extract resolveComposerSendLabel as a pure, unit-tested helper. Trade-off: hardware plain-Return now submits instead of inserting a newline (matching desktop Enter=submit); the newline button and Shift+Return cover multiline. Tests: composerSendLabel (Send/Queue matrix) and replaceTextRange newline cases. Native keyboard behavior verified on the iOS Simulator; Android soft-keyboard paths need on-device QA (noted on the PR). Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 2bacecc commit b1eb3b1

10 files changed

Lines changed: 187 additions & 4 deletions

File tree

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorModule.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class T3ComposerEditorModule : Module() {
5454
"onComposerSelectionChange",
5555
"onComposerFocus",
5656
"onComposerBlur",
57+
"onComposerSubmit",
5758
"onComposerPasteImages",
5859
"onComposerContentSizeChange",
5960
)

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import android.text.Spanned
1313
import android.text.TextWatcher
1414
import android.text.style.ReplacementSpan
1515
import android.view.Gravity
16+
import android.view.KeyEvent
1617
import android.view.ViewGroup
18+
import android.view.inputmethod.EditorInfo
19+
import android.view.inputmethod.InputConnection
20+
import android.view.inputmethod.InputConnectionWrapper
1721
import android.view.inputmethod.InputMethodManager
1822
import android.widget.EditText
1923
import expo.modules.kotlin.AppContext
@@ -31,6 +35,7 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
3135
private val onComposerSelectionChange by EventDispatcher()
3236
private val onComposerFocus by EventDispatcher()
3337
private val onComposerBlur by EventDispatcher()
38+
private val onComposerSubmit by EventDispatcher()
3439
private val onComposerPasteImages by EventDispatcher()
3540
private val onComposerContentSizeChange by EventDispatcher()
3641
private var applyingNativeValue = false
@@ -65,6 +70,11 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
6570
editor.pasteImagesListener = { uris ->
6671
onComposerPasteImages(mapOf("uris" to uris))
6772
}
73+
// A bare Enter (no Shift) submits — mirrors iOS and desktop Enter=submit.
74+
// Empty payload matches the iOS `onComposerSubmit([:])` contract.
75+
editor.submitListener = {
76+
onComposerSubmit(emptyMap<String, Any>())
77+
}
6878
editor.setOnFocusChangeListener { _, hasFocus ->
6979
if (hasFocus) {
7080
onComposerFocus(emptyMap<String, Any>())
@@ -457,6 +467,47 @@ private fun parseTokens(value: String): List<ComposerToken> = try {
457467
private class SelectionAwareEditText(context: Context) : EditText(context) {
458468
var selectionListener: ((Int, Int) -> Unit)? = null
459469
var pasteImagesListener: ((List<String>) -> Unit)? = null
470+
var submitListener: (() -> Unit)? = null
471+
472+
private fun isPlainEnter(keyCode: Int, event: KeyEvent?): Boolean {
473+
val isEnter = keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_NUMPAD_ENTER
474+
// Shift+Enter still inserts a newline; only an unmodified Enter submits.
475+
return isEnter && event?.isShiftPressed != true
476+
}
477+
478+
// Hardware keyboards (and soft keyboards that route Enter as a key event)
479+
// land here. Fire submit on the down edge and consume both edges so the
480+
// multi-line field never inserts the newline.
481+
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
482+
if (isPlainEnter(keyCode, event)) {
483+
submitListener?.invoke()
484+
return true
485+
}
486+
return super.onKeyDown(keyCode, event)
487+
}
488+
489+
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
490+
if (isPlainEnter(keyCode, event)) {
491+
return true
492+
}
493+
return super.onKeyUp(keyCode, event)
494+
}
495+
496+
// Soft keyboards that commit the newline as text (rather than a key event)
497+
// land here; an exact "\n" commit means a bare Return, so submit instead.
498+
// A multi-line paste ("a\nb") is unaffected because it is not exactly "\n".
499+
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? {
500+
val base = super.onCreateInputConnection(outAttrs) ?: return null
501+
return object : InputConnectionWrapper(base, false) {
502+
override fun commitText(text: CharSequence?, newCursorPosition: Int): Boolean {
503+
if (text?.toString() == "\n") {
504+
submitListener?.invoke()
505+
return true
506+
}
507+
return super.commitText(text, newCursorPosition)
508+
}
509+
}
510+
}
460511

461512
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
462513
super.onSelectionChanged(selStart, selEnd)

‎apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,16 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
503503
shouldChangeTextIn range: NSRange,
504504
replacementText text: String
505505
) -> Bool {
506+
// A bare Return (soft keyboard or hardware) submits — matching desktop
507+
// Enter=submit and the composer's queue-when-busy / send-when-idle outbox.
508+
// Only an exact single "\n" is intercepted, so a multi-line paste like
509+
// "a\nb" still inserts normally. The newline button inserts "\n" through
510+
// the controlled value (setText), never this typing path. Hardware
511+
// Command-Return keeps flowing through the UIKeyCommand above.
512+
if text == "\n" {
513+
onComposerSubmit([:])
514+
return false
515+
}
506516
restoreBaseTypingAttributes()
507517
return true
508518
}

‎apps/mobile/src/components/AppSymbol.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
IconCircleXFilled,
2626
IconClock,
2727
IconCopy,
28+
IconCornerDownLeft,
2829
IconDeviceDesktop,
2930
IconDots,
3031
IconDotsCircleHorizontal,
@@ -133,6 +134,7 @@ const ANDROID_ICON_BY_SF_SYMBOL: Partial<Record<SFSymbol, Icon>> = {
133134
play: IconPlayerPlay,
134135
plus: IconPlus,
135136
"qrcode.viewfinder": IconQrcode,
137+
return: IconCornerDownLeft,
136138
"point.3.connected.trianglepath.dotted": IconNetwork,
137139
"point.topleft.down.curvedto.point.bottomright.up": IconGitMerge,
138140
safari: IconExternalLink,

‎apps/mobile/src/features/threads/ThreadComposer.tsx‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
useThreadSettingsSheetPresentation,
7676
type NavigationWithFinishTransitioning,
7777
} from "./use-thread-settings-sheet-presentation";
78+
import { resolveComposerSendLabel } from "./composerSendLabel";
7879

7980
/**
8081
* Height of the collapsed composer (pill + vertical padding, excluding safe-area inset).
@@ -331,8 +332,10 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
331332
props.selectedThread.session?.status === "running" ||
332333
props.selectedThread.session?.status === "starting";
333334

334-
const sendLabel =
335-
props.connectionState !== "connected" || props.queueCount > 0 ? "Queue" : "Send";
335+
const sendLabel = resolveComposerSendLabel({
336+
connectionState: props.connectionState,
337+
queueCount: props.queueCount,
338+
});
336339
const currentModelSelection = props.selectedThread.modelSelection;
337340
const currentRuntimeMode = props.selectedThread.runtimeMode;
338341
const connectionStatus = composerConnectionStatus({
@@ -561,6 +564,20 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
561564
props.selectedThread.id,
562565
props.selectedThread.title,
563566
]);
567+
// Return now submits (queues-when-busy / sends-when-idle), matching desktop
568+
// Enter=submit. The newline button is the cross-platform multiline escape
569+
// hatch: it inserts "\n" at the caret through the same insert-at-selection
570+
// mechanism used for @file / skill tokens (replaceTextRange + selection).
571+
const handleInsertNewline = useCallback(() => {
572+
const result = replaceTextRange(
573+
draftMessage,
574+
composerSelection.start,
575+
composerSelection.end,
576+
"\n",
577+
);
578+
setComposerSelection({ start: result.cursor, end: result.cursor });
579+
onChangeDraftMessage(result.text);
580+
}, [composerSelection.start, composerSelection.end, draftMessage, onChangeDraftMessage]);
564581
const handleCommandSelect = useCallback(
565582
(item: ComposerCommandItem) => {
566583
if (!composerTrigger) return;
@@ -862,6 +879,12 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
862879
onPress={() => void props.onPickDraftImages()}
863880
showChevron={false}
864881
/>
882+
<ComposerToolbarButton
883+
accessibilityLabel="Insert line break"
884+
icon="return"
885+
onPress={handleInsertNewline}
886+
showChevron={false}
887+
/>
865888
<ComposerInlineControl
866889
accessibilityLabel="Model and reasoning settings"
867890
emphasized
@@ -872,6 +895,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
872895
maxWidth={152}
873896
onPress={openSettings}
874897
/>
898+
875899
{showStopAction ? (
876900
<ComposerToolbarButton
877901
accessibilityLabel="Stop"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
3+
import { resolveComposerSendLabel } from "./composerSendLabel";
4+
5+
describe("resolveComposerSendLabel", () => {
6+
it("labels Send when connected and the outbox is empty", () => {
7+
expect(
8+
resolveComposerSendLabel({
9+
connectionState: "connected",
10+
queueCount: 0,
11+
}),
12+
).toBe("Send");
13+
});
14+
15+
it("labels Queue when messages are already queued to send later", () => {
16+
expect(
17+
resolveComposerSendLabel({
18+
connectionState: "connected",
19+
queueCount: 2,
20+
}),
21+
).toBe("Queue");
22+
});
23+
24+
it("labels Queue while the environment is not connected", () => {
25+
expect(
26+
resolveComposerSendLabel({
27+
connectionState: "reconnecting",
28+
queueCount: 0,
29+
}),
30+
).toBe("Queue");
31+
});
32+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { RemoteClientConnectionState } from "../../lib/connection";
2+
3+
/**
4+
* The composer's send affordance doubles as a queue affordance: every "send"
5+
* enqueues to the thread outbox, which sends-when-idle and holds-when-busy.
6+
* The button label mirrors that decision so the user knows whether pressing it
7+
* (or hitting Return) will dispatch now or queue for later.
8+
*
9+
* Returns "Queue" when the message cannot be delivered immediately — the
10+
* environment is not connected, or the outbox already holds messages — and
11+
* "Send" only when it will go out now.
12+
*
13+
* A busy thread is deliberately NOT a queue condition: upstream #6543 made the
14+
* mobile composer steer an active turn by default and removed `activeThreadBusy`
15+
* from `ThreadComposerProps` entirely. This helper mirrors that ternary, so it
16+
* follows.
17+
*/
18+
export function resolveComposerSendLabel(input: {
19+
readonly connectionState: RemoteClientConnectionState;
20+
readonly queueCount: number;
21+
}): "Send" | "Queue" {
22+
if (input.connectionState !== "connected" || input.queueCount > 0) {
23+
return "Queue";
24+
}
25+
return "Send";
26+
}

‎apps/mobile/src/native/T3ComposerEditor.native.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ interface NativeComposerEditorProps extends ViewProps {
7474
readonly onComposerPasteImages?: (event: NativePasteImagesEvent) => void;
7575
readonly onComposerFocus?: () => void;
7676
readonly onComposerBlur?: () => void;
77+
readonly onComposerSubmit?: () => void;
7778
}
7879

7980
const NativeView = requireNativeView<NativeComposerEditorProps>(NATIVE_MODULE_NAME);
@@ -98,6 +99,7 @@ export function ComposerEditor({
9899
onPasteImages,
99100
onFocus,
100101
onBlur,
102+
onSubmit,
101103
contentInsetVertical = 0,
102104
...props
103105
}: ComposerEditorProps) {
@@ -296,6 +298,7 @@ export function ComposerEditor({
296298
onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
297299
onComposerFocus={onFocus}
298300
onComposerBlur={onBlur}
301+
onComposerSubmit={onSubmit}
299302
/>
300303
</TextInputWrapper>
301304
);

‎apps/mobile/src/native/T3ComposerEditor.types.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export interface ComposerEditorProps {
3737
readonly onPasteImages?: (uris: ReadonlyArray<string>) => void;
3838
readonly onFocus?: () => void;
3939
readonly onBlur?: () => void;
40-
/** Invoked by the native editor when Command-Return is pressed on a hardware keyboard. */
40+
/**
41+
* Invoked by the native editor when the composer should submit: any Return
42+
* key with no modifier (soft keyboard or hardware), plus hardware
43+
* Command-Return. Shift-Return still inserts a newline instead of submitting.
44+
*/
4145
readonly onSubmit?: () => void;
4246
}

‎packages/shared/src/composerTrigger.test.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { serializeComposerFileLink, serializeComposerMentionPath } from "./composerTrigger.ts";
3+
import {
4+
replaceTextRange,
5+
serializeComposerFileLink,
6+
serializeComposerMentionPath,
7+
} from "./composerTrigger.ts";
48

59
describe("serializeComposerMentionPath", () => {
610
it("keeps simple mention paths unquoted", () => {
@@ -41,3 +45,29 @@ describe("serializeComposerFileLink", () => {
4145
);
4246
});
4347
});
48+
49+
// The mobile composer's newline button inserts "\n" at the caret through this
50+
// same insert-at-selection helper it uses for @file / skill tokens. These cover
51+
// the newline cases specifically so the button's behaviour stays pinned.
52+
describe("replaceTextRange newline insertion", () => {
53+
it("inserts a newline at a collapsed caret in the middle of the text", () => {
54+
expect(replaceTextRange("abcd", 2, 2, "\n")).toEqual({
55+
text: "ab\ncd",
56+
cursor: 3,
57+
});
58+
});
59+
60+
it("inserts a newline at the end of the text", () => {
61+
expect(replaceTextRange("abc", 3, 3, "\n")).toEqual({
62+
text: "abc\n",
63+
cursor: 4,
64+
});
65+
});
66+
67+
it("replaces a selection range with a newline", () => {
68+
expect(replaceTextRange("abcdef", 1, 4, "\n")).toEqual({
69+
text: "a\nef",
70+
cursor: 2,
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)