I like it move it move it

This commit is contained in:
Oleksandr Bezdieniezhnykh
2024-05-18 17:51:38 +03:00
parent de77c9236f
commit dd0ece36f2
33 changed files with 0 additions and 0 deletions
@@ -0,0 +1,38 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Azaion.Annotator.Extensions;
public static class CanvasExtensions
{
public static readonly DependencyProperty PercentPositionProperty =
DependencyProperty.RegisterAttached("PercentPosition", typeof(Point), typeof(CanvasExtensions),
new PropertyMetadata(new Point(0, 0), OnPercentPositionChanged));
public static readonly DependencyProperty PercentSizeProperty =
DependencyProperty.RegisterAttached("PercentSize", typeof(Point), typeof(CanvasExtensions),
new PropertyMetadata(new Point(0, 0), OnPercentSizeChanged));
private static void OnPercentSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public static void SetPercentPosition(DependencyObject obj, Point value) => obj.SetValue(PercentPositionProperty, value);
private static void OnPercentPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is FrameworkElement element)) return;
if (!(VisualTreeHelper.GetParent(element) is Canvas canvas)) return;
canvas.SizeChanged += (s, arg) =>
{
var percentPosition = (Point)element.GetValue(PercentPositionProperty);
var xPosition = percentPosition.X * canvas.ActualWidth - element.ActualWidth / 2;
var yPosition = percentPosition.Y * canvas.ActualHeight - element.ActualHeight / 2;
Canvas.SetLeft(element, xPosition);
Canvas.SetTop(element, yPosition);
};
}
}
@@ -0,0 +1,32 @@
using System.Windows.Media;
namespace Azaion.Annotator.Extensions;
public static class ColorExtensions
{
public static Color ToColor(this int id)
{
var index = id % ColorValues.Length;
return ToColor($"#{ColorValues[index]}");
}
public static Color ToColor(string hex)
{
var color = (Color)ColorConverter.ConvertFromString(hex);
color.A = 128;
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"
];
}
@@ -0,0 +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();
}
@@ -0,0 +1,10 @@
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();
}
@@ -0,0 +1,14 @@
using System.ComponentModel;
namespace Azaion.Annotator;
public static class SynchronizeInvokeExtensions
{
public static void InvokeEx<T>(this T t, Action<T> action) where T : ISynchronizeInvoke
{
if (t.InvokeRequired)
t.Invoke(action, [t]);
else
action(t);
}
}