Skip to content

Commit

Permalink
feat(BatchRenamer): close window after applying
Browse files Browse the repository at this point in the history
Also reorder options
  • Loading branch information
truemogician committed Jun 6, 2024
1 parent 63f100c commit cdfd650
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 48 deletions.
39 changes: 21 additions & 18 deletions FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,34 @@
<TextBox Name="ReplaceBox" Grid.Row="1" Grid.Column="1" Margin="0,10,0,0" VerticalContentAlignment="Center" TextChanged="ReplaceBoxTextChanged" KeyUp="ReplaceBoxKeyUp" TabIndex="1" />
<Button Name="ApplyButton" Grid.Row="1" Grid.Column="2" Margin="0,10,0,0" Padding="5,1,5,1" IsEnabled="False" Click="ApplyButtonClick">应用</Button>
</Grid>
<Grid Grid.Row="1" Grid.Column="0" Margin="0,15,0,15" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="7*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="7*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="6*" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="1" Grid.Column="0" Margin="0,10" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="CheckBox">
<Setter Property="FontSize" Value="12" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Margin" Value="0,5" />
</Style>
</Grid.Resources>
<CheckBox Name="IgnoreCase" Grid.Column="0" Checked="IgnoreCaseChanged" Unchecked="IgnoreCaseChanged">忽略大小写</CheckBox>
<CheckBox Name="IncludePath" Grid.Column="1" Checked="TargetChanged" Unchecked="TargetChanged">包括路径</CheckBox>
<CheckBox Name="IncludeExtension" Grid.Column="2" Checked="TargetChanged" Unchecked="TargetChanged">包括扩展名</CheckBox>
<CheckBox Name="MatchAll" Grid.Column="3" Checked="MatchAllChanged" Unchecked="MatchAllChanged">匹配所有</CheckBox>
<CheckBox Name="PreserveEntityDate" Grid.Column="4" IsChecked="True">保留文件日期</CheckBox>
<CheckBox Name="ShowPreview" Grid.Column="5" IsChecked="True" Checked="ShowPreviewChecked" Unchecked="ShowPreviewUnchecked">显示预览</CheckBox>
<CheckBox Name="AutoPreview" Grid.Column="6" IsChecked="True" Checked="AutoPreviewChecked" Unchecked="AutoPreviewUnchecked">自动预览</CheckBox>
</Grid>
<CheckBox Name="IncludePath" Grid.Row="0" Grid.Column="0" Checked="TargetChanged" Unchecked="TargetChanged">包括路径</CheckBox>
<CheckBox Name="IncludeExtension" Grid.Row="0" Grid.Column="1" Checked="TargetChanged" Unchecked="TargetChanged">包括扩展名</CheckBox>
<CheckBox Name="MatchAll" Grid.Row="0" Grid.Column="2" Checked="MatchAllChanged" Unchecked="MatchAllChanged">匹配所有</CheckBox>
<CheckBox Name="IgnoreCase" Grid.Row="0" Grid.Column="3" Checked="IgnoreCaseChanged" Unchecked="IgnoreCaseChanged">忽略大小写</CheckBox>
<CheckBox Name="PreserveEntityDate" Grid.Row="1" Grid.Column="0" IsChecked="True">保留日期</CheckBox>
<CheckBox Name="ShowPreview" Grid.Row="1" Grid.Column="1" IsChecked="True" Checked="ShowPreviewChecked" Unchecked="ShowPreviewUnchecked">显示预览</CheckBox>
<CheckBox Name="AutoPreview" Grid.Row="1" Grid.Column="2" IsChecked="True" Checked="AutoPreviewChecked" Unchecked="AutoPreviewUnchecked">自动预览</CheckBox>
<CheckBox Name="CloseAfterApplying" Grid.Row="1" Grid.Column="3" IsChecked="True">应用后关闭</CheckBox>
</Grid>
<Grid Name="PreviewContainer" Grid.Row="2" Grid.Column="0" Margin="0,10,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
Expand Down
84 changes: 54 additions & 30 deletions FilePropertyManager/BatchRenamer/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,7 @@ public MainWindow(IEnumerable<string> 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)
Expand All @@ -60,9 +53,9 @@ public MainWindow(IEnumerable<string> entities) {

public MainWindow(string directory) : this(Directory.GetFileSystemEntries(directory)) { }

public List<string> Entities { get; }
public List<string> Entities { get; private set; }

public List<EntityType> EntitiesTypes { get; }
public List<EntityType> EntitiesTypes { get; private set; }

public Regex? Pattern {
get => _pattern;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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)!);
Expand All @@ -199,6 +225,7 @@ private void Apply() {
}
}
}
return result;
}

private void RefreshPreview() {
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit cdfd650

Please sign in to comment.