refactoring

This commit is contained in:
Koisu 2025-06-24 10:37:58 -07:00
parent 657bde5de7
commit 8138158d3a
3 changed files with 49 additions and 41 deletions

View file

@ -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<PluginPair> 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 <see cref="../../Plugins/Flow.Launcher.Plugin.Explorer/Main.cs" />
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);
}
}
/// <summary>
/// Get an instance of the explorer plugin if it's loaded
/// </summary>
/// <returns>Returns an instance of Flow.Launcher.Plugin.Explorer.Main, if it's not loaded this returns null.</returns>
private IAsyncPlugin GetExplorerPlugin()
{
const string explorerPluginID = "572be03c74c642baae319fc283e561a8";
IEnumerable<PluginPair> 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;
}
/// <summary>
/// Shows the dialog to rename a file system element.
/// </summary>
/// <param name="explorerPlugin">An instance of the Flow.Launcher.Plugin.Explorer.Main, which is invisible in the current namespace</param>
/// <param name="path">The path of the element</param>
/// <param name="name">The new name</param>
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

View file

@ -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));

View file

@ -191,7 +191,7 @@
<system:String x:Key="YearsAgo">{0} years ago</system:String>
<!--Rename File Dialog-->
<system:String x:Key="plugin_explorer_new_file_name">New File name:</system:String>
<system:String x:Key="plugin_explorer_new_file_name">New name:</system:String>
<system:String x:Key="plugin_explorer_rename_file_done">Rename</system:String>
<system:String x:Key="plugin_explorer_rename_a_file">Rename</system:String>
<system:String x:Key="plugin_explorer_not_a_new_name">The given name: {0} was not new.</system:String>