Improve code quality

This commit is contained in:
Jack251970 2025-06-25 22:04:40 +08:00
parent 4d77bad83d
commit 8ed495dc84
2 changed files with 115 additions and 138 deletions

View file

@ -1,140 +1,136 @@
using System;
using System;
using System.IO;
using System.Runtime.Serialization;
namespace Flow.Launcher.Plugin.Explorer.Helper
namespace Flow.Launcher.Plugin.Explorer.Helper;
public static class RenameThing
{
public static class RenameThing
private static void Rename(this FileSystemInfo info, string newName)
{
private static void _rename(this FileSystemInfo info, string newName, IPublicAPI api)
if (info is FileInfo file)
{
if (info is FileInfo)
if (!SharedCommands.FilesFolders.IsValidFileName(newName))
{
if (!SharedCommands.FilesFolders.IsValidFileName(newName))
{
throw new InvalidNameException();
}
FileInfo file = (FileInfo)info;
DirectoryInfo directory;
directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName));
string newPath = Path.Join(directory.FullName, newName);
if (info.FullName == newPath)
{
throw new NotANewNameException("New name was the same as the old name");
}
if (File.Exists(newPath)) throw new ElementAlreadyExistsException();
File.Move(info.FullName, newPath);
return;
throw new InvalidNameException();
}
else if (info is DirectoryInfo)
DirectoryInfo directory;
var rootPath = Path.GetPathRoot(file.FullName);
if (string.IsNullOrEmpty(rootPath)) return;
directory = file.Directory ?? new DirectoryInfo(rootPath);
string newPath = Path.Join(directory.FullName, newName);
if (info.FullName == newPath)
{
if (!SharedCommands.FilesFolders.IsValidDirectoryName(newName))
{
throw new InvalidNameException();
}
DirectoryInfo directory = (DirectoryInfo)info;
DirectoryInfo parent;
parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName));
string newPath = Path.Join(parent.FullName, newName);
if (info.FullName == newPath)
{
throw new NotANewNameException("New name was the same as the old name");
}
if (Directory.Exists(newPath)) throw new ElementAlreadyExistsException();
throw new NotANewNameException("New name was the same as the old name");
}
if (File.Exists(newPath)) throw new ElementAlreadyExistsException();
File.Move(info.FullName, newPath);
return;
}
else if (info is DirectoryInfo directory)
{
if (!SharedCommands.FilesFolders.IsValidDirectoryName(newName))
{
throw new InvalidNameException();
}
DirectoryInfo parent;
var rootPath = Path.GetPathRoot(directory.FullName);
if (string.IsNullOrEmpty(rootPath)) return;
parent = directory.Parent ?? new DirectoryInfo(rootPath);
string newPath = Path.Join(parent.FullName, newName);
if (info.FullName == newPath)
{
throw new NotANewNameException("New name was the same as the old name");
}
if (Directory.Exists(newPath)) throw new ElementAlreadyExistsException();
Directory.Move(info.FullName, newPath);
Directory.Move(info.FullName, newPath);
}
else
{
throw new ArgumentException($"{nameof(info)} must be either, {nameof(FileInfo)} or {nameof(DirectoryInfo)}");
}
}
/// <summary>
/// Renames a file system element (directory or file)
/// </summary>
/// <param name="NewFileName">The requested new name</param>
/// <param name="oldInfo"> The <see cref="FileInfo"/> or <see cref="DirectoryInfo"/> representing the old file</param>
/// <param name="api">An instance of <see cref="IPublicAPI"/>so this can create msgboxes</param>
public static void Rename(string NewFileName, FileSystemInfo oldInfo, IPublicAPI api)
else
{
// if it's just whitespace and nothing else
if (NewFileName.Trim() == "" || NewFileName == "")
{
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name"));
return;
}
throw new ArgumentException($"{nameof(info)} must be either, {nameof(FileInfo)} or {nameof(DirectoryInfo)}");
}
}
try
/// <summary>
/// Renames a file system element (directory or file)
/// </summary>
/// <param name="NewFileName">The requested new name</param>
/// <param name="oldInfo"> The <see cref="FileInfo"/> or <see cref="DirectoryInfo"/> representing the old file</param>
/// <param name="api">An instance of <see cref="IPublicAPI"/>so this can create msgboxes</param>
public static void Rename(string NewFileName, FileSystemInfo oldInfo, IPublicAPI api)
{
// if it's just whitespace and nothing else
if (NewFileName.Trim() == "" || NewFileName == "")
{
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name"));
return;
}
try
{
oldInfo.Rename(NewFileName);
}
catch (Exception exception)
{
switch (exception)
{
oldInfo._rename(NewFileName, api);
}
catch (Exception exception)
{
switch (exception)
{
case FileNotFoundException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_file_not_found"), oldInfo.FullName));
case FileNotFoundException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_file_not_found"), oldInfo.FullName));
return;
case NotANewNameException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName));
return;
case InvalidNameException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_invalid_name"), NewFileName));
return;
case ElementAlreadyExistsException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_element_already_exists"), NewFileName));
break;
default:
string msg = exception.Message;
if (!string.IsNullOrEmpty(msg))
{
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_exception"), exception.Message));
return;
case NotANewNameException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName));
return;
case InvalidNameException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_invalid_name"), NewFileName));
return;
case ElementAlreadyExistsException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_element_already_exists"), NewFileName));
break;
default:
string msg = exception.Message;
if (!string.IsNullOrEmpty(msg))
{
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_exception"), exception.Message));
return;
}
else
{
api.ShowMsgError(api.GetTranslation("plugin_explorer_no_reason_given_exception"));
}
}
else
{
api.ShowMsgError(api.GetTranslation("plugin_explorer_no_reason_given_exception"));
}
return;
}
return;
}
api.ShowMsg(string.Format(api.GetTranslation("plugin_explorer_successful_rename"), NewFileName));
}
}
api.ShowMsg(string.Format(api.GetTranslation("plugin_explorer_successful_rename"), NewFileName));
}
}
internal class NotANewNameException : IOException
{
public NotANewNameException() { }
public NotANewNameException(string message) : base(message) { }
public NotANewNameException(string message, Exception inner) : base(message, inner) { }
protected NotANewNameException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
internal class ElementAlreadyExistsException : IOException {
public ElementAlreadyExistsException() { }
public ElementAlreadyExistsException(string message) : base(message) { }
public ElementAlreadyExistsException(string message, Exception inner) : base(message, inner) { }
protected ElementAlreadyExistsException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
internal class NotANewNameException : IOException
{
public NotANewNameException() { }
public NotANewNameException(string message) : base(message) { }
public NotANewNameException(string message, Exception inner) : base(message, inner) { }
protected NotANewNameException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }
}
internal class ElementAlreadyExistsException : IOException {
public ElementAlreadyExistsException() { }
public ElementAlreadyExistsException(string message) : base(message) { }
public ElementAlreadyExistsException(string message, Exception inner) : base(message, inner) { }
protected ElementAlreadyExistsException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }
}
internal class InvalidNameException : Exception
{
internal class InvalidNameException : Exception
{
public InvalidNameException() { }
public InvalidNameException(string message) : base(message) { }
public InvalidNameException(string message, Exception inner) : base(message, inner) { }
protected InvalidNameException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
SerializationInfo info,
StreamingContext context) : base(info, context) { }
}

View file

@ -1,4 +1,4 @@
using System.IO;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
@ -7,15 +7,11 @@ using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using Flow.Launcher.Plugin.Explorer.Helper;
namespace Flow.Launcher.Plugin.Explorer.Views
{
[INotifyPropertyChanged]
public partial class RenameFile : Window
{
public string NewFileName
{
get => _newFileName;
@ -25,7 +21,6 @@ namespace Flow.Launcher.Plugin.Explorer.Views
}
}
private string _newFileName;
private readonly IPublicAPI _api;
@ -42,25 +37,16 @@ namespace Flow.Launcher.Plugin.Explorer.Views
InitializeComponent();
ShowInTaskbar = false;
RenameTb.Focus();
}
/// <summary>
/// https://stackoverflow.com/a/59560352/24045055
/// </summary>
private async void SelectAll_OnTextBoxGotFocus(object sender, RoutedEventArgs e)
{
var textBox = sender as TextBox;
if (sender is not TextBox textBox) return;
if (_info is DirectoryInfo)
{
await Application.Current.Dispatcher.InvokeAsync(textBox.SelectAll, DispatcherPriority.Background);
@ -70,15 +56,13 @@ namespace Flow.Launcher.Plugin.Explorer.Views
{
string properName = Path.GetFileNameWithoutExtension(info.Name);
Application.Current.Dispatcher.Invoke(textBox.Select, DispatcherPriority.Background, textBox.Text.IndexOf(properName), properName.Length );
}
}
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
{
RenameThing.Rename(NewFileName, _info, _api);
Close();
}
private void BtnCancel(object sender, RoutedEventArgs e)
@ -94,9 +78,6 @@ namespace Flow.Launcher.Plugin.Explorer.Views
OnDoneButtonClick(sender, e);
e.Handled = true;
}
}
}
}