Skip to content

Commit 4ab11db

Browse files
authored
Merge pull request #16 from radroid/t3x/mobile-keyboard-queue
feat(mobile): queue/send from the keyboard Return key (#7, #8)
2 parents d25ec9d + 511d5c7 commit 4ab11db

10 files changed

Lines changed: 219 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ private final class ComposerTextView: UITextView {
6565
var onAttributedMutation: (() -> Void)?
6666
var onSubmit: (() -> Void)?
6767

68+
// Set only while a Shift+Return hardware key command is programmatically
69+
// inserting a line break, so the delegate lets that "\n" through instead of
70+
// treating it as a submit. Reset synchronously after the insert.
71+
private(set) var isInsertingHardLineBreak = false
72+
6873
override var keyCommands: [UIKeyCommand]? {
6974
var commands = super.keyCommands ?? []
7075
let submit = UIKeyCommand(
@@ -75,13 +80,30 @@ private final class ComposerTextView: UITextView {
7580
submit.discoverabilityTitle = "Send Message"
7681
submit.wantsPriorityOverSystemBehavior = true
7782
commands.append(submit)
83+
// Shift+Return inserts a newline on a hardware keyboard (mirrors Android and
84+
// desktop Shift+Enter), since a bare Return now submits. Soft keyboards use
85+
// the composer's line-break button instead.
86+
let newline = UIKeyCommand(
87+
input: "\r",
88+
modifierFlags: .shift,
89+
action: #selector(insertHardLineBreak(_:))
90+
)
91+
newline.discoverabilityTitle = "Insert Line Break"
92+
newline.wantsPriorityOverSystemBehavior = true
93+
commands.append(newline)
7894
return commands
7995
}
8096

8197
@objc private func submitMessage(_ sender: UIKeyCommand) {
8298
onSubmit?()
8399
}
84100

101+
@objc private func insertHardLineBreak(_ sender: UIKeyCommand) {
102+
isInsertingHardLineBreak = true
103+
insertText("\n")
104+
isInsertingHardLineBreak = false
105+
}
106+
85107
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
86108
if action == #selector(paste(_:)) {
87109
let pasteboard = UIPasteboard.general
@@ -496,6 +518,17 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate {
496518
shouldChangeTextIn range: NSRange,
497519
replacementText text: String
498520
) -> Bool {
521+
// A bare Return (soft keyboard or hardware) submits — matching desktop
522+
// Enter=submit and the composer's queue-when-busy / send-when-idle outbox.
523+
// Only an exact single "\n" is intercepted, so a multi-line paste like
524+
// "a\nb" still inserts normally. The newline button inserts "\n" through
525+
// the controlled value (setText), never this typing path. Hardware
526+
// Command-Return keeps flowing through the UIKeyCommand above, and a
527+
// Shift+Return line break is let through via isInsertingHardLineBreak.
528+
if text == "\n", (textView as? ComposerTextView)?.isInsertingHardLineBreak != true {
529+
onComposerSubmit([:])
530+
return false
531+
}
499532
restoreBaseTypingAttributes()
500533
return true
501534
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
IconCircleCheck,
2424
IconCircleXFilled,
2525
IconCopy,
26+
IconCornerDownLeft,
2627
IconDeviceDesktop,
2728
IconDots,
2829
IconDotsCircleHorizontal,
@@ -122,6 +123,7 @@ const ANDROID_ICON_BY_SF_SYMBOL: Partial<Record<SFSymbol, Icon>> = {
122123
play: IconPlayerPlay,
123124
plus: IconPlus,
124125
"qrcode.viewfinder": IconQrcode,
126+
return: IconCornerDownLeft,
125127
"point.3.connected.trianglepath.dotted": IconNetwork,
126128
"point.topleft.down.curvedto.point.bottomright.up": IconGitMerge,
127129
safari: IconExternalLink,

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
} from "../../lib/providerOptions";
7070
import { useComposerPathSearch } from "../../state/use-composer-path-search";
7171
import { ComposerCommandPopover, type ComposerCommandItem } from "./ComposerCommandPopover";
72+
import { resolveComposerSendLabel } from "./composerSendLabel";
7273

7374
/**
7475
* Height of the collapsed composer (pill + vertical padding, excluding safe-area inset).
@@ -309,10 +310,11 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
309310
props.selectedThread.session?.status === "running" ||
310311
props.selectedThread.session?.status === "starting";
311312

312-
const sendLabel =
313-
props.connectionState !== "connected" || props.activeThreadBusy || props.queueCount > 0
314-
? "Queue"
315-
: "Send";
313+
const sendLabel = resolveComposerSendLabel({
314+
connectionState: props.connectionState,
315+
activeThreadBusy: props.activeThreadBusy,
316+
queueCount: props.queueCount,
317+
});
316318
const currentModelSelection = props.selectedThread.modelSelection;
317319
const currentRuntimeMode = props.selectedThread.runtimeMode;
318320
const currentInteractionMode = props.selectedThread.interactionMode ?? "default";
@@ -536,6 +538,20 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
536538
props.selectedThread.id,
537539
props.selectedThread.title,
538540
]);
541+
// Return now submits (queues-when-busy / sends-when-idle), matching desktop
542+
// Enter=submit. The newline button is the cross-platform multiline escape
543+
// hatch: it inserts "\n" at the caret through the same insert-at-selection
544+
// mechanism used for @file / skill tokens (replaceTextRange + selection).
545+
const handleInsertNewline = useCallback(() => {
546+
const result = replaceTextRange(
547+
draftMessage,
548+
composerSelection.start,
549+
composerSelection.end,
550+
"\n",
551+
);
552+
setComposerSelection({ start: result.cursor, end: result.cursor });
553+
onChangeDraftMessage(result.text);
554+
}, [composerSelection.start, composerSelection.end, draftMessage, onChangeDraftMessage]);
539555
const handleCommandSelect = useCallback(
540556
(item: ComposerCommandItem) => {
541557
if (!composerTrigger) return;
@@ -865,6 +881,12 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
865881
onPress={() => void props.onPickDraftImages()}
866882
showChevron={false}
867883
/>
884+
<ComposerToolbarButton
885+
accessibilityLabel="Insert line break"
886+
icon="return"
887+
onPress={handleInsertNewline}
888+
showChevron={false}
889+
/>
868890
<ControlPillMenu
869891
actions={modelMenuActions}
870892
onPressAction={({ nativeEvent }) => handleModelMenuAction(nativeEvent.event)}
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
@@ -71,6 +71,7 @@ interface NativeComposerEditorProps extends ViewProps {
7171
readonly onComposerPasteImages?: (event: NativePasteImagesEvent) => void;
7272
readonly onComposerFocus?: () => void;
7373
readonly onComposerBlur?: () => void;
74+
readonly onComposerSubmit?: () => void;
7475
}
7576

7677
const NativeView = requireNativeView<NativeComposerEditorProps>(NATIVE_MODULE_NAME);
@@ -95,6 +96,7 @@ export function ComposerEditor({
9596
onPasteImages,
9697
onFocus,
9798
onBlur,
99+
onSubmit,
98100
contentInsetVertical = 0,
99101
...props
100102
}: ComposerEditorProps) {
@@ -275,6 +277,7 @@ export function ComposerEditor({
275277
onComposerPasteImages={(event) => onPasteImages?.(event.nativeEvent.uris)}
276278
onComposerFocus={onFocus}
277279
onComposerBlur={onBlur}
280+
onComposerSubmit={onSubmit}
278281
/>
279282
);
280283
}

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