screenshot method

  1. @override
Future<ScreenshotResult> screenshot(
  1. String path
)
override

Capture a PNG to path for device-mode perception; also the coordinate reference (ratio = pixel / size).

Implementation

@override
Future<ScreenshotResult> screenshot(String path) async {
  try {
    await hid.call({'cmd': 'screenshot', 'path': path}, timeout: const Duration(seconds: 10));
    final size = pngSize(path);
    if (size != null) return ScreenshotResult(path: path, width: size.$1, height: size.$2);
  } on DeviceHidError {
    // devicectl below still works without the helper
  }
  final ProcessResult r;
  try {
    r = await runCapped(
      run,
      'xcrun',
      ['devicectl', 'device', 'capture', 'screenshot', '--device', udid, '--destination', path, '-q'],
      timeout: const Duration(seconds: 15),
      phase: 'devicectl screenshot',
    );
  } on Object catch (e) {
    return ScreenshotResult(error: 'could not run devicectl: $e');
  }
  final file = File(path);
  if (r.exitCode != 0 || !file.existsSync()) {
    return ScreenshotResult(error: 'devicectl could not capture the screen: ${'${r.stderr}'.trim()}');
  }
  final size = pngSize(path);
  return ScreenshotResult(path: path, width: size?.$1, height: size?.$2);
}