Files
Lattice/Lattice.UI.DragDrop.WinUI/Extensions/ThemeExtensions.cs
2026-01-18 16:33:35 +03:00

120 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Lattice.Themes;
using Lattice.Themes.Core.Tokens;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace Lattice.UI.DragDrop.WinUI.Extensions;
/// <summary>
/// Методы расширения для работы с темами в DragDrop.
/// </summary>
public static class ThemeExtensions
{
/// <summary>
/// Применяет стиль перетаскивания, основанный на токенах темы.
/// </summary>
public static void ApplyLatticeDragStyle(this Control control)
{
var style = Application.Current.Resources["Lattice.DragDrop.DragSourceStyle"] as Style;
if (style != null)
{
control.Style = style;
}
else
{
// Fallback на старый стиль
control.ApplyDragStyle();
}
}
/// <summary>
/// Применяет стиль цели сброса, основанный на токенах темы.
/// </summary>
public static void ApplyLatticeDropTargetStyle(this Control control)
{
var style = Application.Current.Resources["Lattice.DragDrop.DropTargetStyle"] as Style;
if (style != null)
{
control.Style = style;
}
else
{
// Fallback на старый стиль
control.ApplyDropTargetStyle();
}
}
/// <summary>
/// Переключает визуальное состояние элемента с использованием токенов темы.
/// </summary>
public static void SetLatticeDragVisualState(this Control control, string stateName, bool useTransitions = true)
{
try
{
VisualStateManager.GoToState(control, stateName, useTransitions);
}
catch
{
// Fallback на альтернативные методы с использованием токенов
var themeManager = ThemeManager.Current;
switch (stateName)
{
case "Dragging":
control.Opacity = themeManager.GetTokenValue<double?>(LatticeTokens.OpacityDrag) ?? 0.7;
control.RenderTransform = new Microsoft.UI.Xaml.Media.ScaleTransform
{
ScaleX = 0.95,
ScaleY = 0.95
};
break;
case "DragOver":
var dragOverBrush = themeManager.GetTokenValue<Microsoft.UI.Xaml.Media.Brush>(
LatticeTokens.BrushDragOverlay);
control.Background = dragOverBrush ??
Application.Current.Resources["Lattice.DragDrop.DragOverBackgroundBrush"] as Microsoft.UI.Xaml.Media.Brush;
break;
case "Normal":
control.ClearValue(Control.OpacityProperty);
control.ClearValue(Control.RenderTransformProperty);
control.ClearValue(Control.BackgroundProperty);
control.ClearValue(Control.BorderBrushProperty);
break;
}
}
}
/// <summary>
/// Получает значение токена для использования в DragDrop.
/// </summary>
public static T? GetDragDropToken<T>(this Control control, string tokenKey) where T : class
{
var themeManager = ThemeManager.Current;
return themeManager.GetTokenValue<T>(tokenKey);
}
/// <summary>
/// Создает DragAdorner с использованием токенов темы.
/// </summary>
public static Controls.DragAdorner CreateLatticeDragAdorner(object dragData)
{
return new Controls.DragAdorner
{
DragData = dragData,
Style = Application.Current.Resources["Lattice.DragDrop.DragSourceStyle"] as Style
};
}
/// <summary>
/// Создает DropPreviewAdorner с использованием токенов темы.
/// </summary>
public static Controls.DropPreviewAdorner CreateLatticeDropPreviewAdorner()
{
return new Controls.DropPreviewAdorner
{
Style = Application.Current.Resources[typeof(Controls.DropPreviewAdorner)] as Style
};
}
}