Skip to content

Commit f474af0

Browse files
PR Batch Testert3dotggclaude
committed
fix(mobile): Android sends with Return on a hardware keyboard
The Android composer had no hardware Return handling, so Return always inserted a newline and no chord could send. It now follows the Return key setting like iOS, with Ctrl in place of Command. Soft keyboards still insert a newline. Settings > Keyboard now shows on Android. Ports the composer part of #15030. Co-authored-by: Theo Browne <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1451e7c commit f474af0

8 files changed

Lines changed: 194 additions & 12 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ internal object T3ComposerClipboard {
6262
}
6363

6464
class T3ComposerEditorModule : Module() {
65+
// A flat list of the view's props and events.
66+
@Suppress("LongMethod")
6567
override fun definition() = ModuleDefinition {
6668
Name("T3ComposerEditor")
6769

@@ -104,6 +106,12 @@ class T3ComposerEditorModule : Module() {
104106
Prop("readOnly") { view: T3ComposerEditorView, readOnly: Boolean ->
105107
view.setReadOnly(readOnly)
106108
}
109+
Prop("enterBehavior") { view: T3ComposerEditorView, behavior: String ->
110+
view.setEnterBehavior(behavior)
111+
}
112+
Prop("submitEnabled") { view: T3ComposerEditorView, enabled: Boolean ->
113+
view.setSubmitEnabled(enabled)
114+
}
107115
Prop("scrollEnabled") { view: T3ComposerEditorView, scrollEnabled: Boolean ->
108116
view.setScrollEnabled(scrollEnabled)
109117
}
@@ -128,6 +136,7 @@ class T3ComposerEditorModule : Module() {
128136
"onComposerSelectionChange",
129137
"onComposerFocus",
130138
"onComposerBlur",
139+
"onComposerSubmit",
131140
"onComposerPasteImages",
132141
"onComposerContextPress",
133142
"onComposerPasteContext",

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import android.text.style.ReplacementSpan
1818
import android.util.TypedValue
1919
import android.view.Gravity
2020
import android.view.GestureDetector
21+
import android.view.KeyCharacterMap
2122
import android.view.MotionEvent
2223
import android.view.KeyEvent
2324
import android.view.ViewGroup
@@ -49,6 +50,7 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
4950
private val onComposerSelectionChange by EventDispatcher()
5051
private val onComposerFocus by EventDispatcher()
5152
private val onComposerBlur by EventDispatcher()
53+
private val onComposerSubmit by EventDispatcher()
5254
private val onComposerPasteImages by EventDispatcher()
5355
private val onComposerContextPress by EventDispatcher()
5456
private val onComposerPasteContext by EventDispatcher()
@@ -145,6 +147,9 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
145147
),
146148
)
147149
}
150+
editor.submitListener = { alternate ->
151+
onComposerSubmit(mapOf("alternate" to alternate))
152+
}
148153
editor.setOnFocusChangeListener { _, hasFocus ->
149154
if (hasFocus) {
150155
onComposerFocus(emptyMap<String, Any>())
@@ -312,6 +317,14 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
312317
editor.isCursorVisible = editor.isEnabled && !readOnly
313318
}
314319

320+
fun setEnterBehavior(behavior: String) {
321+
editor.returnSends = behavior != "newline"
322+
}
323+
324+
fun setSubmitEnabled(enabled: Boolean) {
325+
editor.submitEnabled = enabled
326+
}
327+
315328
fun setScrollEnabled(scrollEnabled: Boolean) {
316329
editor.isVerticalScrollBarEnabled = scrollEnabled
317330
}
@@ -588,10 +601,19 @@ internal class SelectionAwareEditText(context: Context) : EditText(context) {
588601
var pasteImagesListener: ((List<String>) -> Unit)? = null
589602
var pasteContextListener: ((Map<String, String>) -> Unit)? = null
590603
var pasteTextListener: ((String, Int, Int) -> Unit)? = null
604+
605+
/** Called for a hardware-keyboard send chord. The argument is true for the alternate send. */
606+
var submitListener: ((Boolean) -> Unit)? = null
591607
var textPasteThresholdBytes = 0
592608
var maxInputChars = Int.MAX_VALUE
593609
var clipboardFragment = ""
594610

611+
/** Whether a hardware Return sends (`enterBehavior` "send") or inserts a newline. */
612+
var returnSends = true
613+
614+
/** The JS wrapper enables submission only when it has an onSubmit handler. */
615+
var submitEnabled = false
616+
595617
/**
596618
* Placeholder shown while the draft is empty. An editable TextView never ellipsizes its hint,
597619
* so a long placeholder wraps once a wide system font or a large font scale (Samsung defaults)
@@ -644,7 +666,38 @@ internal class SelectionAwareEditText(context: Context) : EditText(context) {
644666
return spans.isNotEmpty()
645667
}
646668

669+
/**
670+
* Maps a hardware Return chord to a send, like the iOS composer. The plainer chord does the
671+
* configured behavior and the Ctrl chord does the other one: with "send", Return sends,
672+
* Ctrl-Return sends the alternate way and Shift-Return inserts a newline; with "newline",
673+
* Ctrl-Return sends and Ctrl-Shift-Return sends the alternate way. Returns whether the send is
674+
* the alternate one, or null to insert a newline. Soft keyboards always insert a newline.
675+
*/
676+
private fun submitChord(keyCode: Int, event: KeyEvent): Boolean? {
677+
val isReturn = keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_NUMPAD_ENTER
678+
val fromSoftKeyboard =
679+
event.deviceId == KeyCharacterMap.VIRTUAL_KEYBOARD ||
680+
(event.flags and KeyEvent.FLAG_SOFT_KEYBOARD) != 0
681+
val canSubmit = submitEnabled && submitListener != null && !readOnly
682+
if (!isReturn || fromSoftKeyboard || !canSubmit) {
683+
return null
684+
}
685+
return when {
686+
event.hasNoModifiers() -> if (returnSends) false else null
687+
event.hasModifiers(KeyEvent.META_CTRL_ON) -> returnSends
688+
event.hasModifiers(KeyEvent.META_CTRL_ON or KeyEvent.META_SHIFT_ON) ->
689+
if (returnSends) null else true
690+
else -> null
691+
}
692+
}
693+
647694
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
695+
val alternate = submitChord(keyCode, event)
696+
if (alternate != null) {
697+
// A held Return repeats. Send once, and drop the repeats instead of typing newlines.
698+
if (event.repeatCount == 0) submitListener?.invoke(alternate)
699+
return true
700+
}
648701
val handled = when (keyCode) {
649702
KeyEvent.KEYCODE_DEL -> deleteChip(true)
650703
KeyEvent.KEYCODE_FORWARD_DEL -> deleteChip(false)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package expo.modules.t3composereditor
2+
3+
import android.text.InputType
4+
import android.view.KeyCharacterMap
5+
import android.view.KeyEvent
6+
import org.junit.Assert.assertEquals
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.robolectric.RobolectricTestRunner
10+
import org.robolectric.RuntimeEnvironment
11+
import org.robolectric.annotation.Config
12+
13+
@RunWith(RobolectricTestRunner::class)
14+
@Config(sdk = [36], manifest = Config.NONE)
15+
class ComposerSubmitTest {
16+
private val sends = mutableListOf<Boolean>()
17+
private val editor = SelectionAwareEditText(RuntimeEnvironment.getApplication()).apply {
18+
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
19+
submitEnabled = true
20+
submitListener = { alternate -> sends += alternate }
21+
}
22+
23+
private fun pressReturn(
24+
metaState: Int = 0,
25+
deviceId: Int = HARDWARE_KEYBOARD,
26+
repeatCount: Int = 0
27+
) {
28+
val event = KeyEvent(
29+
0,
30+
0,
31+
KeyEvent.ACTION_DOWN,
32+
KeyEvent.KEYCODE_ENTER,
33+
repeatCount,
34+
metaState,
35+
deviceId,
36+
0,
37+
)
38+
editor.onKeyDown(KeyEvent.KEYCODE_ENTER, event)
39+
}
40+
41+
@Test
42+
fun returnSendsAndCtrlReturnSendsTheAlternateWay() {
43+
pressReturn()
44+
pressReturn(KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON)
45+
46+
assertEquals(listOf(false, true), sends)
47+
assertEquals("", editor.text.toString())
48+
}
49+
50+
@Test
51+
fun shiftReturnAndSoftKeyboardReturnInsertNewlines() {
52+
pressReturn(KeyEvent.META_SHIFT_ON or KeyEvent.META_SHIFT_LEFT_ON)
53+
pressReturn(deviceId = KeyCharacterMap.VIRTUAL_KEYBOARD)
54+
55+
assertEquals(emptyList<Boolean>(), sends)
56+
assertEquals("\n\n", editor.text.toString())
57+
}
58+
59+
@Test
60+
fun newlineBehaviorSendsOnlyWithCtrl() {
61+
editor.returnSends = false
62+
63+
pressReturn()
64+
pressReturn(KeyEvent.META_CTRL_ON)
65+
pressReturn(KeyEvent.META_CTRL_ON or KeyEvent.META_SHIFT_ON)
66+
67+
assertEquals(listOf(false, true), sends)
68+
assertEquals("\n", editor.text.toString())
69+
}
70+
71+
@Test
72+
fun returnInsertsANewlineWhenSubmissionIsUnavailable() {
73+
editor.submitEnabled = false
74+
75+
pressReturn()
76+
77+
assertEquals(emptyList<Boolean>(), sends)
78+
assertEquals("\n", editor.text.toString())
79+
}
80+
81+
@Test
82+
fun returnInsertsANewlineWithoutASubmitListener() {
83+
editor.submitListener = null
84+
85+
pressReturn()
86+
87+
assertEquals(emptyList<Boolean>(), sends)
88+
assertEquals("\n", editor.text.toString())
89+
}
90+
91+
@Test
92+
fun heldReturnSendsOnce() {
93+
pressReturn()
94+
pressReturn(repeatCount = 1)
95+
pressReturn(repeatCount = 2)
96+
97+
assertEquals(listOf(false), sends)
98+
assertEquals("", editor.text.toString())
99+
}
100+
101+
private companion object {
102+
const val HARDWARE_KEYBOARD = 1
103+
}
104+
}

‎apps/mobile/src/features/settings/SettingsKeyboardRouteScreen.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/
1616
import { SettingsChoiceRow } from "./components/SettingsChoiceRow";
1717
import { SettingsSection } from "./components/SettingsSection";
1818

19+
// Android keyboards send with Ctrl where iPad keyboards use Command.
20+
const SEND_MODIFIER = Platform.OS === "android" ? "Ctrl" : "Command";
21+
1922
const ENTER_BEHAVIOR_OPTIONS: ReadonlyArray<{
2023
readonly behavior: ComposerEnterBehavior;
2124
readonly label: string;
@@ -29,7 +32,7 @@ const ENTER_BEHAVIOR_OPTIONS: ReadonlyArray<{
2932
{
3033
behavior: "newline",
3134
label: "Insert new line",
32-
description: "Return inserts a new line. Command-Return sends the message.",
35+
description: `Return inserts a new line. ${SEND_MODIFIER}-Return sends the message.`,
3336
},
3437
];
3538

‎apps/mobile/src/features/settings/SettingsRouteScreen.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,9 @@ function SettingsIndexSections() {
154154
<>
155155
<SettingsSection title="Interface">
156156
<SettingsRow icon="paintbrush" label="Appearance" target="SettingsAppearance" />
157+
<SettingsRow icon="keyboard" label="Keyboard" target="SettingsKeyboard" />
157158
{Platform.OS === "ios" ? (
158-
<>
159-
<SettingsRow icon="keyboard" label="Keyboard" target="SettingsKeyboard" />
160-
<SettingsRow icon="mic" label="Microphone" target="SettingsMicrophone" />
161-
</>
159+
<SettingsRow icon="mic" label="Microphone" target="SettingsMicrophone" />
162160
) : null}
163161
</SettingsSection>
164162

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES } from "@t3tools/client-runtime/
22
import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts";
33
import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";
44
import { composerContextEditorTokens } from "../lib/composerContext";
5+
import { DEFAULT_COMPOSER_ENTER_BEHAVIOR } from "../lib/composerEnterBehavior";
56
import { requireNativeView } from "expo";
67
import { TextInputWrapper } from "expo-paste-input";
78
import {
@@ -84,6 +85,8 @@ interface NativeComposerEditorProps extends ViewProps {
8485
readonly autoFocus: boolean;
8586
readonly autoCorrect: boolean;
8687
readonly spellCheck: boolean;
88+
readonly enterBehavior: string;
89+
readonly submitEnabled: boolean;
8790
readonly onComposerChange: (event: NativeEditorEvent) => void;
8891
readonly onComposerSelectionChange?: (event: NativeSelectionEvent) => void;
8992
readonly onComposerPasteImages?: (event: NativePasteImagesEvent) => void;
@@ -98,6 +101,7 @@ interface NativeComposerEditorProps extends ViewProps {
98101
readonly onComposerPasteText?: (event: NativePasteTextEvent) => void;
99102
readonly onComposerFocus?: () => void;
100103
readonly onComposerBlur?: () => void;
104+
readonly onComposerSubmit?: (event: NativeSyntheticEvent<{ alternate: boolean }>) => void;
101105
}
102106

103107
const NativeView = requireNativeView<NativeComposerEditorProps>(NATIVE_MODULE_NAME);
@@ -123,6 +127,7 @@ export function ComposerEditor({
123127
onPasteText,
124128
onFocus,
125129
onBlur,
130+
onSubmit,
126131
contentInsetVertical = 0,
127132
...props
128133
}: ComposerEditorProps) {
@@ -289,6 +294,8 @@ export function ComposerEditor({
289294
autoFocus={props.autoFocus ?? false}
290295
autoCorrect={props.autoCorrect ?? true}
291296
spellCheck={props.spellCheck ?? true}
297+
enterBehavior={props.enterBehavior ?? DEFAULT_COMPOSER_ENTER_BEHAVIOR}
298+
submitEnabled={onSubmit !== undefined}
292299
textPasteThresholdBytes={onPasteText ? PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES : 0}
293300
maxInputChars={PROVIDER_SEND_TURN_MAX_INPUT_CHARS}
294301
style={{ flex: 1, minHeight: 0 }}
@@ -357,6 +364,11 @@ export function ComposerEditor({
357364
}}
358365
onComposerFocus={onFocus}
359366
onComposerBlur={onBlur}
367+
onComposerSubmit={
368+
onSubmit === undefined
369+
? undefined
370+
: (event) => onSubmit(event.nativeEvent.alternate === true)
371+
}
360372
/>
361373
</TextInputWrapper>
362374
);

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,13 @@ export interface ComposerEditorProps {
6565
readonly onPasteText?: (paste: ComposerTextPaste) => void;
6666
readonly onFocus?: () => void;
6767
readonly onBlur?: () => void;
68-
/**
69-
* Hardware-keyboard Return behavior on iOS. No-op on Android, which has no
70-
* hardware Return handling.
71-
*/
68+
/** Hardware-keyboard Return behavior. Soft keyboards always insert a newline. */
7269
readonly enterBehavior?: ComposerEnterBehavior;
7370
readonly submitTitle?: string;
7471
readonly alternateSubmitTitle?: string;
7572
/**
76-
* `alternate` is true for the Command-modified chord, which sends using the
77-
* opposite of the configured follow-up behavior. Always false on Android,
78-
* which has no hardware Return handling.
73+
* `alternate` is true for the Command-modified chord (Ctrl on Android),
74+
* which sends using the opposite of the configured follow-up behavior.
7975
*/
8076
readonly onSubmit?: (alternate: boolean) => void;
8177
}

‎docs/user/keybindings.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ In the composer, Return sends and `Shift+Return` inserts a new line. `Cmd+Return
7575
also sends. To make Return insert a new line instead, change the Return key
7676
behavior in Settings → Keyboard.
7777

78+
## Android
79+
80+
With a hardware keyboard, the composer works like the iPad one with `Ctrl` in
81+
place of `Cmd`: Return sends, `Shift+Return` inserts a new line, and
82+
`Ctrl+Return` also sends. The on-screen keyboard's Return always inserts a new
83+
line. Change the Return key behavior in Settings → Keyboard.
84+
7885
## Edit the configuration file
7986

8087
Keybindings live on the environment's machine, in

0 commit comments

Comments
 (0)