qamp/Qrakhen.TilingFrames/Controls/TilingHostControl.cs

75 lines
2.4 KiB
C#

using System.Windows;
using System.Windows.Controls;
using CopaData.FileInspector.GUI.TilingPanels.Controls.DragAndDrop;
using CopaData.FileInspector.GUI.TilingPanels.Controls.DropArea;
using CopaData.FileInspector.GUI.TilingPanels.Models;
using CopaData.FileInspector.GUI.ViewModels;
namespace CopaData.FileInspector.GUI.TilingPanels.Controls;
/// <summary>
/// Control that represents the state of a <see cref="Models.TilingHost"/>,
/// with all the necessary interaction logic tied to it.
/// </summary>
public class TilingHostControl : DragAndDropControl
{
public TilingHost? Host => DataContext as TilingHost;
static TilingHostControl()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(TilingHostControl),
new FrameworkPropertyMetadata(typeof(TilingHostControl)));
}
protected override void OnElementDragOver(DragAndDropData data, UIElement targetElement)
{
SetValue(IsDragTargetPropertyKey, true);
}
protected override void OnElementDragLeave(UIElement targetElement)
{
SetValue(IsDragTargetPropertyKey, false);
}
protected override void OnElementDropped(DragAndDropData data, UIElement targetElement)
{
if (data.DataModel is not TilingFrame frame)
{
// not for us
return;
}
DropDecision decision = (DropDecision)targetElement.GetValue(DropArea.DropArea.DropDecisionProperty);
if (decision == DropDecision.Cancel)
return;
TilingDirection direction = (TilingDirection)decision; // simple cast suffices here
if (direction == TilingDirection.None) {
TilingHost.Insert(TilingManagerViewModel.GlobalRoot, Host!, frame);
} else {
TilingPanel.Attach(TilingManagerViewModel.GlobalRoot, Host!, frame, direction);
}
}
#region Dependency Properties
/// <inheritdoc cref="IsDragTarget" />
private static readonly DependencyPropertyKey IsDragTargetPropertyKey =
DependencyProperty.RegisterReadOnly(
nameof(IsDragTarget),
typeof(bool),
typeof(TilingHostControl),
new PropertyMetadata(false));
/// <inheritdoc cref="IsDragTarget" />
public static readonly DependencyProperty IsDragTargetProperty = IsDragTargetPropertyKey.DependencyProperty;
/// <summary>
/// Whether this <see cref="TilingHostControl"/> is being a potential drop target at the moment.
/// </summary>
public bool IsDragTarget => (bool)GetValue(IsDragTargetProperty);
#endregion
}