Skip to content
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
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ Component.Render() → Element tree (records)
- `Reconciler.cs` — orchestration: the `Reconcile` entry, component rendering, child reconciliation, `CanUpdate`
- `Reconciler.Mount.cs` — mount dispatch + composition-primitive handlers (controls mount via their registered `ControlDescriptor`/`IElementHandler`)
- `Reconciler.Update.cs` — update dispatch + composition-primitive handlers (controls update via the same registered descriptors/handlers)
- `Reconciler.<Topic>.cs` — one file per concern (`Modifiers`, `Keyframes`, `Transitions`, `InteractionStates`, `Unmount`, `ReactorState`, `Flyouts`, …). Put new code in the matching topic file, not `Reconciler.cs`, so unrelated PRs don't conflict
- Per-control helpers that need none of the reconciler's private state live in standalone `internal static` classes beside it, so the compiler keeps them out of that state: `ThemeBindingApplier`, `AccessibilityApplier`, `ModifierEvents` (event trampolines, `ModifierEventHandlerState`, control event payloads), `ReferenceEdgeWiring`, `GestureDispatch`, `DragDropDispatch`. Public entry points that predate the split (`ApplyThemeBindings`, `ApplyResourceOverrides`, `Apply`/`UpdateDefaultAutomationName`) stay on `Reconciler` as forwarders in `Reconciler.StaticForwarders.cs`. `ThemeBindingApplier` (style caches), `GestureDispatch` and `DragDropDispatch` (per-element state tables) still own process-wide static state. Source-parsing gates and the reviewer-manifest gate find these files through `ReconcilerSource.HelperFiles` in `tests/Reactor.Tests/AnalyzerTests/ReconcilerSource.cs`, so add a new helper file there too
- `Reconciler.<Topic>.cs` — one file per concern (`Modifiers`, `Keyframes`, `Transitions`, `CompositionState`, `Unmount`, `ReactorState`, `Flyouts`, …). Put new code in the matching topic file, not `Reconciler.cs`, so unrelated PRs don't conflict
- Per-control helpers that need none of the reconciler's private state live in standalone `internal static` classes beside it, so the compiler keeps them out of that state: `ThemeBindingApplier`, `AccessibilityApplier`, `ModifierEvents` (event trampolines, `ModifierEventHandlerState`, control event payloads), `ReferenceEdgeWiring`, `GestureDispatch`, `DragDropDispatch`, `InteractionStateApplier` (`.InteractionStates()` handlers and state transitions; its tracker lives on `CompositionState`). Public entry points that predate the split (`ApplyThemeBindings`, `ApplyResourceOverrides`, `Apply`/`UpdateDefaultAutomationName`) stay on `Reconciler` as forwarders in `Reconciler.StaticForwarders.cs`. `ThemeBindingApplier` (style caches), `GestureDispatch` and `DragDropDispatch` (per-element state tables) still own process-wide static state. Source-parsing gates and the reviewer-manifest gate find these files through `ReconcilerSource.HelperFiles` in `tests/Reactor.Tests/AnalyzerTests/ReconcilerSource.cs`, so add a new helper file there too

### Hooks follow React rules

Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ src/Reactor/ Core framework library
Reconciler.Update.cs Update dispatch and composition-primitive handlers
Reconciler.*.cs One partial per concern (Modifiers, Keyframes, Unmount, ...)
ThemeBindingApplier.cs, ... Internal static helpers the reconciler calls (theme bindings,
accessibility, modifier events, reference edges, gestures, drag-drop)
accessibility, modifier events, reference edges, gestures, drag-drop,
interaction states)
V1Protocol/ Control registry, handlers, descriptors, and child strategies
ChildReconciler.cs Keyed child list reconciliation
ElementPool.cs Control reuse pool
Expand Down
11 changes: 6 additions & 5 deletions docs/_pipeline/templates/architecture-overview.md.dt
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ each have their own `Reconciler.<Topic>.cs` partial (`Modifiers`, `Unmount`,
`Keyframes`, `Transitions`, `ReactorState`, …), while `Reconciler.cs` keeps the
orchestration. Helpers that need none of the reconciler's private state
(`ThemeBindingApplier`, `AccessibilityApplier`, `ModifierEvents`,
`ReferenceEdgeWiring`, `GestureDispatch`, `DragDropDispatch`) are separate
`internal static` classes. There is one reconciler instance per host; component
recursion shares the dispatch.
`ReferenceEdgeWiring`, `GestureDispatch`, `DragDropDispatch`,
`InteractionStateApplier`) are separate `internal static` classes. There is
one reconciler instance per host; component recursion shares the dispatch.

## Hooks — slot table per RenderContext

Expand Down Expand Up @@ -222,8 +222,9 @@ or diagnostics.
implementation in `Reconciler.cs` itself — the dispatch lives there,
the handlers live in `Reconciler.Mount.cs`. Same for Update, and for
modifiers, unmount and animations, which each have their own
`Reconciler.<Topic>.cs`. Events, gestures and drag-drop live in
`ModifierEvents.cs`, `GestureDispatch.cs` and `DragDropDispatch.cs`.
`Reconciler.<Topic>.cs`. Events, gestures, drag-drop and interaction states
live in `ModifierEvents.cs`, `GestureDispatch.cs`, `DragDropDispatch.cs` and
`InteractionStateApplier.cs`.

**The element pool is opt-in by type.** If a custom control needs
pooling, the type must be in `ElementPool.PoolableTypes` and survive a
Expand Down
11 changes: 6 additions & 5 deletions docs/guide/architecture-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ each have their own `Reconciler.<Topic>.cs` partial (`Modifiers`, `Unmount`,
`Keyframes`, `Transitions`, `ReactorState`, …), while `Reconciler.cs` keeps the
orchestration. Helpers that need none of the reconciler's private state
(`ThemeBindingApplier`, `AccessibilityApplier`, `ModifierEvents`,
`ReferenceEdgeWiring`, `GestureDispatch`, `DragDropDispatch`) are separate
`internal static` classes. There is one reconciler instance per host; component
recursion shares the dispatch.
`ReferenceEdgeWiring`, `GestureDispatch`, `DragDropDispatch`,
`InteractionStateApplier`) are separate `internal static` classes. There is
one reconciler instance per host; component recursion shares the dispatch.

## Hooks — slot table per RenderContext

Expand Down Expand Up @@ -502,8 +502,9 @@ or diagnostics.
implementation in `Reconciler.cs` itself — the dispatch lives there,
the handlers live in `Reconciler.Mount.cs`. Same for Update, and for
modifiers, unmount and animations, which each have their own
`Reconciler.<Topic>.cs`. Events, gestures and drag-drop live in
`ModifierEvents.cs`, `GestureDispatch.cs` and `DragDropDispatch.cs`.
`Reconciler.<Topic>.cs`. Events, gestures, drag-drop and interaction states
live in `ModifierEvents.cs`, `GestureDispatch.cs`, `DragDropDispatch.cs` and
`InteractionStateApplier.cs`.

**The element pool is opt-in by type.** If a custom control needs
pooling, the type must be in `ElementPool.PoolableTypes` and survive a
Expand Down
2 changes: 1 addition & 1 deletion src/Reactor.Analyzers/ModifierTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ internal static class ModifierTable
new Dictionary<string, string>(System.StringComparer.Ordinal)
{
["Animate"] = "Reconciler.ApplyPropertyAnimation takes the visual to install its implicit animations.",
["InteractionStates"] = "Reconciler.TransitionToState takes the visual on the first state change to animate it.",
["InteractionStates"] = "InteractionStateApplier.TransitionToState takes the visual on the first state change to animate it.",
["Transition"] = "Reconciler.ApplyEnterTransition/ApplyExitTransition take the visual to animate enter and exit.",
["Keyframes"] = "Reconciler.ApplyKeyframeAnimations takes the visual to start the keyframe animation.",
["LayoutAnimation"] = "Reconciler.ApplyLayoutAnimation takes the visual to install its Offset/Size implicit animations.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using Microsoft.UI.Reactor.Animation;
using Microsoft.UI.Reactor.Controls.Validation;
using Microsoft.UI.Reactor.Core.Diagnostics;
using Microsoft.UI.Reactor.Core.V1Protocol;
using Microsoft.UI.Reactor.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Media.Imaging;
using WinUI = Microsoft.UI.Xaml.Controls;
using WinPrim = Microsoft.UI.Xaml.Controls.Primitives;
using WinShapes = Microsoft.UI.Xaml.Shapes;
using ReactorSourceMap = Microsoft.UI.Reactor.Diagnostics.ReactorSourceMap;

namespace Microsoft.UI.Reactor.Core;

// Reconciler partial: .InteractionStates() modifier.
public sealed partial class Reconciler
/// <summary>
/// The <c>.InteractionStates()</c> modifier: attaches pointer and focus handlers to a control and
/// animates its visual and brushes between the Normal, PointerOver, Pressed and Focused states.
/// The per-control tracker lives on <see cref="Reconciler.CompositionState.InteractionTracker"/>.
/// </summary>
internal static class InteractionStateApplier
{
// ════════════════════════════════════════════════════════════════
// Interaction states (.InteractionStates() modifier)
// ════════════════════════════════════════════════════════════════

/// <summary>
/// The live tracker count, for the test seam below. The trackers themselves live on each
/// control's <see cref="CompositionState.InteractionTracker"/>; hosts on different UI threads
/// update this concurrently, hence the interlocked writes.
/// control's <see cref="Reconciler.CompositionState.InteractionTracker"/>; hosts on different
/// UI threads update this concurrently, hence the interlocked writes.
/// </summary>
private static int s_interactionTrackerCount;

// Test-only accessor (InternalsVisibleTo Reactor.AppTests.Host).
internal static int InteractionTrackerCountForTests => Volatile.Read(ref s_interactionTrackerCount);

private static InteractionStateTracker? InteractionTrackerOf(object sender)
=> sender is UIElement uie ? GetCompositionState(uie)?.InteractionTracker : null;
=> sender is UIElement uie ? Reconciler.GetCompositionState(uie)?.InteractionTracker : null;

// Test-only: drives a hover-in / hover-out transition without synthesizing pointer input.
internal static void SimulatePointerOverForTests(UIElement uie, bool isOver)
Expand Down Expand Up @@ -84,7 +70,7 @@ internal enum InteractionState { Normal, PointerOver, Pressed, Focused }
internal static void ApplyInteractionStates(UIElement uie, InteractionStatesConfig config)
{
ElementPool.MarkVisualTaken(uie);
var composition = GetOrCreateCompositionState(uie);
var composition = Reconciler.GetOrCreateCompositionState(uie);
if (composition.InteractionTracker is not { } tracker)
{
tracker = new InteractionStateTracker();
Expand All @@ -109,7 +95,7 @@ internal static void ApplyInteractionStates(UIElement uie, InteractionStatesConf
/// </summary>
internal static void ClearInteractionStates(UIElement uie, bool restoreBrushes = false)
{
if (GetCompositionState(uie) is not { InteractionTracker: { } tracker } composition) return;
if (Reconciler.GetCompositionState(uie) is not { InteractionTracker: { } tracker } composition) return;
composition.InteractionTracker = null;
Interlocked.Decrement(ref s_interactionTrackerCount);

Expand Down Expand Up @@ -197,11 +183,11 @@ private static void TransitionToState(UIElement uie, InteractionStateTracker tra
// Normal returns the transforms to the values the element's modifiers wrote, not to
// identity, so a hover-out does not discard them (#1613). The visual was taken above,
// so a facade transform has already moved and been recorded.
var rest = RestTransformOf(uie);
var rest = Reconciler.RestTransformOf(uie);

// Compositor properties — animate via Visual. The state takes the opacity channel, so a
// pending opacity restart must not replace it (#1669).
CancelOpacityRestart(uie);
Reconciler.CancelOpacityRestart(uie);
var targetOpacity = values?.Opacity ?? 1.0f;
var opacityAnim = AnimationHelper.CreateScalarTargetAnimation(compositor, targetOpacity, curve);
visual.StartAnimation("Opacity", opacityAnim);
Expand Down Expand Up @@ -352,35 +338,6 @@ private static void ApplyInteractionBrush(
fe.ClearValue(dp);
}

/// <summary>The transform modifier values last written to an element's handed-out Visual.</summary>
internal sealed class TransformRest
{
public Vector3 Scale = Vector3.One;
public Vector3 Translation;
public float RotationDegrees;
// The static .CenterPoint() value; the origin expression's offset while an origin is active.
public Vector3 CenterPoint;
// The active .TransformOrigin(), or null when the pivot is the static CenterPoint (spec 066 §8.2).
public Vector2? Origin;
// The .Opacity() modifier's value while Reactor writes it to the visual, or null.
public float? Opacity;
}

private static TransformRest RestTransformOf(UIElement uie)
=> GetCompositionState(uie)?.TransformRest ?? IdentityRest;

private static readonly TransformRest IdentityRest = new();

/// <summary>
/// The opacity an element rests at: the <c>.Opacity()</c> modifier's value while Reactor owns the
/// visual's opacity (an animated modifier writes only the visual), otherwise <see cref="UIElement.Opacity"/>
/// (a local value or a style's).
/// </summary>
internal static float RestOpacityOf(UIElement uie)
=> GetCompositionState(uie) is { TransformRest.Opacity: { } opacity } composition
&& (composition.ModifierProperties & AnimateProperty.Opacity) != 0
? opacity : (float)uie.Opacity;

private static InteractionStateValues? MergePressed(InteractionStateValues? pointerOver, InteractionStateValues? pressed)
{
if (pressed is null) return pointerOver;
Expand Down
35 changes: 33 additions & 2 deletions src/Reactor/Core/Reconciler.CompositionState.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Numerics;
using Microsoft.UI.Composition;
using Microsoft.UI.Reactor.Animation;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -54,7 +55,7 @@ internal sealed class CompositionState
// entries, the pool's visual reset drops them all (spec 066 §7.3 step 4).
internal Dictionary<KeyframeName, KeyframeTriggerState>? Keyframes;
// The .InteractionStates() tracker while its pointer and focus handlers are attached.
internal InteractionStateTracker? InteractionTracker;
internal InteractionStateApplier.InteractionStateTracker? InteractionTracker;
// Reactor enabled translation on the handed-out visual for a declared Translation channel
// (spec 066 §4.3). The pool's visual reset disables it again and clears this.
internal bool TranslationEnabled;
Expand All @@ -64,7 +65,7 @@ internal sealed class CompositionState
// Set while a visual-lane renter waits to get its XAML opacity on the visual (ElementPool).
internal EventHandler<object>? OpacitySyncLayoutHandler;
// The visual offset the pool's reset wrote, so the renter's sync can tell a later write from it.
internal global::System.Numerics.Vector3? PoolResetOffset;
internal Vector3? PoolResetOffset;

/// <summary>
/// Spec 066 §7.3 step 4: forgets what the previous renter owned on a visual the pool reset.
Expand All @@ -87,6 +88,36 @@ internal void ResetForPool()
}
}

/// <summary>The transform modifier values last written to an element's handed-out Visual.</summary>
internal sealed class TransformRest
{
public Vector3 Scale = Vector3.One;
public Vector3 Translation;
public float RotationDegrees;
// The static .CenterPoint() value; the origin expression's offset while an origin is active.
public Vector3 CenterPoint;
// The active .TransformOrigin(), or null when the pivot is the static CenterPoint (spec 066 §8.2).
public Vector2? Origin;
// The .Opacity() modifier's value while Reactor writes it to the visual, or null.
public float? Opacity;
}

/// <summary>The transform an element's modifiers last wrote to its handed-out Visual, or identity.</summary>
internal static TransformRest RestTransformOf(UIElement uie)
=> GetCompositionState(uie)?.TransformRest ?? IdentityRest;

private static readonly TransformRest IdentityRest = new();

/// <summary>
/// The opacity an element rests at: the <c>.Opacity()</c> modifier's value while Reactor owns the
/// visual's opacity (an animated modifier writes only the visual), otherwise <see cref="UIElement.Opacity"/>
/// (a local value or a style's).
/// </summary>
internal static float RestOpacityOf(UIElement uie)
=> GetCompositionState(uie) is { TransformRest.Opacity: { } opacity } composition
&& (composition.ModifierProperties & AnimateProperty.Opacity) != 0
? opacity : (float)uie.Opacity;

/// <summary>A keyframe entry's previous trigger and the controllers of the channels it started.</summary>
internal record struct KeyframeTriggerState(object? Trigger, Dictionary<string, AnimationController>? Started);

Expand Down
2 changes: 1 addition & 1 deletion src/Reactor/Core/Reconciler.Keyframes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ internal static void ForgetCompositionState(UIElement uie)
CancelOpacityRestart(state);
state.ResetForPool();
}
ClearInteractionStates(uie, restoreBrushes: true);
InteractionStateApplier.ClearInteractionStates(uie, restoreBrushes: true);
}

private static KeyframeEntry[]? WithSharedKeyframes(KeyframeEntry[]? own, KeyframeEntry[]? shared)
Expand Down
2 changes: 1 addition & 1 deletion src/Reactor/Core/Reconciler.Mount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public sealed partial class Reconciler

// Apply interaction states (.InteractionStates() modifier)
if (control is not null && element.InteractionStates is not null)
ApplyInteractionStates(control, element.InteractionStates);
InteractionStateApplier.ApplyInteractionStates(control, element.InteractionStates);

// Apply keyframe animations (.Keyframes() modifier)
if (control is not null && element.KeyframeAnimations is not null)
Expand Down
2 changes: 1 addition & 1 deletion src/Reactor/Core/Reconciler.Unmount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private static void CleanupElementStateForUnmount(FrameworkElement control, Elem
// A control kept out of the pool isn't reused, and a connected-animation source keeps its
// hover brushes for the snapshot WinUI takes on the next frame.
if (element?.InteractionStates is not null)
ClearInteractionStates(control,
InteractionStateApplier.ClearInteractionStates(control,
restoreBrushes: control.GetValue(ReactorAttached.StateProperty) is not ReactorState { PoolExcluded: true });
if (element?.KeyframeAnimations is not null)
ClearKeyframeAnimations(control, element.KeyframeAnimations);
Expand Down
4 changes: 2 additions & 2 deletions src/Reactor/Core/Reconciler.Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -576,9 +576,9 @@ private void ShiftForwardedClaims(UIElement control, int frame, Element? layer =

// Apply or clear interaction states (.InteractionStates() modifier)
if (newEl.InteractionStates is not null)
ApplyInteractionStates(target, newEl.InteractionStates);
InteractionStateApplier.ApplyInteractionStates(target, newEl.InteractionStates);
else if (oldEl.InteractionStates is not null)
ClearInteractionStates(target);
InteractionStateApplier.ClearInteractionStates(target);

// Apply keyframe animations (.Keyframes() modifier)
if (keyframes is not null)
Expand Down
Loading
Loading