Files
community/Ink Canvas/Windows/SettingsViews/SettingsViews/FloatingBarDnDSettingsPanel.xaml.cs
T

164 lines
5.8 KiB
C#
Raw Normal View History

2025-09-07 13:30:46 +08:00
using iNKORE.UI.WPF.DragDrop;
2026-01-02 01:35:22 +08:00
using System;
2025-09-07 13:30:46 +08:00
using System.Collections.ObjectModel;
2025-08-31 15:35:28 +08:00
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
2025-09-07 13:30:46 +08:00
namespace Ink_Canvas.Windows.SettingsViews
{
public class FloatingBarItem
{
2025-08-31 15:35:28 +08:00
public DrawingImage IconSource { get; set; }
}
2025-09-07 13:30:46 +08:00
public partial class FloatingBarDnDSettingsPanel : UserControl
{
public class BarItemsDropTarget : IDropTarget
{
2025-08-31 15:35:28 +08:00
public ObservableCollection<FloatingBarItem> BarItems { get; set; } =
new ObservableCollection<FloatingBarItem>();
2025-09-07 13:30:46 +08:00
void IDropTarget.DragOver(IDropInfo info)
{
2025-08-31 15:35:28 +08:00
info.Effects = DragDropEffects.Move;
info.DropTargetAdorner = DropTargetAdorners.Insert;
}
2025-09-07 13:30:46 +08:00
void IDropTarget.Drop(IDropInfo info)
{
if (info.Data is FloatingBarItem draggedItem)
{
2025-08-31 15:35:28 +08:00
var targetCollection = info.TargetCollection as ObservableCollection<FloatingBarItem>;
var sourceCollection = info.DragInfo.SourceCollection as ObservableCollection<FloatingBarItem>;
Trace.WriteLine(info.InsertIndex);
2026-01-10 17:31:55 +08:00
// 在同一个 ObservableCollection 中移动
2025-09-07 13:30:46 +08:00
if (targetCollection.Equals(sourceCollection))
{
if (info.InsertIndex == 0)
{
targetCollection.Move(targetCollection.IndexOf(info.Data as FloatingBarItem), 0);
}
else if (info.InsertIndex == targetCollection.Count)
{
2025-08-31 15:35:28 +08:00
targetCollection.Remove(info.Data as FloatingBarItem);
targetCollection.Add(info.Data as FloatingBarItem);
2025-09-07 13:30:46 +08:00
}
else if ((info.InsertIndex - targetCollection.IndexOf(info.Data as FloatingBarItem) == 1 &&
info.InsertPosition == RelativeInsertPosition.AfterTargetItem) ||
2025-08-31 15:35:28 +08:00
(info.InsertIndex - targetCollection.IndexOf(info.Data as FloatingBarItem) == 0 &&
2025-09-07 13:30:46 +08:00
info.InsertPosition == RelativeInsertPosition.BeforeTargetItem)) { }
else
{
targetCollection.Move(targetCollection.IndexOf(info.Data as FloatingBarItem), info.InsertIndex - 1);
2025-08-31 15:35:28 +08:00
}
2025-09-07 13:30:46 +08:00
}
else
2026-01-10 17:31:55 +08:00
{ // 跨 ObservableCollection 移动
2025-08-31 15:35:28 +08:00
sourceCollection.Remove(info.Data as FloatingBarItem);
targetCollection.Insert(info.InsertIndex, info.Data as FloatingBarItem);
}
}
}
2025-09-07 13:30:46 +08:00
void IDropTarget.DragEnter(IDropInfo info)
{
2025-08-31 15:35:28 +08:00
}
2025-09-07 13:30:46 +08:00
void IDropTarget.DragLeave(IDropInfo info)
{
2025-08-31 15:35:28 +08:00
}
}
2025-09-07 13:30:46 +08:00
public class BarDrawerItemsDropTarget : IDropTarget
{
2025-08-31 15:35:28 +08:00
public ObservableCollection<FloatingBarItem> BarDrawerItems { get; set; } =
new ObservableCollection<FloatingBarItem>();
2025-09-07 13:30:46 +08:00
void IDropTarget.DragOver(IDropInfo info)
{
2025-08-31 15:35:28 +08:00
info.Effects = DragDropEffects.Move;
info.DropTargetAdorner = DropTargetAdorners.Insert;
}
2025-09-07 13:30:46 +08:00
void IDropTarget.Drop(IDropInfo info)
{
if (info.Data is FloatingBarItem draggedItem)
{
2025-08-31 15:35:28 +08:00
var targetCollection = info.TargetCollection as ObservableCollection<FloatingBarItem>;
var sourceCollection = info.DragInfo.SourceCollection as ObservableCollection<FloatingBarItem>;
2026-01-10 17:31:55 +08:00
// 在同一个 ObservableCollection 中移动
2025-09-07 13:30:46 +08:00
if (targetCollection.Equals(sourceCollection))
{
2025-08-31 15:35:28 +08:00
targetCollection.Insert(info.InsertIndex, info.Data as FloatingBarItem);
2025-09-07 13:30:46 +08:00
}
else
2026-01-10 17:31:55 +08:00
{ // 跨 ObservableCollection 移动
2025-08-31 15:35:28 +08:00
sourceCollection.Remove(info.Data as FloatingBarItem);
targetCollection.Insert(info.InsertIndex, info.Data as FloatingBarItem);
}
}
}
2025-09-07 13:30:46 +08:00
void IDropTarget.DragEnter(IDropInfo info)
{
2025-08-31 15:35:28 +08:00
}
2025-09-07 13:30:46 +08:00
void IDropTarget.DragLeave(IDropInfo info)
{
2025-08-31 15:35:28 +08:00
}
}
public BarItemsDropTarget barItems { get; set; } = new BarItemsDropTarget();
public BarDrawerItemsDropTarget barDrawerItems { get; set; } = new BarDrawerItemsDropTarget();
2025-09-07 13:30:46 +08:00
public FloatingBarDnDSettingsPanel()
{
2025-08-31 15:35:28 +08:00
InitializeComponent();
ToolbarItemsControl.DataContext = barItems;
ToolbarDrawerItemsControl.DataContext = barDrawerItems;
2025-09-07 13:30:46 +08:00
barItems.BarItems.Add(new FloatingBarItem()
{
2025-08-31 15:35:28 +08:00
IconSource = FindResource("EraserIcon") as DrawingImage,
});
2025-09-07 13:30:46 +08:00
barDrawerItems.BarDrawerItems.Add(new FloatingBarItem()
{
2025-08-31 15:35:28 +08:00
IconSource = FindResource("CursorIcon") as DrawingImage,
});
2025-09-07 13:30:46 +08:00
barDrawerItems.BarDrawerItems.Add(new FloatingBarItem()
{
2025-08-31 15:35:28 +08:00
IconSource = FindResource("PenIcon") as DrawingImage,
});
}
2026-03-03 16:04:20 +08:00
2026-01-10 17:31:55 +08:00
/// <summary>
/// 应用主题
/// </summary>
2026-01-02 01:35:22 +08:00
public void ApplyTheme()
{
try
{
ThemeHelper.ApplyThemeToControl(this);
}
catch (Exception ex)
{
2026-01-10 17:31:55 +08:00
System.Diagnostics.Debug.WriteLine($"FloatingBarDnDSettingsPanel 应用主题时出错: {ex.Message}");
2026-01-02 01:35:22 +08:00
}
}
2025-08-31 15:35:28 +08:00
}
}