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 Sentry interfering with MAUI's focus events #1891

Merged
merged 6 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -28,6 +28,7 @@
- Null check HttpContext in SystemWebVersionLocator ([#1881](https://github.com/getsentry/sentry-dotnet/pull/1881))
- Fix detection of .NET Framework 4.8.1 ([#1885](https://github.com/getsentry/sentry-dotnet/pull/1885))
- Flush caching transport with main flush ([#1890](https://github.com/getsentry/sentry-dotnet/pull/1890))
- Fix Sentry interfering with MAUI's focus events ([#1891](https://github.com/getsentry/sentry-dotnet/pull/1891))

## 3.20.1

Expand Down
42 changes: 40 additions & 2 deletions src/Sentry.Maui/Internal/MauiEventsBinder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -27,6 +28,7 @@ internal class MauiEventsBinder : IMauiEventsBinder
private static readonly HashSet<Type> ExplicitlyHandledTypes = new()
{
typeof(Element),
typeof(VisualElement),
typeof(BindableObject),
typeof(Application),
typeof(Window),
Expand Down Expand Up @@ -61,6 +63,11 @@ private void BindApplicationEvents(Application application)
// All elements have a set of common events we can hook
BindElementEvents(e.Element);

if (e.Element is VisualElement visualElement)
{
BindVisualElementEvents(visualElement);
}

// We'll use reflection to attach to other events
// This allows us to attach to events from custom controls
BindReflectedEvents(e.Element);
Expand Down Expand Up @@ -125,6 +132,13 @@ private void BindReflectedEvents(Element element)
var events = elementType.GetEvents(BindingFlags.Instance | BindingFlags.Public);
foreach (var eventInfo in events.Where(e => !ExplicitlyHandledTypes.Contains(e.DeclaringType!)))
{
var browsable = eventInfo.GetCustomAttribute<EditorBrowsableAttribute>();
if (browsable != null && browsable.State != EditorBrowsableState.Always)
{
// These events are not meant for typical consumption.
continue;
}

Action<object, object> handler = (sender, _) =>
{
_hub.AddBreadcrumbForEvent(_options, sender, eventInfo.Name);
Expand Down Expand Up @@ -255,6 +269,30 @@ private void BindElementEvents(Element element)
// element.PropertyChanged
}

private void BindVisualElementEvents(VisualElement element)
{
element.Focused += (sender, e) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(VisualElement.Focused), SystemType, RenderingCategory);

element.Unfocused += (sender, e) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(VisualElement.Unfocused), SystemType, RenderingCategory);

element.Loaded += (sender, e) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(VisualElement.Loaded), SystemType, RenderingCategory);

element.Unloaded += (sender, e) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(VisualElement.Unloaded), SystemType, RenderingCategory);

element.ChildrenReordered += (sender, e) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(VisualElement.ChildrenReordered), SystemType, RenderingCategory);

element.MeasureInvalidated += (sender, e) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(VisualElement.MeasureInvalidated), SystemType, RenderingCategory);

element.SizeChanged += (sender, e) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(VisualElement.SizeChanged), SystemType, RenderingCategory);
}

private void BindShellEvents(Shell shell)
{
// Navigation events
Expand Down Expand Up @@ -309,8 +347,8 @@ private void BindButtonEvents(Button button)
button.Clicked += (sender, _) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(Button.Clicked), UserType, UserActionCategory);
button.Pressed += (sender, _) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(Button.Pressed), UserType,UserActionCategory);
_hub.AddBreadcrumbForEvent(_options, sender, nameof(Button.Pressed), UserType, UserActionCategory);
button.Released += (sender, _) =>
_hub.AddBreadcrumbForEvent(_options, sender, nameof(Button.Released), UserType,UserActionCategory);
_hub.AddBreadcrumbForEvent(_options, sender, nameof(Button.Released), UserType, UserActionCategory);
}
}