diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs b/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs index fe4164e7c..a84979999 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs @@ -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; } }); diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs new file mode 100644 index 000000000..a2e298210 --- /dev/null +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Helper/RenameThing.cs @@ -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) { } + } +} diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml index 62f17bf5b..89c9b2abd 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Languages/en.xaml @@ -195,4 +195,6 @@ New File name: Rename Rename + The given name: {0} was not new. + {0} may not be empty. diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml index c7c81d486..592ec284b 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml @@ -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}"/> diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs index 198928096..c38c0905d 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs @@ -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)) {