This commit is contained in:
辉鸭蛋 2024-12-29 19:00:26 +08:00
parent 60f1a271c4
commit a66b802bbd
3 changed files with 13 additions and 9 deletions

View File

@ -12,4 +12,6 @@ public partial class KeyMouseScriptItem : ObservableObject
private string _createTimeStr = string.Empty; private string _createTimeStr = string.Empty;
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
public string Path { get; set; } = string.Empty;
} }

View File

@ -106,7 +106,7 @@
<DataTemplate> <DataTemplate>
<StackPanel Orientation="Horizontal"> <StackPanel Orientation="Horizontal">
<ui:Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:KeyMouseRecordPage}}, Path=DataContext.StartPlayCommand}" <ui:Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:KeyMouseRecordPage}}, Path=DataContext.StartPlayCommand}"
CommandParameter="{Binding Name}" CommandParameter="{Binding Path}"
Content="播放脚本" Content="播放脚本"
Icon="{ui:SymbolIcon Play24}" /> Icon="{ui:SymbolIcon Play24}" />
</StackPanel> </StackPanel>

View File

@ -45,15 +45,16 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation
private void InitScriptListViewData() private void InitScriptListViewData()
{ {
_scriptItems.Clear(); ScriptItems.Clear();
var fileInfos = LoadScriptFiles(scriptPath) var fileInfos = LoadScriptFiles(scriptPath)
.OrderByDescending(f => f.CreationTime) .OrderByDescending(f => f.CreationTime)
.ToList(); .ToList();
foreach (var f in fileInfos) foreach (var f in fileInfos)
{ {
_scriptItems.Add(new KeyMouseScriptItem ScriptItems.Add(new KeyMouseScriptItem
{ {
Name = f.Name, Name = f.Name,
Path = f.FullName,
CreateTime = f.CreationTime, CreateTime = f.CreationTime,
CreateTimeStr = f.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") CreateTimeStr = f.CreationTime.ToString("yyyy-MM-dd HH:mm:ss")
}); });
@ -67,7 +68,7 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation
Directory.CreateDirectory(folder); Directory.CreateDirectory(folder);
} }
var files = Directory.GetFiles(folder, "*.*", var files = Directory.GetFiles(folder, "*.json",
SearchOption.AllDirectories); SearchOption.AllDirectories);
return files.Select(file => new FileInfo(file)).ToList(); return files.Select(file => new FileInfo(file)).ToList();
@ -120,12 +121,13 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation
} }
[RelayCommand] [RelayCommand]
public async Task OnStartPlay(string name) public async Task OnStartPlay(string path)
{ {
string name = Path.GetFileName(path);
_logger.LogInformation("重放开始:{Name}", name); _logger.LogInformation("重放开始:{Name}", name);
try try
{ {
var s = await File.ReadAllTextAsync(Path.Combine(scriptPath, name)); var s = await File.ReadAllTextAsync(path);
await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage) await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage)
.RunAsync(async () => await KeyMouseMacroPlayer.PlayMacro(s, CancellationContext.Instance.Cts.Token)); .RunAsync(async () => await KeyMouseMacroPlayer.PlayMacro(s, CancellationContext.Instance.Cts.Token));
@ -159,11 +161,11 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation
if (!string.IsNullOrEmpty(str)) if (!string.IsNullOrEmpty(str))
{ {
// 检查原始文件是否存在 // 检查原始文件是否存在
var originalFilePath = Path.Combine(scriptPath, item.Name); var originalFilePath = item.Path;
if (File.Exists(originalFilePath)) if (File.Exists(originalFilePath))
{ {
// 重命名文件 // 重命名文件
File.Move(originalFilePath, Path.Combine(scriptPath, str + ".json")); File.Move(originalFilePath, Path.Combine(Path.GetDirectoryName(originalFilePath)!, str + ".json"));
_snackbarService.Show( _snackbarService.Show(
"修改名称成功", "修改名称成功",
$"脚本名称 {item.Name} 修改为 {str}", $"脚本名称 {item.Name} 修改为 {str}",
@ -199,7 +201,7 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation
} }
try try
{ {
File.Delete(Path.Combine(scriptPath, item.Name)); File.Delete(item.Path);
_snackbarService.Show( _snackbarService.Show(
"删除成功", "删除成功",
$"{item.Name} 已经被删除", $"{item.Name} 已经被删除",