Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sentry user to control resolution of captured Flutter screenshots #1288

Merged
merged 7 commits into from Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Features

- Allow sentry user to control resolution of captured Flutter screenshots ([#1288](https://github.com/getsentry/sentry-dart/pull/1288))
## 6.21.0

### Features
Expand Down
1 change: 1 addition & 0 deletions flutter/example/lib/main.dart
Expand Up @@ -51,6 +51,7 @@ Future<void> setupSentry(AppRunner appRunner, String dsn) async {
options.reportSilentFlutterErrors = true;
options.enableNdkScopeSync = true;
options.attachScreenshot = true;
options.screenshotQuality = SentryScreenshotQuality.low;
options.attachViewHierarchy = true;
// We can enable Sentry debug logging during development. This is likely
// going to log too much for your app, but can be useful when figuring out
Expand Down
1 change: 1 addition & 0 deletions flutter/lib/sentry_flutter.dart
Expand Up @@ -9,5 +9,6 @@ export 'src/flutter_sentry_attachment.dart';
export 'src/sentry_asset_bundle.dart';
export 'src/integrations/on_error_integration.dart';
export 'src/screenshot/sentry_screenshot_widget.dart';
export 'src/screenshot/sentry_screenshot_quality.dart';
export 'src/user_interaction/sentry_user_interaction_widget.dart';
export 'src/binding_wrapper.dart';
20 changes: 16 additions & 4 deletions flutter/lib/src/event_processor/screenshot_event_processor.dart
@@ -1,7 +1,10 @@
import 'dart:async';
import 'dart:math';
import 'dart:typed_data';
import 'dart:ui' as ui show ImageByteFormat;
import 'dart:ui';

import 'package:flutter/widgets.dart';
import 'package:sentry/sentry.dart';
import '../screenshot/sentry_screenshot_widget.dart';
import '../sentry_flutter_options.dart';
Expand Down Expand Up @@ -42,11 +45,10 @@ class ScreenshotEventProcessor extends EventProcessor {

Future<Uint8List?> _createScreenshot() async {
try {
final renderObject =
sentryScreenshotWidgetGlobalKey.currentContext?.findRenderObject();

final renderObject = sentryScreenshotWidgetGlobalKey.currentContext?.findRenderObject();
if (renderObject is RenderRepaintBoundary) {
final image = await renderObject.toImage(pixelRatio: 1);
final pixelRatio = window.devicePixelRatio;
var image = await renderObject.toImage(pixelRatio: pixelRatio);
// At the time of writing there's no other image format available which
// Sentry understands.

Expand All @@ -56,7 +58,17 @@ class ScreenshotEventProcessor extends EventProcessor {
return null;
}

final targetResolution = _options.screenshotQuality.targetResolution();
if (targetResolution != null) {
var ratioWidth = targetResolution / image.width;
var ratioHeight = targetResolution / image.height;
var ratio = min(ratioWidth, ratioHeight);
if (ratio > 0.0 && ratio < 1.0) {
image = await renderObject.toImage(pixelRatio: ratio * pixelRatio);
}
}
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);

final bytes = byteData?.buffer.asUint8List();
if (bytes?.isNotEmpty == true) {
return bytes;
Expand Down
19 changes: 19 additions & 0 deletions flutter/lib/src/screenshot/sentry_screenshot_quality.dart
@@ -0,0 +1,19 @@
enum SentryScreenshotQuality {
denrase marked this conversation as resolved.
Show resolved Hide resolved
full,
high,
medium,
low;

int? targetResolution() {
switch (this) {
case SentryScreenshotQuality.full:
return null; // Keep current scale
case SentryScreenshotQuality.high:
return 1920;
case SentryScreenshotQuality.medium:
return 1280;
case SentryScreenshotQuality.low:
return 854;
denrase marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
4 changes: 4 additions & 0 deletions flutter/lib/src/sentry_flutter_options.dart
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/widgets.dart';

import 'binding_wrapper.dart';
import 'renderer/renderer.dart';
import 'screenshot/sentry_screenshot_quality.dart';

/// This class adds options which are only availble in a Flutter environment.
/// Note that some of these options require native Sentry integration, which is
Expand Down Expand Up @@ -163,6 +164,9 @@ class SentryFlutterOptions extends SentryOptions {
/// The [SentryScreenshotWidget] has to be the root widget of the app.
bool attachScreenshot = false;

/// The quality of the attached screenshot
SentryScreenshotQuality screenshotQuality = SentryScreenshotQuality.high;

/// Enable or disable automatic breadcrumbs for User interactions Using [Listener]
///
/// Requires adding the [SentryUserInteractionWidget] to the widget tree.
Expand Down