Skip to content

Commit

Permalink
TabsControl updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiMisiukevich committed Oct 3, 2019
1 parent 4d29277 commit 583ea00
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
70 changes: 42 additions & 28 deletions PanCardView/Controls/TabsControl.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Threading;
Expand Down Expand Up @@ -70,21 +71,13 @@ static TabsControl()
{
}

private readonly BoxView _currentItemStripeView = new BoxView();
private readonly BoxView _nextItemStripeView = new BoxView();
private CancellationTokenSource _fadeAnimationTokenSource;

private readonly StackLayout _itemsStackLayout = new StackLayout
{
Spacing = 0,
Orientation = StackOrientation.Horizontal,
};

public TabsControl()
{
Children.Add(_itemsStackLayout, new Rectangle(0, 0, 1, 1), AbsoluteLayoutFlags.All);
Children.Add(_currentItemStripeView, new Rectangle(0, 1, 0, 0), AbsoluteLayoutFlags.YProportional);
Children.Add(_nextItemStripeView, new Rectangle(0, 1, 0, 0), AbsoluteLayoutFlags.YProportional);
Children.Add(ItemsStackLayout, new Rectangle(0, 0, 1, 1), AbsoluteLayoutFlags.All);
Children.Add(MainStripeView, new Rectangle(0, 1, 0, 0), AbsoluteLayoutFlags.YProportional);
Children.Add(AdditionalStripeView, new Rectangle(0, 1, 0, 0), AbsoluteLayoutFlags.YProportional);

this.SetBinding(DiffProperty, nameof(CardsView.ProcessorDiff));
this.SetBinding(MaxDiffProperty, nameof(Width));
Expand All @@ -99,6 +92,16 @@ public TabsControl()
Behaviors.Add(new ProtectedControlBehavior());
}

private StackLayout ItemsStackLayout { get; } = new StackLayout
{
Spacing = 0,
Orientation = StackOrientation.Horizontal
};

private BoxView MainStripeView { get; set; } = new BoxView();

private BoxView AdditionalStripeView { get; set; } = new BoxView();

public double Diff
{
get => (double)GetValue(DiffProperty);
Expand Down Expand Up @@ -250,7 +253,7 @@ private void ResetItemsLayout()
try
{
BatchBegin();
_itemsStackLayout.Children.Clear();
ItemsStackLayout.Children.Clear();
if (ItemsSource == null)
{
return;
Expand All @@ -274,7 +277,7 @@ private void ResetItemsLayout()
SelectedIndex = ItemsSource.FindIndex(p);
})
});
_itemsStackLayout.Children.Add(itemView);
ItemsStackLayout.Children.Add(itemView);
}

ResetStripeViewNonBatch();
Expand Down Expand Up @@ -319,9 +322,9 @@ private void ResetStripeView()

private void ResetStripeViewNonBatch()
{
_itemsStackLayout.Margin = new Thickness(0, 0, 0, StripeHeight);
_currentItemStripeView.Color = StripeColor;
_nextItemStripeView.Color = StripeColor;
ItemsStackLayout.Margin = new Thickness(0, 0, 0, StripeHeight);
MainStripeView.Color = StripeColor;
AdditionalStripeView.Color = StripeColor;
}

private void UpdateStripePosition()
Expand Down Expand Up @@ -364,8 +367,8 @@ private void UpdateStripePositionNonBatch()

var itemProgress = Min(Abs(diff) / MaxDiff, 1);

var currentItemView = _itemsStackLayout.Children[SelectedIndex];
var affectedItemView = _itemsStackLayout.Children[affectedIndex];
var currentItemView = ItemsStackLayout.Children[SelectedIndex];
var affectedItemView = ItemsStackLayout.Children[affectedIndex];
if (diff <= 0)
{
CalculateStripePosition(currentItemView, affectedItemView, itemProgress, selectedIndex > affectedIndex);
Expand All @@ -374,18 +377,29 @@ private void UpdateStripePositionNonBatch()
CalculateStripePosition(affectedItemView, currentItemView, 1 - itemProgress, selectedIndex < affectedIndex);
}

private void CalculateStripePosition(View firstView, View secondView, double itemProgress, bool needSecondStripe)
private void CalculateStripePosition(View firstView, View secondView, double itemProgress, bool isSecondStripeVisible)
{
var x = firstView.X + firstView.Width * itemProgress;
var width = firstView.Width * (1 - itemProgress) + secondView.Width * itemProgress;
SetLayoutBounds(_currentItemStripeView, new Rectangle(x, 1, width, StripeHeight));
if (needSecondStripe)
if(itemProgress <= 0 &&
GetLayoutBounds(AdditionalStripeView).Width >
GetLayoutBounds(MainStripeView).Width)
{
_nextItemStripeView.IsVisible = true;
SetLayoutBounds(_nextItemStripeView, new Rectangle(secondView.X, 1, secondView.Width * itemProgress, StripeHeight));
return;
SwapStripeViews();
}
_nextItemStripeView.IsVisible = false;

AdditionalStripeView.IsVisible = isSecondStripeVisible;
var additionalStripeWidth = isSecondStripeVisible ? secondView.Width * itemProgress : 0;
SetLayoutBounds(AdditionalStripeView, new Rectangle(secondView.X, 1, additionalStripeWidth, StripeHeight));

var x = firstView.X + firstView.Width * itemProgress;
var mainStripewidth = firstView.Width * (1 - itemProgress) + secondView.Width * itemProgress - additionalStripeWidth;
SetLayoutBounds(MainStripeView, new Rectangle(x, 1, mainStripewidth, StripeHeight));
}

private void SwapStripeViews()
{
var view = MainStripeView;
MainStripeView = AdditionalStripeView;
AdditionalStripeView = view;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
VerticalOptions="CenterAndExpand"
FontSize="24"
FontAttributes="Bold"
TextColor="CadetBlue"
TextColor="Gold"
Text="{Binding Title}"/>

</DataTemplate>
Expand Down

0 comments on commit 583ea00

Please sign in to comment.