rework to Azaion.Suite

This commit is contained in:
Alex Bezdieniezhnykh
2024-11-21 13:41:32 +02:00
parent 2cf69f4e4e
commit 5a592e9dbf
76 changed files with 1739 additions and 882 deletions
@@ -1,28 +0,0 @@
using System.Windows.Media;
namespace Azaion.Annotator.Extensions;
public static class ColorExtensions
{
public static Color ToColor(this int id)
{
var index = id % ColorValues.Length;
var hex = index == -1
? "#40DDDDDD"
: $"#40{ColorValues[index]}";
var color =(Color)ColorConverter.ConvertFromString(hex);
return color;
}
private static readonly string[] ColorValues =
[
"FF0000", "00FF00", "0000FF", "FFFF00", "FF00FF", "00FFFF", "000000",
"800000", "008000", "000080", "808000", "800080", "008080", "808080",
"C00000", "00C000", "0000C0", "C0C000", "C000C0", "00C0C0", "C0C0C0",
"400000", "004000", "000040", "404000", "400040", "004040", "404040",
"200000", "002000", "000020", "202000", "200020", "002020", "202020",
"600000", "006000", "000060", "606000", "600060", "006060", "606060",
"A00000", "00A000", "0000A0", "A0A000", "A000A0", "00A0A0", "A0A0A0",
"E00000", "00E000", "0000E0", "E0E000", "E000E0", "00E0E0", "E0E0E0"
];
}
@@ -1,52 +1,52 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
namespace Azaion.Annotator.Extensions;
public static class DataGridExtensions
{
public static DataGridCell? GetCell(this DataGrid grid, int rowIndex, int columnIndex = 0)
{
var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
if (row == null)
return null;
var presenter = FindVisualChild<DataGridCellsPresenter>(row);
if (presenter == null)
return null;
var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
if (cell != null) return cell;
// now try to bring into view and retrieve the cell
grid.ScrollIntoView(row, grid.Columns[columnIndex]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
return cell;
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject? dependencyObj) where T : DependencyObject
{
if (dependencyObj == null)
yield break;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObj); i++)
{
var child = VisualTreeHelper.GetChild(dependencyObj, i);
if (child is T dependencyObject)
{
yield return dependencyObject;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
public static TChildItem? FindVisualChild<TChildItem>(DependencyObject? obj) where TChildItem : DependencyObject =>
FindVisualChildren<TChildItem>(obj).FirstOrDefault();
}
// using System.Windows;
// using System.Windows.Controls;
// using System.Windows.Controls.Primitives;
// using System.Windows.Media;
//
// namespace Azaion.Annotator.Extensions;
//
// public static class DataGridExtensions
// {
// public static DataGridCell? GetCell(this DataGrid grid, int rowIndex, int columnIndex = 0)
// {
// var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
// if (row == null)
// return null;
//
// var presenter = FindVisualChild<DataGridCellsPresenter>(row);
// if (presenter == null)
// return null;
//
// var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
// if (cell != null) return cell;
//
// // now try to bring into view and retrieve the cell
// grid.ScrollIntoView(row, grid.Columns[columnIndex]);
// cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
//
// return cell;
// }
//
// private static IEnumerable<T> FindVisualChildren<T>(DependencyObject? dependencyObj) where T : DependencyObject
// {
// if (dependencyObj == null)
// yield break;
//
// for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObj); i++)
// {
// var child = VisualTreeHelper.GetChild(dependencyObj, i);
// if (child is T dependencyObject)
// {
// yield return dependencyObject;
// }
//
// foreach (T childOfChild in FindVisualChildren<T>(child))
// {
// yield return childOfChild;
// }
// }
// }
//
// public static TChildItem? FindVisualChild<TChildItem>(DependencyObject? obj) where TChildItem : DependencyObject =>
// FindVisualChildren<TChildItem>(obj).FirstOrDefault();
// }
@@ -1,12 +0,0 @@
using Newtonsoft.Json.Converters;
namespace Azaion.Annotator.Extensions;
public class DenseDateTimeConverter : IsoDateTimeConverter
{
public DenseDateTimeConverter()
{
DateTimeFormat = "yy-MM-dd HH:mm:ss";
}
}
@@ -1,10 +0,0 @@
using System.IO;
namespace Azaion.Annotator.Extensions;
public static class DirectoryInfoExtensions
{
public static IEnumerable<FileInfo> GetFiles(this DirectoryInfo dir, params string[] searchExtensions) =>
dir.GetFiles("*.*", SearchOption.AllDirectories)
.Where(f => searchExtensions.Any(s => f.Name.Contains(s, StringComparison.CurrentCultureIgnoreCase))).ToList();
}
@@ -1,67 +0,0 @@
namespace Azaion.Annotator.Extensions;
public class ParallelOptions
{
public int ProgressUpdateInterval { get; set; } = 100;
public Func<int, Task> ProgressFn { get; set; } = null!;
public double CpuUtilPercent { get; set; } = 100;
}
public class ParallelExt
{
public static async Task ForEachAsync<T>(ICollection<T> source,
Func<T, CancellationToken, ValueTask> processFn,
ParallelOptions? parallelOptions = null,
CancellationToken cancellationToken = default)
{
parallelOptions ??= new ParallelOptions
{
CpuUtilPercent = 100,
ProgressUpdateInterval = 100,
ProgressFn = i =>
{
Console.WriteLine($"Processed {i} item by Task {Task.CurrentId}%");
return Task.CompletedTask;
}
};
var threadsCount = (int)(Environment.ProcessorCount * parallelOptions.CpuUtilPercent / 100.0);
var processedCount = 0;
var chunkSize = Math.Max(1, (int)(source.Count / (decimal)threadsCount));
var chunks = source.Chunk(chunkSize).ToList();
if (chunks.Count > threadsCount)
{
chunks[^2] = chunks[^2].Concat(chunks.Last()).ToArray();
chunks.RemoveAt(chunks.Count - 1);
}
var progressUpdateLock = new SemaphoreSlim(1);
var tasks = new List<Task>();
foreach (var chunk in chunks)
{
tasks.Add(await Task.Factory.StartNew(async () =>
{
foreach (var item in chunk)
{
await processFn(item, cancellationToken);
Interlocked.Increment(ref processedCount);
if (processedCount % parallelOptions.ProgressUpdateInterval == 0 && parallelOptions.ProgressFn != null)
_ = Task.Run(async () =>
{
await progressUpdateLock.WaitAsync(cancellationToken);
try
{
await parallelOptions.ProgressFn(processedCount);
}
finally
{
progressUpdateLock.Release();
}
}, cancellationToken);
}
}, TaskCreationOptions.LongRunning));
}
await Task.WhenAll(tasks);
}
}
@@ -1,19 +0,0 @@
namespace Azaion.Annotator.Extensions;
public static class ThrottleExt
{
private static bool _throttleOn;
public static async Task Throttle(Func<Task> func, TimeSpan? throttleTime = null)
{
if (_throttleOn)
return;
_throttleOn = true;
await func();
_ = Task.Run(async () =>
{
await Task.Delay(throttleTime ?? TimeSpan.FromMilliseconds(500));
_throttleOn = false;
});
}
}
@@ -1,5 +1,6 @@
using System.Windows;
using Azaion.Annotator.DTO;
using Azaion.Common.DTO.Config;
namespace Azaion.Annotator.Extensions;