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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: DisplayInfo now captured correctly on iOS and Mac Catalyst #3521

Merged
merged 6 commits into from
Aug 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Unable to load DLL sentry-native or one of its dependencies
- On mobile devices, the SDK no longer throws a `FormatException` when trying to report native events ([#3485](https://github.com/getsentry/sentry-dotnet/pull/3485))
- Race condition in `SentryMessageHandler` ([#3477](https://github.com/getsentry/sentry-dotnet/pull/3477))
- Decrease runtime diagnostics circular buffer when profiling, reducing memory usage ([#3491](https://github.com/getsentry/sentry-dotnet/pull/3491))
- DisplayInfo now captured correctly on iOS and Mac Catalyst on non-UI threads ([#3521](https://github.com/getsentry/sentry-dotnet/pull/3521))

### Dependencies

Expand Down
46 changes: 34 additions & 12 deletions src/Sentry.Maui/Internal/MauiDeviceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,23 @@ public static void ApplyMauiDeviceData(this Device device, IDiagnosticLogger? lo
logger?.LogDebug("No permission to read network state from the device.");
}

// https://docs.microsoft.com/dotnet/maui/platform-integration/device/display
var display = DeviceDisplay.MainDisplayInfo;
device.ScreenResolution ??= $"{(int)display.Width}x{(int)display.Height}";
device.ScreenDensity ??= (float)display.Density;
device.Orientation ??= display.Orientation switch
#if MACCATALYST || IOS
if (MainThread.IsMainThread)
{
DisplayOrientation.Portrait => DeviceOrientation.Portrait,
DisplayOrientation.Landscape => DeviceOrientation.Landscape,
_ => null
};
// device.ScreenDpi ??= ?
// ? = display.RefreshRate;
// ? = display.Rotation;
CaptureDisplayInfo();
}
else
{
// On iOS and Mac Catalyst, Accessing DeviceDisplay.Current must be done on the UI thread or else an
// exception will be thrown.
// See https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/device/display?view=net-maui-8.0&tabs=macios#platform-differences
var resetEvent = new ManualResetEventSlim(false);
jamescrosswell marked this conversation as resolved.
Show resolved Hide resolved
MainThread.BeginInvokeOnMainThread(() =>CaptureDisplayInfo(resetEvent));
jamescrosswell marked this conversation as resolved.
Show resolved Hide resolved
resetEvent.Wait();
}
#else
CaptureDisplayInfo();
#endif

// https://docs.microsoft.com/dotnet/maui/platform-integration/device/vibrate
device.SupportsVibration ??= Vibration.Default.IsSupported;
Expand Down Expand Up @@ -114,5 +118,23 @@ public static void ApplyMauiDeviceData(this Device device, IDiagnosticLogger? lo
// Log, but swallow the exception so we can continue sending events
logger?.LogError(ex, "Error getting MAUI device information.");
}

void CaptureDisplayInfo(ManualResetEventSlim? resetEvent = null)
{
// https://docs.microsoft.com/dotnet/maui/platform-integration/device/display
var display = DeviceDisplay.MainDisplayInfo;
device.ScreenResolution ??= $"{(int)display.Width}x{(int)display.Height}";
device.ScreenDensity ??= (float)display.Density;
device.Orientation ??= display.Orientation switch
{
DisplayOrientation.Portrait => DeviceOrientation.Portrait,
DisplayOrientation.Landscape => DeviceOrientation.Landscape,
_ => null
};
// device.ScreenDpi ??= ?
// ? = display.RefreshRate;
// ? = display.Rotation;
resetEvent?.Set();
}
}
}
Loading