Skip to content

Commit

Permalink
General performance improvements (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
IeuanWalker committed Jun 20, 2024
1 parent 79ff4b7 commit fb1c94a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
5 changes: 1 addition & 4 deletions Scr/Breadcrumb.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<ContentView x:Class="Breadcrumb.Breadcrumb"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Padding="4"
AutomationProperties.IsInAccessibleTree="False">
<ContentView.Content>
<ScrollView x:Name="BreadCrumbsScrollView"
Expand All @@ -12,9 +11,7 @@
Orientation="Horizontal">
<HorizontalStackLayout x:Name="BreadCrumbContainer"
AutomationProperties.IsInAccessibleTree="False"
HorizontalOptions="Fill"
Loaded="BreadCrumbContainer_Loaded"
VerticalOptions="Center" />
Loaded="BreadCrumbContainer_Loaded" />
</ScrollView>
</ContentView.Content>
</ContentView>
50 changes: 20 additions & 30 deletions Scr/Breadcrumb.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class Breadcrumb : ContentView
#region Control properties

// Separator
public static readonly BindableProperty SeparatorProperty = BindableProperty.Create(nameof(Separator), typeof(ImageSource), typeof(Breadcrumb), new FontImageSource { Glyph = " / ", Color = Colors.Black, Size = 15, });
public static readonly BindableProperty SeparatorProperty = BindableProperty.Create(nameof(Separator), typeof(ImageSource), typeof(Breadcrumb), new FontImageSource { Glyph = " / ", Color = Colors.Black, Size = 15, }, BindingMode.OneTime);

public ImageSource Separator
{
Expand All @@ -18,7 +18,7 @@ public ImageSource Separator
}

// Separator height
public static readonly BindableProperty SeparatorHeightProperty = BindableProperty.Create(nameof(SeparatorHeight), typeof(double), typeof(Breadcrumb), 15d);
public static readonly BindableProperty SeparatorHeightProperty = BindableProperty.Create(nameof(SeparatorHeight), typeof(double), typeof(Breadcrumb), 15d, BindingMode.OneTime);

public double SeparatorHeight
{
Expand All @@ -27,7 +27,7 @@ public double SeparatorHeight
}

// FirstBreadCrumb
public static readonly BindableProperty FirstBreadcrumbProperty = BindableProperty.Create(nameof(FirstBreadcrumb), typeof(ImageSource), typeof(Breadcrumb), null);
public static readonly BindableProperty FirstBreadcrumbProperty = BindableProperty.Create(nameof(FirstBreadcrumb), typeof(ImageSource), typeof(Breadcrumb), null, BindingMode.OneTime);

public ImageSource FirstBreadcrumb
{
Expand All @@ -36,7 +36,7 @@ public ImageSource FirstBreadcrumb
}

// Scrollbar Visibility
public static readonly BindableProperty ScrollBarVisibilityProperty = BindableProperty.Create(nameof(ScrollBarVisibility), typeof(ScrollBarVisibility), typeof(Breadcrumb), ScrollBarVisibility.Never);
public static readonly BindableProperty ScrollBarVisibilityProperty = BindableProperty.Create(nameof(ScrollBarVisibility), typeof(ScrollBarVisibility), typeof(Breadcrumb), ScrollBarVisibility.Never, BindingMode.OneTime);

public ScrollBarVisibility ScrollBarVisibility
{
Expand All @@ -45,7 +45,7 @@ public ScrollBarVisibility ScrollBarVisibility
}

// FontSize
public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(double), typeof(Breadcrumb), 15d);
public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(double), typeof(Breadcrumb), 15d, BindingMode.OneTime);

[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
Expand All @@ -55,7 +55,7 @@ public double FontSize
}

// Text Color
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(Breadcrumb), Colors.Black);
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(Breadcrumb), Colors.Black, BindingMode.OneTime);

public Color TextColor
{
Expand All @@ -64,7 +64,7 @@ public Color TextColor
}

// Corner radius
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(Breadcrumb), 10f);
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(Breadcrumb), 10f, BindingMode.OneTime);

public float CornerRadius
{
Expand All @@ -73,7 +73,7 @@ public float CornerRadius
}

// Breadcrumb margin
public static readonly BindableProperty BreadcrumbMarginProperty = BindableProperty.Create(nameof(BreadcrumbMargin), typeof(Thickness), typeof(Breadcrumb), new Thickness(0));
public static readonly BindableProperty BreadcrumbMarginProperty = BindableProperty.Create(nameof(BreadcrumbMargin), typeof(Thickness), typeof(Breadcrumb), new Thickness(0), BindingMode.OneTime);

public Thickness BreadcrumbMargin
{
Expand All @@ -82,7 +82,7 @@ public Thickness BreadcrumbMargin
}

// BreadcrumbBackgroundColor
public static readonly BindableProperty BreadcrumbBackgroundColorProperty = BindableProperty.Create(nameof(BreadcrumbBackgroundColor), typeof(Color), typeof(Breadcrumb), Colors.Transparent);
public static readonly BindableProperty BreadcrumbBackgroundColorProperty = BindableProperty.Create(nameof(BreadcrumbBackgroundColor), typeof(Color), typeof(Breadcrumb), Colors.Transparent, BindingMode.OneTime);

public Color BreadcrumbBackgroundColor
{
Expand All @@ -91,7 +91,7 @@ public Color BreadcrumbBackgroundColor
}

// LastBreadcrumbTextColor
public static readonly BindableProperty LastBreadcrumbTextColorProperty = BindableProperty.Create(nameof(LastBreadcrumbTextColor), typeof(Color), typeof(Breadcrumb), Colors.Black);
public static readonly BindableProperty LastBreadcrumbTextColorProperty = BindableProperty.Create(nameof(LastBreadcrumbTextColor), typeof(Color), typeof(Breadcrumb), Colors.Black, BindingMode.OneTime);

public Color LastBreadcrumbTextColor
{
Expand All @@ -100,7 +100,7 @@ public Color LastBreadcrumbTextColor
}

// LastBreadcrumbCornerRadius
public static readonly BindableProperty LastBreadcrumbCornerRadiusProperty = BindableProperty.Create(nameof(LastBreadcrumbCornerRadius), typeof(float), typeof(Breadcrumb), 10f);
public static readonly BindableProperty LastBreadcrumbCornerRadiusProperty = BindableProperty.Create(nameof(LastBreadcrumbCornerRadius), typeof(float), typeof(Breadcrumb), 10f, BindingMode.OneTime);

public float LastBreadcrumbCornerRadius
{
Expand All @@ -109,7 +109,7 @@ public float LastBreadcrumbCornerRadius
}

// LastBreadcrumbBackgroundColor
public static readonly BindableProperty LastBreadcrumbBackgroundColorProperty = BindableProperty.Create(nameof(LastBreadcrumbBackgroundColor), typeof(Color), typeof(Breadcrumb), Colors.Transparent);
public static readonly BindableProperty LastBreadcrumbBackgroundColorProperty = BindableProperty.Create(nameof(LastBreadcrumbBackgroundColor), typeof(Color), typeof(Breadcrumb), Colors.Transparent, BindingMode.OneTime);

public Color LastBreadcrumbBackgroundColor
{
Expand All @@ -118,7 +118,7 @@ public Color LastBreadcrumbBackgroundColor
}

// AnimationSpeed
public static readonly BindableProperty AnimationSpeedProperty = BindableProperty.Create(nameof(AnimationSpeed), typeof(uint), typeof(Breadcrumb), (uint)800);
public static readonly BindableProperty AnimationSpeedProperty = BindableProperty.Create(nameof(AnimationSpeed), typeof(uint), typeof(Breadcrumb), (uint)800, BindingMode.OneTime);

public uint AnimationSpeed
{
Expand All @@ -127,7 +127,7 @@ public uint AnimationSpeed
}

// IsNavigationEnabled
public static readonly BindableProperty IsNavigationEnabledProperty = BindableProperty.Create(nameof(IsNavigationEnabled), typeof(bool), typeof(Breadcrumb), true);
public static readonly BindableProperty IsNavigationEnabledProperty = BindableProperty.Create(nameof(IsNavigationEnabled), typeof(bool), typeof(Breadcrumb), true, BindingMode.OneTime);

public bool IsNavigationEnabled
{
Expand All @@ -142,7 +142,7 @@ public Breadcrumb()
InitializeComponent();
}

async void BreadCrumbContainer_Loaded(object sender, EventArgs e)
async void BreadCrumbContainer_Loaded(object? sender, EventArgs e)
{
BreadCrumbContainer.Loaded -= BreadCrumbContainer_Loaded;

Expand Down Expand Up @@ -208,9 +208,6 @@ async void BreadCrumbContainer_Loaded(object sender, EventArgs e)
/// <summary>
/// Creates a new Breadcrumb object
/// </summary>
/// <param name="page"></param>
/// <param name="isLast"></param>
/// <param name="isFirst"></param>
Border BreadcrumbCreator(Page page, bool isLast, bool isFirst)
{
// Create border control for the breadcrumb
Expand All @@ -222,13 +219,12 @@ Border BreadcrumbCreator(Page page, bool isLast, bool isFirst)
}
: new Border();

container.BackgroundColor = Colors.Transparent;
container.StrokeShape = new RoundRectangle
{
CornerRadius = isLast ? LastBreadcrumbCornerRadius : CornerRadius
};
container.Padding = 10;
container.Stroke = Colors.Transparent;
container.StrokeThickness = 0;
container.Margin = BreadcrumbMargin;
container.VerticalOptions = LayoutOptions.Center;
container.SetBinding(BackgroundColorProperty, new Binding(isLast ? nameof(LastBreadcrumbBackgroundColor) : nameof(BreadcrumbBackgroundColor), source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(Breadcrumb))));
Expand All @@ -239,18 +235,15 @@ Border BreadcrumbCreator(Page page, bool isLast, bool isFirst)
{
container.Content = new Image
{
Source = FirstBreadcrumb,
VerticalOptions = LayoutOptions.Center
Source = FirstBreadcrumb
};
}
else
{
Label breadcrumbText = new()
{
Text = page.Title,
FontSize = FontSize,
VerticalOptions = LayoutOptions.Center,
VerticalTextAlignment = TextAlignment.Center
FontSize = FontSize
};
breadcrumbText.SetBinding(Label.TextColorProperty, new Binding(isLast ? nameof(LastBreadcrumbTextColor) : nameof(TextColor), source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(Breadcrumb))));
AutomationProperties.SetIsInAccessibleTree(breadcrumbText, false);
Expand All @@ -264,8 +257,6 @@ Border BreadcrumbCreator(Page page, bool isLast, bool isFirst)
/// <summary>
/// Animates item added to stack
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
async void AnimatedStack_ChildAdded(object? sender, ElementEventArgs e)
{
// iOS scroll to end fix
Expand All @@ -276,10 +267,10 @@ async void AnimatedStack_ChildAdded(object? sender, ElementEventArgs e)

Animation lastBreadcrumbAnimation = new()
{
{ 0, 1, new Animation(_ => ((View)BreadCrumbContainer.Children.Last()).TranslationX = _, Application.Current?.MainPage?.Width ?? 0, 0, Easing.Linear) }
{ 0, 1, new Animation(_ => ((View)BreadCrumbContainer.Children[^1]).TranslationX = _, Application.Current?.MainPage?.Width ?? 0, 0, Easing.Linear) }
};

Point point = BreadCrumbsScrollView.GetScrollPositionForElement((View)BreadCrumbContainer.Children.Last(), ScrollToPosition.End);
Point point = BreadCrumbsScrollView.GetScrollPositionForElement((View)BreadCrumbContainer.Children[^1], ScrollToPosition.End);
lastBreadcrumbAnimation.Add(0, 1, new Animation(_ => BreadCrumbsScrollView.ScrollToAsync((View?)BreadCrumbContainer.Children.LastOrDefault(), ScrollToPosition.MakeVisible, true), BreadCrumbsScrollView.X, point.X - 6));

lastBreadcrumbAnimation.Commit(this, nameof(lastBreadcrumbAnimation), 16, AnimationSpeed);
Expand All @@ -288,7 +279,6 @@ async void AnimatedStack_ChildAdded(object? sender, ElementEventArgs e)
/// <summary>
/// Navigates the user back to chosen selectedPage in the Navigation stack
/// </summary>
/// <param name="selectedPage"></param>
async Task GoBack(Page selectedPage)
{
// Check if selectedPage is still in Navigation Stack
Expand Down

0 comments on commit fb1c94a

Please sign in to comment.