From 80e402e386f1264384714dbde9ae55bbec737023 Mon Sep 17 00:00:00 2001 From: muqing <1966944300@qq.com> Date: Wed, 22 Jan 2025 16:24:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A0=87=E7=AD=BE=E9=A1=B5?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=92=8C=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `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` 为空的检查。 --- Editor/EditorTreeView.cs | 10 +++++++++- Editor/EditorWin.xaml.cs | 31 +++++++++++++++++++++++++++++-- muqing/wj.cs | 6 +++++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Editor/EditorTreeView.cs b/Editor/EditorTreeView.cs index f673778..2ccdc7c 100644 --- a/Editor/EditorTreeView.cs +++ b/Editor/EditorTreeView.cs @@ -30,6 +30,13 @@ public class EditorTreeView : Microsoft.UI.Xaml.Controls.TreeView } else { + //检查是否存在此标签了 + var a = EditorWin.TabViewList.FirstOrDefault(tabItem => tabItem.Tag != null && tabItem.Tag.ToString().Equals(invokedItem.Dir)); + if (a!=null) + { + SelectedItem?.Invoke(a); + return; + } Debug.WriteLine($"File clicked: {invokedItem.Name}"); var wb = wj.dqwb(invokedItem.Dir); var codeEditorControl = new CodeEditorControl(); @@ -40,7 +47,7 @@ public class EditorTreeView : Microsoft.UI.Xaml.Controls.TreeView { Header = invokedItem.Name, IconSource = new SymbolIconSource() { Symbol = Symbol.Document }, - Content = codeEditorControl + Content = codeEditorControl,Tag=invokedItem.Dir }); //codeEditorControl.Editor.CanUndo codeEditorControl.Editor.EmptyUndoBuffer(); @@ -57,6 +64,7 @@ public class EditorTreeView : Microsoft.UI.Xaml.Controls.TreeView } public event Action AddTabItem; + public event Action SelectedItem; diff --git a/Editor/EditorWin.xaml.cs b/Editor/EditorWin.xaml.cs index 9ef4f1f..637e032 100644 --- a/Editor/EditorWin.xaml.cs +++ b/Editor/EditorWin.xaml.cs @@ -1,11 +1,14 @@ 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; @@ -14,9 +17,9 @@ namespace RustTools.Editor; /// public sealed partial class EditorWin : WindowEx { - public static ObservableCollection TabViewList = new(); + public static ObservableCollection TabViewList = []; //Ŀ¼б - public ObservableCollection DataSource = new(); + public ObservableCollection DataSource = []; /// /// ༭ @@ -49,6 +52,10 @@ public sealed partial class EditorWin : WindowEx tabview.TabItemsSource = TabViewList; treeView.AddTabItem += TreeView_AddTabItem; + treeView.SelectedItem += (a) => + { + tabview.SelectedItem = a; + }; } private void TreeView_AddTabItem(TabViewItem obj,CodeEditorControl codeEditorControl){ @@ -69,6 +76,26 @@ public sealed partial class EditorWin : WindowEx } 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 ϼӦIJ + //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; // ¼Ѵ + } + }; } diff --git a/muqing/wj.cs b/muqing/wj.cs index af515af..0ebaae4 100644 --- a/muqing/wj.cs +++ b/muqing/wj.cs @@ -31,8 +31,12 @@ public class wj public const string UUID = "win.uuid"; } - public static bool xrwb(string name, string str) + public static bool xrwb(string? name, string str) { + if (string.IsNullOrEmpty(name)) + { + return false; + } // 获取当前执行文件的目录 //var currentDirectory = Directory.GetCurrentDirectory(); var filePath = Path.Combine(CD, name);