Browse Source

Add settings option for stack expanders

pull/333/head
Ionite 12 months ago
parent
commit
1a999758da
No known key found for this signature in database
  1. 5
      StabilityMatrix.Avalonia/ViewModels/Dialogs/PropertyGridViewModel.cs
  2. 12
      StabilityMatrix.Avalonia/ViewModels/Inference/Modules/ModuleBase.cs
  3. 12
      StabilityMatrix.Avalonia/ViewModels/Inference/StackExpanderViewModel.cs
  4. 72
      StabilityMatrix.Core/Extensions/EnumerableExtensions.cs

5
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.ComponentModel;
using System.Linq;
using Avalonia.PropertyGrid.ViewModels; using Avalonia.PropertyGrid.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using OneOf; using OneOf;
using StabilityMatrix.Avalonia.DesignData;
using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Avalonia.ViewModels.Base;
using StabilityMatrix.Avalonia.Views.Dialogs; using StabilityMatrix.Avalonia.Views.Dialogs;
using StabilityMatrix.Core.Attributes; using StabilityMatrix.Core.Attributes;

12
StabilityMatrix.Avalonia/ViewModels/Inference/Modules/ModuleBase.cs

@ -8,18 +8,20 @@ namespace StabilityMatrix.Avalonia.ViewModels.Inference.Modules;
public abstract class ModuleBase : StackExpanderViewModel, IComfyStep, IInputImageProvider public abstract class ModuleBase : StackExpanderViewModel, IComfyStep, IInputImageProvider
{ {
protected readonly ServiceManager<ViewModelBase> VmFactory;
/// <inheritdoc /> /// <inheritdoc />
protected ModuleBase(ServiceManager<ViewModelBase> vmFactory) protected ModuleBase(ServiceManager<ViewModelBase> vmFactory)
: base(vmFactory) { } : base(vmFactory)
{
VmFactory = vmFactory;
}
/// <inheritdoc /> /// <inheritdoc />
public void ApplyStep(ModuleApplyStepEventArgs e) public void ApplyStep(ModuleApplyStepEventArgs e)
{ {
if ( if (
( (e.IsEnabledOverrides.TryGetValue(GetType(), out var isEnabledOverride) && !isEnabledOverride) || !IsEnabled
e.IsEnabledOverrides.TryGetValue(GetType(), out var isEnabledOverride)
&& !isEnabledOverride
) || !IsEnabled
) )
{ {
return; return;

12
StabilityMatrix.Avalonia/ViewModels/Inference/StackExpanderViewModel.cs

@ -1,6 +1,7 @@
using System.Linq; using System.Linq;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Newtonsoft.Json; using Newtonsoft.Json;
using StabilityMatrix.Avalonia.Controls; using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Models.Inference; using StabilityMatrix.Avalonia.Models.Inference;
@ -27,6 +28,9 @@ public partial class StackExpanderViewModel : StackViewModelBase
[property: JsonIgnore] [property: JsonIgnore]
private string? titleExtra; private string? titleExtra;
[ObservableProperty]
private bool isEnabled;
/// <summary> /// <summary>
/// True if parent StackEditableCard is in edit mode (can drag to reorder) /// True if parent StackEditableCard is in edit mode (can drag to reorder)
/// </summary> /// </summary>
@ -34,8 +38,12 @@ public partial class StackExpanderViewModel : StackViewModelBase
[property: JsonIgnore] [property: JsonIgnore]
private bool isEditEnabled; private bool isEditEnabled;
[ObservableProperty] /// <summary>
private bool isEnabled; /// True to show the settings button, invokes <see cref="SettingsCommand"/> when clicked
/// </summary>
public virtual bool IsSettingsEnabled { get; set; }
public virtual IRelayCommand? SettingsCommand { get; set; }
/// <inheritdoc /> /// <inheritdoc />
public StackExpanderViewModel(ServiceManager<ViewModelBase> vmFactory) public StackExpanderViewModel(ServiceManager<ViewModelBase> vmFactory)

72
StabilityMatrix.Core/Extensions/EnumerableExtensions.cs

@ -2,47 +2,61 @@
public static class EnumerableExtensions public static class EnumerableExtensions
{ {
public static IEnumerable<(int, T)> Enumerate<T>( public static IEnumerable<(int, T)> Enumerate<T>(this IEnumerable<T> items, int start)
this IEnumerable<T> items, {
int start
) {
return items.Select((item, index) => (index + start, item)); return items.Select((item, index) => (index + start, item));
} }
public static IEnumerable<(int, T)> Enumerate<T>( public static IEnumerable<(int, T)> Enumerate<T>(this IEnumerable<T> items)
this IEnumerable<T> items {
) {
return items.Select((item, index) => (index, item)); return items.Select((item, index) => (index, item));
} }
/// <summary> /// <summary>
/// Nested for loop helper /// Nested for loop helper
/// </summary> /// </summary>
public static IEnumerable<(T, T)> Product<T>(this IEnumerable<T> items, IEnumerable<T> other) public static IEnumerable<(T, T)> Product<T>(this IEnumerable<T> items, IEnumerable<T> other)
{ {
return from item1 in items return from item1 in items from item2 in other select (item1, item2);
from item2 in other }
select (item1, item2);
}
public static async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>( public static async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>(
this IEnumerable<TSource> source, Func<TSource, Task<TResult>> method, this IEnumerable<TSource> source,
int concurrency = int.MaxValue) Func<TSource, Task<TResult>> method,
int concurrency = int.MaxValue
)
{ {
using var semaphore = new SemaphoreSlim(concurrency); 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);
}
/// <summary>
/// Executes a specified action on each element in a collection.
/// </summary>
/// <typeparam name="T">The type of elements in the collection.</typeparam>
/// <param name="items">The collection to iterate over.</param>
/// <param name="action">The action to perform on each element in the collection.</param>
public static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
{
foreach (var item in items)
{ {
try action(item);
{ }
// ReSharper disable once AccessToDisposedClosure
await semaphore.WaitAsync().ConfigureAwait(false);
return await method(s).ConfigureAwait(false);
}
finally
{
// ReSharper disable once AccessToDisposedClosure
semaphore.Release();
}
})).ConfigureAwait(false);
} }
} }

Loading…
Cancel
Save