Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Runtime.CompilerServices;
using KGySoft.CoreLibraries;
using StabilityMatrix.Core.Helper.Cache;

namespace StabilityMatrix.Avalonia.Controls.VendorLabs.Cache;

Expand All @@ -23,7 +16,7 @@ public class InMemoryStorage<T>
private readonly LinkedList<InMemoryStorageItem<T>> _lruList = [];

private int _maxItemCount;
private object _settingMaxItemCountLocker = new();
private readonly Lock _settingMaxItemCountLocker = new();

/// <summary>
/// Gets or sets the maximum count of Items that can be stored in this InMemoryStorage instance.
Expand Down
18 changes: 6 additions & 12 deletions StabilityMatrix.Avalonia/Helpers/EnumHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.Api;

namespace StabilityMatrix.Avalonia.Helpers;

public static class EnumHelpers
{
public static IEnumerable<CivitPeriod> AllCivitPeriods { get; } =
Enum.GetValues(typeof(CivitPeriod)).Cast<CivitPeriod>();
public static IEnumerable<CivitPeriod> AllCivitPeriods { get; } = Enum.GetValues<CivitPeriod>();

public static IEnumerable<CivitSortMode> AllSortModes { get; } =
Enum.GetValues(typeof(CivitSortMode)).Cast<CivitSortMode>();
public static IEnumerable<CivitSortMode> AllSortModes { get; } = Enum.GetValues<CivitSortMode>();

public static IEnumerable<CivitModelType> AllCivitModelTypes { get; } =
Enum.GetValues(typeof(CivitModelType))
.Cast<CivitModelType>()
Enum.GetValues<CivitModelType>()
.Where(t => t == CivitModelType.All || t.ConvertTo<SharedFolderType>() > 0)
.OrderBy(t => t.ToString());

public static IEnumerable<CivitModelType> MetadataEditorCivitModelTypes { get; } =
Enum.GetValues(typeof(CivitModelType)).Cast<CivitModelType>().OrderBy(t => t.ToString());
Enum.GetValues<CivitModelType>().OrderBy(t => t.ToString());

public static IEnumerable<CivitBaseModelType> AllCivitBaseModelTypes { get; } =
Enum.GetValues(typeof(CivitBaseModelType)).Cast<CivitBaseModelType>();
Enum.GetValues<CivitBaseModelType>();

public static IEnumerable<CivitBaseModelType> MetadataEditorCivitBaseModelTypes { get; } =
AllCivitBaseModelTypes.Where(x => x != CivitBaseModelType.All);
Expand Down
3 changes: 1 addition & 2 deletions StabilityMatrix.Avalonia/Helpers/IOCommands.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Input;
using StabilityMatrix.Core.Processes;

namespace StabilityMatrix.Avalonia.Helpers;
Expand Down
9 changes: 2 additions & 7 deletions StabilityMatrix.Avalonia/Helpers/ImageProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SkiaSharp;
using SkiaSharp;
using StabilityMatrix.Core.Extensions;

namespace StabilityMatrix.Avalonia.Helpers;
Expand Down Expand Up @@ -45,9 +42,7 @@ public static SKImage CreateImageGrid(IReadOnlyList<SKImage> images, int spacing
// Draw images
using var canvas = new SKCanvas(output);

foreach (
var (row, column) in Enumerable.Range(0, rows).Product(Enumerable.Range(0, columns))
)
foreach (var (row, column) in Enumerable.Range(0, rows).Product(Enumerable.Range(0, columns)))
{
// Stop if we have drawn all images
var index = row * columns + column;
Expand Down
24 changes: 9 additions & 15 deletions StabilityMatrix.Avalonia/Helpers/ImageSearcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using FuzzySharp;
using FuzzySharp;
using FuzzySharp.PreProcess;
using StabilityMatrix.Core.Models.Database;

Expand Down Expand Up @@ -27,8 +26,7 @@ public Func<LocalImageFile, bool> GetPredicate(string? searchQuery)

if (
SearchOptions.HasFlag(ImageSearchOptions.FileName)
&& Fuzz.WeightedRatio(searchQuery, file.FileName, PreprocessMode.Full)
> MinimumFuzzScore
&& Fuzz.WeightedRatio(searchQuery, file.FileName, PreprocessMode.Full) > MinimumFuzzScore
)
{
return true;
Expand All @@ -53,22 +51,18 @@ public Func<LocalImageFile, bool> GetPredicate(string? searchQuery)
) ?? false
)
|| SearchOptions.HasFlag(ImageSearchOptions.Seed)
&& parameters.Seed
.ToString()
&& parameters
.Seed.ToString()
.StartsWith(searchQuery, StringComparison.OrdinalIgnoreCase)
|| SearchOptions.HasFlag(ImageSearchOptions.Sampler)
&& (
parameters.Sampler?.StartsWith(
searchQuery,
StringComparison.OrdinalIgnoreCase
) ?? false
parameters.Sampler?.StartsWith(searchQuery, StringComparison.OrdinalIgnoreCase)
?? false
)
|| SearchOptions.HasFlag(ImageSearchOptions.ModelName)
&& (
parameters.ModelName?.StartsWith(
searchQuery,
StringComparison.OrdinalIgnoreCase
) ?? false
parameters.ModelName?.StartsWith(searchQuery, StringComparison.OrdinalIgnoreCase)
?? false
)
)
{
Expand All @@ -90,6 +84,6 @@ public enum ImageSearchOptions
Seed = 1 << 3,
Sampler = 1 << 4,
ModelName = 1 << 5,
All = int.MaxValue
All = int.MaxValue,
}
}
6 changes: 1 addition & 5 deletions StabilityMatrix.Avalonia/Helpers/PngDataHelper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text;
using System.Text.Json;
using Force.Crc32;
using StabilityMatrix.Avalonia.Models;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using AsyncAwaitBestPractices;
using AutoComplete.Builders;
using AutoComplete.Clients.IndexSearchers;
Expand Down Expand Up @@ -358,12 +354,12 @@ private IEnumerable<ICompletionData> GetCompletionNetworkTypes(string searchTerm
{
(PromptExtraNetworkType.Lora, "lora"),
(PromptExtraNetworkType.LyCORIS, "lyco"),
(PromptExtraNetworkType.Embedding, "embedding")
(PromptExtraNetworkType.Embedding, "embedding"),
};

return availableTypes
.Where(
type => type.Item1.GetStringValue().StartsWith(searchTerm, StringComparison.OrdinalIgnoreCase)
.Where(type =>
type.Item1.GetStringValue().StartsWith(searchTerm, StringComparison.OrdinalIgnoreCase)
)
.Select(type => new ModelTypeCompletionData(type.Item2, type.Item1));
}
Expand All @@ -381,7 +377,7 @@ private IEnumerable<ICompletionData> GetCompletionTags(string searchTerm, int it
{
Term = searchTerm,
MaxItemCount = itemsCount,
SuggestWhenFoundStartsWith = suggest
SuggestWhenFoundStartsWith = suggest,
};

var result = searcher.Search(searchOptions);
Expand Down
4 changes: 2 additions & 2 deletions StabilityMatrix.Avalonia/Services/RunningPackageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ IPyRunner pyRunner
)
{
// Get lock
using var @lock = await packageLocks.LockAsync(installedPackage.Id, cancellationToken);
using var _ = await packageLocks.LockAsync(installedPackage.Id, cancellationToken);

// Ignore if already running after lock
if (RunningPackages.ContainsKey(installedPackage.Id))
Expand Down Expand Up @@ -266,7 +266,7 @@ await basePackage.RunPackage(
public async Task StopPackage(Guid id, CancellationToken cancellationToken = default)
{
// Get lock
using var @lock = await packageLocks.LockAsync(id, cancellationToken);
using var _ = await packageLocks.LockAsync(id, cancellationToken);

// Ignore if not running after lock
if (!RunningPackages.TryGetValue(id, out var vm))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Collections.Frozen;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
Expand All @@ -15,7 +16,6 @@
using FluentAvalonia.UI.Controls;
using Injectio.Attributes;
using Microsoft.Extensions.Logging;
using Refit;
using StabilityMatrix.Avalonia.Animations;
using StabilityMatrix.Avalonia.Languages;
using StabilityMatrix.Avalonia.Models;
Expand Down Expand Up @@ -65,7 +65,7 @@ IModelImportService modelImportService
[NotifyPropertyChangedFor(nameof(CanGoNext), nameof(CanGoPrevious))]
public required partial int CurrentIndex { get; set; }

private List<string> ignoredFileNameFormatVars =
private readonly FrozenSet<string> ignoredFileNameFormatVars =
[
"seed",
"prompt",
Expand All @@ -86,17 +86,17 @@ IModelImportService modelImportService
.Substitutions.Where(kv => !ignoredFileNameFormatVars.Contains(kv.Key))
.Select(kv => new FileNameFormatVar { Variable = $"{{{kv.Key}}}", Example = kv.Value.Invoke() });

private SourceCache<CivitImage, string> imageCache = new(x => x.Url);
private readonly SourceCache<CivitImage, string> imageCache = new(x => x.Url);

public IObservableCollection<ImageSource> ImageSources { get; set; } =
new ObservableCollectionExtended<ImageSource>();

private SourceCache<CivitModelVersion, int> modelVersionCache = new(x => x.Id);
private readonly SourceCache<CivitModelVersion, int> modelVersionCache = new(x => x.Id);

public IObservableCollection<ModelVersionViewModel> ModelVersions { get; set; } =
new ObservableCollectionExtended<ModelVersionViewModel>();

private SourceCache<CivitFile, int> civitFileCache = new(x => x.Id);
private readonly SourceCache<CivitFile, int> civitFileCache = new(x => x.Id);

public IObservableCollection<CivitFileViewModel> CivitFiles { get; set; } =
new ObservableCollectionExtended<CivitFileViewModel>();
Expand Down Expand Up @@ -670,12 +670,15 @@ private async Task DeleteModelVersion(CivitModelVersion modelVersion)
if (file is not { Type: CivitFileType.Model, Hashes.BLAKE3: not null })
continue;

var matchingModels = (await modelIndexService.FindByHashAsync(file.Hashes.BLAKE3)).ToList();
List<LocalModelFile> matchingModels =
[
.. (await modelIndexService.FindByHashAsync(file.Hashes.BLAKE3)),
];

if (matchingModels.Count == 0)
{
await modelIndexService.RefreshIndex();
matchingModels = (await modelIndexService.FindByHashAsync(file.Hashes.BLAKE3)).ToList();
matchingModels = [.. (await modelIndexService.FindByHashAsync(file.Hashes.BLAKE3))];
}

if (matchingModels.Count == 0)
Expand Down
6 changes: 1 addition & 5 deletions StabilityMatrix.Avalonia/ViewModels/ConsoleViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.ComponentModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using System.Web;
using Avalonia.Threading;
Expand Down
2 changes: 1 addition & 1 deletion StabilityMatrix.Core/Helper/HardwareInfo/HardwareHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static partial class HardwareHelper
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

private static IReadOnlyList<GpuInfo>? cachedGpuInfos;
private static readonly object cachedGpuInfosLock = new();
private static readonly Lock cachedGpuInfosLock = new();

private static readonly Lazy<IHardwareInfo> HardwareInfoLazy = new(() => new Hardware.Info.HardwareInfo()
);
Expand Down
Loading