Skip to content

Commit 7cc0ba1

Browse files
radroidclaude
authored 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 203cb8e commit 7cc0ba1

10 files changed

Lines changed: 197 additions & 6 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>())
@@ -450,6 +460,47 @@ private fun parseTokens(value: String): List<ComposerToken> = try {
450460
private class SelectionAwareEditText(context: Context) : EditText(context) {
451461
var selectionListener: ((Int, Int) -> Unit)? = null
452462
var pasteImagesListener: ((List<String>) -> Unit)? = null
463+
var submitListener: (() -> Unit)? = null
464+
465+
private fun isPlainEnter(keyCode: Int, event: KeyEvent?): Boolean {
466+
val isEnter = keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_NUMPAD_ENTER
467+
// Shift+Enter still inserts a newline; only an unmodified Enter submits.
468+
return isEnter && event?.isShiftPressed != true
469+
}
470+
471+
// Hardware keyboards (and soft keyboards that route Enter as a key event)
472+
// land here. Fire submit on the down edge and consume both edges so the
473+
// multi-line field never inserts the newline.
474+
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
475+
if (isPlainEnter(keyCode, event)) {
476+
submitListener?.invoke()
477+
return true
478+
}
479+
return super.onKeyDown(keyCode, event)
480+
}
481+
482+
override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
483+
if (isPlainEnter(keyCode, event)) {
484+
return true
485+
}
486+
return super.onKeyUp(keyCode, event)
487+
}
488+
489+
// Soft keyboards that commit the newline as text (rather than a key event)
490+
// land here; an exact "\n" commit means a bare Return, so submit instead.
491+
// A multi-line paste ("a\nb") is unaffected because it is not exactly "\n".
492+
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection? {
493+
val base = super.onCreateInputConnection(outAttrs) ?: return null
494+
return object : InputConnectionWrapper(base, false) {
495+
override fun commitText(text: CharSequence?, newCursorPosition: Int): Boolean {
496+
if (text?.toString() == "\n") {
497+
submitListener?.invoke()
498+
return true
499+
}
500+
return super.commitText(text, newCursorPosition)
501+
}
502+
}
503+
}
453504

454505
override fun onSelectionChanged(selStart: Int, selEnd: Int) {
455506
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
@@ -497,6 +497,16 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
497497
shouldChangeTextIn range: NSRange,
498498
replacementText text: String
499499
) -> Bool {
500+
// A bare Return (soft keyboard or hardware) submits — matching desktop
501+
// Enter=submit and the composer's queue-when-busy / send-when-idle outbox.
502+
// Only an exact single "\n" is intercepted, so a multi-line paste like
503+
// "a\nb" still inserts normally. The newline button inserts "\n" through
504+
// the controlled value (setText), never this typing path. Hardware
505+
// Command-Return keeps flowing through the UIKeyCommand above.
506+
if text == "\n" {
507+
onComposerSubmit([:])
508+
return false
509+
}
500510
restoreBaseTypingAttributes()
501511
return true
502512
}

‎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,
@@ -130,6 +131,7 @@ const ANDROID_ICON_BY_SF_SYMBOL: Partial<Record<SFSymbol, Icon>> = {
130131
play: IconPlayerPlay,
131132
plus: IconPlus,
132133
"qrcode.viewfinder": IconQrcode,
134+
return: IconCornerDownLeft,
133135
"point.3.connected.trianglepath.dotted": IconNetwork,
134136
"point.topleft.down.curvedto.point.bottomright.up": IconGitMerge,
135137
safari: IconExternalLink,

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import { useComposerPathSearch } from "../../state/use-composer-path-search";
6767
import { ComposerCommandPopover, type ComposerCommandItem } from "./ComposerCommandPopover";
6868
import { ThreadSettingsSheet, threadSettingsSummaryLabel } from "./ThreadSettingsSheet";
6969
import { useThreadSettingsSheetPresentation } from "./use-thread-settings-sheet-presentation";
70+
import { resolveComposerSendLabel } from "./composerSendLabel";
7071

7172
/**
7273
* Height of the collapsed composer (pill + vertical padding, excluding safe-area inset).
@@ -318,10 +319,11 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
318319
props.selectedThread.session?.status === "running" ||
319320
props.selectedThread.session?.status === "starting";
320321

321-
const sendLabel =
322-
props.connectionState !== "connected" || props.activeThreadBusy || props.queueCount > 0
323-
? "Queue"
324-
: "Send";
322+
const sendLabel = resolveComposerSendLabel({
323+
connectionState: props.connectionState,
324+
activeThreadBusy: props.activeThreadBusy,
325+
queueCount: props.queueCount,
326+
});
325327
const currentModelSelection = props.selectedThread.modelSelection;
326328
const currentRuntimeMode = props.selectedThread.runtimeMode;
327329
const currentInteractionMode = props.selectedThread.interactionMode ?? "default";
@@ -547,6 +549,20 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
547549
props.selectedThread.id,
548550
props.selectedThread.title,
549551
]);
552+
// Return now submits (queues-when-busy / sends-when-idle), matching desktop
553+
// Enter=submit. The newline button is the cross-platform multiline escape
554+
// hatch: it inserts "\n" at the caret through the same insert-at-selection
555+
// mechanism used for @file / skill tokens (replaceTextRange + selection).
556+
const handleInsertNewline = useCallback(() => {
557+
const result = replaceTextRange(
558+
draftMessage,
559+
composerSelection.start,
560+
composerSelection.end,
561+
"\n",
562+
);
563+
setComposerSelection({ start: result.cursor, end: result.cursor });
564+
onChangeDraftMessage(result.text);
565+
}, [composerSelection.start, composerSelection.end, draftMessage, onChangeDraftMessage]);
550566
const handleCommandSelect = useCallback(
551567
(item: ComposerCommandItem) => {
552568
if (!composerTrigger) return;
@@ -792,6 +808,12 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
792808
onPress={() => void props.onPickDraftImages()}
793809
showChevron={false}
794810
/>
811+
<ComposerToolbarButton
812+
accessibilityLabel="Insert line break"
813+
icon="return"
814+
onPress={handleInsertNewline}
815+
showChevron={false}
816+
/>
795817
<ComposerToolbarTrigger
796818
accessibilityLabel="Thread settings"
797819
iconNode={
@@ -801,6 +823,7 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
801823
maxWidth={320}
802824
onPress={settingsSheetPresentation.open}
803825
/>
826+
804827
{showStopAction ? (
805828
<ComposerToolbarButton
806829
accessibilityLabel="Stop"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
3+
import { resolveComposerSendLabel } from "./composerSendLabel";
4+
5+
describe("resolveComposerSendLabel", () => {
6+
it("labels Send when connected, idle, and the outbox is empty", () => {
7+
expect(
8+
resolveComposerSendLabel({
9+
connectionState: "connected",
10+
activeThreadBusy: false,
11+
queueCount: 0,
12+
}),
13+
).toBe("Send");
14+
});
15+
16+
it("labels Queue while the active thread is busy", () => {
17+
expect(
18+
resolveComposerSendLabel({
19+
connectionState: "connected",
20+
activeThreadBusy: true,
21+
queueCount: 0,
22+
}),
23+
).toBe("Queue");
24+
});
25+
26+
it("labels Queue when messages are already queued to send later", () => {
27+
expect(
28+
resolveComposerSendLabel({
29+
connectionState: "connected",
30+
activeThreadBusy: false,
31+
queueCount: 2,
32+
}),
33+
).toBe("Queue");
34+
});
35+
36+
it("labels Queue while the environment is not connected", () => {
37+
expect(
38+
resolveComposerSendLabel({
39+
connectionState: "reconnecting",
40+
activeThreadBusy: false,
41+
queueCount: 0,
42+
}),
43+
).toBe("Queue");
44+
});
45+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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, the active thread is still working, or the
11+
* outbox already holds messages — and "Send" only when it will go out now.
12+
*/
13+
export function resolveComposerSendLabel(input: {
14+
readonly connectionState: RemoteClientConnectionState;
15+
readonly activeThreadBusy: boolean;
16+
readonly queueCount: number;
17+
}): "Send" | "Queue" {
18+
if (input.connectionState !== "connected" || input.activeThreadBusy || input.queueCount > 0) {
19+
return "Queue";
20+
}
21+
return "Send";
22+
}

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

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

7879
const NativeView = requireNativeView<NativeComposerEditorProps>(NATIVE_MODULE_NAME);
@@ -97,6 +98,7 @@ export function ComposerEditor({
9798
onPasteImages,
9899
onFocus,
99100
onBlur,
101+
onSubmit,
100102
contentInsetVertical = 0,
101103
...props
102104
}: ComposerEditorProps) {
@@ -279,6 +281,7 @@ export function ComposerEditor({
279281
onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
280282
onComposerFocus={onFocus}
281283
onComposerBlur={onBlur}
284+
onComposerSubmit={onSubmit}
282285
/>
283286
</TextInputWrapper>
284287
);

‎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)