mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
segment abstraction
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
13
src/Snap.Hutao/Snap.Hutao/Control/IColorSegment.cs
Normal file
13
src/Snap.Hutao/Snap.Hutao/Control/IColorSegment.cs
Normal 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; }
|
||||
}
|
||||
@@ -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, });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user