This commit is contained in:
TheXamlGuy
2024-05-06 21:17:20 +01:00
parent 40845bb5b3
commit ff8e97f030
13 changed files with 145 additions and 26 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.1.0-beta1" />
<PackageReference Include="Avalonia" Version="11.1.0-beta2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Toolkit.Foundation\Toolkit.Foundation.csproj" />
+8 -8
View File
@@ -1,23 +1,23 @@
namespace Toolkit.Foundation;
public record Enumerate<TValue> : IEnumerate
public record Enumerate<TValue> :
IEnumerate
{
public object? Key { get; init; }
public EnumerateMode Mode { get; init; }
public static Enumerate<TValue, TOptions> With<TOptions>(TOptions options) where TOptions : class
{
return new Enumerate<TValue, TOptions>(options);
}
}
public interface IEnumerate
{
object? Key { get; init; }
}
public record Enumerate<TValue, TOptions>(TOptions? Options = null) : IEnumerate
public record Enumerate<TValue, TOptions>(TOptions? Options = null) :
IEnumerate
where TOptions : class
{
public object? Key { get; init; }
public EnumerateMode Mode { get; init; }
}
+7
View File
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public enum EnumerateMode
{
Append,
Reset
}
+8
View File
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IEnumerate
{
object? Key { get; init; }
EnumerateMode Mode { get; init; }
}
+7 -1
View File
@@ -1,7 +1,13 @@
namespace Toolkit.Foundation;
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
[AttributeUsage(AttributeTargets.Class, Inherited = true)]
public class NotificationAttribute(object key) : Attribute
{
public object Key => key;
}
public class EnumerateAttribute(object key,
EnumerateMode mode = EnumerateMode.Reset) : NotificationAttribute(key)
{
public EnumerateMode Mode => mode;
}
@@ -309,18 +309,20 @@ public partial class ObservableCollectionViewModel<TViewModel> :
}
public async Task Enumerate()
{
if (this.GetAttribute<EnumerateAttribute>() is EnumerateAttribute attribute)
{
if (attribute.Mode == EnumerateMode.Reset)
{
Clear();
object? key = this.GetAttribute<NotificationAttribute>()
is NotificationAttribute attribute
? this.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key
: null;
await Publisher.PublishUI(CreateEnumeration(key));
}
protected virtual IEnumerate CreateEnumeration(object? key) =>
object? key = this.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key;
await Publisher.PublishUI(PrepareEnumeration(key));
}
}
protected virtual IEnumerate PrepareEnumeration(object? key) =>
new Enumerate<TViewModel>() with { Key = key };
public void Insert(int index, TViewModel item) =>
+12
View File
@@ -74,3 +74,15 @@ public partial class ObservableViewModel :
return Task.CompletedTask;
}
}
public partial class ObservableViewModel<TValue>(IServiceProvider provider,
IServiceFactory factory,
IMediator mediator,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer) : ObservableViewModel(provider, factory, mediator, publisher, subscriber, disposer)
where TValue : notnull
{
[ObservableProperty]
private TValue? value;
}
+11 -1
View File
@@ -54,7 +54,17 @@ public class SubscriptionManager(SubscriptionCollection subscriptions) :
{
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
{
subscriptions.AddOrUpdate($"{(key is not null ? $"{key}:" : "")}{argumentType}", _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
if (key is not null)
{
subscriptions.AddOrUpdate($"{key}:{argumentType}", _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
{
collection.Add(new WeakReference(subscriber));
return collection;
});
}
subscriptions.AddOrUpdate($"{argumentType}", _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
{
collection.Add(new WeakReference(subscriber));
return collection;
@@ -1,10 +1,86 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Toolkit.UI.Controls.Avalonia;
namespace Toolkit.UI.Avalonia;
public class ListBoxItemExtension
{
public static readonly AttachedProperty<bool> IsItemClickEnabledProperty =
AvaloniaProperty.RegisterAttached<ListBoxItem, bool>("IsItemClickEnabled",
typeof(ListBoxItemExtension), false);
public static readonly RoutedEvent<ItemInvokedEventArgs> ItemClickEvent =
RoutedEvent.Register<ItemInvokedEventArgs>("ItemClick",
RoutingStrategies.Bubble, typeof(ListBoxItemExtension));
static ListBoxItemExtension()
{
IsItemClickEnabledProperty.Changed.AddClassHandler<ListBoxItem>(OnIsItemClickEnabledPropertyChanged);
}
private static void OnIsItemClickEnabledPropertyChanged(ListBoxItem sender,
AvaloniaPropertyChangedEventArgs args)
{
bool TrySetupListBox()
{
if (sender.GetLogicalAncestors().OfType<ListBox>().FirstOrDefault() is ListBox listBox)
{
void OnItemInvoked(object? _, FluentAvalonia.UI.Controls.NavigationViewItemInvokedEventArgs args)
{
if (args.InvokedItemContainer == sender)
{
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemClickEvent });
}
}
void OnSelectionChanged(object? sender, SelectionChangedEventArgs args)
{
foreach (object item in args.AddedItems)
{
if (listBox.ContainerFromItem(item) == sender)
{
}
}
}
listBox.SelectionChanged += OnSelectionChanged;
return true;
}
return false;
}
if (!TrySetupListBox())
{
void OnAttachedToVisualTree(object? _, VisualTreeAttachmentEventArgs __)
{
sender.AttachedToVisualTree -= OnAttachedToVisualTree;
TrySetupListBox();
}
sender.AttachedToVisualTree += OnAttachedToVisualTree;
}
}
public static bool GetIsItemClickEnabled(ListBoxItem element) =>
element.GetValue(IsItemClickEnabledProperty);
public static void SetIsItemClickEnabled(ListBoxItem element, bool value) =>
element.SetValue(IsItemClickEnabledProperty, value);
public static void AddItemClickHandler(ListBoxItem element, EventHandler<ItemInvokedEventArgs> handler) =>
element.AddHandler(ItemClickEvent, handler);
public static void RemoveItemClickHandler(ListBoxItem element, EventHandler<ItemInvokedEventArgs> handler) =>
element.RemoveHandler(ItemClickEvent, handler);
}
public class NavigationViewItemExtension
{
public static readonly AttachedProperty<bool> IsItemClickEnabledProperty =
@@ -5,8 +5,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.1.0-beta1" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.1.0-beta1" />
<PackageReference Include="Avalonia" Version="11.1.0-beta2" />
<PackageReference Include="Avalonia.Xaml.Behaviors" Version="11.1.0-beta2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Toolkit.Foundation\Toolkit.Foundation.csproj" />
@@ -4,7 +4,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:fluent="using:FluentAvalonia.Styling"
xmlns:labs="using:Avalonia.Labs.Controls">
<fluent:FluentAvaloniaTheme />
<StyleInclude Source="ControlResources.axaml" />
<labs:ControlThemes />
</Styles>
@@ -1,10 +1,9 @@
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using FluentAvalonia.Styling;
namespace Toolkit.UI.Controls.Avalonia;
public class ThemeResources :
Styles
public class ThemeResources : FluentAvaloniaTheme
{
public ThemeResources(IServiceProvider? provider = null)
{
@@ -6,7 +6,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.1.0-beta1" />
<PackageReference Include="Avalonia" Version="11.1.0-beta2" />
<PackageReference Include="Avalonia.Labs.Controls" Version="11.0.10.1" />
<PackageReference Include="FluentAvaloniaUI" Version="2.1.0-preview1" />
</ItemGroup>