Files
pipelines/app/lib/features/chat/domain/chat_clipboard_paste_native.dart
T
alan 8138cdc3d9 Linux desktop: optimized release build, installer, Linux gaps
Build: split Dart debug info out of libapp.so (13.96 -> 11.27 MB, symbols
kept in the cache) and fix icon tree shaking, which Flutter 3.44's Linux
CMake backend silently disables by quoting the define; the build image
patches its SDK copy (MaterialIcons 1.6 MB -> 25 KB). Impeller cannot be
enabled in a Linux release build and was slower in a profile-build bench.

Install: `./install --linux --install` installs the bundle into
<prefix>/lib/app-linux with com.pipelines.app.desktop (the GTK/Wayland
app id), its icon and a ~/.local/bin/pipelines-app link, through
scripts/install-linux-app.sh.

Linux gaps: notifications through the freedesktop daemon; HTML artifacts
explain the missing web view instead of throwing; no Share for files;
a GTK clipboard reader for pasting files and screenshots (Ctrl+V and
Omarchy's Super+V); GTK header bar only on GNOME; window icon.

Bench: native fixture frame log, wheel/mark steps and
tool/perf/linux_desktop_bench.py (private Xvfb); results in
app/notes/linux-desktop-optimized.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:54:18 +00:00

129 lines
4.5 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'chat_attachment_picker.dart';
import 'chat_clipboard_paste.dart';
/// The desktop Runners' clipboard reader: macOS's
/// (macos/Runner/MainFlutterWindow.swift) and Linux's
/// (linux/runner/clipboard_files.cc).
const MethodChannel chatClipboardFilesChannel = MethodChannel(
'pipelines/clipboard_files',
);
/// Clipboard-file paste for a desktop host whose Runner answers
/// [chatClipboardFilesChannel].
///
/// Flutter's own [Clipboard] carries text only, so a native host reads the
/// pasteboard through the Runner. Each subscription watches for the platform
/// paste shortcut (Cmd+V on macOS, Ctrl+V on Linux). When its composer may take the paste it consumes
/// the key, asks the Runner for the pasteboard's files (Finder file URLs, else
/// a copied or screenshot image) and hands them over; a pasteboard without
/// files pastes its text into the composer instead, so the consumed key never
/// loses a text paste.
ChatClipboardPaste nativeChatClipboardPaste({
MethodChannel channel = chatClipboardFilesChannel,
}) => ChatClipboardPaste(
subscribe: (onFiles, canAccept, onError, onText, canAcceptText) {
var sequence = 0;
KeyEventResult handle(KeyEvent event) {
if (event is! KeyDownEvent ||
event.logicalKey != LogicalKeyboardKey.keyV ||
!chatIsNativePasteShortcut() ||
!canAccept()) {
return KeyEventResult.ignored;
}
unawaited(
_readPasteboard(channel, onFiles, onError, onText, () => ++sequence),
);
return KeyEventResult.handled;
}
FocusManager.instance.addEarlyKeyEventHandler(handle);
return () => FocusManager.instance.removeEarlyKeyEventHandler(handle);
},
);
/// Whether the held modifiers make V the host's paste shortcut: Cmd alone on
/// macOS; Ctrl on Linux, also with Super held (Omarchy's universal Super+V
/// sends Ctrl+V while Super is down).
bool chatIsNativePasteShortcut([TargetPlatform? platform]) {
final keys = HardwareKeyboard.instance;
if (keys.isAltPressed || keys.isShiftPressed) return false;
return (platform ?? defaultTargetPlatform) == TargetPlatform.linux
? keys.isControlPressed
: keys.isMetaPressed && !keys.isControlPressed;
}
Future<void> _readPasteboard(
MethodChannel channel,
ChatPasteFiles onFiles,
ChatPasteFailed onError,
ChatPasteText onText,
int Function() nextSequence,
) async {
Map<Object?, Object?>? reply;
try {
reply = await channel.invokeMapMethod<Object?, Object?>('read');
} catch (error) {
onError('Could not read pasted files: $error');
}
final files = <ChatPickedFile>[];
for (final raw in (reply?['files'] as List<Object?>?) ?? const []) {
if (raw is! Map) continue;
final mime = (raw['mime'] as String?) ?? '';
final recorded = ((raw['name'] as String?) ?? '').trim();
final bytes = raw['bytes'];
files.add(
ChatPickedFile(
name: recorded.isEmpty
? chatPastedAttachmentName(mime, nextSequence())
: recorded,
mime: mime,
bytes: bytes is Uint8List ? bytes : Uint8List(0),
),
);
}
final failures = [
for (final failure in (reply?['errors'] as List<Object?>?) ?? const [])
'$failure',
];
if (files.isNotEmpty) {
// As on web: kept files first, since adding clears an older error.
onFiles(files);
failures.forEach(onError);
return;
}
failures.forEach(onError);
if (failures.isNotEmpty) return;
try {
final text = (await Clipboard.getData(Clipboard.kTextPlain))?.text;
if (text != null && text.isNotEmpty) {
onText(text);
return;
}
} catch (_) {
onError('Could not read clipboard text.');
return;
}
// Neither files, an image nor text: say what the clipboard held rather
// than letting the paste do nothing.
final types = [
for (final type in (reply?['types'] as List<Object?>?) ?? const []) '$type',
];
if (reply != null) onError(chatNothingToPasteMessage(types));
}
/// The error for a paste whose clipboard held nothing the composer can take.
String chatNothingToPasteMessage(List<String> types) => types.isEmpty
? 'Nothing to paste: the clipboard is empty.'
: 'Nothing to paste: the clipboard holds ${types.take(6).join(', ')}.';
/// Whether [platform] has a Runner that answers [chatClipboardFilesChannel].
bool chatHostReadsClipboardFiles(TargetPlatform platform) =>
!kIsWeb &&
(platform == TargetPlatform.macOS || platform == TargetPlatform.linux);