renaming half-works

This commit is contained in:
Koisu 2025-06-22 12:41:52 -07:00
parent 459d85d395
commit 5ae6f971a0
5 changed files with 132 additions and 47 deletions

View file

@ -11,6 +11,7 @@ using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks;
using Flow.Launcher.Plugin.Explorer.Helper;
using Flow.Launcher.Plugin.Explorer.ViewModels;
using Flow.Launcher.Plugin.Explorer.Views;
using System.Windows.Controls;
namespace Flow.Launcher.Plugin.Explorer
{
@ -192,18 +193,33 @@ namespace Flow.Launcher.Plugin.Explorer
contextMenus.Add(new Result
{
Title = "Rename",
SubTitle = "Opens a dialogue to this",
SubTitle = "Opens a dialogue to rename this",
Action = _ =>
{
RenameFile window = new(Context.API);
Context.API.FocusQueryTextBox();
if (!(window.ShowDialog() ?? false))
Type T;
RenameFile window;
switch (record.Type)
{
Context.API.FocusQueryTextBox();
return false;
case ResultType.Folder:
window = new RenameFile(Context.API, new DirectoryInfo(record.FullPath));
break;
case ResultType.File:
window = new RenameFile(Context.API, new FileInfo(record.FullPath));
break;
default:
Context.API.ShowMsgError("Cannot rename this.");
return false;
}
Context.API.FocusQueryTextBox();
window.ShowDialog();
return false;
}
});

View file

@ -0,0 +1,55 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace Flow.Launcher.Plugin.Explorer.Helper
{
public static class RenameThing
{
public static void Rename(this FileSystemInfo info, string newName)
{
if (info is FileInfo)
{
FileInfo file = new FileInfo(info.FullName);
DirectoryInfo directory;
directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName));
if (Path.Join(info.FullName, directory.Name) == newName)
{
throw new NotANewNameException("New name was the same as the old name");
}
File.Move(info.FullName, Path.Join(directory.FullName, newName));
return;
}
else if (info is DirectoryInfo)
{
DirectoryInfo directory = new DirectoryInfo(info.FullName);
DirectoryInfo parent;
parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName));
if (Path.Join(parent.FullName, directory.Name) == newName)
{
throw new NotANewNameException("New name was the same as the old name");
}
Directory.Move(info.FullName, Path.Join(parent.FullName, newName));
}
else
{
throw new ArgumentException($"{nameof(info)} must be either, {nameof(FileInfo)} or {nameof(DirectoryInfo)}");
}
}
}
[Serializable]
public 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) { }
}
}

View file

@ -195,4 +195,6 @@
<system:String x:Key="plugin_explorer_new_file_name">New File 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>
<system:String x:Key="plugin_explorer_field_may_not_be_empty">{0} may not be empty.</system:String>
</ResourceDictionary>

View file

@ -84,8 +84,8 @@
Width="135"
HorizontalAlignment="Left"
VerticalAlignment="Center"
DataObject.Pasting="TextBox_Pasting"
PreviewKeyDown="TxtCurrentActionKeyword_OnKeyDown"
DataObject.Pasting="RenameTb_Pasting"
PreviewKeyDown="RenameTb_OnKeyDown"
Text="{Binding NewFileName}"/>
</StackPanel>
</StackPanel>

View file

@ -1,27 +1,31 @@
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using Flow.Launcher.Plugin.Explorer.Helper;
namespace Flow.Launcher.Plugin.Explorer.Views
{
[INotifyPropertyChanged]
public partial class RenameFile : Window
public partial class RenameFile : Window
{
public string NewFileName
{
get => newFileName;
get => _newFileName;
set
{
_ = SetProperty(ref newFileName, value);
_ = SetProperty(ref _newFileName, value);
}
}
private string newFileName = "gayTest";
private string _newFileName;
private string renamingText = "fes";
public string RenamingText
{
@ -32,9 +36,11 @@ namespace Flow.Launcher.Plugin.Explorer.Views
}
}
private readonly IPublicAPI _api;
private readonly string _oldFilePath;
private readonly FileSystemInfo _info;
public RenameFile(IPublicAPI api)
public RenameFile(IPublicAPI api, FileSystemInfo info)
{
_api = api;
@ -42,42 +48,48 @@ namespace Flow.Launcher.Plugin.Explorer.Views
InitializeComponent();
RenameTb.Focus();
Deactivated += (s, e) =>
{
DialogResult = false;
Close();
};
ShowInTaskbar = false;
RenameTb.Select(RenameTb.Text.Length, 0);
RenameTb.SelectAll();
_info = info;
_oldFilePath = _info.FullName;
_newFileName = _info.Name;
}
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
{
// if it's just whitespace and nothing else
if (_newFileName.Trim() == "")
{
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name"));
Show();
return;
}
if ()
try
{
_info.Rename(_newFileName);
}
catch (Exception exception)
{
switch (exception)
{
case FileNotFoundException:
// if (ActionKeyword == Query.GlobalPluginWildcardSign)
// switch (CurrentActionKeyword.KeywordProperty, KeywordEnabled)
// {
// case (Settings.ActionKeyword.FileContentSearchActionKeyword, true):
// _api.ShowMsgBox(_api.GetTranslation("plugin_explorer_globalActionKeywordInvalid"));
// return;
// case (Settings.ActionKeyword.QuickAccessActionKeyword, true):
// _api.ShowMsgBox(_api.GetTranslation("plugin_explorer_quickaccess_globalActionKeywordInvalid"));
// return;
// }
// if (!KeywordEnabled || !_api.ActionKeywordAssigned(ActionKeyword))
// {
// DialogResult = true;
// Close();
// return;
// }
// The keyword is not valid, so show message
_api.ShowMsgBox("new action keyword");
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_file_not_found"), _oldFilePath));
break;
case NotANewNameException notANewNameException:
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_not_a_new_name"), _newFileName));
_api.ShowMainWindow();
break;
default:
_api.ShowMsgError(exception.ToString());
break;
}
}
Close();
}
private void BtnCancel(object sender, RoutedEventArgs e)
@ -85,7 +97,7 @@ namespace Flow.Launcher.Plugin.Explorer.Views
Close();
}
private void TxtCurrentActionKeyword_OnKeyDown(object sender, KeyEventArgs e)
private void RenameTb_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
@ -100,7 +112,7 @@ namespace Flow.Launcher.Plugin.Explorer.Views
}
private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
private void RenameTb_Pasting(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(DataFormats.Text))
{