Skip to content

Commit

Permalink
CQ: Fix Forward & Back buttons for RTL
Browse files Browse the repository at this point in the history
  • Loading branch information
XTorLukas committed Dec 1, 2024
1 parent d7b8474 commit 702f84b
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/Files.App/Helpers/Navigation/NavigationInteractionTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Files.App.Helpers
{
internal sealed class NavigationInteractionTracker : IDisposable
{
private static IRealTimeLayoutService RealTimeLayoutService => Ioc.Default.GetRequiredService<IRealTimeLayoutService>();

private static int RePos => RealTimeLayoutService.FlowDirection is FlowDirection.LeftToRight ? 1 : -1;
private static (float Min, float Max) RePosB => RealTimeLayoutService.FlowDirection is FlowDirection.LeftToRight ? (-96f, 0f) : (0f, 96f);
private static (float Min, float Max) RePosF => RealTimeLayoutService.FlowDirection is FlowDirection.LeftToRight ? (0f, 96f) : (-96f, 0f);

public bool CanNavigateForward
{
get
Expand All @@ -26,7 +32,11 @@ public bool CanNavigateForward
if (!_disposed)
{
_props.InsertBoolean(nameof(CanNavigateForward), value);
_tracker.MaxPosition = new(value ? 96f : 0f);
if (RePos == 1)
_tracker.MaxPosition = new(value ? 96 : 0);
else
_tracker.MinPosition = new(value ? -96 : 0);

}
}
}
Expand All @@ -43,7 +53,10 @@ public bool CanNavigateBackward
if (!_disposed)
{
_props.InsertBoolean(nameof(CanNavigateBackward), value);
_tracker.MinPosition = new(value ? -96f : 0f);
if (RePos == 1)
_tracker.MinPosition = new(value ? -96 : 0);
else
_tracker.MaxPosition = new(value ? 96 : 0);
}
}
}
Expand Down Expand Up @@ -89,6 +102,19 @@ public NavigationInteractionTracker(UIElement rootElement, UIElement backIcon, U

_pointerPressedHandler = new(PointerPressed);
_rootElement.AddHandler(UIElement.PointerPressedEvent, _pointerPressedHandler, true);

RealTimeLayoutService.FlowDirectionChanged += RealTimeLayoutService_FlowDirectionChanged;
}

private void RealTimeLayoutService_FlowDirectionChanged(object? sender, FlowDirection e)
{
var canBack = CanNavigateBackward;
var canForward = CanNavigateForward;

CanNavigateBackward = canBack;
CanNavigateForward = canForward;

SetupAnimations();
}

[MemberNotNull(nameof(_tracker), nameof(_source), nameof(_trackerOwner))]
Expand All @@ -113,17 +139,17 @@ private void SetupAnimations()
{
var compositor = _rootVisual.Compositor;

var backResistance = CreateResistanceCondition(-96f, 0f);
var forwardResistance = CreateResistanceCondition(0f, 96f);
var backResistance = CreateResistanceCondition(RePosB.Min, RePosB.Max);
var forwardResistance = CreateResistanceCondition(RePosF.Min, RePosF.Max);
List<CompositionConditionalValue> conditionalValues = [backResistance, forwardResistance];
_source.ConfigureDeltaPositionXModifiers(conditionalValues);

var backAnim = compositor.CreateExpressionAnimation("(-clamp(tracker.Position.X, -96, 0) * 2) - 48");
var backAnim = compositor.CreateExpressionAnimation($"(clamp(tracker.Position.X, {RePosB.Min}, {RePosB.Max}) * 2 * ({-RePos})) - 48");
backAnim.SetReferenceParameter("tracker", _tracker);
backAnim.SetReferenceParameter("props", _props);
_backVisual.StartAnimation("Translation.X", backAnim);

var forwardAnim = compositor.CreateExpressionAnimation("(-clamp(tracker.Position.X, 0, 96) * 2) + 48");
var forwardAnim = compositor.CreateExpressionAnimation($"(clamp(tracker.Position.X, {RePosF.Min}, {RePosF.Max}) * 2 * ({-RePos})) + 48");
forwardAnim.SetReferenceParameter("tracker", _tracker);
forwardAnim.SetReferenceParameter("props", _props);
_forwardVisual.StartAnimation("Translation.X", forwardAnim);
Expand Down Expand Up @@ -210,11 +236,11 @@ public void IdleStateEntered(InteractionTracker sender, InteractionTrackerIdleSt
EventHandler<OverscrollNavigationEventArgs>? navEvent = _parent.NavigationRequested;
if (navEvent is not null)
{
if (sender.Position.X > 0 && _parent.CanNavigateForward)
if (sender.Position.X * RePos > 0 && _parent.CanNavigateForward)
{
navEvent(_parent, OverscrollNavigationEventArgs.Forward);
}
else if (sender.Position.X < 0 && _parent.CanNavigateBackward)
else if (sender.Position.X * RePos < 0 && _parent.CanNavigateBackward)
{
navEvent(_parent, OverscrollNavigationEventArgs.Back);
}
Expand All @@ -238,12 +264,12 @@ public void ValuesChanged(InteractionTracker sender, InteractionTrackerValuesCha
if (!_shouldAnimate)
return;

if (args.Position.X <= -64)
if (args.Position.X * RePos <= -64)
{
_parent._backVisual.StartAnimation("Scale", _scaleAnimation);
_shouldAnimate = false;
}
else if (args.Position.X >= 64)
else if (args.Position.X * RePos >= 64)
{
_parent._forwardVisual.StartAnimation("Scale", _scaleAnimation);
_shouldAnimate = false;
Expand Down

0 comments on commit 702f84b

Please sign in to comment.