WIn_RustTools/Editor/EditorWin.xaml.cs
muqing 80e402e386 优化标签页管理和事件处理逻辑
在 `EditorTreeView.cs` 文件中:
- 增加了检查是否存在相同标签页的逻辑,并在存在时选中该标签页。
- 增加了 `SelectedItem` 事件,用于在标签页已经存在时触发。
- 在添加新的 `TabViewItem` 时,增加了 `Tag` 属性以存储文件路径。

在 `EditorWin.xaml.cs` 文件中:
- 修改了 `TabViewList` 和 `DataSource` 的初始化方式,从 `new()` 改为 `[]`。
- 在 `EditorWin` 的构造函数中,增加了对 `treeView.SelectedItem` 事件的处理逻辑。
- 在 `TreeView_AddTabItem` 方法中,增加了对 `codeEditorControl.KeyDown` 事件的处理逻辑,当按下 `Ctrl + S` 时,保存当前文件内容并清空撤销缓冲区。

在 `wj.cs` 文件中:
- 修改了 `xrwb` 方法的签名,将 `name` 参数改为可空类型,并增加了对 `name` 为空的检查逻辑,如果 `name` 为空则返回 `false`。

修复标签页选择逻辑并添加快捷键支持

在 `EditorTreeView.cs` 文件中:
- 在 `else` 分支中添加了检查是否已存在标签页的逻辑,如果存在则选中该标签页并返回。
- 在 `AddTabItem` 事件后添加了 `SelectedItem` 事件。

在 `EditorWin.xaml.cs` 文件中:
- 添加了 `Microsoft.UI.Input` 和 `Windows.System` 的引用。
- 将 `TabViewList` 和 `DataSource` 的初始化从 `new()` 改为 `[]`。
- 在 `treeView.SelectedItem` 事件中添加了选中标签页的逻辑。
- 在 `TreeView_AddTabItem` 方法中添加了对 `codeEditorControl.KeyDown` 事件的处理,支持 `Ctrl + S` 快捷键保存文件。

在 `wj.cs` 文件中:
- 修改了 `xrwb` 方法的签名,参数 `name` 允许为空,并在方法内部添加了对 `name` 为空的检查。
2025-01-22 16:24:37 +08:00

152 lines
4.7 KiB
C#

using System.Collections.ObjectModel;
using System.Diagnostics;
using Microsoft.UI;
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using RustTools.muqing;
using Windows.System;
using Windows.UI.Core;
using WinRT.Interop;
using WinUIEditor;
namespace RustTools.Editor;
/// <summary>
/// 编辑器主窗口
/// </summary>
public sealed partial class EditorWin : WindowEx
{
public static ObservableCollection<TabViewItem> TabViewList = [];
//目录列表
public ObservableCollection<FileItem> DataSource = [];
/// <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;
treeView.SelectedItem += (a) =>
{
tabview.SelectedItem = a;
};
}
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);
};
codeEditorControl.KeyDown+= (sender, e) =>
{
// 检查是否按下了 Ctrl 键
var isCtrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control) == CoreVirtualKeyStates.Down;
// 检查是否按下了 J 键
if (e.Key == VirtualKey.S && isCtrlPressed)
{
// 执行 Ctrl + S 组合键对应的操作
//Debug.WriteLine("Ctrl + S pressed");
if (obj==null||string.IsNullOrEmpty(obj.Tag.ToString()))
{
return;
}
//Debug.WriteLine($"保存文件 {codeEditorControl.Editor.GetText(codeEditorControl.Editor.Length)}");
wj.xrwb(obj.Tag.ToString(), codeEditorControl.Editor.GetText(codeEditorControl.Editor.Length));
codeEditorControl.Editor.EmptyUndoBuffer();
obj.Header = title;
e.Handled = true; // 标记事件已处理
}
};
}
//用户有没有保存
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);
}
}
}