WIn_RustTools/RustTools/Editor/EditorTreeView.cs

143 lines
4.5 KiB
C#
Raw Normal View History

2024-08-13 10:47:12 +00:00

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;
2024-08-13 11:30:40 +00:00
using System.Diagnostics;
2024-08-13 10:47:12 +00:00
namespace RustTools.Editor;
public class EditorTreeView: TreeView
{
public EditorTreeView()
{
//CanDragItems = false;
//CanReorderItems =false;
2024-08-13 11:30:40 +00:00
//DragEnter += EditorTreeView_DragEnter;
2024-08-13 10:47:12 +00:00
DragItemsStarting += TreeView_DragItemsStarting;
DragItemsCompleted += EditorTreeView_DragItemsCompleted;
2024-08-13 11:30:40 +00:00
//DragOver += TreeView_DragOver;
//Drop += TreeView_Drop;
2024-08-13 10:47:12 +00:00
// 在构造函数中注册事件处理程序
}
private void TreeView_DragItemsStarting(TreeView sender, TreeViewDragItemsStartingEventArgs e)
{
//gj.sc("TreeView_DragItemsStarting"+e);
2024-08-13 11:30:40 +00:00
var items = e.Items.Cast<ExplorerItem>().ToList();
var firstItem = items.First();
2024-08-13 10:47:12 +00:00
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) {
2024-08-13 11:30:40 +00:00
// 获取拖拽的项
var items = e.Items.Cast<ExplorerItem>().ToList();
// 检查是否有至少一个项被放下
if (items.Any())
{
var firstItem = items.First(); // 获取第一个放下的项
// 输出放下项的信息
Debug.WriteLine($"放下了 {firstItem.Dir}");
}
2024-08-13 10:47:12 +00:00
}
/// <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;
}
}
}