Skip to content

Commit

Permalink
Fix: DisplayInfo now captured correctly on iOS and Mac Catalyst (#3521)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrosswell authored Aug 12, 2024
1 parent 8e893cb commit f4d21b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
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
47 changes: 35 additions & 12 deletions src/Sentry.Maui/Internal/MauiDeviceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,24 @@ 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
using var resetEvent = new ManualResetEventSlim(false);
// ReSharper disable once AccessToDisposedClosure - not disposed until lambda completes
MainThread.BeginInvokeOnMainThread(() => CaptureDisplayInfo(resetEvent));
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 +119,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();
}
}
}

0 comments on commit f4d21b7

Please sign in to comment.