@@ -6,6 +6,7 @@ public static class MenuKeys
|
|||||||
public const string MenuKeyBadge = "Badge";
|
public const string MenuKeyBadge = "Badge";
|
||||||
public const string MenuKeyBanner = "Banner";
|
public const string MenuKeyBanner = "Banner";
|
||||||
public const string MenuKeyButtonGroup = "ButtonGroup";
|
public const string MenuKeyButtonGroup = "ButtonGroup";
|
||||||
|
public const string MenuKeyClassInput = "Class Input";
|
||||||
public const string MenuKeyDialog = "Dialog";
|
public const string MenuKeyDialog = "Dialog";
|
||||||
public const string MenuKeyDivider = "Divider";
|
public const string MenuKeyDivider = "Divider";
|
||||||
public const string MenuKeyDualBadge = "DualBadge";
|
public const string MenuKeyDualBadge = "DualBadge";
|
||||||
|
|||||||
@@ -47,6 +47,5 @@
|
|||||||
</Grid>
|
</Grid>
|
||||||
</Border>
|
</Border>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</UserControl>
|
</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" Separator=" " />
|
||||||
|
<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.MenuKeyBadge => new BadgeDemoViewModel(),
|
||||||
MenuKeys.MenuKeyBanner => new BannerDemoViewModel(),
|
MenuKeys.MenuKeyBanner => new BannerDemoViewModel(),
|
||||||
MenuKeys.MenuKeyButtonGroup => new ButtonGroupDemoViewModel(),
|
MenuKeys.MenuKeyButtonGroup => new ButtonGroupDemoViewModel(),
|
||||||
|
MenuKeys.MenuKeyClassInput => new ClassInputDemoViewModel(),
|
||||||
MenuKeys.MenuKeyDialog => new DialogDemoViewModel(),
|
MenuKeys.MenuKeyDialog => new DialogDemoViewModel(),
|
||||||
MenuKeys.MenuKeyDivider => new DividerDemoViewModel(),
|
MenuKeys.MenuKeyDivider => new DividerDemoViewModel(),
|
||||||
MenuKeys.MenuKeyDualBadge => new DualBadgeDemoViewModel(),
|
MenuKeys.MenuKeyDualBadge => new DualBadgeDemoViewModel(),
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ public class MenuViewModel: ViewModelBase
|
|||||||
new() { MenuHeader = "Badge", Key = MenuKeys.MenuKeyBadge },
|
new() { MenuHeader = "Badge", Key = MenuKeys.MenuKeyBadge },
|
||||||
new() { MenuHeader = "Banner", Key = MenuKeys.MenuKeyBanner },
|
new() { MenuHeader = "Banner", Key = MenuKeys.MenuKeyBanner },
|
||||||
new() { MenuHeader = "Button Group", Key = MenuKeys.MenuKeyButtonGroup, Status = "Updated"},
|
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 = "Dialog", Key = MenuKeys.MenuKeyDialog },
|
||||||
new() { MenuHeader = "Divider", Key = MenuKeys.MenuKeyDivider },
|
new() { MenuHeader = "Divider", Key = MenuKeys.MenuKeyDivider },
|
||||||
new() { MenuHeader = "DualBadge", Key = MenuKeys.MenuKeyDualBadge },
|
new() { MenuHeader = "DualBadge", Key = MenuKeys.MenuKeyDualBadge },
|
||||||
|
|||||||
27
src/Ursa.Themes.Semi/Controls/ControlClassesInput.axaml
Normal file
27
src/Ursa.Themes.Semi/Controls/ControlClassesInput.axaml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
<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"
|
||||||
|
AllowDuplicates="False"
|
||||||
|
Separator="{TemplateBinding Separator}"
|
||||||
|
Tags="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TargetClasses, Mode=TwoWay}" />
|
||||||
|
</ControlTemplate>
|
||||||
|
</Setter>
|
||||||
|
<!--
|
||||||
|
<Setter Property="ContextFlyout">
|
||||||
|
<MenuFlyout>
|
||||||
|
<MenuItem Header="Undo" Command="{Binding $parent[u:ControlClassesInput].UnDo}" />
|
||||||
|
<MenuItem Header="Redo" Command="{Binding $parent[u:ControlClassesInput].Redo}" />
|
||||||
|
<MenuItem Header="Clear" Command="{Binding $parent[u:ControlClassesInput].Clear}" />
|
||||||
|
</MenuFlyout>
|
||||||
|
</Setter>
|
||||||
|
-->
|
||||||
|
</ControlTheme>
|
||||||
|
</ResourceDictionary>
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
<ResourceInclude Source="Badge.axaml" />
|
<ResourceInclude Source="Badge.axaml" />
|
||||||
<ResourceInclude Source="Banner.axaml" />
|
<ResourceInclude Source="Banner.axaml" />
|
||||||
<ResourceInclude Source="ButtonGroup.axaml" />
|
<ResourceInclude Source="ButtonGroup.axaml" />
|
||||||
|
<ResourceInclude Source="ControlClassesInput.axaml" />
|
||||||
<ResourceInclude Source="Dialog.axaml" />
|
<ResourceInclude Source="Dialog.axaml" />
|
||||||
<ResourceInclude Source="DialogShared.axaml" />
|
<ResourceInclude Source="DialogShared.axaml" />
|
||||||
<ResourceInclude Source="Divider.axaml" />
|
<ResourceInclude Source="Divider.axaml" />
|
||||||
|
|||||||
177
src/Ursa/Controls/ControlClassesInput/ControlClassesInput.cs
Normal file
177
src/Ursa/Controls/ControlClassesInput/ControlClassesInput.cs
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
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();
|
||||||
|
private bool _disableHistory = false;
|
||||||
|
|
||||||
|
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<string> SeparatorProperty =
|
||||||
|
TagInput.SeparatorProperty.AddOwner<ControlClassesInput>();
|
||||||
|
|
||||||
|
public string Separator
|
||||||
|
{
|
||||||
|
get => GetValue(SeparatorProperty);
|
||||||
|
set => SetValue(SeparatorProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private ObservableCollection<string> _targetClasses;
|
||||||
|
|
||||||
|
internal static readonly DirectProperty<ControlClassesInput, ObservableCollection<string>> TargetClassesProperty = AvaloniaProperty.RegisterDirect<ControlClassesInput, ObservableCollection<string>>(
|
||||||
|
nameof(TargetClasses), o => o.TargetClasses, (o, v) => o.TargetClasses = v);
|
||||||
|
|
||||||
|
internal ObservableCollection<string> TargetClasses
|
||||||
|
{
|
||||||
|
get => _targetClasses;
|
||||||
|
set => SetAndRaise(TargetClassesProperty, ref _targetClasses, 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 void OnClassesChanged(AvaloniaPropertyChangedEventArgs<ObservableCollection<string>?> args)
|
||||||
|
{
|
||||||
|
var newValue = args.NewValue.Value;
|
||||||
|
if (newValue is null)
|
||||||
|
{
|
||||||
|
SaveHistory(new List<string>(), true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var classes = newValue.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct().ToList();
|
||||||
|
SaveHistory(classes, true);
|
||||||
|
if (newValue is INotifyCollectionChanged incc)
|
||||||
|
{
|
||||||
|
incc.CollectionChanged+=InccOnCollectionChanged;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InccOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
||||||
|
{
|
||||||
|
if(_disableHistory) return;
|
||||||
|
SaveHistory(TargetClasses?.ToList() ?? new List<string>(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SaveHistory(List<string> strings, bool fromInput)
|
||||||
|
{
|
||||||
|
_history.AddLast(strings);
|
||||||
|
_undoHistory.Clear();
|
||||||
|
if (_history.Count > CountOfHistoricalRecord)
|
||||||
|
{
|
||||||
|
_history.RemoveFirst();
|
||||||
|
}
|
||||||
|
SetClassesToTarget(fromInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetClassesToTarget(bool fromInput)
|
||||||
|
{
|
||||||
|
List<string> strings;
|
||||||
|
if (_history.Count == 0)
|
||||||
|
{
|
||||||
|
strings = new List<string>();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strings = _history.Last.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fromInput)
|
||||||
|
{
|
||||||
|
SetCurrentValue(TargetClassesProperty, new ObservableCollection<string>(strings));
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
_disableHistory = true;
|
||||||
|
TargetClasses.Clear();
|
||||||
|
foreach (var value in _history.Last.Value)
|
||||||
|
{
|
||||||
|
TargetClasses.Add(value);
|
||||||
|
}
|
||||||
|
_disableHistory = false;
|
||||||
|
SetClassesToTarget(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Redo()
|
||||||
|
{
|
||||||
|
var node = _undoHistory.First;
|
||||||
|
_undoHistory.RemoveFirst();
|
||||||
|
_history.AddLast(node);
|
||||||
|
_disableHistory = true;
|
||||||
|
TargetClasses.Clear();
|
||||||
|
foreach (var value in _history.Last.Value)
|
||||||
|
{
|
||||||
|
TargetClasses.Add(value);
|
||||||
|
}
|
||||||
|
_disableHistory = false;
|
||||||
|
SetClassesToTarget(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
SaveHistory(new List<string>(), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -190,6 +190,12 @@ public class TagInput : TemplatedControl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (e.Action == NotifyCollectionChangedAction.Reset)
|
||||||
|
{
|
||||||
|
Items.Clear();
|
||||||
|
Items.Add(_textBox);
|
||||||
|
InvalidateVisual();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user