rename file from IDE
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
using Godot;
|
||||
using SharpIDE.Application.Features.FileWatching;
|
||||
using SharpIDE.Application.Features.SolutionDiscovery;
|
||||
|
||||
namespace SharpIDE.Godot.Features.SolutionExplorer.ContextMenus.Dialogs;
|
||||
|
||||
public partial class RenameFileDialog : ConfirmationDialog
|
||||
{
|
||||
private LineEdit _nameLineEdit = null!;
|
||||
|
||||
public SharpIdeFile File { get; set; } = null!;
|
||||
|
||||
[Inject] private readonly IdeFileOperationsService _ideFileOperationsService = null!;
|
||||
|
||||
private bool _isNameValid = true;
|
||||
private string _fileParentPath = null!;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_fileParentPath = Path.GetDirectoryName(File.Path)!;
|
||||
_nameLineEdit = GetNode<LineEdit>("%FileNameLineEdit");
|
||||
_nameLineEdit.Text = File.Name;
|
||||
_nameLineEdit.GrabFocus();
|
||||
// select the name without the extension
|
||||
_nameLineEdit.Select(0, File.Name.LastIndexOf('.'));
|
||||
_nameLineEdit.TextChanged += ValidateNewFileName;
|
||||
Confirmed += OnConfirmed;
|
||||
}
|
||||
|
||||
private void ValidateNewFileName(string newFileNameText)
|
||||
{
|
||||
_isNameValid = true;
|
||||
var newFileName = newFileNameText.Trim();
|
||||
if (string.IsNullOrEmpty(newFileName) || System.IO.File.Exists(Path.Combine(_fileParentPath, newFileName)))
|
||||
{
|
||||
_isNameValid = false;
|
||||
}
|
||||
var textColour = _isNameValid ? new Color(1, 1, 1) : new Color(1, 0, 0);
|
||||
_nameLineEdit.AddThemeColorOverride("font_color", textColour);
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event is InputEventKey { Pressed: true, Keycode: Key.Enter })
|
||||
{
|
||||
EmitSignalConfirmed();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnConfirmed()
|
||||
{
|
||||
if (_isNameValid is false) return;
|
||||
var fileName = _nameLineEdit.Text.Trim();
|
||||
if (string.IsNullOrEmpty(fileName))
|
||||
{
|
||||
GD.PrintErr("File name cannot be empty.");
|
||||
return;
|
||||
}
|
||||
|
||||
_ = Task.GodotRun(async () =>
|
||||
{
|
||||
await _ideFileOperationsService.RenameFile(File, fileName);
|
||||
});
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dq72hpd4r54w4
|
||||
@@ -0,0 +1,26 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://b775b5j4rkxxw"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://dq72hpd4r54w4" path="res://Features/SolutionExplorer/ContextMenus/Dialogs/RenameFileDialog.cs" id="1_0pttq"]
|
||||
|
||||
[node name="RenameFileDialog" type="ConfirmationDialog"]
|
||||
oversampling_override = 1.0
|
||||
title = "Rename: Directory"
|
||||
position = Vector2i(0, 36)
|
||||
size = Vector2i(405, 115)
|
||||
visible = true
|
||||
script = ExtResource("1_0pttq")
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="."]
|
||||
offset_left = 8.0
|
||||
offset_top = 8.0
|
||||
offset_right = 397.0
|
||||
offset_bottom = 66.0
|
||||
|
||||
[node name="Label" type="Label" parent="VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Enter name:"
|
||||
|
||||
[node name="FileNameLineEdit" type="LineEdit" parent="VBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "ExistingDirectoryName"
|
||||
@@ -1,6 +1,7 @@
|
||||
using Godot;
|
||||
using SharpIDE.Application.Features.SolutionDiscovery;
|
||||
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
|
||||
using SharpIDE.Godot.Features.SolutionExplorer.ContextMenus.Dialogs;
|
||||
|
||||
namespace SharpIDE.Godot.Features.SolutionExplorer;
|
||||
|
||||
@@ -9,11 +10,13 @@ file enum FileContextMenuOptions
|
||||
Open = 0,
|
||||
RevealInFileExplorer = 1,
|
||||
CopyFullPath = 2,
|
||||
Delete = 3
|
||||
Rename = 3,
|
||||
Delete = 4
|
||||
}
|
||||
|
||||
public partial class SolutionExplorerPanel
|
||||
{
|
||||
private readonly PackedScene _renameFileDialogScene = GD.Load<PackedScene>("uid://b775b5j4rkxxw");
|
||||
private void OpenContextMenuFile(SharpIdeFile file)
|
||||
{
|
||||
var menu = new PopupMenu();
|
||||
@@ -23,6 +26,7 @@ public partial class SolutionExplorerPanel
|
||||
menu.AddSeparator();
|
||||
menu.AddItem("Copy Full Path", (int)FileContextMenuOptions.CopyFullPath);
|
||||
menu.AddSeparator();
|
||||
menu.AddItem("Rename", (int)FileContextMenuOptions.Rename);
|
||||
menu.AddItem("Delete", (int)FileContextMenuOptions.Delete);
|
||||
if (file.Parent is SharpIdeSolutionFolder) menu.SetItemDisabled((int)FileContextMenuOptions.Delete, true);
|
||||
menu.PopupHide += () => menu.QueueFree();
|
||||
@@ -41,6 +45,13 @@ public partial class SolutionExplorerPanel
|
||||
{
|
||||
DisplayServer.ClipboardSet(file.Path);
|
||||
}
|
||||
else if (actionId is FileContextMenuOptions.Rename)
|
||||
{
|
||||
var renameFileDialog = _renameFileDialogScene.Instantiate<RenameFileDialog>();
|
||||
renameFileDialog.File = file;
|
||||
AddChild(renameFileDialog);
|
||||
renameFileDialog.PopupCentered();
|
||||
}
|
||||
else if (actionId is FileContextMenuOptions.Delete)
|
||||
{
|
||||
var confirmedTcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
Reference in New Issue
Block a user