add file/image paste to the Claude composer via native clipboard

Flutter's clipboard is text-only and tmux/send-keys carry text only, so
a pasted file or image must reach Claude as an @path reference (per the
T-134 spike). A native clide/clipboard MethodChannel reads the non-text
clipboard: GTK (gtk_clipboard_wait_for_image/uris) on Linux, NSPasteboard
on macOS. The composer overrides PasteTextIntent — Ctrl/Cmd+V resolves a
file path or writes a clipboard image to a cache dir, inserts the @path,
and falls back to plain-text paste otherwise. No new package dependency.

macOS handler is written but unverified on this Linux box — needs a build
on a Mac. Linux path builds and is covered by tests.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-22 23:36:18 +02:00
co-authored by Claude Opus 4.7
parent b3fa69c982
commit 8fd251ac48
7 changed files with 314 additions and 0 deletions
@@ -79,5 +79,32 @@ void main() {
await pump(tester, enabled: false);
expect(tester.widget<EditableText>(find.byType(EditableText)).readOnly, isTrue);
});
testWidgets('Ctrl+V routes through the paste resolver and inserts its @path', (tester) async {
// WidgetsApp provides DefaultTextEditingShortcuts in the real app;
// the bare harness doesn't, so wrap explicitly to map Ctrl+V ->
// PasteTextIntent, which the composer's Actions override intercepts.
await tester.pumpWidget(harness(
f,
DefaultTextEditingShortcuts(
child: ClaudeComposer(
onSubmit: (_) {},
pasteResolver: () async => '@/tmp/shot.png',
),
),
));
tester.widget<EditableText>(find.byType(EditableText)).focusNode.requestFocus();
await tester.pump();
await tester.sendKeyDownEvent(LogicalKeyboardKey.control);
await tester.sendKeyEvent(LogicalKeyboardKey.keyV);
await tester.sendKeyUpEvent(LogicalKeyboardKey.control);
await tester.pumpAndSettle();
expect(
tester.widget<EditableText>(find.byType(EditableText)).controller.text,
'@/tmp/shot.png',
);
});
});
}
@@ -0,0 +1,92 @@
/// Tests for clipboard paste resolution (T-138): files become `@path`
/// tokens, a raw image is written to a temp file and referenced by
/// `@path`, and an empty clipboard falls back to text (null). Also
/// covers the NativeClipboard channel client's decode + missing-handler
/// guard.
library;
import 'dart:io';
import 'package:clide/builtin/claude/src/clipboard_paste.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
class _FakeSource implements ClipboardSource {
_FakeSource({this.files = const [], this.image});
final List<String> files;
final Uint8List? image;
@override
Future<List<String>> readFiles() async => files;
@override
Future<Uint8List?> readImage() async => image;
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('resolveClipboardAttachment', () {
test('files become space-joined @path tokens (no temp file)', () async {
final source = _FakeSource(files: ['/home/u/a.txt', '/home/u/b.png']);
final result = await resolveClipboardAttachment(source);
expect(result, '@/home/u/a.txt @/home/u/b.png');
});
test('a raw image is written to a temp file and referenced by @path', () async {
final dir = await Directory.systemTemp.createTemp('clide-paste-test-');
addTearDown(() => dir.delete(recursive: true));
final bytes = Uint8List.fromList([0x89, 0x50, 0x4e, 0x47, 1, 2, 3]);
final fixedNow = DateTime.fromMillisecondsSinceEpoch(1700000000000);
final result = await resolveClipboardAttachment(
_FakeSource(image: bytes),
tempDir: dir,
now: () => fixedNow,
);
final expectedPath = '${dir.path}/paste-1700000000000.png';
expect(result, '@$expectedPath');
expect(await File(expectedPath).readAsBytes(), bytes);
});
test('files take precedence over an image', () async {
final result = await resolveClipboardAttachment(
_FakeSource(files: ['/x/y'], image: Uint8List.fromList([1, 2])),
);
expect(result, '@/x/y');
});
test('empty clipboard returns null (fall back to text)', () async {
expect(await resolveClipboardAttachment(_FakeSource()), isNull);
});
});
group('NativeClipboard', () {
const channel = MethodChannel('clide/clipboard');
final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
tearDown(() => messenger.setMockMethodCallHandler(channel, null));
test('readFiles decodes the native string list', () async {
messenger.setMockMethodCallHandler(channel, (call) async {
expect(call.method, 'readFiles');
return <String>['/a/b', '/c/d'];
});
expect(await const NativeClipboard().readFiles(), ['/a/b', '/c/d']);
});
test('readImage decodes native bytes', () async {
final bytes = Uint8List.fromList([1, 2, 3, 4]);
messenger.setMockMethodCallHandler(channel, (call) async {
expect(call.method, 'readImage');
return bytes;
});
expect(await const NativeClipboard().readImage(), bytes);
});
test('missing platform handler degrades to empty / null', () async {
// No mock handler registered -> MissingPluginException, swallowed.
expect(await const NativeClipboard().readFiles(), isEmpty);
expect(await const NativeClipboard().readImage(), isNull);
});
});
}