From 8ed495dc84d6ad305021beaeea5d1308c5e5b47a Mon Sep 17 00:00:00 2001
From: Jack251970 <1160210343@qq.com>
Date: Wed, 25 Jun 2025 22:04:40 +0800
Subject: [PATCH] Improve code quality
---
.../Helper/RenameThing.cs | 226 +++++++++---------
.../Views/RenameFile.xaml.cs | 27 +--
2 files changed, 115 insertions(+), 138 deletions(-)
diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs
index f6712453f..7743bc527 100644
--- a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs
+++ b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs
@@ -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)}");
- }
-
}
- ///
- /// Renames a file system element (directory or file)
- ///
- /// The requested new name
- /// The or representing the old file
- /// An instance of so this can create msgboxes
-
- 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
+ ///
+ /// Renames a file system element (directory or file)
+ ///
+ /// The requested new name
+ /// The or representing the old file
+ /// An instance of so this can create msgboxes
+ 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) { }
+}
diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs
index 3e12fd4cd..4591fa76e 100644
--- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs
+++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs
@@ -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();
-
-
-
}
+
///
/// https://stackoverflow.com/a/59560352/24045055
///
-
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;
}
-
-
-
}
}
}