Skip to content

Commit

Permalink
💄 FixedWrapPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
rmbadmin committed Jan 29, 2024
1 parent 9805ad4 commit 8fff1d1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 30 deletions.
21 changes: 10 additions & 11 deletions src/BD.WTTS.Client.Avalonia/UI/Styling/Controls/CarouselItems.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
BorderThickness="0"
ClipToBounds="True"
CornerRadius="8">
<Panel>
<Grid RowDefinitions="*,5,Auto">
<Carousel Name="CarouselControl" Classes="AnimeSlide">
<Carousel.ItemTemplate>
<DataTemplate DataType="sys:Object">
Expand All @@ -44,23 +44,23 @@
<Button
Name="Left"
HorizontalAlignment="Left"
Background="#80000000"
Classes="action"
IsVisible="False"
Theme="{StaticResource TransparentButton}">
<ui:SymbolIcon FontSize="14" Symbol="ChevronLeft" />
IsVisible="False">
<ui:SymbolIcon FontSize="18" Symbol="ChevronLeft" />
</Button>
<Button
Name="Right"
HorizontalAlignment="Right"
Background="#80000000"
Classes="action"
IsVisible="False"
Theme="{StaticResource TransparentButton}">
<ui:SymbolIcon FontSize="14" Symbol="ChevronRight" />
IsVisible="False">
<ui:SymbolIcon FontSize="18" Symbol="ChevronRight" />
</Button>

<Panel
Grid.Row="2"
Height="10"
Margin="0,0,0,5"
HorizontalAlignment="Center"
VerticalAlignment="Bottom">
<ItemsRepeater Name="Swiper" IsVisible="False">
Expand Down Expand Up @@ -94,17 +94,16 @@
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</Panel>
</Panel>
</Grid>
</Border>
</ControlTemplate>
</Setter>
</Style>


<Style Selector="spp|CarouselItems /template/ Button.action">
<Setter Property="Margin" Value="4" />
<Setter Property="Width" Value="36" />
<Setter Property="Height" Value="36" />
<Setter Property="Height" Value="64" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public int ItemsPerPage
set => SetValue(ItemsPerPageProperty, value);
}

private Carousel? _carouselControl;
private Button? _leftButton;
private Button? _rightButton;
private ItemsRepeater? _swiper;
private Timer? _timer;
Carousel? _carouselControl;
Button? _leftButton;
Button? _rightButton;
ItemsRepeater? _swiper;
Timer? _timer;

public ICommand? CarouselBannerIndexCommand { get; }

Expand Down Expand Up @@ -90,11 +90,15 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

_carouselControl.GetObservable(Carousel.SelectedIndexProperty)
.Subscribe(_ => SwipersLoad());

RefreshItemsSource();
}
}

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);

if (change.Property == ItemCountProperty)
{
RefreshItemsSource();
Expand Down Expand Up @@ -125,8 +129,26 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
}
}
}
}

base.OnPropertyChanged(change);
protected override void OnPointerEntered(PointerEventArgs e)
{
base.OnPointerEntered(e);

if (_swiper == null || _leftButton == null || _rightButton == null)
return;

_leftButton.IsVisible = _rightButton.IsVisible = _swiper.IsVisible;
}

protected override void OnPointerExited(PointerEventArgs e)
{
base.OnPointerExited(e);

if (_swiper == null || _leftButton == null || _rightButton == null)
return;

_leftButton.IsVisible = _rightButton.IsVisible = false;
}

void RefreshItemsSource()
Expand All @@ -138,13 +160,13 @@ void RefreshItemsSource()
}
}

private void CarouselBannerIndex(int index)
void CarouselBannerIndex(int index)
{
if (_carouselControl != null)
_carouselControl.SelectedIndex = index;
}

private void SwipersLoad()
void SwipersLoad()
{
if (_carouselControl == null || _swiper == null || _leftButton == null || _rightButton == null)
return;
Expand All @@ -156,12 +178,12 @@ private void SwipersLoad()
}
if (_carouselControl.ItemCount == 1)
{
_leftButton.IsVisible = _rightButton.IsVisible = _swiper.IsVisible = false;
_swiper.IsVisible = false;
return;
}
else
{
_leftButton.IsVisible = _rightButton.IsVisible = _swiper.IsVisible = true;
_swiper.IsVisible = true;
var arr = new Dictionary<int, string>();
for (var i = 0; i < _carouselControl.ItemCount; i++)
{
Expand All @@ -174,7 +196,10 @@ private void SwipersLoad()
}
}

private void SwiperNext()
/// <summary>
/// 滑动到下一个
/// </summary>
public void SwiperNext()
{
if (_carouselControl == null || _carouselControl.ItemCount < 1)
return;
Expand All @@ -189,7 +214,10 @@ private void SwiperNext()
}
}

private void SwiperPrevious()
/// <summary>
/// 滑动到上一个
/// </summary>
public void SwiperPrevious()
{
if (_carouselControl == null || _carouselControl.ItemCount < 1)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

namespace BD.WTTS.UI.Views.Controls;

/// <summary>
/// 固定的 WrapPanel
/// </summary>
public class FixedWrapPanel : Panel, INavigableContainer
{
/// <summary>
/// Defines the <see cref="ItemsPerLine"/> property.
/// </summary>
public static readonly StyledProperty<int> ItemsPerLineProperty =
AvaloniaProperty.Register<FixedWrapPanel, int>(nameof(ItemsPerLine), 3);

/// <summary>
/// Defines the <see cref="Spacing"/> property.
/// </summary>
public static readonly StyledProperty<double> SpacingProperty =
AvaloniaProperty.Register<FixedWrapPanel, double>(nameof(Spacing), 0);

Expand All @@ -15,18 +24,25 @@ static FixedWrapPanel()
AffectsMeasure<FixedWrapPanel>(ItemsPerLineProperty);
}

/// <summary>
/// ItemsPerLine
/// </summary>
public int ItemsPerLine
{
get => GetValue(ItemsPerLineProperty);
set => SetValue(ItemsPerLineProperty, value);
}

/// <summary>
/// Spacing
/// </summary>
public double Spacing
{
get => GetValue(SpacingProperty);
set => SetValue(SpacingProperty, value);
}

/// <inheritdoc/>
IInputElement INavigableContainer.GetControl(NavigationDirection direction, IInputElement? from, bool wrap)
{
int index = from is not null ? Children.IndexOf((Control)from) : -1;
Expand Down Expand Up @@ -66,9 +82,10 @@ IInputElement INavigableContainer.GetControl(NavigationDirection direction, IInp
return this;
}

/// <inheritdoc/>
protected override Size MeasureOverride(Size constraint)
{
double itemWidth = constraint.Width / ItemsPerLine;
double itemWidth = (constraint.Width - Spacing * (ItemsPerLine - 1)) / ItemsPerLine;
MutableSize currentLineSize = default;
MutableSize panelSize = default;
Size lineConstraint = new(constraint.Width, constraint.Height);
Expand Down Expand Up @@ -110,9 +127,10 @@ protected override Size MeasureOverride(Size constraint)
return panelSize.ToSize();
}

/// <inheritdoc/>
protected override Size ArrangeOverride(Size finalSize)
{
double itemWidth = finalSize.Width / ItemsPerLine;
double itemWidth = (finalSize.Width - Spacing * (ItemsPerLine - 1)) / ItemsPerLine;
int firstInLine = 0;
double accumulatedHeight = 0;
var currentLineSize = default(MutableSize);
Expand Down Expand Up @@ -151,7 +169,7 @@ protected override Size ArrangeOverride(Size finalSize)
return finalSize;
}

private void ArrangeLine(double y, double height, int start, int end, double width)
void ArrangeLine(double y, double height, int start, int end, double width)
{
double x = 0;
for (int i = start; i < end; i++)
Expand All @@ -162,12 +180,20 @@ private void ArrangeLine(double y, double height, int start, int end, double wid
continue;
}

child.Arrange(new Rect(x, y, width - Spacing, height));
x += width + Spacing;
if (i < end - 1)
{
child.Arrange(new Rect(x, y, width - Spacing, height));
x += width + Spacing;
}
else
{
child.Arrange(new Rect(x, y, width, height));
x += width;
}
}
}

private struct MutableSize
record struct MutableSize
{
internal MutableSize(double width, double height)
{
Expand All @@ -184,7 +210,7 @@ internal MutableSize(Size size)
internal double Width;
internal double Height;

internal Size ToSize()
internal readonly Size ToSize()
{
return new Size(Width, Height);
}
Expand Down

0 comments on commit 8fff1d1

Please sign in to comment.