WIn_RustTools/RustTools/Editor/EditorTreeView.cs
2024-08-13 19:30:40 +08:00

143 lines
4.5 KiB
C#

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml;
using RustTools.muqing;
using Windows.ApplicationModel.DataTransfer;
using Microsoft.UI.Xaml.Input;
using Windows.Storage;
using RustTools.Helpers;
using Microsoft.UI.Xaml.Media;
using Windows.Foundation;
using System.Diagnostics;
namespace RustTools.Editor;
public class EditorTreeView: TreeView
{
public EditorTreeView()
{
//CanDragItems = false;
//CanReorderItems =false;
//DragEnter += EditorTreeView_DragEnter;
DragItemsStarting += TreeView_DragItemsStarting;
DragItemsCompleted += EditorTreeView_DragItemsCompleted;
//DragOver += TreeView_DragOver;
//Drop += TreeView_Drop;
// 在构造函数中注册事件处理程序
}
private void TreeView_DragItemsStarting(TreeView sender, TreeViewDragItemsStartingEventArgs e)
{
//gj.sc("TreeView_DragItemsStarting"+e);
var items = e.Items.Cast<ExplorerItem>().ToList();
var firstItem = items.First();
gj.sc(firstItem.Dir);
e.Data.SetData("path", firstItem.Dir);
e.Data.RequestedOperation = DataPackageOperation.Move; // Use Copy to avoid moving items
}
private void EditorTreeView_DragItemsCompleted(TreeView sender, TreeViewDragItemsCompletedEventArgs e) {
// 获取拖拽的项
var items = e.Items.Cast<ExplorerItem>().ToList();
// 检查是否有至少一个项被放下
if (items.Any())
{
var firstItem = items.First(); // 获取第一个放下的项
// 输出放下项的信息
Debug.WriteLine($"放下了 {firstItem.Dir}");
}
}
/// <summary>
/// 如果是shift则开启多选状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView_KeyDown(object sender, KeyRoutedEventArgs e)
{
// 检查是否按下了Shift键
if (e.Key == Windows.System.VirtualKey.Shift)
{
gj.sc(e.Key);
// 启用多选模式
SelectionMode = TreeViewSelectionMode.Multiple;
}
}
private void treeView_KeyUp(object sender, KeyRoutedEventArgs e)
{
// 检查是否松开了Shift键
if (e.Key == Windows.System.VirtualKey.Shift)
{ // 清除当前多选模式下选中的项目
var selectedItems = SelectedItems.ToList();
foreach (var item in selectedItems)
{
SelectedItems.Remove(item);
}
gj.sc("取消");
// 取消多选模式
SelectionMode = TreeViewSelectionMode.Single;
//treeView.SelectedItems.Clear();
}
}
/// <summary>
/// 其他窗口的文件夹拖过来的例子
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TreeView_DragOver(object sender, DragEventArgs e)
{
gj.sc(e);
// Allow dragging but prevent reordering
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
e.AcceptedOperation = DataPackageOperation.Move; // Use Copy to avoid reordering
e.Handled = true;
}
}
/// <summary>
/// 其他文件夹拖过来的放下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void TreeView_Drop(object sender, DragEventArgs e)
{
gj.sc("TreeView_Drop: " + e);
// Handle drop event if needed but prevent reordering
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
var storageItems = await e.DataView.GetStorageItemsAsync();
// 获取目标文件夹,即拖放的位置
// 获取目标位置
var treeView = sender as TreeView;
var data = await e.DataView.GetDataAsync("path");
gj.sc(data);
foreach (var item in storageItems)
{
if (item is StorageFolder folder)
{
// 移动文件夹
gj.sc(folder.Path+"-->");
//var newFolder = await folder.MoveAsync(targetFolder, folder.Name, NameCollisionOption.ReplaceExisting);
}
else if (item is StorageFile file)
{
// 移动文件
//var newFile = await file.MoveAsync(targetFolder, file.Name, NameCollisionOption.ReplaceExisting);
}
}
e.Handled = true;
}
}
}