diff --git a/FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml b/FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml index ab96346..5f2e123 100644 --- a/FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml +++ b/FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml @@ -36,31 +36,34 @@ - - - - - - - - - - + + + + + + + + + + + - 忽略大小写 - 包括路径 - 包括扩展名 - 匹配所有 - 保留文件日期 - 显示预览 - 自动预览 - + 包括路径 + 包括扩展名 + 匹配所有 + 忽略大小写 + 保留日期 + 显示预览 + 自动预览 + 应用后关闭 + diff --git a/FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml.cs b/FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml.cs index 49b0865..ecdd0e0 100644 --- a/FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml.cs +++ b/FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml.cs @@ -39,14 +39,7 @@ public MainWindow(IEnumerable entities) { Directory.Exists(e) ? EntityType.Directory : throw new FileNotFoundException(null, e) ) .ToList(); - var tmp = Entities.IndexJoin(EntitiesTypes).ToList(); - tmp.Sort( - (a, b) => b.Second == a.Second - ? string.Compare(a.First, b.First, StringComparison.Ordinal) - : (int)b.Second - (int)a.Second - ); - Entities = tmp.Select(x => x.First).ToList(); - EntitiesTypes = tmp.Select(x => x.Second).ToList(); + SortEntities(); FileNames = Entities.Select( (e, i) => EntitiesTypes[i] == EntityType.File ? Path.GetFileNameWithoutExtension(e) : Path.GetFileName(e) @@ -60,9 +53,9 @@ public MainWindow(IEnumerable entities) { public MainWindow(string directory) : this(Directory.GetFileSystemEntries(directory)) { } - public List Entities { get; } + public List Entities { get; private set; } - public List EntitiesTypes { get; } + public List EntitiesTypes { get; private set; } public Regex? Pattern { get => _pattern; @@ -82,6 +75,17 @@ public Regex? Pattern { private event EventHandler PatternChanged = delegate { }; + private void SortEntities() { + var tmp = Entities.IndexJoin(EntitiesTypes).ToList(); + tmp.Sort( + (a, b) => b.Second == a.Second + ? string.Compare(a.First, b.First, StringComparison.Ordinal) + : (int)b.Second - (int)a.Second + ); + Entities = tmp.Select(x => x.First).ToList(); + EntitiesTypes = tmp.Select(x => x.Second).ToList(); + } + private string? GetNewEntity(int index) { if (MatchCollections![index].Count == 0) return null; @@ -100,6 +104,26 @@ public Regex? Pattern { }; } + private void UpdateFileNames() { + FileNames = (IncludePath.IsChecked, IncludeExtension.IsChecked) switch { + (true, true) => Entities, + (true, false) => Entities.Select( + (e, i) => Path.Combine( + Path.GetDirectoryName(e)!, + EntitiesTypes[i] == EntityType.File ? Path.GetFileNameWithoutExtension(e) : Path.GetFileName(e) + ) + ) + .ToList(), + (false, true) => Entities.Select(Path.GetFileName).ToList(), + (false, false) => Entities.Select( + (e, i) => + EntitiesTypes[i] == EntityType.File ? Path.GetFileNameWithoutExtension(e) : Path.GetFileName(e) + ) + .ToList(), + _ => throw new ArgumentOutOfRangeException() + }; + } + private void Display(Paragraph target, bool highlightMatch = true) { target.Inlines.Clear(); if (!highlightMatch) { @@ -168,11 +192,13 @@ private void Display(Paragraph target, bool highlightMatch = true) { } } - private void Apply() { + private string?[] Apply() { + var result = new string?[Entities.Count]; for (var i = 0; i < Entities.Count; ++i) { string? entity = Entities[i]; var type = EntitiesTypes[i]; string? newEntity = GetNewEntity(i); + result[i] = newEntity; if (newEntity is null || !PathExtensions.IsFullPathValid(newEntity)) continue; Directory.CreateDirectory(Path.GetDirectoryName(newEntity)!); @@ -199,6 +225,7 @@ private void Apply() { } } } + return result; } private void RefreshPreview() { @@ -244,23 +271,7 @@ private void WindowLoaded(object sender, EventArgs e) { private void IgnoreCaseChanged(object sender, RoutedEventArgs args) => UpdatePattern(); private void TargetChanged(object sender, RoutedEventArgs args) { - FileNames = (IncludePath.IsChecked, IncludeExtension.IsChecked) switch { - (true, true) => Entities, - (true, false) => Entities.Select( - (e, i) => Path.Combine( - Path.GetDirectoryName(e)!, - EntitiesTypes[i] == EntityType.File ? Path.GetFileNameWithoutExtension(e) : Path.GetFileName(e) - ) - ) - .ToList(), - (false, true) => Entities.Select(Path.GetFileName).ToList(), - (false, false) => Entities.Select( - (e, i) => - EntitiesTypes[i] == EntityType.File ? Path.GetFileNameWithoutExtension(e) : Path.GetFileName(e) - ) - .ToList(), - _ => throw new ArgumentOutOfRangeException() - }; + UpdateFileNames(); UpdatePattern(); if (Equals(sender, IncludePath)) { EntitiesBox.HorizontalScrollBarVisibility = ResultsBox.HorizontalScrollBarVisibility = @@ -332,8 +343,21 @@ private void SyncScroll(object sender, ScrollChangedEventArgs args) { private void PreviewButtonClick(object sender, RoutedEventArgs e) => RefreshPreview(); private void ApplyButtonClick(object sender, RoutedEventArgs e) { - Apply(); - Close(); + string?[] map = Apply(); + if (CloseAfterApplying.IsChecked == true) + Close(); + else { + for (var i = 0; i < map.Length; ++i) { + string? value = map[i]; + if (value is not null) + Entities[i] = value; + } + SortEntities(); + UpdateFileNames(); + MatchCollections?.Clear(); + ResultsParagraph.Inlines.Clear(); + Display(EntitiesParagraph, false); + } } }