Skip to content

Commit 702f84b

Browse files
committed
CQ: Fix Forward & Back buttons for RTL
1 parent d7b8474 commit 702f84b

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

src/Files.App/Helpers/Navigation/NavigationInteractionTracker.cs

+36-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ namespace Files.App.Helpers
1414
{
1515
internal sealed class NavigationInteractionTracker : IDisposable
1616
{
17+
private static IRealTimeLayoutService RealTimeLayoutService => Ioc.Default.GetRequiredService<IRealTimeLayoutService>();
18+
19+
private static int RePos => RealTimeLayoutService.FlowDirection is FlowDirection.LeftToRight ? 1 : -1;
20+
private static (float Min, float Max) RePosB => RealTimeLayoutService.FlowDirection is FlowDirection.LeftToRight ? (-96f, 0f) : (0f, 96f);
21+
private static (float Min, float Max) RePosF => RealTimeLayoutService.FlowDirection is FlowDirection.LeftToRight ? (0f, 96f) : (-96f, 0f);
22+
1723
public bool CanNavigateForward
1824
{
1925
get
@@ -26,7 +32,11 @@ public bool CanNavigateForward
2632
if (!_disposed)
2733
{
2834
_props.InsertBoolean(nameof(CanNavigateForward), value);
29-
_tracker.MaxPosition = new(value ? 96f : 0f);
35+
if (RePos == 1)
36+
_tracker.MaxPosition = new(value ? 96 : 0);
37+
else
38+
_tracker.MinPosition = new(value ? -96 : 0);
39+
3040
}
3141
}
3242
}
@@ -43,7 +53,10 @@ public bool CanNavigateBackward
4353
if (!_disposed)
4454
{
4555
_props.InsertBoolean(nameof(CanNavigateBackward), value);
46-
_tracker.MinPosition = new(value ? -96f : 0f);
56+
if (RePos == 1)
57+
_tracker.MinPosition = new(value ? -96 : 0);
58+
else
59+
_tracker.MaxPosition = new(value ? 96 : 0);
4760
}
4861
}
4962
}
@@ -89,6 +102,19 @@ public NavigationInteractionTracker(UIElement rootElement, UIElement backIcon, U
89102

90103
_pointerPressedHandler = new(PointerPressed);
91104
_rootElement.AddHandler(UIElement.PointerPressedEvent, _pointerPressedHandler, true);
105+
106+
RealTimeLayoutService.FlowDirectionChanged += RealTimeLayoutService_FlowDirectionChanged;
107+
}
108+
109+
private void RealTimeLayoutService_FlowDirectionChanged(object? sender, FlowDirection e)
110+
{
111+
var canBack = CanNavigateBackward;
112+
var canForward = CanNavigateForward;
113+
114+
CanNavigateBackward = canBack;
115+
CanNavigateForward = canForward;
116+
117+
SetupAnimations();
92118
}
93119

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

116-
var backResistance = CreateResistanceCondition(-96f, 0f);
117-
var forwardResistance = CreateResistanceCondition(0f, 96f);
142+
var backResistance = CreateResistanceCondition(RePosB.Min, RePosB.Max);
143+
var forwardResistance = CreateResistanceCondition(RePosF.Min, RePosF.Max);
118144
List<CompositionConditionalValue> conditionalValues = [backResistance, forwardResistance];
119145
_source.ConfigureDeltaPositionXModifiers(conditionalValues);
120146

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

126-
var forwardAnim = compositor.CreateExpressionAnimation("(-clamp(tracker.Position.X, 0, 96) * 2) + 48");
152+
var forwardAnim = compositor.CreateExpressionAnimation($"(clamp(tracker.Position.X, {RePosF.Min}, {RePosF.Max}) * 2 * ({-RePos})) + 48");
127153
forwardAnim.SetReferenceParameter("tracker", _tracker);
128154
forwardAnim.SetReferenceParameter("props", _props);
129155
_forwardVisual.StartAnimation("Translation.X", forwardAnim);
@@ -210,11 +236,11 @@ public void IdleStateEntered(InteractionTracker sender, InteractionTrackerIdleSt
210236
EventHandler<OverscrollNavigationEventArgs>? navEvent = _parent.NavigationRequested;
211237
if (navEvent is not null)
212238
{
213-
if (sender.Position.X > 0 && _parent.CanNavigateForward)
239+
if (sender.Position.X * RePos > 0 && _parent.CanNavigateForward)
214240
{
215241
navEvent(_parent, OverscrollNavigationEventArgs.Forward);
216242
}
217-
else if (sender.Position.X < 0 && _parent.CanNavigateBackward)
243+
else if (sender.Position.X * RePos < 0 && _parent.CanNavigateBackward)
218244
{
219245
navEvent(_parent, OverscrollNavigationEventArgs.Back);
220246
}
@@ -238,12 +264,12 @@ public void ValuesChanged(InteractionTracker sender, InteractionTrackerValuesCha
238264
if (!_shouldAnimate)
239265
return;
240266

241-
if (args.Position.X <= -64)
267+
if (args.Position.X * RePos <= -64)
242268
{
243269
_parent._backVisual.StartAnimation("Scale", _scaleAnimation);
244270
_shouldAnimate = false;
245271
}
246-
else if (args.Position.X >= 64)
272+
else if (args.Position.X * RePos >= 64)
247273
{
248274
_parent._forwardVisual.StartAnimation("Scale", _scaleAnimation);
249275
_shouldAnimate = false;

0 commit comments

Comments
 (0)