feat: class input control.
This commit is contained in:
@@ -6,6 +6,7 @@ public static class MenuKeys
|
||||
public const string MenuKeyBadge = "Badge";
|
||||
public const string MenuKeyBanner = "Banner";
|
||||
public const string MenuKeyButtonGroup = "ButtonGroup";
|
||||
public const string MenuKeyClassInput = "Class Input";
|
||||
public const string MenuKeyDialog = "Dialog";
|
||||
public const string MenuKeyDivider = "Divider";
|
||||
public const string MenuKeyDualBadge = "DualBadge";
|
||||
|
||||
@@ -47,6 +47,5 @@
|
||||
</Grid>
|
||||
</Border>
|
||||
</Grid>
|
||||
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
|
||||
16
demo/Ursa.Demo/Pages/ClassInputDemo.axaml
Normal file
16
demo/Ursa.Demo/Pages/ClassInputDemo.axaml
Normal file
@@ -0,0 +1,16 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:u="https://irihi.tech/ursa"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Ursa.Demo.Pages.ClassInputDemo">
|
||||
<StackPanel HorizontalAlignment="Left" Spacing="20">
|
||||
<u:ControlClassesInput Name="input" />
|
||||
<Button u:ControlClassesInput.Source="{Binding #input}" Content="Hello Panda" />
|
||||
<Button u:ControlClassesInput.Source="{Binding #input}" Content="Hello Panda" />
|
||||
<Button u:ControlClassesInput.Source="{Binding #input}" Content="Hello Panda" />
|
||||
<TextBox u:ControlClassesInput.Source="{Binding #input}" Width="100" Text="100"></TextBox>
|
||||
<ProgressBar u:ControlClassesInput.Source="{Binding #input}" Width="200" Value="20" ShowProgressText="True"></ProgressBar>
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
13
demo/Ursa.Demo/Pages/ClassInputDemo.axaml.cs
Normal file
13
demo/Ursa.Demo/Pages/ClassInputDemo.axaml.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
|
||||
namespace Ursa.Demo.Pages;
|
||||
|
||||
public partial class ClassInputDemo : UserControl
|
||||
{
|
||||
public ClassInputDemo()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
8
demo/Ursa.Demo/ViewModels/ClassInputDemoViewModel.cs
Normal file
8
demo/Ursa.Demo/ViewModels/ClassInputDemoViewModel.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
|
||||
public class ClassInputDemoViewModel: ObservableObject
|
||||
{
|
||||
|
||||
}
|
||||
@@ -28,6 +28,7 @@ public class MainViewViewModel : ViewModelBase
|
||||
MenuKeys.MenuKeyBadge => new BadgeDemoViewModel(),
|
||||
MenuKeys.MenuKeyBanner => new BannerDemoViewModel(),
|
||||
MenuKeys.MenuKeyButtonGroup => new ButtonGroupDemoViewModel(),
|
||||
MenuKeys.MenuKeyClassInput => new ClassInputDemoViewModel(),
|
||||
MenuKeys.MenuKeyDialog => new DialogDemoViewModel(),
|
||||
MenuKeys.MenuKeyDivider => new DividerDemoViewModel(),
|
||||
MenuKeys.MenuKeyDualBadge => new DualBadgeDemoViewModel(),
|
||||
|
||||
@@ -15,6 +15,7 @@ public class MenuViewModel: ViewModelBase
|
||||
new() { MenuHeader = "Badge", Key = MenuKeys.MenuKeyBadge },
|
||||
new() { MenuHeader = "Banner", Key = MenuKeys.MenuKeyBanner },
|
||||
new() { MenuHeader = "Button Group", Key = MenuKeys.MenuKeyButtonGroup, Status = "Updated"},
|
||||
new() { MenuHeader = "Class Input", Key = MenuKeys.MenuKeyClassInput, Status = "New" },
|
||||
new() { MenuHeader = "Dialog", Key = MenuKeys.MenuKeyDialog },
|
||||
new() { MenuHeader = "Divider", Key = MenuKeys.MenuKeyDivider },
|
||||
new() { MenuHeader = "DualBadge", Key = MenuKeys.MenuKeyDualBadge },
|
||||
|
||||
14
src/Ursa.Themes.Semi/Controls/ControlClassesInput.axaml
Normal file
14
src/Ursa.Themes.Semi/Controls/ControlClassesInput.axaml
Normal file
@@ -0,0 +1,14 @@
|
||||
<ResourceDictionary
|
||||
xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:u="https://irihi.tech/ursa">
|
||||
<!-- Add Resources Here -->
|
||||
<ControlTheme x:Key="{x:Type u:ControlClassesInput}" TargetType="u:ControlClassesInput">
|
||||
<Setter Property="Width" Value="200" />
|
||||
<Setter Property="Template">
|
||||
<ControlTemplate TargetType="u:ControlClassesInput">
|
||||
<u:TagInput HorizontalAlignment="Stretch" Tags="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TargetClasses, Mode=TwoWay}" />
|
||||
</ControlTemplate>
|
||||
</Setter>
|
||||
</ControlTheme>
|
||||
</ResourceDictionary>
|
||||
@@ -4,6 +4,7 @@
|
||||
<ResourceInclude Source="Badge.axaml" />
|
||||
<ResourceInclude Source="Banner.axaml" />
|
||||
<ResourceInclude Source="ButtonGroup.axaml" />
|
||||
<ResourceInclude Source="ControlClassesInput.axaml" />
|
||||
<ResourceInclude Source="Dialog.axaml" />
|
||||
<ResourceInclude Source="DialogShared.axaml" />
|
||||
<ResourceInclude Source="Divider.axaml" />
|
||||
|
||||
147
src/Ursa/Controls/ControlClassesInput/ControlClassesInput.cs
Normal file
147
src/Ursa/Controls/ControlClassesInput/ControlClassesInput.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using Avalonia;
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class ControlClassesInput: TemplatedControl
|
||||
{
|
||||
LinkedList<List<string>> _history = new();
|
||||
LinkedList<List<string>> _undoHistory = new();
|
||||
|
||||
public int CountOfHistoricalRecord { get; set; } = 10;
|
||||
|
||||
public static readonly StyledProperty<Control?> TargetProperty = AvaloniaProperty.Register<ControlClassesInput, Control?>(
|
||||
nameof(Target));
|
||||
|
||||
public Control? Target
|
||||
{
|
||||
get => GetValue(TargetProperty);
|
||||
set => SetValue(TargetProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<ObservableCollection<string>?> TargetClassesProperty = AvaloniaProperty.Register<ControlClassesInput, ObservableCollection<string>?>(
|
||||
nameof(TargetClasses));
|
||||
|
||||
public ObservableCollection<string>? TargetClasses
|
||||
{
|
||||
get => GetValue(TargetClassesProperty);
|
||||
set => SetValue(TargetClassesProperty, value);
|
||||
}
|
||||
|
||||
public static readonly AttachedProperty<ControlClassesInput?> SourceProperty =
|
||||
AvaloniaProperty.RegisterAttached<ControlClassesInput, StyledElement, ControlClassesInput?>("Source");
|
||||
|
||||
public static void SetSource(StyledElement obj, ControlClassesInput value) => obj.SetValue(SourceProperty, value);
|
||||
public static ControlClassesInput? GetSource(StyledElement obj) => obj.GetValue(SourceProperty);
|
||||
|
||||
static ControlClassesInput()
|
||||
{
|
||||
TargetClassesProperty.Changed.AddClassHandler<ControlClassesInput, ObservableCollection<string>?>((o,e)=>o.OnClassesChanged(e));
|
||||
SourceProperty.Changed.AddClassHandler<StyledElement, ControlClassesInput?>(HandleSourceChange);
|
||||
}
|
||||
|
||||
public ControlClassesInput()
|
||||
{
|
||||
TargetClasses = new ObservableCollection<string>();
|
||||
TargetClasses.CollectionChanged += InccOnCollectionChanged;
|
||||
}
|
||||
|
||||
private List<StyledElement> _targets = new();
|
||||
|
||||
private static void HandleSourceChange(StyledElement arg1, AvaloniaPropertyChangedEventArgs<ControlClassesInput?> arg2)
|
||||
{
|
||||
var newControl = arg2.NewValue.Value;
|
||||
if (newControl is null) return;
|
||||
newControl._targets.Add(arg1);
|
||||
var oldControl = arg2.OldValue.Value;
|
||||
if (oldControl is not null)
|
||||
{
|
||||
newControl._targets.Remove(oldControl);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly char[] _separators = {' ', '\t', '\n', '\r'};
|
||||
|
||||
private void OnClassesChanged(AvaloniaPropertyChangedEventArgs<ObservableCollection<string>?> args)
|
||||
{
|
||||
var newValue = args.NewValue.Value;
|
||||
if (newValue is null)
|
||||
{
|
||||
SaveHistory(new List<string>());
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
var classes = newValue.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct().ToList();
|
||||
SaveHistory(classes);
|
||||
if (newValue is INotifyCollectionChanged incc)
|
||||
{
|
||||
incc.CollectionChanged+=InccOnCollectionChanged;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void InccOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
SaveHistory(TargetClasses?.ToList() ?? new List<string>());
|
||||
}
|
||||
|
||||
private void SaveHistory(List<string> strings)
|
||||
{
|
||||
_history.AddLast(strings);
|
||||
if (_history.Count > CountOfHistoricalRecord)
|
||||
{
|
||||
_history.RemoveFirst();
|
||||
}
|
||||
SetClassesToTarget();
|
||||
}
|
||||
|
||||
private void SetClassesToTarget()
|
||||
{
|
||||
List<string> strings;
|
||||
if (_history.Count == 0)
|
||||
{
|
||||
strings = new List<string>();
|
||||
}
|
||||
else
|
||||
{
|
||||
strings = _history.Last.Value;
|
||||
}
|
||||
if (Target is not null)
|
||||
{
|
||||
Target.Classes.Replace(strings);
|
||||
}
|
||||
foreach (var target in _targets)
|
||||
{
|
||||
target.Classes.Replace(strings);
|
||||
}
|
||||
}
|
||||
|
||||
public void UnDo()
|
||||
{
|
||||
var node = _history.Last;
|
||||
_history.RemoveLast();
|
||||
_undoHistory.AddFirst(node);
|
||||
SetCurrentValue(TargetClassesProperty, new AvaloniaList<string>(node.Value));
|
||||
SetClassesToTarget();
|
||||
}
|
||||
|
||||
public void Redo()
|
||||
{
|
||||
var node = _undoHistory.First;
|
||||
_undoHistory.RemoveFirst();
|
||||
_history.AddLast(node);
|
||||
SetCurrentValue(TargetClassesProperty, new AvaloniaList<string>(node.Value));
|
||||
SetClassesToTarget();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
SaveHistory(new List<string>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user