From 8138158d3a64d593d8eb93f7488fe8de86da0004 Mon Sep 17 00:00:00 2001 From: Koisu Date: Tue, 24 Jun 2025 10:37:58 -0700 Subject: [PATCH] refactoring --- Flow.Launcher/ViewModel/MainViewModel.cs | 76 +++++++++++-------- .../Helper/RenameThing.cs | 12 +-- .../Languages/en.xaml | 2 +- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 491914ff1..e007c3735 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -23,7 +23,7 @@ using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedCommands; using Flow.Launcher.Storage; using Microsoft.VisualStudio.Threading; -using Svg; + namespace Flow.Launcher.ViewModel { @@ -932,45 +932,61 @@ namespace Flow.Launcher.ViewModel [RelayCommand] private void RenameFile() { + // at runtime this is an instance the Flow.Launcher.Plugin.Explorer.Main + dynamic explorerPlugin = GetExplorerPlugin(); - const string explorerPluginID = "572be03c74c642baae319fc283e561a8"; - // check if explorer plugin is enabled - IEnumerable explorerPluginMatches = App.API.GetAllPlugins().Where( - plugin => plugin.Metadata.ID == explorerPluginID); - - if (!explorerPluginMatches.Any() || explorerPluginMatches == null) - { - - timesTriedToRenameFileWithExplorerDisabled++; - return; - } - else if ((!explorerPluginMatches.Any() || explorerPluginMatches == null) && timesTriedToRenameFileWithExplorerDisabled > 3) + if (explorerPlugin == null && timesTriedToRenameFileWithExplorerDisabled > 3) { App.API.ShowMsg(App.API.GetTranslation("AreTryingToRenameFile"), App.API.GetTranslation("ExplorerNeedsEnabledForRenameFile")); timesTriedToRenameFileWithExplorerDisabled = 0; return; } + else if (explorerPlugin == null) + { + timesTriedToRenameFileWithExplorerDisabled++; + return; + } else { - // at runtime the type of the will be - dynamic explorerPlugin = explorerPluginMatches.First(); // assuming there's only one match string path = SelectedResults?.SelectedItem?.Result.SubTitle ?? ""; string name = SelectedResults?.SelectedItem?.Result.Title ?? ""; - if (path.Trim() == "" || name.Trim() == "") return; - if (File.Exists(Path.Join(path, name))) - { - explorerPlugin.Plugin.RenameDialog(new FileInfo(Path.Join(path, name)), App.API); - return; - } - if (Directory.Exists(path)) - { - explorerPlugin.Plugin.RenameDialog(new DirectoryInfo(path), App.API); // this feels kinda hacky - return; - } - - - - + if (string.IsNullOrWhiteSpace(path) || string.IsNullOrWhiteSpace(name)) return; + ShowRenamingDialog(explorerPlugin, path, name); + } + } + /// + /// Get an instance of the explorer plugin if it's loaded + /// + /// Returns an instance of Flow.Launcher.Plugin.Explorer.Main, if it's not loaded this returns null. + private IAsyncPlugin GetExplorerPlugin() + { + const string explorerPluginID = "572be03c74c642baae319fc283e561a8"; + IEnumerable explorerPluginMatches = App.API.GetAllPlugins().Where( + plugin => plugin.Metadata.ID == explorerPluginID); + if (explorerPluginMatches.Any()) + { + // assuming it's the first plugin as no 2 plugins can be loaded with the same ID + return explorerPluginMatches.First().Plugin; + } + return null; + } + /// + /// Shows the dialog to rename a file system element. + /// + /// An instance of the Flow.Launcher.Plugin.Explorer.Main, which is invisible in the current namespace + /// The path of the element + /// The new name + private void ShowRenamingDialog(dynamic explorerPlugin, string path, string name) + { + if (File.Exists(Path.Join(path, name))) + { + explorerPlugin.RenameDialog(new FileInfo(Path.Join(path, name)), App.API); + return; + } + if (Directory.Exists(path)) + { + explorerPlugin.RenameDialog(new DirectoryInfo(path), App.API); + return; } } #endregion diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs index 4501d4337..a7f9bc3ef 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs @@ -1,10 +1,5 @@ using System; -using System.CodeDom; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Threading.Tasks; -using JetBrains.Annotations; namespace Flow.Launcher.Plugin.Explorer.Helper { @@ -18,7 +13,7 @@ namespace Flow.Launcher.Plugin.Explorer.Helper { throw new InvalidNameException(); } - FileInfo file = new FileInfo(info.FullName); + FileInfo file = (FileInfo)info; DirectoryInfo directory; directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName)); @@ -31,14 +26,11 @@ namespace Flow.Launcher.Plugin.Explorer.Helper } else if (info is DirectoryInfo) { - - - if (!api.IsValidDirectoryName(newName)) { throw new InvalidNameException(); } - DirectoryInfo directory = new DirectoryInfo(info.FullName); + DirectoryInfo directory = (DirectoryInfo)info; DirectoryInfo parent; parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName)); diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml index 87d50ef77..4992e52d3 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml @@ -191,7 +191,7 @@ {0} years ago - New File name: + New name: Rename Rename The given name: {0} was not new.