Skip to content

Commit e51c849

Browse files
Normalize event/callback naming conventions (#260) (#301)
* Normalize event/callback naming conventions Rename bare-name AccessKeyDisplayRequested extension to OnAccessKeyDisplayRequested with [Obsolete] shim. Change NavigationDiagnostics events from Action<T> to EventHandler<T> to follow .NET event guidelines. Update reactor.api.txt and test handlers. Fixes #260 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix EventHandler signatures in AppTests.Host and NavigationDemo Update handler signatures from Action<T> to EventHandler<T> (object? sender, T e) pattern in NavigationDemo diagnostics and selftest NavigationCoverageFixtures. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix docs navigation app for EventHandler type Update diagnostic event handlers from Action<T> to EventHandler<T> to match the changed event signatures in NavigationDiagnostics. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5b37118 commit e51c849

8 files changed

Lines changed: 53 additions & 51 deletions

File tree

docs/_pipeline/apps/navigation/App.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,10 @@ public override Element Render()
278278

279279
UseEffect(() =>
280280
{
281-
Action<NavigationDiagnosticEvent> onRequested =
282-
e => updateLog(l => [.. l, $"Requested: {e.From}{e.To}"]);
283-
Action<NavigationDiagnosticEvent> onCompleted =
284-
e => updateLog(l => [.. l, $"Completed: {e.To}"]);
281+
EventHandler<NavigationDiagnosticEvent> onRequested =
282+
(_, e) => updateLog(l => [.. l, $"Requested: {e.From}{e.To}"]);
283+
EventHandler<NavigationDiagnosticEvent> onCompleted =
284+
(_, e) => updateLog(l => [.. l, $"Completed: {e.To}"]);
285285

286286
NavigationDiagnostics.NavigationRequested += onRequested;
287287
NavigationDiagnostics.NavigationCompleted += onCompleted;

plugins/reactor/skills/reactor-dsl/references/reactor.api.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,6 @@ StackElement.Set(Action<StackPanel> configure) → StackElement
477477
StackElement.Spacing(double spacing) → StackElement
478478
SwipeControlElement.Set(Action<SwipeControl> configure) → SwipeControlElement
479479
T.AccessKey<T>(string key) → T
480-
T.AccessKeyDisplayRequested<T>(Action handler) → T
481-
T.AccessKeyDisplayRequested<T>(Action<UIElement, AccessKeyDisplayRequestedEventArgs> handler) → T
482480
T.AccessibilityHidden<T>() → T
483481
T.AccessibilityView<T>(AccessibilityView view) → T
484482
T.Animate<T>(Curve curve, AnimateProperty properties = AnimateProperty.All) → T
@@ -542,6 +540,8 @@ T.MaxHeight<T>(double h) → T
542540
T.MaxWidth<T>(double w) → T
543541
T.MinHeight<T>(double h) → T
544542
T.MinWidth<T>(double w) → T
543+
T.OnAccessKeyDisplayRequested<T>(Action handler) → T
544+
T.OnAccessKeyDisplayRequested<T>(Action<UIElement, AccessKeyDisplayRequestedEventArgs> handler) → T
545545
T.OnCharacterReceived<T>(Action<UIElement, CharacterReceivedRoutedEventArgs> handler) → T
546546
T.OnDoubleTap<T>(Action handler) → T
547547
T.OnDoubleTap<T>(Action<Point> handler) → T

samples/NavigationDemo/App.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,13 @@ public override Element Render()
141141
// Subscribe to navigation diagnostics for the log panel
142142
UseEffect(() =>
143143
{
144-
void onCompleted(NavigationDiagnosticEvent e) =>
144+
void onCompleted(object? sender, NavigationDiagnosticEvent e) =>
145145
updateDiagLog(log => [.. log.TakeLast(19), $"[{DateTime.Now:HH:mm:ss}] {e.Mode}: {e.From} -> {e.To}"]);
146-
void onCancelled(NavigationDiagnosticEvent e) =>
146+
void onCancelled(object? sender, NavigationDiagnosticEvent e) =>
147147
updateDiagLog(log => [.. log.TakeLast(19), $"[{DateTime.Now:HH:mm:ss}] BLOCKED: {e.Mode} {e.From} -> {e.To} ({e.Reason})"]);
148-
void onCacheHit(CacheDiagnosticEvent e) =>
148+
void onCacheHit(object? sender, CacheDiagnosticEvent e) =>
149149
updateDiagLog(log => [.. log.TakeLast(19), $"[{DateTime.Now:HH:mm:ss}] Cache HIT: {e.Route}"]);
150-
void onCacheMiss(CacheDiagnosticEvent e) =>
150+
void onCacheMiss(object? sender, CacheDiagnosticEvent e) =>
151151
updateDiagLog(log => [.. log.TakeLast(19), $"[{DateTime.Now:HH:mm:ss}] Cache MISS: {e.Route}"]);
152152

153153
NavigationDiagnostics.NavigationCompleted += onCompleted;

skills/reactor.api.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,6 @@ StackElement.Set(Action<StackPanel> configure) → StackElement
477477
StackElement.Spacing(double spacing) → StackElement
478478
SwipeControlElement.Set(Action<SwipeControl> configure) → SwipeControlElement
479479
T.AccessKey<T>(string key) → T
480-
T.AccessKeyDisplayRequested<T>(Action handler) → T
481-
T.AccessKeyDisplayRequested<T>(Action<UIElement, AccessKeyDisplayRequestedEventArgs> handler) → T
482480
T.AccessibilityHidden<T>() → T
483481
T.AccessibilityView<T>(AccessibilityView view) → T
484482
T.Animate<T>(Curve curve, AnimateProperty properties = AnimateProperty.All) → T
@@ -542,6 +540,8 @@ T.MaxHeight<T>(double h) → T
542540
T.MaxWidth<T>(double w) → T
543541
T.MinHeight<T>(double h) → T
544542
T.MinWidth<T>(double w) → T
543+
T.OnAccessKeyDisplayRequested<T>(Action handler) → T
544+
T.OnAccessKeyDisplayRequested<T>(Action<UIElement, AccessKeyDisplayRequestedEventArgs> handler) → T
545545
T.OnCharacterReceived<T>(Action<UIElement, CharacterReceivedRoutedEventArgs> handler) → T
546546
T.OnDoubleTap<T>(Action handler) → T
547547
T.OnDoubleTap<T>(Action<Point> handler) → T

src/Reactor/Core/Navigation/NavigationDiagnostics.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,84 @@ namespace Microsoft.UI.Reactor.Navigation;
1010
public static class NavigationDiagnostics
1111
{
1212
/// <summary>Fires when a navigation is attempted (before guards run).</summary>
13-
public static event Action<NavigationDiagnosticEvent>? NavigationRequested;
13+
public static event EventHandler<NavigationDiagnosticEvent>? NavigationRequested;
1414

1515
/// <summary>Fires when a navigation completes successfully.</summary>
16-
public static event Action<NavigationDiagnosticEvent>? NavigationCompleted;
16+
public static event EventHandler<NavigationDiagnosticEvent>? NavigationCompleted;
1717

1818
/// <summary>Fires when a navigation guard cancels a navigation.</summary>
19-
public static event Action<NavigationDiagnosticEvent>? NavigationCancelled;
19+
public static event EventHandler<NavigationDiagnosticEvent>? NavigationCancelled;
2020

2121
/// <summary>Fires on a cache hit during navigation.</summary>
22-
public static event Action<CacheDiagnosticEvent>? CacheHit;
22+
public static event EventHandler<CacheDiagnosticEvent>? CacheHit;
2323

2424
/// <summary>Fires on a cache miss during navigation.</summary>
25-
public static event Action<CacheDiagnosticEvent>? CacheMiss;
25+
public static event EventHandler<CacheDiagnosticEvent>? CacheMiss;
2626

2727
/// <summary>Fires when a page is evicted from the cache.</summary>
28-
public static event Action<CacheDiagnosticEvent>? CacheEviction;
28+
public static event EventHandler<CacheDiagnosticEvent>? CacheEviction;
2929

3030
/// <summary>Fires when a transition animation starts.</summary>
31-
public static event Action<TransitionDiagnosticEvent>? TransitionStarted;
31+
public static event EventHandler<TransitionDiagnosticEvent>? TransitionStarted;
3232

3333
/// <summary>Fires when a transition animation completes.</summary>
34-
public static event Action<TransitionDiagnosticEvent>? TransitionCompleted;
34+
public static event EventHandler<TransitionDiagnosticEvent>? TransitionCompleted;
3535

3636
/// <summary>Fires when a deep link resolves (match or miss).</summary>
37-
public static event Action<DeepLinkDiagnosticEvent>? DeepLinkResolved;
37+
public static event EventHandler<DeepLinkDiagnosticEvent>? DeepLinkResolved;
3838

3939
internal static void OnNavigationRequested(object from, object to, NavigationMode mode)
4040
{
4141
Debug.WriteLine($"[Reactor.Nav] Requested: {mode} from {from}{to}");
42-
NavigationRequested?.Invoke(new(from, to, mode));
42+
NavigationRequested?.Invoke(null, new(from, to, mode));
4343
}
4444

4545
internal static void OnNavigationCompleted(object from, object to, NavigationMode mode)
4646
{
4747
Debug.WriteLine($"[Reactor.Nav] Completed: {mode} from {from}{to}");
48-
NavigationCompleted?.Invoke(new(from, to, mode));
48+
NavigationCompleted?.Invoke(null, new(from, to, mode));
4949
}
5050

5151
internal static void OnNavigationCancelled(object from, object to, NavigationMode mode, string reason)
5252
{
5353
Debug.WriteLine($"[Reactor.Nav] Cancelled: {mode} from {from}{to} ({reason})");
54-
NavigationCancelled?.Invoke(new(from, to, mode) { Reason = reason });
54+
NavigationCancelled?.Invoke(null, new(from, to, mode) { Reason = reason });
5555
}
5656

5757
internal static void OnCacheHit(object route)
5858
{
5959
Debug.WriteLine($"[Reactor.Nav] Cache HIT: {route}");
60-
CacheHit?.Invoke(new(route));
60+
CacheHit?.Invoke(null, new(route));
6161
}
6262

6363
internal static void OnCacheMiss(object route)
6464
{
6565
Debug.WriteLine($"[Reactor.Nav] Cache MISS: {route}");
66-
CacheMiss?.Invoke(new(route));
66+
CacheMiss?.Invoke(null, new(route));
6767
}
6868

6969
internal static void OnCacheEviction(object route)
7070
{
7171
Debug.WriteLine($"[Reactor.Nav] Cache EVICT: {route}");
72-
CacheEviction?.Invoke(new(route));
72+
CacheEviction?.Invoke(null, new(route));
7373
}
7474

7575
internal static void OnTransitionStarted(NavigationTransition transition, NavigationMode mode)
7676
{
7777
Debug.WriteLine($"[Reactor.Nav] Transition START: {transition.GetType().Name} ({mode})");
78-
TransitionStarted?.Invoke(new(transition, mode));
78+
TransitionStarted?.Invoke(null, new(transition, mode));
7979
}
8080

8181
internal static void OnTransitionCompleted(NavigationTransition transition, NavigationMode mode)
8282
{
8383
Debug.WriteLine($"[Reactor.Nav] Transition END: {transition.GetType().Name} ({mode})");
84-
TransitionCompleted?.Invoke(new(transition, mode));
84+
TransitionCompleted?.Invoke(null, new(transition, mode));
8585
}
8686

8787
internal static void OnDeepLinkResolved(string path, bool matched, int routeCount)
8888
{
8989
Debug.WriteLine($"[Reactor.Nav] DeepLink: '{path}' → {(matched ? $"matched ({routeCount} routes)" : "no match")}");
90-
DeepLinkResolved?.Invoke(new(path, matched, routeCount));
90+
DeepLinkResolved?.Invoke(null, new(path, matched, routeCount));
9191
}
9292
}
9393

src/Reactor/Elements/ElementExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,15 +2041,25 @@ public static T XYFocusKeyboardNavigation<T>(this T el, Microsoft.UI.Xaml.Input.
20412041
/// Handler for UIElement.AccessKeyDisplayRequested — fires when the access-key
20422042
/// bubble should appear (e.g., user pressed Alt). Use to customize the visual.
20432043
/// </summary>
2044-
public static T AccessKeyDisplayRequested<T>(this T el, Action handler) where T : Element =>
2044+
public static T OnAccessKeyDisplayRequested<T>(this T el, Action handler) where T : Element =>
20452045
Modify(el, new ElementModifiers { OnAccessKeyDisplayRequested = (_, _) => handler() });
20462046

20472047
/// <summary>
20482048
/// Handler for UIElement.AccessKeyDisplayRequested with full event args.
20492049
/// </summary>
2050-
public static T AccessKeyDisplayRequested<T>(this T el, Action<UIElement, Microsoft.UI.Xaml.Input.AccessKeyDisplayRequestedEventArgs> handler) where T : Element =>
2050+
public static T OnAccessKeyDisplayRequested<T>(this T el, Action<UIElement, Microsoft.UI.Xaml.Input.AccessKeyDisplayRequestedEventArgs> handler) where T : Element =>
20512051
Modify(el, new ElementModifiers { OnAccessKeyDisplayRequested = handler });
20522052

2053+
/// <inheritdoc cref="OnAccessKeyDisplayRequested{T}(T, Action)"/>
2054+
[Obsolete("Use OnAccessKeyDisplayRequested. See #260.")]
2055+
public static T AccessKeyDisplayRequested<T>(this T el, Action handler) where T : Element =>
2056+
OnAccessKeyDisplayRequested(el, handler);
2057+
2058+
/// <inheritdoc cref="OnAccessKeyDisplayRequested{T}(T, Action{UIElement, Microsoft.UI.Xaml.Input.AccessKeyDisplayRequestedEventArgs})"/>
2059+
[Obsolete("Use OnAccessKeyDisplayRequested. See #260.")]
2060+
public static T AccessKeyDisplayRequested<T>(this T el, Action<UIElement, Microsoft.UI.Xaml.Input.AccessKeyDisplayRequestedEventArgs> handler) where T : Element =>
2061+
OnAccessKeyDisplayRequested(el, handler);
2062+
20532063
/// <summary>
20542064
/// Binds this element to an imperative <see cref="Microsoft.UI.Reactor.Input.ElementRef"/>.
20552065
/// Obtain the ref from <c>ctx.UseElementFocus()</c> (or construct one manually) and use

tests/Reactor.AppTests.Host/SelfTest/Fixtures/NavigationCoverageFixtures.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ internal class NavDiagnosticsEvents(Harness h) : SelfTestFixtureBase(h)
523523
public override async Task RunAsync()
524524
{
525525
var completedEvents = new List<NavigationDiagnosticEvent>();
526-
void handler(NavigationDiagnosticEvent e) => completedEvents.Add(e);
526+
void handler(object? sender, NavigationDiagnosticEvent e) => completedEvents.Add(e);
527527
NavigationDiagnostics.NavigationCompleted += handler;
528528

529529
try

tests/Reactor.Tests/NavigationDiagnosticsCoverageTests.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ public void OnNavigationRequested_Fires_Event_With_Payload()
2525
{
2626
var (from, to) = UniqueKeys();
2727
NavigationDiagnosticEvent? captured = null;
28-
Action<NavigationDiagnosticEvent> handler = e =>
28+
EventHandler<NavigationDiagnosticEvent> handler = (_, e) =>
2929
{
30-
// From/To are typed `object`; compare via Equals to avoid CS0252.
3130
if (Equals(e.From, from) && Equals(e.To, to)) captured = e;
3231
};
3332
NavigationDiagnostics.NavigationRequested += handler;
@@ -50,9 +49,8 @@ public void OnNavigationCompleted_Fires_Event()
5049
{
5150
var (from, to) = UniqueKeys();
5251
NavigationDiagnosticEvent? captured = null;
53-
Action<NavigationDiagnosticEvent> handler = e =>
52+
EventHandler<NavigationDiagnosticEvent> handler = (_, e) =>
5453
{
55-
// From/To are typed `object`; compare via Equals to avoid CS0252.
5654
if (Equals(e.From, from) && Equals(e.To, to)) captured = e;
5755
};
5856
NavigationDiagnostics.NavigationCompleted += handler;
@@ -73,9 +71,8 @@ public void OnNavigationCancelled_Captures_Reason()
7371
{
7472
var (from, to) = UniqueKeys();
7573
NavigationDiagnosticEvent? captured = null;
76-
Action<NavigationDiagnosticEvent> handler = e =>
74+
EventHandler<NavigationDiagnosticEvent> handler = (_, e) =>
7775
{
78-
// From/To are typed `object`; compare via Equals to avoid CS0252.
7976
if (Equals(e.From, from) && Equals(e.To, to)) captured = e;
8077
};
8178
NavigationDiagnostics.NavigationCancelled += handler;
@@ -101,11 +98,9 @@ public void OnCacheHit_OnCacheMiss_OnCacheEviction_Fire()
10198
var hits = new List<CacheDiagnosticEvent>();
10299
var misses = new List<CacheDiagnosticEvent>();
103100
var evicts = new List<CacheDiagnosticEvent>();
104-
// CacheDiagnosticEvent.Route is typed as object; compare via Equals to
105-
// avoid the reference-comparison footgun (CS0252).
106-
Action<CacheDiagnosticEvent> hh = e => { if (Equals(e.Route, rHit)) hits.Add(e); };
107-
Action<CacheDiagnosticEvent> mh = e => { if (Equals(e.Route, rMiss)) misses.Add(e); };
108-
Action<CacheDiagnosticEvent> eh = e => { if (Equals(e.Route, rEvict)) evicts.Add(e); };
101+
EventHandler<CacheDiagnosticEvent> hh = (_, e) => { if (Equals(e.Route, rHit)) hits.Add(e); };
102+
EventHandler<CacheDiagnosticEvent> mh = (_, e) => { if (Equals(e.Route, rMiss)) misses.Add(e); };
103+
EventHandler<CacheDiagnosticEvent> eh = (_, e) => { if (Equals(e.Route, rEvict)) evicts.Add(e); };
109104

110105
NavigationDiagnostics.CacheHit += hh;
111106
NavigationDiagnostics.CacheMiss += mh;
@@ -130,13 +125,11 @@ public void OnCacheHit_OnCacheMiss_OnCacheEviction_Fire()
130125
[Fact]
131126
public void OnTransitionStarted_OnTransitionCompleted_Fire()
132127
{
133-
// Filter by transition instance identity — concurrent navigation tests fire
134-
// their own transitions through the same global event.
135128
var transition = new SlideTransition();
136129
TransitionDiagnosticEvent? start = null;
137130
TransitionDiagnosticEvent? end = null;
138-
Action<TransitionDiagnosticEvent> sh = e => { if (ReferenceEquals(e.Transition, transition)) start = e; };
139-
Action<TransitionDiagnosticEvent> eh = e => { if (ReferenceEquals(e.Transition, transition)) end = e; };
131+
EventHandler<TransitionDiagnosticEvent> sh = (_, e) => { if (ReferenceEquals(e.Transition, transition)) start = e; };
132+
EventHandler<TransitionDiagnosticEvent> eh = (_, e) => { if (ReferenceEquals(e.Transition, transition)) end = e; };
140133
NavigationDiagnostics.TransitionStarted += sh;
141134
NavigationDiagnostics.TransitionCompleted += eh;
142135
try
@@ -160,7 +153,7 @@ public void OnDeepLinkResolved_Fires_With_Match_And_Miss()
160153
{
161154
var pathHit = $"/home-{Guid.NewGuid():N}";
162155
DeepLinkDiagnosticEvent? captured = null;
163-
Action<DeepLinkDiagnosticEvent> handler = e => { if (e.Path == pathHit) captured = e; };
156+
EventHandler<DeepLinkDiagnosticEvent> handler = (_, e) => { if (e.Path == pathHit) captured = e; };
164157
NavigationDiagnostics.DeepLinkResolved += handler;
165158
try
166159
{
@@ -175,10 +168,9 @@ public void OnDeepLinkResolved_Fires_With_Match_And_Miss()
175168
Assert.True(captured.Matched);
176169
Assert.Equal(2, captured.RouteCount);
177170

178-
// Re-fire for the miss branch (matched=false formats differently)
179171
var pathMiss = $"/missing-{Guid.NewGuid():N}";
180172
DeepLinkDiagnosticEvent? miss = null;
181-
Action<DeepLinkDiagnosticEvent> handler2 = e => { if (e.Path == pathMiss) miss = e; };
173+
EventHandler<DeepLinkDiagnosticEvent> handler2 = (_, e) => { if (e.Path == pathMiss) miss = e; };
182174
NavigationDiagnostics.DeepLinkResolved += handler2;
183175
try
184176
{

0 commit comments

Comments
 (0)