segment abstraction

This commit is contained in:
Lightczx
2023-08-30 15:51:59 +08:00
parent 2b53ffd4d2
commit 4e0d83726e
3 changed files with 25 additions and 11 deletions

View File

@@ -1,15 +1,11 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
using Windows.UI;
namespace Snap.Hutao.Control;
internal sealed class ColorSegment
internal sealed class ColorSegment : IColorSegment
{
public ColorSegment(Color color, double value)
{

View File

@@ -0,0 +1,13 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Windows.UI;
namespace Snap.Hutao.Control;
internal interface IColorSegment
{
Color Color { get; }
double Value { get; }
}

View File

@@ -9,10 +9,10 @@ using System.Runtime.InteropServices;
namespace Snap.Hutao.Control;
[DependencyProperty("Source", typeof(List<ColorSegment>), default!, nameof(OnSourceChanged))]
[DependencyProperty("Source", typeof(List<IColorSegment>), default!, nameof(OnSourceChanged))]
internal sealed partial class SegmentedBar : ContentControl
{
private readonly LinearGradientBrush brush = new();
private readonly LinearGradientBrush brush = new() { StartPoint = new(0, 0), EndPoint = new(1, 0), };
public SegmentedBar()
{
@@ -26,13 +26,18 @@ internal sealed partial class SegmentedBar : ContentControl
{
SegmentedBar segmentedBar = (SegmentedBar)obj;
segmentedBar.brush.GradientStops.Clear();
GradientStopCollection collection = segmentedBar.brush.GradientStops;
collection.Clear();
if (args.NewValue as List<ColorSegment> is [_, ..] list)
if (args.NewValue as List<IColorSegment> is [_, ..] list)
{
foreach (ref readonly ColorSegment segment in CollectionsMarshal.AsSpan(list))
double total = list.Sum(seg => seg.Value);
double offset = 0;
foreach (ref readonly IColorSegment segment in CollectionsMarshal.AsSpan(list))
{
collection.Add(new GradientStop() { Color = segment.Color, Offset = offset, });
offset += segment.Value / total;
collection.Add(new GradientStop() { Color = segment.Color, Offset = offset, });
}
}
}