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; /// /// Визуальный элемент для предварительного просмотра области сброса. /// [TemplateVisualState(Name = "Normal", GroupName = "CommonStates")] [TemplateVisualState(Name = "Highlighted", GroupName = "CommonStates")] public class DropPreviewAdorner : Control { /// /// Идентификатор свойства для цвета предварительного просмотра. /// public static readonly DependencyProperty PreviewColorProperty = DependencyProperty.Register( "PreviewColor", typeof(Color), typeof(DropPreviewAdorner), new PropertyMetadata(Colors.DodgerBlue)); /// /// Идентификатор свойства для толщины границы. /// public static readonly DependencyProperty PreviewThicknessProperty = DependencyProperty.Register( "PreviewThickness", typeof(double), typeof(DropPreviewAdorner), new PropertyMetadata(2.0)); /// /// Идентификатор свойства для кисти границы. /// public static readonly DependencyProperty PreviewBrushProperty = DependencyProperty.Register( "PreviewBrush", typeof(Brush), typeof(DropPreviewAdorner), new PropertyMetadata(null)); /// /// Инициализирует новый экземпляр класса . /// public DropPreviewAdorner() { DefaultStyleKey = typeof(DropPreviewAdorner); IsHitTestVisible = false; } /// /// Получает или задает цвет предварительного просмотра. /// public Color PreviewColor { get => (Color)GetValue(PreviewColorProperty); set => SetValue(PreviewColorProperty, value); } /// /// Получает или задает толщину границы. /// public double PreviewThickness { get => (double)GetValue(PreviewThicknessProperty); set => SetValue(PreviewThicknessProperty, value); } /// /// Получает или задает кисть границы. /// public Brush PreviewBrush { get => (Brush)GetValue(PreviewBrushProperty); set => SetValue(PreviewBrushProperty, value); } /// /// Показывает элемент с указанными границами. /// /// Границы для отображения. 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); } /// /// Скрывает элемент. /// 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(); } /// /// Обновляет позицию элемента. /// /// Новые границы. 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; } }