diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/PropertyGridViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/PropertyGridViewModel.cs index 9a467227..d83f1db6 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/PropertyGridViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/PropertyGridViewModel.cs @@ -1,11 +1,8 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.ComponentModel; -using System.Linq; using Avalonia.PropertyGrid.ViewModels; using CommunityToolkit.Mvvm.ComponentModel; using OneOf; -using StabilityMatrix.Avalonia.DesignData; using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Avalonia.Views.Dialogs; using StabilityMatrix.Core.Attributes; diff --git a/StabilityMatrix.Avalonia/ViewModels/Inference/Modules/ModuleBase.cs b/StabilityMatrix.Avalonia/ViewModels/Inference/Modules/ModuleBase.cs index a8436b63..d8323406 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Inference/Modules/ModuleBase.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Inference/Modules/ModuleBase.cs @@ -8,18 +8,20 @@ namespace StabilityMatrix.Avalonia.ViewModels.Inference.Modules; public abstract class ModuleBase : StackExpanderViewModel, IComfyStep, IInputImageProvider { + protected readonly ServiceManager VmFactory; + /// protected ModuleBase(ServiceManager vmFactory) - : base(vmFactory) { } + : base(vmFactory) + { + VmFactory = vmFactory; + } /// public void ApplyStep(ModuleApplyStepEventArgs e) { if ( - ( - e.IsEnabledOverrides.TryGetValue(GetType(), out var isEnabledOverride) - && !isEnabledOverride - ) || !IsEnabled + (e.IsEnabledOverrides.TryGetValue(GetType(), out var isEnabledOverride) && !isEnabledOverride) || !IsEnabled ) { return; diff --git a/StabilityMatrix.Avalonia/ViewModels/Inference/StackExpanderViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Inference/StackExpanderViewModel.cs index 4780cdd5..e3ed8966 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Inference/StackExpanderViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Inference/StackExpanderViewModel.cs @@ -1,6 +1,7 @@ using System.Linq; using System.Text.Json.Nodes; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using Newtonsoft.Json; using StabilityMatrix.Avalonia.Controls; using StabilityMatrix.Avalonia.Models.Inference; @@ -27,6 +28,9 @@ public partial class StackExpanderViewModel : StackViewModelBase [property: JsonIgnore] private string? titleExtra; + [ObservableProperty] + private bool isEnabled; + /// /// True if parent StackEditableCard is in edit mode (can drag to reorder) /// @@ -34,8 +38,12 @@ public partial class StackExpanderViewModel : StackViewModelBase [property: JsonIgnore] private bool isEditEnabled; - [ObservableProperty] - private bool isEnabled; + /// + /// True to show the settings button, invokes when clicked + /// + public virtual bool IsSettingsEnabled { get; set; } + + public virtual IRelayCommand? SettingsCommand { get; set; } /// public StackExpanderViewModel(ServiceManager vmFactory) diff --git a/StabilityMatrix.Core/Extensions/EnumerableExtensions.cs b/StabilityMatrix.Core/Extensions/EnumerableExtensions.cs index 26ae5452..49dfa962 100644 --- a/StabilityMatrix.Core/Extensions/EnumerableExtensions.cs +++ b/StabilityMatrix.Core/Extensions/EnumerableExtensions.cs @@ -2,47 +2,61 @@ public static class EnumerableExtensions { - public static IEnumerable<(int, T)> Enumerate( - this IEnumerable items, - int start - ) { + public static IEnumerable<(int, T)> Enumerate(this IEnumerable items, int start) + { return items.Select((item, index) => (index + start, item)); } - - public static IEnumerable<(int, T)> Enumerate( - this IEnumerable items - ) { + + public static IEnumerable<(int, T)> Enumerate(this IEnumerable items) + { return items.Select((item, index) => (index, item)); } - + /// /// Nested for loop helper /// public static IEnumerable<(T, T)> Product(this IEnumerable items, IEnumerable other) { - return from item1 in items - from item2 in other - select (item1, item2); - } - + return from item1 in items from item2 in other select (item1, item2); + } + public static async Task> SelectAsync( - this IEnumerable source, Func> method, - int concurrency = int.MaxValue) + this IEnumerable source, + Func> method, + int concurrency = int.MaxValue + ) { using var semaphore = new SemaphoreSlim(concurrency); - return await Task.WhenAll(source.Select(async s => + return await Task.WhenAll( + source.Select(async s => + { + try + { + // ReSharper disable once AccessToDisposedClosure + await semaphore.WaitAsync().ConfigureAwait(false); + return await method(s).ConfigureAwait(false); + } + finally + { + // ReSharper disable once AccessToDisposedClosure + semaphore.Release(); + } + }) + ) + .ConfigureAwait(false); + } + + /// + /// Executes a specified action on each element in a collection. + /// + /// The type of elements in the collection. + /// The collection to iterate over. + /// The action to perform on each element in the collection. + public static void ForEach(this IEnumerable items, Action action) + { + foreach (var item in items) { - try - { - // ReSharper disable once AccessToDisposedClosure - await semaphore.WaitAsync().ConfigureAwait(false); - return await method(s).ConfigureAwait(false); - } - finally - { - // ReSharper disable once AccessToDisposedClosure - semaphore.Release(); - } - })).ConfigureAwait(false); + action(item); + } } }