| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using SwitchBlade.Contracts; |
| | | 6 | | using SwitchBlade.Core; |
| | | 7 | | |
| | | 8 | | namespace SwitchBlade.Services |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Coordinates staggered badge animations for Alt+Number shortcuts. |
| | | 12 | | /// Delegates the actual animation execution to an IBadgeAnimator strategy. |
| | | 13 | | /// Uses debouncing to prevent animation fighting during rapid input. |
| | | 14 | | /// </summary> |
| | | 15 | | public class BadgeAnimationService |
| | | 16 | | { |
| | | 17 | | private readonly IBadgeAnimator _animator; |
| | | 18 | | private readonly IDelayProvider _delayProvider; |
| | | 19 | | private CancellationTokenSource? _animationCts; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Duration of each badge's animation in milliseconds. |
| | | 23 | | /// </summary> |
| | 47 | 24 | | public int AnimationDurationMs { get; set; } = 150; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Delay between each badge's animation start in milliseconds. |
| | | 28 | | /// </summary> |
| | 64 | 29 | | public int StaggerDelayMs { get; set; } = 75; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Starting X offset for slide-in animation (negative = from left). |
| | | 33 | | /// </summary> |
| | 34 | 34 | | public double StartingOffsetX { get; set; } = -20; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Debounce interval: how long to wait after the last trigger before |
| | | 38 | | /// actually starting animations. Prevents wasteful animation starts |
| | | 39 | | /// during rapid typing. Matched to one stagger step for natural feel. |
| | | 40 | | /// </summary> |
| | 42 | 41 | | public int DebounceMs { get; set; } = 75; |
| | | 42 | | |
| | 19 | 43 | | public BadgeAnimationService(IBadgeAnimator animator, IDelayProvider? delayProvider = null) |
| | 19 | 44 | | { |
| | 19 | 45 | | _animator = animator ?? throw new ArgumentNullException(nameof(animator)); |
| | 18 | 46 | | _delayProvider = delayProvider ?? new SystemDelayProvider(); |
| | 18 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Resets the animation state for the provided items. |
| | | 51 | | /// Use this when you want to force re-animation (e.g. on new search or window open). |
| | | 52 | | /// </summary> |
| | | 53 | | public static void ResetAnimationState(IEnumerable<WindowItem>? items) |
| | 2 | 54 | | { |
| | 3 | 55 | | if (items == null) return; |
| | | 56 | | |
| | | 57 | | // We just reset the flag. We do NOT reset the visual Opacity/TranslateX here. |
| | | 58 | | // Pushing visual state to hidden happens just-in-time in TriggerStaggeredAnimationAsync. |
| | 5 | 59 | | foreach (var item in items) |
| | 1 | 60 | | { |
| | 1 | 61 | | item.HasBeenAnimated = false; |
| | 1 | 62 | | } |
| | 1 | 63 | | SwitchBlade.Core.Logger.Log($"[BadgeAnimation] ResetAnimationState: Reset HasBeenAnimated flag for items"); |
| | 2 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Triggers staggered animations for the given window items. |
| | | 68 | | /// Only items with shortcuts (index 0-9) and not previously animated will animate. |
| | | 69 | | /// Uses debouncing: if called again within DebounceMs, the previous call is cancelled. |
| | | 70 | | /// This ensures animations only play once typing settles, preventing jitter. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="items">The items to animate.</param> |
| | | 73 | | /// <param name="skipDebounce">When true, skips the debounce delay (e.g., for hotkey/initial load).</param> |
| | | 74 | | public async Task TriggerStaggeredAnimationAsync(IEnumerable<WindowItem>? items, bool skipDebounce = false) |
| | 18 | 75 | | { |
| | 18 | 76 | | SwitchBlade.Core.Logger.Log($"[BadgeAnimation] TriggerStaggeredAnimationAsync: Starting"); |
| | 19 | 77 | | if (items == null) return; |
| | | 78 | | |
| | | 79 | | // Cancel any pending animation cycle from a previous call |
| | 17 | 80 | | _animationCts?.Cancel(); |
| | 17 | 81 | | _animationCts?.Dispose(); |
| | 17 | 82 | | _animationCts = new CancellationTokenSource(); |
| | 17 | 83 | | var ct = _animationCts.Token; |
| | | 84 | | |
| | | 85 | | // Immediately hide all badges that need animating (so they don't show stale state) |
| | 97 | 86 | | foreach (var item in items) |
| | 23 | 87 | | { |
| | 23 | 88 | | if (item.IsShortcutVisible && !item.HasBeenAnimated) |
| | 18 | 89 | | { |
| | 18 | 90 | | item.ResetBadgeAnimation(); |
| | 18 | 91 | | } |
| | 23 | 92 | | } |
| | | 93 | | |
| | | 94 | | // Debounce: wait for typing to settle before starting the animation cycle. |
| | | 95 | | // If another call arrives during this window, this one is cancelled. |
| | | 96 | | // Skip debounce for hotkey/initial load so the animation feels responsive. |
| | 17 | 97 | | if (!skipDebounce) |
| | 6 | 98 | | { |
| | | 99 | | try |
| | 6 | 100 | | { |
| | 6 | 101 | | await _delayProvider.Delay(DebounceMs, ct); |
| | 5 | 102 | | } |
| | 1 | 103 | | catch (OperationCanceledException) |
| | 1 | 104 | | { |
| | 1 | 105 | | return; |
| | | 106 | | } |
| | 5 | 107 | | } |
| | | 108 | | |
| | 17 | 109 | | if (ct.IsCancellationRequested) return; |
| | | 110 | | |
| | 15 | 111 | | int maxShortcutIndex = -1; |
| | 15 | 112 | | int animatedCount = 0; |
| | 15 | 113 | | int skippedCount = 0; |
| | | 114 | | |
| | 86 | 115 | | foreach (var item in items) |
| | 21 | 116 | | { |
| | 22 | 117 | | if (ct.IsCancellationRequested) return; |
| | | 118 | | |
| | 20 | 119 | | if (!item.IsShortcutVisible) |
| | 1 | 120 | | { |
| | 1 | 121 | | continue; |
| | | 122 | | } |
| | | 123 | | |
| | 19 | 124 | | bool shouldAnimate = !item.HasBeenAnimated; |
| | | 125 | | |
| | 19 | 126 | | if (shouldAnimate) |
| | 15 | 127 | | { |
| | 15 | 128 | | int delay = item.ShortcutIndex * StaggerDelayMs; |
| | | 129 | | |
| | | 130 | | // Delegate execution to the strategy |
| | 15 | 131 | | _animator.Animate(item, delay, AnimationDurationMs, StartingOffsetX); |
| | | 132 | | |
| | | 133 | | // Mark as animated immediately so we don't re-animate on next pass |
| | 15 | 134 | | item.HasBeenAnimated = true; |
| | 15 | 135 | | animatedCount++; |
| | | 136 | | |
| | 15 | 137 | | if (item.ShortcutIndex > maxShortcutIndex) |
| | 15 | 138 | | { |
| | 15 | 139 | | maxShortcutIndex = item.ShortcutIndex; |
| | 15 | 140 | | } |
| | 15 | 141 | | } |
| | | 142 | | else |
| | 4 | 143 | | { |
| | | 144 | | // Already animated - ensure it's visible |
| | 4 | 145 | | item.BadgeOpacity = 1.0; |
| | 4 | 146 | | item.BadgeTranslateX = 0; |
| | 4 | 147 | | skippedCount++; |
| | 4 | 148 | | } |
| | 19 | 149 | | } |
| | | 150 | | |
| | 14 | 151 | | SwitchBlade.Core.Logger.Log($"[BadgeAnimation] TriggerStaggeredAnimationAsync: Animated={animatedCount}, Ski |
| | | 152 | | |
| | | 153 | | // Wait for all animations to complete (approximate based on max duration) |
| | 14 | 154 | | if (maxShortcutIndex >= 0) |
| | 11 | 155 | | { |
| | 11 | 156 | | int maxDelay = (maxShortcutIndex + 1) * StaggerDelayMs + AnimationDurationMs; |
| | | 157 | | try |
| | 11 | 158 | | { |
| | 11 | 159 | | await _delayProvider.Delay(maxDelay, ct); |
| | 10 | 160 | | } |
| | 1 | 161 | | catch (OperationCanceledException) |
| | 1 | 162 | | { |
| | | 163 | | // Animation cycle was superseded — that's fine |
| | 1 | 164 | | } |
| | 11 | 165 | | } |
| | 18 | 166 | | } |
| | | 167 | | } |
| | | 168 | | } |