using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Reflection.Metadata; using System.Runtime.InteropServices; using Microsoft.UI; using Microsoft.UI.Text; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Documents; using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media.Imaging; using RustTools.muqing; using Windows.ApplicationModel.Core; using Windows.ApplicationModel.DataTransfer; using Windows.Foundation; using Windows.Graphics.Imaging; using Windows.Storage.Pickers; using Windows.Storage; using Windows.UI.Core; using Windows.UI.Core.Preview; using Windows.UI.Popups; using Windows.UI.WindowManagement; using WinRT.Interop; namespace RustTools.Editor; /// /// 编辑器主窗口 /// public sealed partial class EditorWin : WindowEx { //目录列表 public ObservableCollection DataSource=new (); private Microsoft.UI.Windowing.AppWindow? app; public EditorWin() { InitializeComponent(); gj.SetBackTheme(this); ExtendsContentIntoTitleBar = true; var frame = App.AppTitlebar as FrameworkElement; if (frame != null) { gj.UpdateTitleBar(this, frame.ActualTheme); page.RequestedTheme = frame.ActualTheme; } //WindowManager.Get(this).IsMinimizable = false; //app = GetAppWindowForCurrentWindow(); //app.Closing += OnClosing; //Closed += EditorWin_Closed; var directoryInfo = new DirectoryInfo("D:\\steam\\steamapps\\common\\Rusted Warfare\\mods\\units\\赤道·联合进攻0.9补丁版0.2"); DataSource = new EditorLoad().GetData(directoryInfo.FullName); Title=directoryInfo.Name; TitleText.Text = directoryInfo.Name; Closed += EditorWin_Closed; var v = wj.dqwb("D:\\steam\\steamapps\\common\\Rusted Warfare\\mods\\units\\赤道·联合进攻0.9补丁版0.2\\侵蚀者共和国\\指挥中心\\corrode_command.ini"); //CodeEditor.Document.SetText(TextSetOptions.None,v); //EditorView.Editor.SetText(v); //CodeEditorControl.HighlightingLanguage. } //用户有没有保存 private bool IsSave = false; private ContentDialog? ClosedDialog; private async void EditorWin_Closed(object sender, WindowEventArgs e) { if (IsSave == false) { // 防止窗口关闭 e.Handled = true; } if (ClosedDialog != null) { return; } // 显示一个对话框,告知用户窗口不能关闭 ClosedDialog = new ContentDialog { XamlRoot = page.XamlRoot, Title = "警告", Content = "你还有未保存的文件在编译器中。", PrimaryButtonText = "保存关闭", PrimaryButtonStyle = Application.Current.Resources["AccentButtonStyle"] as Style, SecondaryButtonText = "直接关闭", CloseButtonText = "取消" }; var result = await ClosedDialog.ShowAsync(); if (result == ContentDialogResult.Primary) { IsSave = true; Application.Current.Exit(); return; } else if (result == ContentDialogResult.Secondary) { IsSave = true; Application.Current.Exit(); return; } ClosedDialog = null; } private Microsoft.UI.Windowing.AppWindow GetAppWindowForCurrentWindow() { var hWnd = WindowNative.GetWindowHandle(this); var myWndId = Win32Interop.GetWindowIdFromWindow(hWnd); return Microsoft.UI.Windowing.AppWindow.GetFromWindowId(myWndId); } private void TreeView_ItemInvoked(TreeView sender, TreeViewItemInvokedEventArgs args) { // 获取当前选中的项 // 获取被点击的项 var invokedItem = args.InvokedItem; // 检查是否重复选择 if (sender.SelectedItem is ExplorerItem selectedItem && invokedItem != null && selectedItem == invokedItem) { // 如果是同一个项,直接返回,不进行进一步处理 return; } if (invokedItem is not ExplorerItem explorerItem) { return; } if (explorerItem.Type == ExplorerItem.ExplorerItemType.Folder) { var directoryInfo = new DirectoryInfo(explorerItem.Dir); } } private void TreeView_DragItemsStarting(TreeView sender, TreeViewDragItemsStartingEventArgs args) { args.Cancel = true; // 设置拖动的数据 var items = args.Items.Cast(); var v = items.ToList()[0]; args.Data.SetData("TreeViewItems", v.Dir); args.Data.RequestedOperation = DataPackageOperation.Move; } //在拖动项目悬停在 TreeView 上时,为放置操作提供视觉反馈。 private void TreeView_DragOver(object sender, DragEventArgs e) { // 检查是否存在 "TreeViewItems" 数据 gj.sc("aaaaaa"); if (e.DataView.Contains("TreeViewItems")) { e.AcceptedOperation = DataPackageOperation.Move; // 允许将文件夹拖放 } else { gj.sc("No"); e.AcceptedOperation = DataPackageOperation.None; // 不接受拖放操作 } } //处理项目放置的逻辑,将文件或文件夹移动到新的位置。 private async void TreeView_Drop(object sender, DragEventArgs e) { gj.sc(e); // 获取拖动的数据 if (e.DataView.Contains("TreeViewItems")) { // 确保数据不为空 if (await e.DataView.GetDataAsync("TreeViewItems") is string Dir) { gj.sc(Dir); // 获取放置位置的 TreeViewItem var treeView = sender as TreeView; ExplorerItem v = treeView.DataContext is ExplorerItem; //var dropPosition = e.GetPosition(treeView); //data.Name gj.sc(v.Name); } } } private IList initialSelectedItems; /// /// 如果是shift则开启多选状态 /// /// /// private void treeView_KeyDown(object sender, KeyRoutedEventArgs e) { // 检查是否按下了Shift键 if (e.Key == Windows.System.VirtualKey.Shift) { gj.sc(e.Key); // 启用多选模式 treeView.SelectionMode = TreeViewSelectionMode.Multiple; } } private void treeView_KeyUp(object sender, KeyRoutedEventArgs e) { // 检查是否松开了Shift键 if (e.Key == Windows.System.VirtualKey.Shift) { // 清除当前多选模式下选中的项目 var selectedItems = treeView.SelectedItems.ToList(); foreach (var item in selectedItems) { treeView.SelectedItems.Remove(item); } gj.sc("取消"); // 取消多选模式 treeView.SelectionMode = TreeViewSelectionMode.Single; //treeView.SelectedItems.Clear(); } } private void tabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args) { sender.TabItems.Remove(args.Tab); } }