WIn_RustTools/Editor/EditorWin.xaml.cs
muqing 7deaa65c39 更新引用和事件处理,移除 ButtonViewModel 类
在 `EditorWin.xaml.cs` 中:
- 添加了 `System.Text.Encodings.Web` 和 `System.Text.Json` 的引用。
- 在 `TreeView_AddTabItem` 方法中,添加了对 `title` 为空的检查。
- 将 `UpdateUI` 事件替换为 `SavePointLeft` 事件。
- 将 `KeyDown` 事件替换为 `PreviewKeyDown` 事件。
- 添加了对 `Ctrl + Alt + L` 快捷键的处理,用于格式化 JSON 文本。

在 `RustTools.csproj` 中:
- 更新了多个包的版本,包括 `CommunityToolkit.Mvvm`、`Microsoft.Extensions.Hosting`、`Microsoft.WindowsAppSDK`、`System.Management` 和 `WinUIEx`。
- 移除了 `Microsoft.Windows.CsWinRT` 包的引用。
- 添加了 `CommunityToolkit.WinUI.Controls.Sizers` 包的引用。

在 `ButtonViewModel.cs` 中:
- 移除了 `ButtonViewModel` 类及其内容。

在 `RankingViewModel.cs` 中:
- 在 `RankingList` 为空时,添加了对 `modData` 和 `modData.Data` 为空的检查。
- 更新了 `item.Icon` 的替换逻辑。

在 `UserPage.xaml.cs` 中:
- 在用户登录逻辑中,添加了对 `userSpaceInfo` 为空的检查。
2025-01-24 20:01:29 +08:00

176 lines
5.8 KiB
C#

using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text.Encodings.Web;
using System.Text.Json;
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.Helpers;
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();
if (title == null)
{
return;
}
//修改了编辑器的内容
codeEditorControl.Editor.SavePointLeft += (sender, args) =>
{
//是否有可以撤销的历史记录
obj.Header = $"{title} *";
};
codeEditorControl.PreviewKeyDown+= (sender, e) =>
{
// 检查是否按下了 Ctrl 键
var isCtrlPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control) == CoreVirtualKeyStates.Down;
var isALtPressed = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control) == CoreVirtualKeyStates.Down;
// 检查是否按下了 Alt 键
// 检查是否按下了 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; // 标记事件已处理
}
else if (e.Key == VirtualKey.L && isCtrlPressed && isALtPressed)
{
try
{
var text = codeEditorControl.Editor.GetText(codeEditorControl.Editor.Length);
if (title.EndsWith(".json"))
{
// 格式化 JSON
var jsonElement = JsonSerializer.Deserialize<JsonElement>(text);
var formattedJson = JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping // 保留 UTF-8 字符
});
codeEditorControl.Editor.SetText(formattedJson);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
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);
}
}
}