Flow.Launcher/Plugins/Flow.Launcher.Plugin.Explorer/Views/RenameFile.xaml.cs

129 lines
3.6 KiB
C#
Raw Normal View History

2025-06-22 19:41:52 +00:00
using System;
using System.IO;
using System.Linq;
using System.Windows;
2025-06-22 20:45:42 +00:00
using System.Windows.Controls;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
2025-06-22 19:41:52 +00:00
using Flow.Launcher.Plugin.Explorer.Helper;
2025-06-22 20:45:42 +00:00
using Microsoft.VisualBasic.Logging;
namespace Flow.Launcher.Plugin.Explorer.Views
{
2025-06-22 19:41:52 +00:00
[INotifyPropertyChanged]
2025-06-22 19:41:52 +00:00
public partial class RenameFile : Window
{
public string NewFileName
{
2025-06-22 19:41:52 +00:00
get => _newFileName;
set
{
2025-06-22 19:41:52 +00:00
_ = SetProperty(ref _newFileName, value);
}
}
2025-06-22 19:41:52 +00:00
private string _newFileName;
2025-06-22 20:45:42 +00:00
private readonly IPublicAPI _api;
2025-06-22 19:41:52 +00:00
private readonly string _oldFilePath;
2025-06-22 19:41:52 +00:00
private readonly FileSystemInfo _info;
2025-06-22 19:41:52 +00:00
public RenameFile(IPublicAPI api, FileSystemInfo info)
{
_api = api;
InitializeComponent();
RenameTb.Focus();
2025-06-22 20:45:42 +00:00
ShowInTaskbar = false;
2025-06-22 19:41:52 +00:00
RenameTb.SelectAll();
2025-06-22 20:45:42 +00:00
2025-06-22 19:41:52 +00:00
_info = info;
_oldFilePath = _info.FullName;
2025-06-22 20:45:42 +00:00
NewFileName = _info.Name;
2025-06-22 19:41:52 +00:00
}
private void OnDoneButtonClick(object sender, RoutedEventArgs e)
{
2025-06-22 20:45:42 +00:00
2025-06-22 19:41:52 +00:00
// if it's just whitespace and nothing else
2025-06-22 20:45:42 +00:00
_api.LogInfo(nameof(RenameFile),$"THIS IS NEW FILE NAME: {NewFileName}");
if (NewFileName.Trim() == "" || NewFileName == "")
2025-06-22 19:41:52 +00:00
{
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_field_may_not_be_empty"), "New file name"));
return;
}
2025-06-22 20:45:42 +00:00
2025-06-22 19:41:52 +00:00
try
{
2025-06-22 20:45:42 +00:00
_info.Rename(NewFileName);
2025-06-22 19:41:52 +00:00
}
catch (Exception exception)
{
switch (exception)
{
case FileNotFoundException:
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_file_not_found"), _oldFilePath));
break;
2025-06-22 20:45:42 +00:00
case NotANewNameException:
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName));
2025-06-22 19:41:52 +00:00
_api.ShowMainWindow();
break;
2025-06-22 20:45:42 +00:00
case InvalidNameException:
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_invalid_name"), NewFileName));
break;
case IOException iOException:
if (iOException.Message.Contains("incorrect"))
{
_api.ShowMsgError(string.Format(_api.GetTranslation("plugin_explorer_invalid_name"), NewFileName));
break;
}
else
{
goto default;
}
2025-06-22 19:41:52 +00:00
default:
_api.ShowMsgError(exception.ToString());
break;
}
}
Close();
2025-06-22 20:45:42 +00:00
}
private void BtnCancel(object sender, RoutedEventArgs e)
{
Close();
}
2025-06-22 19:41:52 +00:00
private void RenameTb_OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
btnDone.Focus();
OnDoneButtonClick(sender, e);
e.Handled = true;
}
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
2025-06-22 20:45:42 +00:00
}
}