在 `EditorTreeView.cs` 中: - 添加了 `Microsoft.UI.Xaml.Controls`、`RustTools.muqing` 和 `WinUIEditor` 的引用。 - 将 `EditorTreeView` 类从继承 `TreeView` 改为继承 `Microsoft.UI.Xaml.Controls.TreeView`。 - 修改了事件处理程序的绑定方式,去掉了 `this` 关键字。 - 修改了 `TreeView_ItemInvoked`、`TreeView_Expanding` 和 `TreeView_Collapsed` 方法的参数类型,使用了 `Microsoft.UI.Xaml.Controls.TreeView`。 - 在 `TreeView_ItemInvoked` 方法中,添加了处理文件点击事件的逻辑,包括创建 `CodeEditorControl` 实例并将其添加到 `TabViewItem` 中。 - 添加了 `AddTabItem` 事件。 在 `EditorWin.xaml` 中: - 添加了 `WinUIEditor` 的命名空间引用。 - 在 `Grid` 中添加了 `RowDefinition`,用于定义 `TabView` 的头部和内容区域的高度。 - 注释掉了 `CodeEditorControl` 的 XAML 定义。 在 `EditorWin.xaml.cs` 中: - 添加了 `Microsoft.UI.Xaml.Media` 和 `WinUIEditor` 的引用。 - 在 `EditorWin` 构造函数中,添加了 `gridedit.SizeChanged` 事件处理程序,用于调整 `TabView` 的高度。 - 添加了 `TreeView_AddTabItem` 方法,用于处理 `AddTabItem` 事件。 - 删除了 `MyTabView_AddTabButtonClick` 方法。 在 `RustTools.csproj` 中: - 添加了 `WinUIEdit` 包的引用。 重构 EditorTreeView 并添加 WinUI 支持 将 EditorTreeView 类更改为 Microsoft.UI.Xaml.Controls.TreeView,添加相关命名空间引用。更新方法参数类型并处理文件点击事件。调整 EditorWin.xaml 和 EditorWin.xaml.cs 文件,添加事件处理逻辑。移除 MyTabView_AddTabButtonClick 方法,并在 RustTools.csproj 中添加 WinUIEdit 包引用。
125 lines
3.5 KiB
C#
125 lines
3.5 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using Microsoft.UI;
|
|
using Microsoft.UI.Xaml;
|
|
using Microsoft.UI.Xaml.Controls;
|
|
using Microsoft.UI.Xaml.Input;
|
|
using Microsoft.UI.Xaml.Media;
|
|
using RustTools.muqing;
|
|
using WinRT.Interop;
|
|
using WinUIEditor;
|
|
namespace RustTools.Editor;
|
|
/// <summary>
|
|
/// 编辑器主窗口
|
|
/// </summary>
|
|
public sealed partial class EditorWin : WindowEx
|
|
{
|
|
public static ObservableCollection<TabViewItem> TabViewList = new();
|
|
//目录列表
|
|
public ObservableCollection<FileItem> DataSource = new();
|
|
|
|
/// <summary>
|
|
/// 编辑器主窗口
|
|
/// 传入路径
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
public EditorWin(string path)
|
|
{
|
|
InitializeComponent();
|
|
gj.SetBackTheme(this);
|
|
ExtendsContentIntoTitleBar = true;
|
|
var frame = App.AppTitlebar as FrameworkElement;
|
|
if (frame != null)
|
|
{
|
|
gj.UpdateTitleBar(this, frame.ActualTheme);
|
|
page.RequestedTheme = frame.ActualTheme;
|
|
}
|
|
//Debug.WriteLine(path);
|
|
var directoryInfo = new DirectoryInfo(path);
|
|
Title = directoryInfo.Name;
|
|
TitleText.Text = directoryInfo.Name;
|
|
DataSource = EditorLoad.GetData(directoryInfo.FullName);
|
|
treeView.ItemsSource = DataSource;
|
|
Closed += EditorWin_Closed;
|
|
//设置TabView的高度
|
|
gridedit.SizeChanged += (sender, args) =>
|
|
{
|
|
tabview.Height = gridedit.ActualHeight - 10;
|
|
};
|
|
|
|
tabview.TabItemsSource = TabViewList;
|
|
treeView.AddTabItem += TreeView_AddTabItem;
|
|
}
|
|
|
|
private void TreeView_AddTabItem(TabViewItem obj,CodeEditorControl codeEditorControl){
|
|
tabview.SelectedItem = obj;
|
|
var title = obj.Header.ToString();
|
|
codeEditorControl.Editor.UpdateUI += (sender, args) =>
|
|
{
|
|
//是否有可以撤销的历史记录
|
|
var a = codeEditorControl.Editor.CanUndo();
|
|
|
|
if (a)
|
|
{
|
|
obj.Header = $"{title} *";
|
|
}
|
|
else
|
|
{
|
|
obj.Header = title;
|
|
}
|
|
Debug.WriteLine($"UpdateUI :" + a);
|
|
};
|
|
}
|
|
|
|
|
|
//用户有没有保存
|
|
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 void MyTabView_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
|
|
{
|
|
if (args.Tab is TabViewItem tab)
|
|
{
|
|
TabViewList.Remove(tab);
|
|
}
|
|
}
|
|
}
|