Browse Source

Add Paginator control

pull/165/head
Ionite 1 year ago
parent
commit
d7f8b559ca
No known key found for this signature in database
  1. 1
      StabilityMatrix.Avalonia/App.axaml
  2. 62
      StabilityMatrix.Avalonia/Controls/Paginator.axaml
  3. 166
      StabilityMatrix.Avalonia/Controls/Paginator.axaml.cs
  4. 10
      StabilityMatrix.Avalonia/Converters/StringFormatConverters.cs

1
StabilityMatrix.Avalonia/App.axaml

@ -57,6 +57,7 @@
<StyleInclude Source="Controls/SelectImageCard.axaml"/>
<StyleInclude Source="Controls/ImageFolderCard.axaml"/>
<StyleInclude Source="Controls/SharpenCard.axaml"/>
<StyleInclude Source="Controls/Paginator.axaml"/>
<Style Selector="DockControl">
<Setter Property="(DockProperties.ControlRecycling)" Value="{StaticResource ControlRecyclingKey}" />

62
StabilityMatrix.Avalonia/Controls/Paginator.axaml

@ -0,0 +1,62 @@
<Styles
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:StabilityMatrix.Avalonia.Controls"
xmlns:icons="clr-namespace:Projektanker.Icons.Avalonia;assembly=Projektanker.Icons.Avalonia"
xmlns:lang="clr-namespace:StabilityMatrix.Avalonia.Languages"
xmlns:converters="clr-namespace:StabilityMatrix.Avalonia.Converters">
<Design.PreviewWith>
<controls:Paginator TotalPages="5" CurrentPageNumber="1" />
</Design.PreviewWith>
<Style Selector="controls|Paginator">
<Setter Property="Template">
<ControlTemplate>
<StackPanel
Margin="8"
HorizontalAlignment="Center"
Orientation="Vertical">
<TextBlock Margin="0,0,4,4" TextAlignment="Center">
<Run Text="{x:Static lang:Resources.Label_Page}" />
<Run Text="{TemplateBinding CurrentPageNumber, Converter={x:Static converters:StringFormatConverters.Decimal}}" />
<Run Text="/" />
<Run Text="{TemplateBinding TotalPages, Converter={x:Static converters:StringFormatConverters.Decimal}}" />
</TextBlock>
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<Button
Margin="0,0,8,0"
IsEnabled="{TemplateBinding CanNavBack}"
Command="{TemplateBinding FirstPageCommand}"
ToolTip.ShowDelay="1000"
ToolTip.Tip="{x:Static lang:Resources.Label_FirstPage}">
<icons:Icon Value="fa-solid fa-backward-fast" />
</Button>
<Button
Margin="0,0,8,0"
IsEnabled="{TemplateBinding CanNavBack}"
Command="{TemplateBinding PreviousPageCommand}"
ToolTip.ShowDelay="1000"
ToolTip.Tip="{x:Static lang:Resources.Label_PreviousPage}">
<icons:Icon Value="fa-solid fa-caret-left" />
</Button>
<Button
Margin="0,0,8,0"
IsEnabled="{TemplateBinding CanNavForward}"
Command="{TemplateBinding NextPageCommand}"
ToolTip.ShowDelay="1000"
ToolTip.Tip="{x:Static lang:Resources.Label_NextPage}">
<icons:Icon Value="fa-solid fa-caret-right" />
</Button>
<Button
Command="{TemplateBinding LastPageCommand}"
IsEnabled="{TemplateBinding CanNavForward}"
ToolTip.ShowDelay="1000"
ToolTip.Tip="{x:Static lang:Resources.Label_LastPage}">
<icons:Icon Value="fa-solid fa-forward-fast" />
</Button>
</StackPanel>
</StackPanel>
</ControlTemplate>
</Setter>
</Style>
</Styles>

166
StabilityMatrix.Avalonia/Controls/Paginator.axaml.cs

@ -0,0 +1,166 @@
using System.Diagnostics.CodeAnalysis;
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls.Primitives;
using AvaloniaEdit.Utils;
using CommunityToolkit.Mvvm.Input;
namespace StabilityMatrix.Avalonia.Controls;
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public class Paginator : TemplatedControl
{
private bool isFirstTemplateApplied;
private ICommand? firstPageCommandBinding;
private ICommand? previousPageCommandBinding;
private ICommand? nextPageCommandBinding;
private ICommand? lastPageCommandBinding;
public static readonly StyledProperty<int> CurrentPageNumberProperty =
AvaloniaProperty.Register<Paginator, int>("CurrentPageNumber", 1);
public int CurrentPageNumber
{
get => GetValue(CurrentPageNumberProperty);
set => SetValue(CurrentPageNumberProperty, value);
}
public static readonly StyledProperty<int> TotalPagesProperty = AvaloniaProperty.Register<
Paginator,
int
>("TotalPages", 1);
public int TotalPages
{
get => GetValue(TotalPagesProperty);
set => SetValue(TotalPagesProperty, value);
}
public static readonly StyledProperty<ICommand?> FirstPageCommandProperty =
AvaloniaProperty.Register<Paginator, ICommand?>("FirstPageCommand");
public ICommand? FirstPageCommand
{
get => GetValue(FirstPageCommandProperty);
set => SetValue(FirstPageCommandProperty, value);
}
public static readonly StyledProperty<ICommand?> PreviousPageCommandProperty =
AvaloniaProperty.Register<Paginator, ICommand?>("PreviousPageCommand");
public ICommand? PreviousPageCommand
{
get => GetValue(PreviousPageCommandProperty);
set => SetValue(PreviousPageCommandProperty, value);
}
public static readonly StyledProperty<ICommand?> NextPageCommandProperty =
AvaloniaProperty.Register<Paginator, ICommand?>("NextPageCommand");
public ICommand? NextPageCommand
{
get => GetValue(NextPageCommandProperty);
set => SetValue(NextPageCommandProperty, value);
}
public static readonly StyledProperty<ICommand?> LastPageCommandProperty =
AvaloniaProperty.Register<Paginator, ICommand?>("LastPageCommand");
public ICommand? LastPageCommand
{
get => GetValue(LastPageCommandProperty);
set => SetValue(LastPageCommandProperty, value);
}
public static readonly StyledProperty<bool> CanNavForwardProperty = AvaloniaProperty.Register<
Paginator,
bool
>("CanNavForward");
public bool CanNavForward
{
get => GetValue(CanNavForwardProperty);
set => SetValue(CanNavForwardProperty, value);
}
public static readonly StyledProperty<bool> CanNavBackProperty = AvaloniaProperty.Register<
Paginator,
bool
>("CanNavBack");
public bool CanNavBack
{
get => GetValue(CanNavBackProperty);
set => SetValue(CanNavBackProperty, value);
}
/// <inheritdoc />
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
if (!isFirstTemplateApplied)
{
firstPageCommandBinding = FirstPageCommand;
previousPageCommandBinding = PreviousPageCommand;
nextPageCommandBinding = NextPageCommand;
lastPageCommandBinding = LastPageCommand;
isFirstTemplateApplied = true;
}
// Wrap the commands
FirstPageCommand = new RelayCommand(() =>
{
if (CurrentPageNumber > 1)
{
CurrentPageNumber = 1;
}
firstPageCommandBinding?.Execute(null);
});
PreviousPageCommand = new RelayCommand(() =>
{
if (CurrentPageNumber > 1)
{
CurrentPageNumber--;
}
previousPageCommandBinding?.Execute(null);
});
NextPageCommand = new RelayCommand(() =>
{
if (CurrentPageNumber < TotalPages)
{
CurrentPageNumber++;
}
nextPageCommandBinding?.Execute(null);
});
LastPageCommand = new RelayCommand(() =>
{
if (CurrentPageNumber < TotalPages)
{
CurrentPageNumber = TotalPages;
}
lastPageCommandBinding?.Execute(null);
});
}
/// <inheritdoc />
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
// Update the CanNavForward and CanNavBack properties
if (change.Property == CurrentPageNumberProperty && change.NewValue is int)
{
CanNavForward = (int)change.NewValue < TotalPages;
CanNavBack = (int)change.NewValue > 1;
}
else if (change.Property == TotalPagesProperty && change.NewValue is int)
{
CanNavForward = CurrentPageNumber < (int)change.NewValue;
CanNavBack = CurrentPageNumber > 1;
}
}
}

10
StabilityMatrix.Avalonia/Converters/StringFormatConverters.cs

@ -0,0 +1,10 @@
using Avalonia.Data.Converters;
namespace StabilityMatrix.Avalonia.Converters;
public static class StringFormatConverters
{
private static StringFormatValueConverter? _decimalConverter;
public static StringFormatValueConverter Decimal =>
_decimalConverter ??= new StringFormatValueConverter("{0:D}", null);
}
Loading…
Cancel
Save