Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

desktop: two spaces in a terminal no longer turn into ". "

0xken153a7ee449f9c1
diff --git a/desktop/test/terminal_view_interaction_test.dart b/desktop/test/terminal_view_interaction_test.dart
index 704f29993..551480aea 100644
--- a/desktop/test/terminal_view_interaction_test.dart
+++ b/desktop/test/terminal_view_interaction_test.dart
@@ -64,6 +64,99 @@ void main() {
}),
);
+ group('macOS "Add period with double-space"', () {
+ Future<List<String>> mount(WidgetTester tester) async {
+ final terminal = Terminal(maxLines: 20)..resize(80, 4);
+ final output = <String>[];
+ terminal.onOutput = output.add;
+ await tester.pumpWidget(
+ MaterialApp(
+ home: SizedBox(
+ width: 800,
+ height: 200,
+ child: TerminalView(terminal, autofocus: true),
+ ),
+ ),
+ );
+ await tester.pump();
+ return output;
+ }
+
+ void native(WidgetTester tester, String text, {TextRange? composing}) {
+ tester.testTextInput.updateEditingValue(
+ TextEditingValue(
+ text: text,
+ selection: TextSelection.collapsed(offset: text.length),
+ composing: composing ?? TextRange.empty,
+ ),
+ );
+ }
+
+ // The second press never arrives as a space: macOS asks the input client to replace the
+ // first one with ". ", which reached the pty as a Backspace and ". ". So the space bar is
+ // typed straight into the terminal and never reaches the input method.
+ testWidgets(
+ 'the space bar is typed directly, so two presses stay two spaces',
+ (tester) async {
+ final output = await mount(tester);
+ native(tester, 'a');
+ await tester.pump();
+ for (var press = 0; press < 2; press++) {
+ expect(
+ await tester.sendKeyEvent(
+ LogicalKeyboardKey.space,
+ character: ' ',
+ platform: 'macos',
+ ),
+ isTrue,
+ );
+ }
+ // The native buffer follows, so the next letter the input method inserts diffs cleanly.
+ expect(tester.testTextInput.editingState?['text'], 'a ');
+ native(tester, 'a b');
+ await tester.pump();
+ expect(output.join(), 'a b');
+ },
+ variant: TargetPlatformVariant.only(TargetPlatform.macOS),
+ );
+
+ testWidgets(
+ 'a space that commits a composition still goes to the input method',
+ (tester) async {
+ final output = await mount(tester);
+ native(tester, 'ni', composing: const TextRange(start: 0, end: 2));
+ await tester.pump();
+ expect(
+ await tester.sendKeyEvent(
+ LogicalKeyboardKey.space,
+ character: ' ',
+ platform: 'macos',
+ ),
+ isFalse,
+ );
+ expect(output, isEmpty);
+ },
+ variant: TargetPlatformVariant.only(TargetPlatform.macOS),
+ );
+
+ testWidgets(
+ 'other platforms leave the space bar to the input method',
+ (tester) async {
+ final output = await mount(tester);
+ expect(
+ await tester.sendKeyEvent(
+ LogicalKeyboardKey.space,
+ character: ' ',
+ platform: 'linux',
+ ),
+ isFalse,
+ );
+ expect(output, isEmpty);
+ },
+ variant: TargetPlatformVariant.only(TargetPlatform.linux),
+ );
+ });
+
testWidgets(
'an observed terminal exposes no accessibility editing action',
(tester) async {
diff --git a/desktop/third_party/xterm/lib/src/terminal_view.dart b/desktop/third_party/xterm/lib/src/terminal_view.dart
index c76a4c582..50fcc7e9a 100644
--- a/desktop/third_party/xterm/lib/src/terminal_view.dart
+++ b/desktop/third_party/xterm/lib/src/terminal_view.dart
@@ -594,6 +594,25 @@ class TerminalViewState extends State<TerminalView> {
_scrollToBottom();
return KeyEventResult.handled;
}
+ // macOS's "Add period with double-space" (Keyboard › Text Input, on by
+ // default) acts on the input method's side: the second press does not
+ // insert a space, it asks the client to REPLACE the first one with ". ".
+ // A prompt is not prose, and neither the input configuration nor the app's
+ // own defaults turn it off (both measured). So the space bar never reaches
+ // the input method here: it is typed straight into the buffer and the pty.
+ // Unless something is being composed — a Telex word, a CJK candidate, a
+ // dead key — where the space belongs to the IME, which commits with it.
+ final editor = _customTextEditKey.currentState;
+ if (defaultTargetPlatform == TargetPlatform.macOS &&
+ event.character == ' ' &&
+ !reservesTerminalKey &&
+ !HardwareKeyboard.instance.isAltPressed &&
+ !isComposing &&
+ editor != null) {
+ editor.insertTyped(' ');
+ return KeyEventResult.handled;
+ }
+
if (isTextInput && !reservesTerminalKey) {
// Do not let another Flutter shortcut consume this before macOS gets a
// chance to update the native text-input client.
diff --git a/desktop/third_party/xterm/lib/src/ui/custom_text_edit.dart b/desktop/third_party/xterm/lib/src/ui/custom_text_edit.dart
index 4e92c8687..2e0f683f8 100644
--- a/desktop/third_party/xterm/lib/src/ui/custom_text_edit.dart
+++ b/desktop/third_party/xterm/lib/src/ui/custom_text_edit.dart
@@ -161,6 +161,27 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
_connection?.setEditingState(value);
}
+ /// Types [text] at the cursor without going through the platform's input
+ /// method: the native buffer is updated to match and the terminal receives
+ /// it, exactly as if the input client had inserted it.
+ ///
+ /// For a key the terminal must not let the input method see — on macOS the
+ /// space bar, whose second press "Add period with double-space" turns into
+ /// a REPLACEMENT of the first space with ". " (`TerminalView`). Never call
+ /// while composing: marked text belongs to the IME.
+ void insertTyped(String text) {
+ final value = _currentEditingState;
+ final selection = value.selection.isValid
+ ? value.selection
+ : TextSelection.collapsed(offset: value.text.length);
+ final next = value.replaced(selection, text);
+ _cancelPendingDeletes();
+ _currentEditingState = next;
+ _semanticEditingState.value = next;
+ _connection?.setEditingState(next);
+ _syncTerminalText(next.text);
+ }
+
/// Clears the native input buffer after the terminal accepts a command.
/// It intentionally stays intact between ordinary key presses: Vietnamese
/// Telex needs the preceding `u` available to convert it into `ư`.
@@ -236,7 +257,9 @@ class CustomTextEditState extends State<CustomTextEdit> with TextInputClient {
// letters instead of `hôm`; a CJK candidate window dies the same way.
// A desktop IME composes through marked text, which neither flag
// touches, so those platforms keep the strict config — a terminal has
- // no business autocorrecting a command.
+ // no business autocorrecting a command. ⚠️ Flutter's macOS input plugin
+ // reads none of these flags, so on macOS they protect nothing; see
+ // [insertTyped] for the one substitution that is kept away instead.
//
// iOS has no finer knob: that one `autocorrect` gates its autocorrection
// AND its Telex conversion. Android's composing hangs off