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.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;

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
{
protected readonly ServiceManager<ViewModelBase> VmFactory;
/// <inheritdoc />
protected ModuleBase(ServiceManager<ViewModelBase> vmFactory)
: base(vmFactory) { }
: base(vmFactory)
{
VmFactory = vmFactory;
}
/// <inheritdoc />
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;

12
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;
/// <summary>
/// True if parent StackEditableCard is in edit mode (can drag to reorder)
/// </summary>
@ -34,8 +38,12 @@ public partial class StackExpanderViewModel : StackViewModelBase
[property: JsonIgnore]
private bool isEditEnabled;
[ObservableProperty]
private bool isEnabled;
/// <summary>
/// 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 />
public StackExpanderViewModel(ServiceManager<ViewModelBase> vmFactory)

72
StabilityMatrix.Core/Extensions/EnumerableExtensions.cs

@ -2,47 +2,61 @@
public static class EnumerableExtensions
{
public static IEnumerable<(int, T)> Enumerate<T>(
this IEnumerable<T> items,
int start
) {
public static IEnumerable<(int, T)> Enumerate<T>(this IEnumerable<T> items, int start)
{
return items.Select((item, index) => (index + start, item));
}
public static IEnumerable<(int, T)> Enumerate<T>(
this IEnumerable<T> items
) {
public static IEnumerable<(int, T)> Enumerate<T>(this IEnumerable<T> items)
{
return items.Select((item, index) => (index, item));
}
/// <summary>
/// Nested for loop helper
/// </summary>
public static IEnumerable<(T, T)> Product<T>(this IEnumerable<T> items, IEnumerable<T> 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<IEnumerable<TResult>> SelectAsync<TSource, TResult>(
this IEnumerable<TSource> source, Func<TSource, Task<TResult>> method,
int concurrency = int.MaxValue)
this IEnumerable<TSource> source,
Func<TSource, Task<TResult>> 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);
}
/// <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
{
// 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);
}
}
}

Loading…
Cancel
Save