using System; using Xui.Core.Animation; using Xui.Core.Canvas; using Xui.Core.DI; using Xui.Core.Math2D; using Xui.Core.UI; using Xui.Core.UI.Input; using NFloat = System.Runtime.InteropServices.NFloat; public sealed class TransitionView : View { private NFloat position = (NFloat)0.15; private NFloat from; private NFloat target = (NFloat)0.15; private TimeSpan? start; private bool running; private IDeviceInfo? device; protected override void OnActivate() { device = this.GetService(); if (device is not null) device.Changed += OnPreferencesChanged; } protected override void OnDeactivate() { if (device is not null) device.Changed -= OnPreferencesChanged; device = null; running = false; position = target; start = null; InvalidateRender(); } private void OnPreferencesChanged() { if (device?.PrefersReducedMotion == true) { running = false; position = target; InvalidateRender(); } } public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase) { if (phase != EventPhase.Bubble || e.Type != PointerEventType.Down || e.State.Button != PointerButton.Left) return; from = position; target = target < (NFloat)0.5 ? (NFloat)0.85 : (NFloat)0.15; start = null; running = device?.PrefersReducedMotion != true; if (running) RequestAnimationFrame(); else position = target; InvalidateRender(); } protected override void AnimateCore(TimeSpan previousTime, TimeSpan currentTime) { if (!running) return; start ??= currentTime; NFloat t = NFloat.Clamp((NFloat)((currentTime - start.Value).TotalSeconds / 0.6), 0, 1); position = from + (target - from) * Easing.EaseInOutCubic(t); InvalidateRender(); if (t < 1) RequestAnimationFrame(); else { position = target; running = false; } } protected override void RenderCore(IContext context) { context.SetFill(new Color(0xFAF7FFFF)); context.FillRect(Frame); NFloat travel = NFloat.Max(0, Frame.Width - 64); context.SetFill(new Color(0xEADCF5FF)); context.FillRect(new Rect(Frame.X + 32, Frame.Center.Y - 2, travel, 4)); context.BeginPath(); context.Arc((Frame.X + 32 + travel * position, Frame.Center.Y), 24, 0, NFloat.Pi * 2); context.SetFill(new Color(0x8538CEFF)); context.Fill(); context.SetFont(new Font(18, "Inter")); context.TextAlign = TextAlign.Center; context.FillText("Tap to move; tap again to reverse", (Frame.Center.X, Frame.Y + 56)); } }