Adjust project structure

This commit is contained in:
NaBian committed 2019-04-05 22:23:19 +08:00
1 parent d38912f5da
commit e3c436f939
2111 files changed
+14028 -47199

No files matched your search

@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Windows.Media;
namespace HandyControl.Tools.Extension
{
/// <summary>
/// 颜色扩展类
/// </summary>
public static class ColorExtension
{
/// <summary>
/// 将颜色转换为10进制表示(rgb顺序颠倒)
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static int ToInt32(this Color color) => color.R << 16 | color.G << 8 | color.B;
/// <summary>
/// 将颜色转换为10进制表示(rgb顺序颠倒)
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
public static int ToInt32Reverse(this Color color) => color.R | color.G << 8 | color.B << 18;
internal static List<byte> ToList(this Color color) => new List<byte>
{
color.R,
color.G,
color.B
};
}
}
@@ -0,0 +1,78 @@
using System;
using System.Threading.Tasks;
using System.Windows;
using HandyControl.Controls;
namespace HandyControl.Tools.Extension
{
public interface IDialogResultable<T>
{
T Result { get; set; }
Action CloseAction { get; set; }
}
public static class DialogExtension
{
public static Task<TResult> GetResultAsync<TResult>(this Dialog dialog)
{
var tcs = new TaskCompletionSource<TResult>();
try
{
if (dialog.IsClosed)
{
SetResult();
}
else
{
dialog.Unloaded += OnUnloaded;
dialog.GetViewModel<IDialogResultable<TResult>>().CloseAction = dialog.Close;
}
}
catch (Exception e)
{
tcs.TrySetException(e);
}
return tcs.Task;
// ReSharper disable once ImplicitlyCapturedClosure
void OnUnloaded(object sender, RoutedEventArgs args)
{
dialog.Unloaded -= OnUnloaded;
SetResult();
}
void SetResult()
{
try
{
tcs.TrySetResult(dialog.GetViewModel<IDialogResultable<TResult>>().Result);
}
catch (Exception e)
{
tcs.TrySetException(e);
}
}
}
public static Dialog Initialize<TViewModel>(this Dialog dialog, Action<TViewModel> configure)
{
configure?.Invoke(dialog.GetViewModel<TViewModel>());
return dialog;
}
public static TViewModel GetViewModel<TViewModel>(this Dialog dialog)
{
if (!(dialog.Content is FrameworkElement frameworkElement))
throw new InvalidOperationException("The dialog is not a derived class of the FrameworkElement. ");
if (!(frameworkElement.DataContext is TViewModel viewModel))
throw new InvalidOperationException($"The view model of the dialog is not the {typeof(TViewModel)} type or its derived class. ");
return viewModel;
}
}
}
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace HandyControl.Tools.Extension
{
public static class EnumerableExtension
{
public static IEnumerable<TSource> Do<TSource>(this IEnumerable<TSource> source, Action<TSource> predicate)
{
var enumerable = source as IList<TSource> ?? source.ToList();
foreach (var item in enumerable)
{
predicate.Invoke(item);
}
return enumerable;
}
}
}
@@ -0,0 +1,50 @@
using System;
using System.Windows;
using System.Windows.Media;
using HandyControl.Expression.Drawing;
namespace HandyControl.Tools.Extension
{
public static class GeometryExtension
{
/// <summary>
/// 获取路径总长度
/// </summary>
/// <param name="geometry"></param>
/// <returns></returns>
public static double GetTotalLength(this Geometry geometry)
{
if (geometry == null) return 0;
var pathGeometry = PathGeometry.CreateFromGeometry(geometry);
pathGeometry.GetPointAtFractionLength(1e-4, out var point, out _);
var length = (pathGeometry.Figures[0].StartPoint - point).Length * 1e+4;
return length;
}
/// <summary>
/// 获取路径总长度
/// </summary>
/// <param name="geometry"></param>
/// <param name="size"></param>
/// <param name="strokeThickness"></param>
/// <returns></returns>
public static double GetTotalLength(this Geometry geometry, Size size, double strokeThickness = 1)
{
if (geometry == null) return 0;
if (MathHelper.IsVerySmall(size.Width) || MathHelper.IsVerySmall(size.Height)) return 0;
var length = GetTotalLength(geometry);
var sw = geometry.Bounds.Width / size.Width;
var sh = geometry.Bounds.Height / size.Height;
var min = Math.Min(sw, sh);
if (MathHelper.IsVerySmall(min) || MathHelper.IsVerySmall(strokeThickness)) return 0;
length /= min;
return length / strokeThickness;
}
}
}
@@ -0,0 +1,45 @@
using System.Windows;
namespace HandyControl.Tools.Extension
{
// ReSharper disable once InconsistentNaming
public static class UIElementExtension
{
/// <summary>
/// 显示元素
/// </summary>
/// <param name="element"></param>
public static void Show(this UIElement element)
{
element.Visibility = Visibility.Visible;
}
/// <summary>
/// 显示元素
/// </summary>
/// <param name="element"></param>
/// <param name="show"></param>
public static void Show(this UIElement element, bool show)
{
element.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
}
/// <summary>
/// 不现实元素,但保留空间
/// </summary>
/// <param name="element"></param>
public static void Hide(this UIElement element)
{
element.Visibility = Visibility.Hidden;
}
/// <summary>
/// 不显示元素,且不保留空间
/// </summary>
/// <param name="element"></param>
public static void Collapse(this UIElement element)
{
element.Visibility = Visibility.Collapsed;
}
}
}