e0fe30b380
- Bugfix: The Name-filter was based on Name and FullName, instead of how it is now, OldName and OldFullName. This would previously cause the filter to apply to the already previewed changes.
121 lines
4.7 KiB
C#
121 lines
4.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
|
|
using Avalonia.Input;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Markup.Xaml;
|
|
using Avalonia.Interactivity;
|
|
|
|
using BFR.Helpers;
|
|
using BFR.DataModels;
|
|
|
|
namespace BFR
|
|
{
|
|
public partial class MainWindow : Window
|
|
{
|
|
public async Task OpenDirectoryButtonClick() => OpenDirectory(await new OpenFolderDialog() { Directory = WorkingDirectory }.ShowAsync(this));
|
|
public void OpenDirectory(string directory)
|
|
{
|
|
WorkingDirectory = directory;
|
|
AllFiles.ReplaceAll(Directory.GetFiles(WorkingDirectory).Select(x => new FileModel(x)));
|
|
Filter();
|
|
}
|
|
|
|
public void FilterChanged(object sender, KeyEventArgs e) => Filter();
|
|
public void FilterChanged(object sender, RoutedEventArgs e) => Filter();
|
|
public void Filter()
|
|
{
|
|
// Filter all files in the directory for those satisfying the given filters
|
|
Files.ReplaceAll(AllFiles.Where(x =>
|
|
(FilterExtension == "" || x.Extension == FilterExtension)
|
|
&& (FilterFullName ? x.OldFullName : x.OldName).RegexContains(FilterRegex ? FilterPattern : Regex.Escape(FilterPattern))));
|
|
|
|
Preview();
|
|
}
|
|
|
|
public void PreviewChanged(object sender, KeyEventArgs e) => Preview(); // TextBox.KeyUp
|
|
public void PreviewChanged(object sender, RoutedEventArgs e) => Preview(); // CheckBox.Click
|
|
public void PreviewChanged(object sender, SelectionChangedEventArgs e) => Preview(); // ComboBox.SelectionChanged
|
|
public void PreviewChanged(object sender, NumericUpDownValueChangedEventArgs e) => Preview(); // NumericUpDown.ValueChanged
|
|
public void Preview()
|
|
{
|
|
// Reset all files to how they currently exist
|
|
foreach (var file in Files)
|
|
file.Path = file.OldPath;
|
|
|
|
// Apply operations
|
|
foreach (var operation in Operations)
|
|
operation.ApplyTo(Files);
|
|
|
|
// Validate that there are any changes, and that the new file names are all unique.
|
|
IsCommitButtonEnabled =
|
|
Files.Any(x => x.Path != x.OldPath) // Check for changes
|
|
&& Files.GroupBy(x => x.Path).All(g => g.Count() == 1) // Check for duplicates
|
|
&& !Files.Any(x => x.FullName == ""); // Check for invalid file names; TODO: Further validation
|
|
|
|
// Refresh the file list to guarantee that changes are displayed. TODO: Find a better way to do this.
|
|
Files.ReplaceAll(new List<FileModel>(Files));
|
|
}
|
|
|
|
public void AddOperation()
|
|
{
|
|
if (Operations.Count >= 1)
|
|
Operations.Insert(
|
|
SelectedOperation >= 0 ? SelectedOperation + 1 : Operations.Count,
|
|
OperationTypes[SelectedOperationType].Create());
|
|
else Operations.Add(OperationTypes[SelectedOperationType].Create());
|
|
Preview();
|
|
}
|
|
|
|
public void RemoveOperation()
|
|
{
|
|
if (SelectedOperation >= 0)
|
|
Operations.RemoveAt(SelectedOperation);
|
|
else Operations.RemoveAt(0);
|
|
Preview();
|
|
}
|
|
|
|
public void MoveOperation(object sender, SpinEventArgs e)
|
|
{
|
|
if (Operations.Count > 1)
|
|
if (e.Direction == SpinDirection.Increase && SelectedOperation > 0)
|
|
Operations.Move(SelectedOperation, SelectedOperation - 1);
|
|
else if (e.Direction == SpinDirection.Decrease && SelectedOperation < Operations.Count - 1)
|
|
Operations.Move(SelectedOperation, SelectedOperation + 1);
|
|
Preview();
|
|
}
|
|
|
|
public void Commit()
|
|
{
|
|
foreach (var file in Files)
|
|
if (file.OldPath != file.Path)
|
|
File.Move(file.OldPath, file.Path);
|
|
|
|
UndoStack.Push(new List<FileModel>(Files));
|
|
UndoCount = UndoStack.Count;
|
|
|
|
OpenDirectory(WorkingDirectory);
|
|
}
|
|
|
|
public void Undo()
|
|
{
|
|
foreach (var file in UndoStack.Pop())
|
|
File.Move(file.Path, file.OldPath);
|
|
|
|
UndoCount = UndoStack.Count;
|
|
OpenDirectory(WorkingDirectory);
|
|
}
|
|
|
|
public MainWindow() => InitializeComponent();
|
|
private void InitializeComponent()
|
|
{
|
|
AvaloniaXamlLoader.Load(this);
|
|
DataContext = this;
|
|
OpenDirectory(Environment.OSVersion.Platform == PlatformID.Win32NT ? @"C:\Users" : @"\home");
|
|
}
|
|
}
|
|
} |