From 6e5b3012dc0a4d7d3f87354a788eef91ded5bab4 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Mon, 24 Apr 2023 22:41:01 +0800 Subject: [PATCH] feat: add item styles, add color converter. --- demo/Ursa.Demo/Pages/TimelineDemo.axaml | 20 ++++- src/Ursa.Themes.Semi/Controls/Timeline.axaml | 52 ++++++++---- ...melineItemTypeToIconForegroundConverter.cs | 82 +++++++++++++++++++ .../Themes/Light/Timeline.axaml | 8 ++ .../Themes/Light/_index.axaml | 1 + src/Ursa/Controls/Timeline/TimelineItem.cs | 43 +++++++++- .../Controls/Timeline/TimelineItemType.cs | 1 - 7 files changed, 183 insertions(+), 24 deletions(-) create mode 100644 src/Ursa.Themes.Semi/Converters/TimelineItemTypeToIconForegroundConverter.cs create mode 100644 src/Ursa.Themes.Semi/Themes/Light/Timeline.axaml diff --git a/demo/Ursa.Demo/Pages/TimelineDemo.axaml b/demo/Ursa.Demo/Pages/TimelineDemo.axaml index 0671c98..cafed3a 100644 --- a/demo/Ursa.Demo/Pages/TimelineDemo.axaml +++ b/demo/Ursa.Demo/Pages/TimelineDemo.axaml @@ -16,9 +16,23 @@ - - - + + + + diff --git a/src/Ursa.Themes.Semi/Controls/Timeline.axaml b/src/Ursa.Themes.Semi/Controls/Timeline.axaml index 801865d..63e311c 100644 --- a/src/Ursa.Themes.Semi/Controls/Timeline.axaml +++ b/src/Ursa.Themes.Semi/Controls/Timeline.axaml @@ -1,6 +1,7 @@ @@ -22,48 +23,64 @@ + + - - - + + + + + + + + + + + DefaultBrushProperty = AvaloniaProperty.Register( + nameof(DefaultBrush)); + + public IBrush DefaultBrush + { + get => GetValue(DefaultBrushProperty); + set => SetValue(DefaultBrushProperty, value); + } + + public static readonly StyledProperty OngoingBrushProperty = AvaloniaProperty.Register( + nameof(OngoingBrush)); + + public IBrush OngoingBrush + { + get => GetValue(OngoingBrushProperty); + set => SetValue(OngoingBrushProperty, value); + } + + public static readonly StyledProperty SuccessBrushProperty = AvaloniaProperty.Register( + nameof(SuccessBrush)); + + public IBrush SuccessBrush + { + get => GetValue(SuccessBrushProperty); + set => SetValue(SuccessBrushProperty, value); + } + + public static readonly StyledProperty WarningBrushProperty = AvaloniaProperty.Register( + nameof(WarningBrush)); + + public IBrush WarningBrush + { + get => GetValue(WarningBrushProperty); + set => SetValue(WarningBrushProperty, value); + } + + public static readonly StyledProperty ErrorBrushProperty = AvaloniaProperty.Register( + nameof(ErrorBrush)); + + public IBrush ErrorBrush + { + get => GetValue(ErrorBrushProperty); + set => SetValue(ErrorBrushProperty, value); + } + + + public object? Convert(IList values, Type targetType, object? parameter, CultureInfo culture) + { + if (values[0] is TimelineItemType type) + { + switch (type) + { + case TimelineItemType.Error: + return ErrorBrush; + case TimelineItemType.Warning: + return WarningBrush; + case TimelineItemType.Success: + return SuccessBrush; + case TimelineItemType.Ongoing: + return OngoingBrush; + case TimelineItemType.Default: + if (values[1] is IBrush brush) + { + return brush; + } + return DefaultBrush; + } + } + return AvaloniaProperty.UnsetValue; + } +} \ No newline at end of file diff --git a/src/Ursa.Themes.Semi/Themes/Light/Timeline.axaml b/src/Ursa.Themes.Semi/Themes/Light/Timeline.axaml new file mode 100644 index 0000000..e879046 --- /dev/null +++ b/src/Ursa.Themes.Semi/Themes/Light/Timeline.axaml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/src/Ursa.Themes.Semi/Themes/Light/_index.axaml b/src/Ursa.Themes.Semi/Themes/Light/_index.axaml index 7a530cc..004d6a1 100644 --- a/src/Ursa.Themes.Semi/Themes/Light/_index.axaml +++ b/src/Ursa.Themes.Semi/Themes/Light/_index.axaml @@ -5,5 +5,6 @@ + diff --git a/src/Ursa/Controls/Timeline/TimelineItem.cs b/src/Ursa/Controls/Timeline/TimelineItem.cs index 915644c..9ba9f9c 100644 --- a/src/Ursa/Controls/Timeline/TimelineItem.cs +++ b/src/Ursa/Controls/Timeline/TimelineItem.cs @@ -8,11 +8,25 @@ using Avalonia.Media; namespace Ursa.Controls; -[PseudoClasses(PC_First, PC_Last)] +[PseudoClasses(PC_First, PC_Last, PC_Default, PC_Ongoing, PC_Success, PC_Warning, PC_Error)] public class TimelineItem: ContentControl { - public const string PC_First = ":first"; - public const string PC_Last = ":last"; + private const string PC_First = ":first"; + private const string PC_Last = ":last"; + private const string PC_Default = ":default"; + private const string PC_Ongoing = ":ongoing"; + private const string PC_Success = ":success"; + private const string PC_Warning = ":warning"; + private const string PC_Error = ":error"; + + private static readonly IReadOnlyDictionary _itemTypeMapping = new Dictionary + { + {TimelineItemType.Default, PC_Default}, + {TimelineItemType.Ongoing, PC_Ongoing}, + {TimelineItemType.Success, PC_Success}, + {TimelineItemType.Warning, PC_Warning}, + {TimelineItemType.Error, PC_Error}, + }; public static readonly StyledProperty IconForegroundProperty = AvaloniaProperty.Register(nameof(IconForeground)); @@ -49,9 +63,32 @@ public class TimelineItem: ContentControl set => SetValue(DescriptionTemplateProperty, value); } + public static readonly StyledProperty ItemTypeProperty = AvaloniaProperty.Register( + nameof(ItemType)); + + public TimelineItemType ItemType + { + get => GetValue(ItemTypeProperty); + set => SetValue(ItemTypeProperty, value); + } + internal void SetIndex(bool isFirst, bool isLast) { PseudoClasses.Set(PC_First, isFirst); PseudoClasses.Set(PC_Last, isLast); } + + static TimelineItem() + { + ItemTypeProperty.Changed.AddClassHandler((o, e) => { o.OnItemTypeChanged(e); }); + } + + private void OnItemTypeChanged(AvaloniaPropertyChangedEventArgs args) + { + var oldValue = args.GetOldValue(); + var newValue = args.GetNewValue(); + PseudoClasses.Set(_itemTypeMapping[oldValue], false); + PseudoClasses.Set(_itemTypeMapping[newValue], true); + } + } \ No newline at end of file diff --git a/src/Ursa/Controls/Timeline/TimelineItemType.cs b/src/Ursa/Controls/Timeline/TimelineItemType.cs index 2fa2f60..6b90703 100644 --- a/src/Ursa/Controls/Timeline/TimelineItemType.cs +++ b/src/Ursa/Controls/Timeline/TimelineItemType.cs @@ -2,7 +2,6 @@ namespace Ursa.Controls; public enum TimelineItemType { - None, Default, Ongoing, Success,