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.

3.5 KB
Raw Blame
import 'dart:io';

import 'package:flutter/services.dart';

/// Flutter's own `Clipboard` (package:flutter/services.dart) only ever exposes `text/plain` — see
/// `phone/terminal_paste.dart`'s `pasteClipboard()`. Reading a real IMAGE off the clipboard (a
/// screenshot, "Copy Image" from a browser, ...) needs a native round trip instead: NSPasteboard on
/// macOS (`macos/Runner/MainFlutterWindow.swift`), the GTK clipboard on Linux
/// (`linux/runner/my_application.cc`), UIPasteboard on iOS (`ios/Runner/AppDelegate.swift`, read
/// only), ClipboardManager on Android (`android/app/src/main/kotlin/.../MainActivity.kt`, read
/// only). All answer over the same channel name and method, so this wrapper is the one place call
/// sites need to know about.
class NativeClipboard {
  NativeClipboard._();

  static const MethodChannel _channel = MethodChannel(
    'harness/clipboard_image',
  );

  /// Reads the system clipboard for an image, returned as PNG bytes.
  ///
  /// EMPTY bytes (iOS, Android) mean an image IS on the clipboard but could not be re-encoded, so a caller
  /// can say "unreadable" rather than "nothing there". Every other call site already treats empty
  /// as "no image".
  ///
  /// Returns `null` on any platform without a native handler for this channel (Windows — the
  /// runner is unexercised, see CLAUDE.md) or when the clipboard genuinely holds no image, so call
  /// sites can use one check to fall through to today's text-paste behavior either way.
  static Future<Uint8List?> readImagePng() async {
    if (Platform.isWindows || Platform.isFuchsia) return null;
    try {
      final bytes = await _channel.invokeMethod<Uint8List>('readImagePng');
      return bytes;
    } on MissingPluginException {
      return null;
    } catch (_) {
      return null;
    }
  }

  /// Whether the system clipboard holds an image, asked without reading it — so iOS shows no
  /// "Allow Paste" prompt and Android no "pasted from your clipboard" toast, and it can be asked as
  /// often as the key strip's `paste` needs to know whether to show (`phone/terminal_key_bar.dart`).
  ///
  /// False on any platform without a native handler for it (iOS and Android only), or when the
  /// question cannot be answered.
  static Future<bool> hasImage() async {
    if (!Platform.isIOS && !Platform.isAndroid) return false;
    try {
      return await _channel.invokeMethod<bool>('hasImage') ?? false;
    } on MissingPluginException {
      return false;
    } catch (_) {
      return false;
    }
  }

  /// Writes `pngBytes` onto the system clipboard, replacing whatever was there — the LOCAL half
  /// of native image drag-drop (see `_dropImage` in `widgets/pane_grid.dart`): when the pane's
  /// machine is this same computer, the app puts the bytes on ITS OWN clipboard directly instead
  /// of sending them over the terminal wire, then forwards a Ctrl+V so the engine reads them
  /// exactly as it already does for an ordinary local clipboard paste.
  ///
  /// Returns `false` on any platform without a native handler for this channel, or when the
  /// native side could not decode/write the bytes — callers should treat that as "could not do
  /// the local shortcut" rather than surfacing a crash.
  static Future<bool> writeImagePng(Uint8List pngBytes) async {
    if (!Platform.isMacOS && !Platform.isLinux) return false;
    try {
      final wrote = await _channel.invokeMethod<bool>(
        'writeImagePng',
        pngBytes,
      );
      return wrote ?? false;
    } on MissingPluginException {
      return false;
    } catch (_) {
      return false;
    }
  }
}