2019-11-14 18:08:21 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2019-11-14 19:42:25 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text.RegularExpressions;
|
2019-11-14 18:08:21 +00:00
|
|
|
|
2019-11-17 15:22:19 +00:00
|
|
|
using Avalonia;
|
2019-11-13 19:26:04 +00:00
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
|
2019-11-14 18:08:21 +00:00
|
|
|
using BFR.Helpers;
|
2019-11-14 19:42:25 +00:00
|
|
|
using BFR.DataModels;
|
2019-12-01 13:08:01 +00:00
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using Avalonia.Input;
|
|
|
|
using Avalonia.LogicalTree;
|
|
|
|
using Avalonia.VisualTree;
|
2019-11-14 18:08:21 +00:00
|
|
|
|
2019-11-13 19:26:04 +00:00
|
|
|
namespace BFR
|
|
|
|
{
|
2019-11-13 22:01:08 +00:00
|
|
|
public partial class MainWindow : Window
|
2019-11-13 19:26:04 +00:00
|
|
|
{
|
2019-11-21 18:07:46 +00:00
|
|
|
public readonly AvaloniaProperty[] HandledProperties = new AvaloniaProperty[] { TextBox.TextProperty, ComboBox.SelectedItemProperty, NumericUpDown.ValueProperty, CheckBox.IsCheckedProperty };
|
2019-11-23 17:40:55 +00:00
|
|
|
private bool HandleEvents = false;
|
2019-11-21 18:07:46 +00:00
|
|
|
|
2019-11-16 01:00:17 +00:00
|
|
|
public async Task OpenDirectoryButtonClick() => OpenDirectory(await new OpenFolderDialog() { Directory = WorkingDirectory }.ShowAsync(this));
|
2019-11-14 18:08:21 +00:00
|
|
|
public void OpenDirectory(string directory)
|
|
|
|
{
|
|
|
|
WorkingDirectory = directory;
|
|
|
|
AllFiles.ReplaceAll(Directory.GetFiles(WorkingDirectory).Select(x => new FileModel(x)));
|
|
|
|
Filter();
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:07:46 +00:00
|
|
|
public void FilterChanged(object sender, AvaloniaPropertyChangedEventArgs e)
|
|
|
|
{
|
2019-11-23 17:40:55 +00:00
|
|
|
if(HandledProperties.Contains(e.Property) && HandleEvents)
|
2019-11-21 18:07:46 +00:00
|
|
|
Filter(); // Bind to PropertyChanged
|
|
|
|
}
|
2019-11-14 18:08:21 +00:00
|
|
|
public void Filter()
|
|
|
|
{
|
2019-11-14 19:42:25 +00:00
|
|
|
// Filter all files in the directory for those satisfying the given filters
|
|
|
|
Files.ReplaceAll(AllFiles.Where(x =>
|
2019-11-17 15:22:19 +00:00
|
|
|
(FilterExtension == "" || x.OldExtension == FilterExtension)
|
2019-11-17 01:41:05 +00:00
|
|
|
&& (FilterFullName ? x.OldFullName : x.OldName).RegexContains(FilterRegex ? FilterPattern : Regex.Escape(FilterPattern))));
|
2019-11-14 18:08:21 +00:00
|
|
|
|
|
|
|
Preview();
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:07:46 +00:00
|
|
|
public void PreviewChanged(object sender, AvaloniaPropertyChangedEventArgs e)
|
|
|
|
{
|
2019-11-23 17:40:55 +00:00
|
|
|
if (HandledProperties.Contains(e.Property) && HandleEvents)
|
2019-11-21 18:07:46 +00:00
|
|
|
Preview(); // Bind to PropertyChanged
|
|
|
|
}
|
2019-11-14 18:08:21 +00:00
|
|
|
public void Preview()
|
|
|
|
{
|
|
|
|
// Reset all files to how they currently exist
|
|
|
|
foreach (var file in Files)
|
|
|
|
file.Path = file.OldPath;
|
|
|
|
|
2019-11-16 01:47:07 +00:00
|
|
|
// Apply operations
|
|
|
|
foreach (var operation in Operations)
|
|
|
|
operation.ApplyTo(Files);
|
2019-11-14 18:08:21 +00:00
|
|
|
|
2019-11-17 01:41:05 +00:00
|
|
|
// Validate that there are any changes, and that the new file names are all unique.
|
|
|
|
IsCommitButtonEnabled =
|
2019-11-21 19:02:29 +00:00
|
|
|
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 empty file names
|
|
|
|
&& !Files.Any(x => "?%*:<>|/\\\"".Any(y => x.FullName.Contains(y))); // Check for invalid characters
|
2019-11-17 01:41:05 +00:00
|
|
|
|
2019-11-14 18:08:21 +00:00
|
|
|
// Refresh the file list to guarantee that changes are displayed. TODO: Find a better way to do this.
|
|
|
|
Files.ReplaceAll(new List<FileModel>(Files));
|
|
|
|
}
|
|
|
|
|
2019-11-17 01:22:13 +00:00
|
|
|
public void AddOperation()
|
|
|
|
{
|
2019-11-23 17:40:55 +00:00
|
|
|
HandleEvents = false;
|
2019-11-17 01:41:05 +00:00
|
|
|
if (Operations.Count >= 1)
|
|
|
|
Operations.Insert(
|
|
|
|
SelectedOperation >= 0 ? SelectedOperation + 1 : Operations.Count,
|
|
|
|
OperationTypes[SelectedOperationType].Create());
|
|
|
|
else Operations.Add(OperationTypes[SelectedOperationType].Create());
|
2019-11-23 17:40:55 +00:00
|
|
|
HandleEvents = true;
|
2019-11-17 01:22:13 +00:00
|
|
|
Preview();
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:08:01 +00:00
|
|
|
private ListBox OperationsListBox;
|
|
|
|
private ListBoxItem DragItem;
|
|
|
|
|
|
|
|
public void DeleteOperation(object sender, PointerReleasedEventArgs e)
|
2019-11-17 01:22:13 +00:00
|
|
|
{
|
2019-12-01 13:08:01 +00:00
|
|
|
var hoveredItem = (ListBoxItem)OperationsListBox.GetLogicalChildren().FirstOrDefault(x => this.GetVisualsAt(e.GetPosition(this)).Contains(((IVisual)x).GetVisualChildren().First()));
|
|
|
|
if(hoveredItem != null) Operations.RemoveAt(OperationsListBox.GetLogicalChildren().ToList().IndexOf(hoveredItem));
|
2019-11-17 01:22:13 +00:00
|
|
|
Preview();
|
|
|
|
}
|
|
|
|
|
2019-12-01 13:08:01 +00:00
|
|
|
public void StartMoveOperation(object sender, PointerPressedEventArgs e) =>
|
|
|
|
DragItem = OperationsListBox.GetLogicalChildren().Cast<ListBoxItem>().Single(x => x.IsPointerOver);
|
|
|
|
|
|
|
|
public void EndMoveOperation(object sender, PointerReleasedEventArgs e)
|
2019-11-17 01:22:13 +00:00
|
|
|
{
|
2019-12-01 13:08:01 +00:00
|
|
|
var hoveredItem = (ListBoxItem)OperationsListBox.GetLogicalChildren().FirstOrDefault(x => this.GetVisualsAt(e.GetPosition(this)).Contains(((IVisual)x).GetVisualChildren().First()));
|
|
|
|
if (DragItem == null ||
|
|
|
|
hoveredItem == null ||
|
|
|
|
DragItem == hoveredItem)
|
|
|
|
return;
|
|
|
|
Operations.Move(
|
|
|
|
OperationsListBox.GetLogicalChildren().ToList().IndexOf(DragItem),
|
|
|
|
OperationsListBox.GetLogicalChildren().ToList().IndexOf(hoveredItem));
|
|
|
|
DragItem = null;
|
2019-11-17 01:22:13 +00:00
|
|
|
Preview();
|
|
|
|
}
|
|
|
|
|
2019-11-17 01:41:05 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-16 01:00:17 +00:00
|
|
|
public MainWindow() => InitializeComponent();
|
2019-11-13 19:26:04 +00:00
|
|
|
private void InitializeComponent()
|
|
|
|
{
|
|
|
|
AvaloniaXamlLoader.Load(this);
|
2019-11-13 19:40:30 +00:00
|
|
|
DataContext = this;
|
2019-11-14 18:08:21 +00:00
|
|
|
OpenDirectory(Environment.OSVersion.Platform == PlatformID.Win32NT ? @"C:\Users" : @"\home");
|
2019-11-23 17:40:55 +00:00
|
|
|
HandleEvents = true;
|
2019-12-01 13:08:01 +00:00
|
|
|
OperationsListBox = this.Find<ListBox>("OperationsListBox");
|
2019-11-13 19:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|