DragAndDrop core
This commit is contained in:
141
Lattice.UI.DragDrop.WinUI/Controls/DropPreviewAdorner.cs
Normal file
141
Lattice.UI.DragDrop.WinUI/Controls/DropPreviewAdorner.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
using Microsoft.UI;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using System;
|
||||
using Windows.UI;
|
||||
|
||||
namespace Lattice.UI.DragDrop.WinUI.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Визуальный элемент для предварительного просмотра области сброса.
|
||||
/// </summary>
|
||||
[TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
|
||||
[TemplateVisualState(Name = "Highlighted", GroupName = "CommonStates")]
|
||||
public class DropPreviewAdorner : Control
|
||||
{
|
||||
/// <summary>
|
||||
/// Идентификатор свойства для цвета предварительного просмотра.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PreviewColorProperty =
|
||||
DependencyProperty.Register(
|
||||
"PreviewColor",
|
||||
typeof(Color),
|
||||
typeof(DropPreviewAdorner),
|
||||
new PropertyMetadata(Colors.DodgerBlue));
|
||||
|
||||
/// <summary>
|
||||
/// Идентификатор свойства для толщины границы.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PreviewThicknessProperty =
|
||||
DependencyProperty.Register(
|
||||
"PreviewThickness",
|
||||
typeof(double),
|
||||
typeof(DropPreviewAdorner),
|
||||
new PropertyMetadata(2.0));
|
||||
|
||||
/// <summary>
|
||||
/// Идентификатор свойства для кисти границы.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty PreviewBrushProperty =
|
||||
DependencyProperty.Register(
|
||||
"PreviewBrush",
|
||||
typeof(Brush),
|
||||
typeof(DropPreviewAdorner),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// Инициализирует новый экземпляр класса <see cref="DropPreviewAdorner"/>.
|
||||
/// </summary>
|
||||
public DropPreviewAdorner()
|
||||
{
|
||||
DefaultStyleKey = typeof(DropPreviewAdorner);
|
||||
IsHitTestVisible = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получает или задает цвет предварительного просмотра.
|
||||
/// </summary>
|
||||
public Color PreviewColor
|
||||
{
|
||||
get => (Color)GetValue(PreviewColorProperty);
|
||||
set => SetValue(PreviewColorProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получает или задает толщину границы.
|
||||
/// </summary>
|
||||
public double PreviewThickness
|
||||
{
|
||||
get => (double)GetValue(PreviewThicknessProperty);
|
||||
set => SetValue(PreviewThicknessProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получает или задает кисть границы.
|
||||
/// </summary>
|
||||
public Brush PreviewBrush
|
||||
{
|
||||
get => (Brush)GetValue(PreviewBrushProperty);
|
||||
set => SetValue(PreviewBrushProperty, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Показывает элемент с указанными границами.
|
||||
/// </summary>
|
||||
/// <param name="bounds">Границы для отображения.</param>
|
||||
public void Show(Core.Geometry.Rect bounds)
|
||||
{
|
||||
Width = bounds.Width;
|
||||
Height = bounds.Height;
|
||||
|
||||
var translateTransform = new TranslateTransform
|
||||
{
|
||||
X = bounds.X,
|
||||
Y = bounds.Y
|
||||
};
|
||||
|
||||
RenderTransform = translateTransform;
|
||||
Visibility = Visibility.Visible;
|
||||
|
||||
VisualStateManager.GoToState(this, "Highlighted", true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Скрывает элемент.
|
||||
/// </summary>
|
||||
public void Hide()
|
||||
{
|
||||
VisualStateManager.GoToState(this, "Normal", true);
|
||||
|
||||
// Отложенное скрытие для плавной анимации
|
||||
var timer = new DispatcherTimer
|
||||
{
|
||||
Interval = TimeSpan.FromMilliseconds(150)
|
||||
};
|
||||
|
||||
timer.Tick += (s, e) =>
|
||||
{
|
||||
timer.Stop();
|
||||
Visibility = Visibility.Collapsed;
|
||||
};
|
||||
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Обновляет позицию элемента.
|
||||
/// </summary>
|
||||
/// <param name="bounds">Новые границы.</param>
|
||||
public void UpdatePosition(Core.Geometry.Rect bounds)
|
||||
{
|
||||
if (RenderTransform is TranslateTransform transform)
|
||||
{
|
||||
transform.X = bounds.X;
|
||||
transform.Y = bounds.Y;
|
||||
}
|
||||
|
||||
Width = bounds.Width;
|
||||
Height = bounds.Height;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user