diff --git a/src/Snap.Hutao/Snap.Hutao/App.xaml b/src/Snap.Hutao/Snap.Hutao/App.xaml index eca83605..3cda18bc 100644 --- a/src/Snap.Hutao/Snap.Hutao/App.xaml +++ b/src/Snap.Hutao/Snap.Hutao/App.xaml @@ -23,6 +23,7 @@ + diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Panel/EqualPanel.cs b/src/Snap.Hutao/Snap.Hutao/Control/Panel/EqualPanel.cs new file mode 100644 index 00000000..ebf1e140 --- /dev/null +++ b/src/Snap.Hutao/Snap.Hutao/Control/Panel/EqualPanel.cs @@ -0,0 +1,89 @@ +// Copyright (c) DGP Studio. All rights reserved. +// Licensed under the MIT license. + +using Microsoft.UI.Xaml; +using System.Data; +using System.Runtime.InteropServices; +using Windows.Foundation; + +namespace Snap.Hutao.Control.Panel; + +[DependencyProperty("Spacing", typeof(double), default(double), nameof(OnSpacingChanged))] +internal partial class EqualPanel : Microsoft.UI.Xaml.Controls.Panel +{ + private double maxItemWidth; + private double maxItemHeight; + private int visibleItemsCount; + + public EqualPanel() + { + RegisterPropertyChangedCallback(HorizontalAlignmentProperty, OnHorizontalAlignmentChanged); + } + + protected override Size MeasureOverride(Size availableSize) + { + maxItemWidth = 0; + maxItemHeight = 0; + + List elements = [.. Children.Where(element => element.Visibility == Visibility.Visible)]; + visibleItemsCount = elements.Count; + + foreach (ref readonly UIElement child in CollectionsMarshal.AsSpan(elements)) + { + child.Measure(availableSize); + maxItemWidth = Math.Max(maxItemWidth, child.DesiredSize.Width); + maxItemHeight = Math.Max(maxItemHeight, child.DesiredSize.Height); + } + + if (visibleItemsCount > 0) + { + // Return equal widths based on the widest item + // In very specific edge cases the AvailableWidth might be infinite resulting in a crash. + if (HorizontalAlignment is not HorizontalAlignment.Stretch || double.IsInfinity(availableSize.Width)) + { + return new Size((maxItemWidth * visibleItemsCount) + (Spacing * (visibleItemsCount - 1)), maxItemHeight); + } + else + { + // Equal columns based on the available width, adjust for spacing + double totalWidth = availableSize.Width - (Spacing * (visibleItemsCount - 1)); + maxItemWidth = totalWidth / visibleItemsCount; + return new Size(availableSize.Width, maxItemHeight); + } + } + else + { + return new Size(0, 0); + } + } + + protected override Size ArrangeOverride(Size finalSize) + { + double x = 0; + + // Check if there's more (little) width available - if so, set max item width to the maximum possible as we have an almost perfect height. + if (finalSize.Width > (visibleItemsCount * maxItemWidth) + (Spacing * (visibleItemsCount - 1))) + { + maxItemWidth = (finalSize.Width - (Spacing * (visibleItemsCount - 1))) / visibleItemsCount; + } + + IEnumerable elements = Children.Where(static e => e.Visibility == Visibility.Visible); + foreach (UIElement child in elements) + { + child.Arrange(new Rect(x, 0, maxItemWidth, maxItemHeight)); + x += maxItemWidth + Spacing; + } + + return finalSize; + } + + private static void OnSpacingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + (d as EqualPanel)?.InvalidateMeasure(); + } + + private void OnHorizontalAlignmentChanged(DependencyObject sender, DependencyProperty dp) + { + InvalidateMeasure(); + } +} \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Control/Panel/PanelSelector.xaml b/src/Snap.Hutao/Snap.Hutao/Control/Panel/PanelSelector.xaml index 8304b0b1..ba913e4e 100644 --- a/src/Snap.Hutao/Snap.Hutao/Control/Panel/PanelSelector.xaml +++ b/src/Snap.Hutao/Snap.Hutao/Control/Panel/PanelSelector.xaml @@ -6,6 +6,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:shcm="using:Snap.Hutao.Control.Markup" + Style="{StaticResource DefaultSegmentedStyle}" mc:Ignorable="d"> + + + + + + + + + 1 + + + + + 1 + + + + + 1 + + + + 1 + 2 + + + + + + + \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj index bd6cc9cd..be4d16e4 100644 --- a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj +++ b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj @@ -156,6 +156,7 @@ + @@ -348,6 +349,11 @@ + + + MSBuild:Compile + + MSBuild:Compile diff --git a/src/Snap.Hutao/Snap.Hutao/View/Control/StatisticsSegmented.xaml b/src/Snap.Hutao/Snap.Hutao/View/Control/StatisticsSegmented.xaml index d5d4d5ac..1857bace 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/Control/StatisticsSegmented.xaml +++ b/src/Snap.Hutao/Snap.Hutao/View/Control/StatisticsSegmented.xaml @@ -6,6 +6,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:shcm="using:Snap.Hutao.Control.Markup" + Style="{StaticResource DefaultSegmentedStyle}" mc:Ignorable="d">