WIn_RustTools/RustTools/Editor/EditorWin.xaml.cs
2024-08-13 18:47:12 +08:00

176 lines
5.8 KiB
C#

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Metadata;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Documents;
using Microsoft.UI.Xaml.Input;
using RustTools.muqing;
using Windows.ApplicationModel.DataTransfer;
using WinRT.Interop;
namespace RustTools.Editor;
/// <summary>
/// 编辑器主窗口
/// </summary>
public sealed partial class EditorWin : WindowEx
{
private readonly ObservableCollection<TabViewItem> TabViewList = new();
//目录列表
public ObservableCollection<ExplorerItem> DataSource=new ();
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;
}
//new CodeEditorControl();
//new Editor.
//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;
}
//用户有没有保存
private bool IsSave = true;
private ContentDialog? ClosedDialog = null;
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);
}
/// <summary>
/// 选中事件
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void TreeView_ItemInvoked(TreeView sender, TreeViewItemInvokedEventArgs e)
{
// 获取当前选中的项
// 获取被点击的项
var invokedItem = e.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 TabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
{
TabViewList.Remove(args.Tab);
}
/// <summary>
/// 双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeView_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
gj.sc(e.OriginalSource);
var treeViewItem = e.OriginalSource as Grid;
if (treeViewItem == null) { return; }
if (treeViewItem.DataContext is not ExplorerItem explorerItem) { return; }
if (explorerItem.Type == ExplorerItem.ExplorerItemType.File)
{
// 查找特定的 TabViewItem
var tabViewItemToFind = TabViewList.FirstOrDefault(item => (item as TabViewItem)?.Tag.ToString() == explorerItem.Dir) as TabViewItem;
// 获取 TabViewItem 的索引位置
if (tabViewItemToFind != null)
{
tabview.SelectedIndex = TabViewList.IndexOf(tabViewItemToFind);
return;
}
var fileInfo = new FileInfo(explorerItem.Dir);
//var name = Path.GetFileNameWithoutExtension(fileInfo.FullName);
var newItem = new TabViewItem()
{
Tag = explorerItem.Dir,
// 获取文件名,不包含扩展名
Header = fileInfo.Name,
IconSource = new Microsoft.UI.Xaml.Controls.SymbolIconSource() { Symbol = Symbol.Document }
};
//if(tabview.TabItems.)
var v = wj.dqwb(explorerItem.Dir);
var textControlBox = new RichEditBox();
//textControlBox.Height = Height;
textControlBox.Document.SetText(Microsoft.UI.Text.TextSetOptions.None, v);
newItem.Content = textControlBox;
TabViewList.Insert(0, newItem);
tabview.SelectedIndex = 0;
}
}
}