rename directory from IDE
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using Godot;
|
||||
using SharpIDE.Application.Features.FileWatching;
|
||||
using SharpIDE.Application.Features.SolutionDiscovery;
|
||||
using SharpIDE.Application.Features.SolutionDiscovery.VsPersistence;
|
||||
|
||||
namespace SharpIDE.Godot.Features.SolutionExplorer.ContextMenus.Dialogs;
|
||||
|
||||
public partial class RenameDirectoryDialog : ConfirmationDialog
|
||||
{
|
||||
private LineEdit _nameLineEdit = null!;
|
||||
|
||||
public SharpIdeFolder Folder { get; set; } = null!;
|
||||
public TreeItem FolderTreeItem { get; set; } = null!;
|
||||
|
||||
[Inject] private readonly IdeFileOperationsService _ideFileOperationsService = null!;
|
||||
|
||||
private bool _isNameValid = true;
|
||||
private string _folderParentPath = null!;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_folderParentPath = Path.GetDirectoryName(Folder.Path)!;
|
||||
_nameLineEdit = GetNode<LineEdit>("%DirectoryNameLineEdit");
|
||||
_nameLineEdit.Text = Folder.Name;
|
||||
_nameLineEdit.GrabFocus();
|
||||
_nameLineEdit.SelectAll();
|
||||
_nameLineEdit.TextChanged += ValidateNewDirectoryName;
|
||||
Confirmed += OnConfirmed;
|
||||
}
|
||||
|
||||
private void ValidateNewDirectoryName(string newDirectoryNameText)
|
||||
{
|
||||
_isNameValid = true;
|
||||
var newDirectoryName = newDirectoryNameText.Trim();
|
||||
if (string.IsNullOrEmpty(newDirectoryName) || Directory.Exists(Path.Combine(_folderParentPath, newDirectoryName)))
|
||||
{
|
||||
_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 directoryName = _nameLineEdit.Text.Trim();
|
||||
if (string.IsNullOrEmpty(directoryName))
|
||||
{
|
||||
GD.PrintErr("Directory name cannot be empty.");
|
||||
return;
|
||||
}
|
||||
|
||||
_ = Task.GodotRun(async () =>
|
||||
{
|
||||
await _ideFileOperationsService.RenameDirectory(Folder, directoryName);
|
||||
await this.InvokeAsync(() => FolderTreeItem.SetText(0, directoryName));
|
||||
});
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://br4u8ymur3yg5
|
||||
@@ -0,0 +1,26 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://btebkg8bo3b37"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://br4u8ymur3yg5" path="res://Features/SolutionExplorer/ContextMenus/Dialogs/RenameDirectoryDialog.cs" id="1_6fale"]
|
||||
|
||||
[node name="RenameDirectoryDialog" type="ConfirmationDialog"]
|
||||
oversampling_override = 1.0
|
||||
title = "Rename: Directory"
|
||||
position = Vector2i(0, 36)
|
||||
size = Vector2i(405, 115)
|
||||
visible = true
|
||||
script = ExtResource("1_6fale")
|
||||
|
||||
[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="DirectoryNameLineEdit" type="LineEdit" parent="VBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "ExistingDirectoryName"
|
||||
@@ -10,7 +10,8 @@ file enum FolderContextMenuOptions
|
||||
{
|
||||
CreateNew = 1,
|
||||
RevealInFileExplorer = 2,
|
||||
Delete = 3
|
||||
Delete = 3,
|
||||
Rename = 4
|
||||
}
|
||||
|
||||
file enum CreateNewSubmenuOptions
|
||||
@@ -22,7 +23,11 @@ file enum CreateNewSubmenuOptions
|
||||
public partial class SolutionExplorerPanel
|
||||
{
|
||||
[Inject] private readonly IdeFileOperationsService _ideFileOperationsService = null!;
|
||||
private void OpenContextMenuFolder(SharpIdeFolder folder)
|
||||
|
||||
private readonly PackedScene _newDirectoryDialogScene = GD.Load<PackedScene>("uid://bgi4u18y8pt4x");
|
||||
private readonly PackedScene _newCsharpFileDialogScene = GD.Load<PackedScene>("uid://chnb7gmcdg0ww");
|
||||
private readonly PackedScene _renameDirectoryDialogScene = GD.Load<PackedScene>("uid://btebkg8bo3b37");
|
||||
private void OpenContextMenuFolder(SharpIdeFolder folder, TreeItem folderTreeItem)
|
||||
{
|
||||
var menu = new PopupMenu();
|
||||
AddChild(menu);
|
||||
@@ -35,6 +40,7 @@ public partial class SolutionExplorerPanel
|
||||
|
||||
menu.AddItem("Reveal in File Explorer", (int)FolderContextMenuOptions.RevealInFileExplorer);
|
||||
menu.AddItem("Delete", (int)FolderContextMenuOptions.Delete);
|
||||
menu.AddItem("Rename", (int)FolderContextMenuOptions.Rename);
|
||||
menu.PopupHide += () => menu.QueueFree();
|
||||
menu.IdPressed += id =>
|
||||
{
|
||||
@@ -68,6 +74,14 @@ public partial class SolutionExplorerPanel
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (actionId is FolderContextMenuOptions.Rename)
|
||||
{
|
||||
var renameDirectoryDialog = _renameDirectoryDialogScene.Instantiate<RenameDirectoryDialog>();
|
||||
renameDirectoryDialog.Folder = folder;
|
||||
renameDirectoryDialog.FolderTreeItem = folderTreeItem;
|
||||
AddChild(renameDirectoryDialog);
|
||||
renameDirectoryDialog.PopupCentered();
|
||||
}
|
||||
};
|
||||
|
||||
var globalMousePosition = GetGlobalMousePosition();
|
||||
@@ -75,8 +89,6 @@ public partial class SolutionExplorerPanel
|
||||
menu.Popup();
|
||||
}
|
||||
|
||||
private readonly PackedScene _newDirectoryDialogScene = GD.Load<PackedScene>("uid://bgi4u18y8pt4x");
|
||||
private readonly PackedScene _newCsharpFileDialogScene = GD.Load<PackedScene>("uid://chnb7gmcdg0ww");
|
||||
private void OnCreateNewSubmenuPressed(long id, IFolderOrProject folder)
|
||||
{
|
||||
var actionId = (CreateNewSubmenuOptions)id;
|
||||
|
||||
@@ -49,7 +49,7 @@ public partial class SolutionExplorerPanel : MarginContainer
|
||||
case (MouseButtonMask.Left, RefCountedContainer<SharpIdeProjectModel>): break;
|
||||
case (MouseButtonMask.Right, RefCountedContainer<SharpIdeProjectModel> projectContainer): OpenContextMenuProject(projectContainer.Item); break;
|
||||
case (MouseButtonMask.Left, RefCountedContainer<SharpIdeFolder>): break;
|
||||
case (MouseButtonMask.Right, RefCountedContainer<SharpIdeFolder> folderContainer): OpenContextMenuFolder(folderContainer.Item); break;
|
||||
case (MouseButtonMask.Right, RefCountedContainer<SharpIdeFolder> folderContainer): OpenContextMenuFolder(folderContainer.Item, selected); break;
|
||||
case (MouseButtonMask.Left, RefCountedContainer<SharpIdeSolutionFolder>): break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user