feat: make CurrentPage independent.
This commit is contained in:
@@ -35,6 +35,7 @@
|
|||||||
MinWidth="0"
|
MinWidth="0"
|
||||||
HorizontalContentAlignment="Stretch"
|
HorizontalContentAlignment="Stretch"
|
||||||
VerticalContentAlignment="Stretch"
|
VerticalContentAlignment="Stretch"
|
||||||
|
ShowButtonSpinner="{TemplateBinding ShowButtonSpinner}"
|
||||||
AllowSpin="{TemplateBinding AllowSpin}"
|
AllowSpin="{TemplateBinding AllowSpin}"
|
||||||
Background="{TemplateBinding Background}"
|
Background="{TemplateBinding Background}"
|
||||||
BorderBrush="{TemplateBinding BorderBrush}"
|
BorderBrush="{TemplateBinding BorderBrush}"
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
<Setter Property="Template">
|
<Setter Property="Template">
|
||||||
<ControlTemplate TargetType="u:Pagination">
|
<ControlTemplate TargetType="u:Pagination">
|
||||||
<StackPanel Orientation="Horizontal">
|
<StackPanel Orientation="Horizontal">
|
||||||
<u:PaginationButton Name="{x:Static u:Pagination.PART_PreviousButton}">
|
<u:PaginationButton u:DisabledAdorner.IsEnabled="True" Name="{x:Static u:Pagination.PART_PreviousButton}">
|
||||||
<PathIcon
|
<PathIcon
|
||||||
Width="12"
|
Width="12"
|
||||||
Height="12"
|
Height="12"
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
Foreground="{DynamicResource PaginationButtonIconForeground}" />
|
||||||
</u:PaginationButton>
|
</u:PaginationButton>
|
||||||
<StackPanel Name="{x:Static u:Pagination.PART_ButtonPanel}" Orientation="Horizontal" />
|
<StackPanel Name="{x:Static u:Pagination.PART_ButtonPanel}" Orientation="Horizontal" />
|
||||||
<u:PaginationButton Name="{x:Static u:Pagination.PART_NextButton}">
|
<u:PaginationButton u:DisabledAdorner.IsEnabled="True" Name="{x:Static u:Pagination.PART_NextButton}">
|
||||||
<PathIcon
|
<PathIcon
|
||||||
Width="12"
|
Width="12"
|
||||||
Height="12"
|
Height="12"
|
||||||
@@ -114,9 +114,9 @@
|
|||||||
</Style>
|
</Style>
|
||||||
</Style>
|
</Style>
|
||||||
<Style Selector="^:selected">
|
<Style Selector="^:selected">
|
||||||
<Setter Property="u:PaginationButton.Background" Value="{DynamicResource PaginationButtonSelectedBackground}" />
|
<Setter Property="Background" Value="{DynamicResource PaginationButtonSelectedBackground}" />
|
||||||
<Setter Property="u:PaginationButton.Foreground" Value="{DynamicResource PaginationButtonSelectedForeground}" />
|
<Setter Property="Foreground" Value="{DynamicResource PaginationButtonSelectedForeground}" />
|
||||||
<Setter Property="u:PaginationButton.FontWeight" Value="600" />
|
<Setter Property="FontWeight" Value="600" />
|
||||||
</Style>
|
</Style>
|
||||||
</ControlTheme>
|
</ControlTheme>
|
||||||
|
|
||||||
|
|||||||
@@ -110,6 +110,15 @@ public abstract class NumericUpDown : TemplatedControl, IClearControl
|
|||||||
set => SetValue(AllowSpinProperty, value);
|
set => SetValue(AllowSpinProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<bool> ShowButtonSpinnerProperty =
|
||||||
|
ButtonSpinner.ShowButtonSpinnerProperty.AddOwner<NumericUpDown>();
|
||||||
|
|
||||||
|
public bool ShowButtonSpinner
|
||||||
|
{
|
||||||
|
get => GetValue(ShowButtonSpinnerProperty);
|
||||||
|
set => SetValue(ShowButtonSpinnerProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
public event EventHandler<SpinEventArgs>? Spinned;
|
public event EventHandler<SpinEventArgs>? Spinned;
|
||||||
|
|
||||||
static NumericUpDown()
|
static NumericUpDown()
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ using Avalonia.Controls.Metadata;
|
|||||||
using Avalonia.Controls.Primitives;
|
using Avalonia.Controls.Primitives;
|
||||||
using Avalonia.Interactivity;
|
using Avalonia.Interactivity;
|
||||||
using Avalonia.Styling;
|
using Avalonia.Styling;
|
||||||
|
using Avalonia.Utilities;
|
||||||
|
using Irihi.Avalonia.Shared.Helpers;
|
||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
@@ -29,26 +31,10 @@ public class Pagination: TemplatedControl
|
|||||||
private readonly PaginationButton[] _buttons = new PaginationButton[7];
|
private readonly PaginationButton[] _buttons = new PaginationButton[7];
|
||||||
private ComboBox? _sizeChangerComboBox;
|
private ComboBox? _sizeChangerComboBox;
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
public static readonly StyledProperty<int?> CurrentPageProperty = AvaloniaProperty.Register<Pagination, int?>(
|
||||||
{
|
|
||||||
base.OnApplyTemplate(e);
|
|
||||||
if (_previousButton != null) _previousButton.Click -= OnButtonClick;
|
|
||||||
if (_nextButton != null) _nextButton.Click -= OnButtonClick;
|
|
||||||
_previousButton = e.NameScope.Find<PaginationButton>(PART_PreviousButton);
|
|
||||||
_nextButton = e.NameScope.Find<PaginationButton>(PART_NextButton);
|
|
||||||
_buttonPanel = e.NameScope.Find<StackPanel>(PART_ButtonPanel);
|
|
||||||
_sizeChangerComboBox = e.NameScope.Find<ComboBox>(PART_SizeChangerComboBox);
|
|
||||||
if (_previousButton != null) _previousButton.Click += OnButtonClick;
|
|
||||||
if (_nextButton != null) _nextButton.Click += OnButtonClick;
|
|
||||||
InitializePanelButtons();
|
|
||||||
UpdateButtons(0);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<int> CurrentPageProperty = AvaloniaProperty.Register<Pagination, int>(
|
|
||||||
nameof(CurrentPage));
|
nameof(CurrentPage));
|
||||||
|
|
||||||
public int CurrentPage
|
public int? CurrentPage
|
||||||
{
|
{
|
||||||
get => GetValue(CurrentPageProperty);
|
get => GetValue(CurrentPageProperty);
|
||||||
set => SetValue(CurrentPageProperty, value);
|
set => SetValue(CurrentPageProperty, value);
|
||||||
@@ -122,39 +108,57 @@ public class Pagination: TemplatedControl
|
|||||||
public static readonly StyledProperty<bool> ShowQuickJumpProperty = AvaloniaProperty.Register<Pagination, bool>(
|
public static readonly StyledProperty<bool> ShowQuickJumpProperty = AvaloniaProperty.Register<Pagination, bool>(
|
||||||
nameof(ShowQuickJump));
|
nameof(ShowQuickJump));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This feature is not implemented yet.
|
||||||
|
/// </summary>
|
||||||
public bool ShowQuickJump
|
public bool ShowQuickJump
|
||||||
{
|
{
|
||||||
get => GetValue(ShowQuickJumpProperty);
|
get => GetValue(ShowQuickJumpProperty);
|
||||||
set => SetValue(ShowQuickJumpProperty, value);
|
set => SetValue(ShowQuickJumpProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
static Pagination()
|
||||||
{
|
{
|
||||||
base.OnPropertyChanged(change);
|
PageSizeProperty.Changed.AddClassHandler<Pagination, int>((pagination, args)=>pagination.OnPageSizeChanged(args));
|
||||||
// When page size is updated, the selected current page must be updated.
|
CurrentPageProperty.Changed.AddClassHandler<Pagination, int?>((pagination, args) =>
|
||||||
if (change.Property == PageSizeProperty)
|
pagination.UpdateButtonsByCurrentPage(args.NewValue.Value));
|
||||||
{
|
TotalCountProperty.Changed.AddClassHandler<Pagination, int>((pagination, args) =>
|
||||||
int oldPageSize = change.GetOldValue<int>();
|
pagination.UpdateButtonsByCurrentPage(pagination.CurrentPage));
|
||||||
int index = oldPageSize * CurrentPage;
|
|
||||||
UpdateButtons(index);
|
|
||||||
}
|
}
|
||||||
else if (change.Property == TotalCountProperty || change.Property == CurrentPageProperty)
|
|
||||||
|
private void OnPageSizeChanged(AvaloniaPropertyChangedEventArgs<int> args)
|
||||||
{
|
{
|
||||||
int index = PageSize * CurrentPage;
|
int pageCount = TotalCount / args.NewValue.Value;
|
||||||
UpdateButtons(index);
|
int residue = TotalCount % args.NewValue.Value;
|
||||||
|
if (residue > 0)
|
||||||
|
{
|
||||||
|
pageCount++;
|
||||||
}
|
}
|
||||||
|
PageCount = pageCount;
|
||||||
|
if (CurrentPage > PageCount)
|
||||||
|
{
|
||||||
|
CurrentPage = null;
|
||||||
|
}
|
||||||
|
UpdateButtonsByCurrentPage(CurrentPage);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnApplyTemplate(e);
|
||||||
|
Button.ClickEvent.AddHandler(OnButtonClick, _previousButton, _nextButton);
|
||||||
|
_previousButton = e.NameScope.Find<PaginationButton>(PART_PreviousButton);
|
||||||
|
_nextButton = e.NameScope.Find<PaginationButton>(PART_NextButton);
|
||||||
|
_buttonPanel = e.NameScope.Find<StackPanel>(PART_ButtonPanel);
|
||||||
|
_sizeChangerComboBox = e.NameScope.Find<ComboBox>(PART_SizeChangerComboBox);
|
||||||
|
Button.ClickEvent.AddHandler(OnButtonClick, _previousButton, _nextButton);
|
||||||
|
InitializePanelButtons();
|
||||||
|
UpdateButtonsByCurrentPage(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnButtonClick(object? sender, RoutedEventArgs e)
|
private void OnButtonClick(object? sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (Equals(sender, _previousButton))
|
var diff = Equals(sender, _previousButton) ? -1 : 1;
|
||||||
{
|
AddCurrentPage(diff);
|
||||||
AddCurrentPage(-1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
AddCurrentPage(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitializePanelButtons()
|
private void InitializePanelButtons()
|
||||||
@@ -166,7 +170,7 @@ public class Pagination: TemplatedControl
|
|||||||
var button = new PaginationButton() { Page = i, IsVisible = true };
|
var button = new PaginationButton() { Page = i, IsVisible = true };
|
||||||
_buttonPanel.Children.Add(button);
|
_buttonPanel.Children.Add(button);
|
||||||
_buttons![i - 1] = button;
|
_buttons![i - 1] = button;
|
||||||
button.Click+= OnPageButtonClick;
|
Button.ClickEvent.AddHandler(OnPageButtonClick, button);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,19 +195,26 @@ public class Pagination: TemplatedControl
|
|||||||
|
|
||||||
private void AddCurrentPage(int pageChange)
|
private void AddCurrentPage(int pageChange)
|
||||||
{
|
{
|
||||||
int newValue = CurrentPage + pageChange;
|
int newValue = (CurrentPage ?? 0) + pageChange;
|
||||||
if (newValue <= 0) newValue = 1;
|
newValue = Clamp(newValue, 1, PageCount);
|
||||||
else if(newValue>=PageCount) newValue = PageCount;
|
; SetCurrentValue(CurrentPageProperty, newValue);
|
||||||
CurrentPage = newValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateButtons(int index)
|
private int Clamp(int value, int min, int max)
|
||||||
|
{
|
||||||
|
return value < min ? min : value > max ? max : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update Button Content and Visibility by current page.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="page"></param>
|
||||||
|
private void UpdateButtonsByCurrentPage(int? page)
|
||||||
{
|
{
|
||||||
if (_buttonPanel is null) return;
|
if (_buttonPanel is null) return;
|
||||||
if (PageSize == 0) return;
|
if (PageSize == 0) return;
|
||||||
|
|
||||||
int currentIndex = index;
|
int? currentPage = CurrentPage;
|
||||||
|
|
||||||
int pageCount = TotalCount / PageSize;
|
int pageCount = TotalCount / PageSize;
|
||||||
int residue = TotalCount % PageSize;
|
int residue = TotalCount % PageSize;
|
||||||
if (residue > 0)
|
if (residue > 0)
|
||||||
@@ -211,11 +222,6 @@ public class Pagination: TemplatedControl
|
|||||||
pageCount++;
|
pageCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
PageCount = pageCount;
|
|
||||||
|
|
||||||
int currentPage = currentIndex/ PageSize;
|
|
||||||
if (currentPage == 0) currentPage++;
|
|
||||||
|
|
||||||
if (pageCount <= 7)
|
if (pageCount <= 7)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 7; i++)
|
for (int i = 0; i < 7; i++)
|
||||||
@@ -237,9 +243,8 @@ public class Pagination: TemplatedControl
|
|||||||
{
|
{
|
||||||
_buttons[i].IsVisible = true;
|
_buttons[i].IsVisible = true;
|
||||||
}
|
}
|
||||||
int mid = currentPage;
|
int mid = currentPage ?? 0;
|
||||||
if (mid < 4) mid = 4;
|
mid = Clamp(mid, 4, pageCount - 3);
|
||||||
else if (mid > pageCount - 3) mid = pageCount - 3;
|
|
||||||
_buttons[3].Page = mid;
|
_buttons[3].Page = mid;
|
||||||
_buttons[2].Page = mid - 1;
|
_buttons[2].Page = mid - 1;
|
||||||
_buttons[4].Page = mid + 1;
|
_buttons[4].Page = mid + 1;
|
||||||
@@ -247,7 +252,7 @@ public class Pagination: TemplatedControl
|
|||||||
_buttons[6].Page = pageCount;
|
_buttons[6].Page = pageCount;
|
||||||
if(mid>4)
|
if(mid>4)
|
||||||
{
|
{
|
||||||
_buttons[1].SetStatus(0, false, true, false);
|
_buttons[1].SetStatus(-1, false, true, false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -255,7 +260,7 @@ public class Pagination: TemplatedControl
|
|||||||
}
|
}
|
||||||
if(mid<pageCount-3)
|
if(mid<pageCount-3)
|
||||||
{
|
{
|
||||||
_buttons[5].SetStatus(0, false, false, true);
|
_buttons[5].SetStatus(-1, false, false, true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -275,8 +280,9 @@ public class Pagination: TemplatedControl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CurrentPage = currentPage;
|
PageCount = pageCount;
|
||||||
if (_previousButton != null) _previousButton.IsEnabled = CurrentPage > 1;
|
SetCurrentValue(CurrentPageProperty, currentPage);
|
||||||
if( _nextButton!=null) _nextButton.IsEnabled = CurrentPage < PageCount;
|
if (_previousButton != null) _previousButton.IsEnabled = (CurrentPage??int.MaxValue) > 1;
|
||||||
|
if (_nextButton != null) _nextButton.IsEnabled = (CurrentPage ?? 0) < PageCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user