capture method

Future<CockpitCapturedScreenshot> capture({
  1. required GlobalKey<State<StatefulWidget>> repaintBoundaryKey,
  2. required CockpitScreenshotRequest request,
  3. CockpitSnapshot? snapshot,
  4. double pixelRatio = 1.0,
})

Implementation

Future<CockpitCapturedScreenshot> capture({
  required GlobalKey repaintBoundaryKey,
  required CockpitScreenshotRequest request,
  CockpitSnapshot? snapshot,
  double pixelRatio = 1.0,
}) async {
  if (SchedulerBinding.instance.schedulerPhase != SchedulerPhase.idle) {
    await WidgetsBinding.instance.endOfFrame;
  }

  final context = repaintBoundaryKey.currentContext;
  if (context == null) {
    throw StateError('CockpitSurface capture boundary is not mounted.');
  }

  final boundary = context.findRenderObject();
  if (boundary is! RenderRepaintBoundary) {
    throw StateError(
      'CockpitSurface capture boundary is not a RepaintBoundary.',
    );
  }

  final image = await boundary.toImage(pixelRatio: pixelRatio);
  final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  image.dispose();

  if (byteData == null) {
    throw StateError('Failed to encode CockpitSurface capture as PNG.');
  }

  final bytes = byteData.buffer.asUint8List(
    byteData.offsetInBytes,
    byteData.lengthInBytes,
  );
  if (bytes.isEmpty) {
    throw StateError('CockpitSurface capture encoded an empty PNG.');
  }

  return CockpitCapturedScreenshot(
    artifact: CockpitArtifactRef(
      role: 'screenshot',
      relativePath: cockpitScreenshotRelativePathFor(request),
    ),
    bytes: bytes,
    snapshot: snapshot,
  );
}