|
| 1 | +using Dalamud.Interface.Colors; |
| 2 | +using Dalamud.Interface.Utility.Raii; |
| 3 | +using ECommons.ImGuiMethods; |
| 4 | + |
| 5 | +namespace SomethingNeedDoing.Gui.Modals; |
| 6 | +public static class RenameFolderModal |
| 7 | +{ |
| 8 | + private static Vector2 Size = new(400, 150); |
| 9 | + private static bool IsOpen = false; |
| 10 | + |
| 11 | + private static string _renameFolderBuffer = string.Empty; |
| 12 | + private static string _folderToRename = string.Empty; |
| 13 | + |
| 14 | + public static void Open(string folderPath) |
| 15 | + { |
| 16 | + IsOpen = true; |
| 17 | + _renameFolderBuffer = folderPath; |
| 18 | + _folderToRename = folderPath; |
| 19 | + } |
| 20 | + |
| 21 | + public static void Close() |
| 22 | + { |
| 23 | + IsOpen = false; |
| 24 | + ImGui.CloseCurrentPopup(); |
| 25 | + } |
| 26 | + |
| 27 | + public static void DrawModal() |
| 28 | + { |
| 29 | + if (!IsOpen) return; |
| 30 | + |
| 31 | + ImGui.OpenPopup($"RenameFolderPopup##{nameof(RenameFolderModal)}"); |
| 32 | + |
| 33 | + ImGui.SetNextWindowPos(ImGui.GetMainViewport().GetCenter(), ImGuiCond.Appearing, new Vector2(0.5f, 0.5f)); |
| 34 | + ImGui.SetNextWindowSize(Size); |
| 35 | + |
| 36 | + using var style = ImRaii.PushStyle(ImGuiStyleVar.WindowPadding, new Vector2(15, 15)); |
| 37 | + using var popup = ImRaii.PopupModal($"RenameFolderPopup##{nameof(RenameFolderModal)}", ref IsOpen, ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoTitleBar); |
| 38 | + if (!popup) return; |
| 39 | + |
| 40 | + ImGuiEx.Icon(FontAwesomeHelper.IconRename); |
| 41 | + ImGui.SameLine(); |
| 42 | + ImGui.Text("Rename Folder"); |
| 43 | + ImGui.Separator(); |
| 44 | + ImGui.Spacing(); |
| 45 | + |
| 46 | + ImGui.Text("Enter new folder name:"); |
| 47 | + ImGui.SetNextItemWidth(-1); |
| 48 | + ImGuiUtils.SetFocusIfAppearing(); |
| 49 | + |
| 50 | + var enterPressed = ImGui.IsKeyPressed(ImGuiKey.Enter) && ImGui.IsWindowFocused(); |
| 51 | + |
| 52 | + using (ImRaii.PushColor(ImGuiCol.FrameBg, new Vector4(0.15f, 0.15f, 0.15f, 1.0f))) |
| 53 | + ImGui.InputText("##RenameFolderInput", ref _renameFolderBuffer, 100); |
| 54 | + |
| 55 | + ImGui.Spacing(); |
| 56 | + ImGui.Separator(); |
| 57 | + ImGui.Spacing(); |
| 58 | + |
| 59 | + var invalid = false; |
| 60 | + if (!string.IsNullOrEmpty(_renameFolderBuffer) && C.GetFolderPaths().Any(f => f == _renameFolderBuffer)) |
| 61 | + { |
| 62 | + invalid = true; |
| 63 | + ImGuiEx.Text(ImGuiColors.DalamudRed, $"Folder name '{_renameFolderBuffer}' already exists."); |
| 64 | + } |
| 65 | + |
| 66 | + var confirmed = false; |
| 67 | + using (ImRaii.Disabled(invalid)) |
| 68 | + using (ImRaii.PushColor(ImGuiCol.Button, new Vector4(0.3f, 0.5f, 0.3f, 1.0f)).Push(ImGuiCol.ButtonHovered, new Vector4(0.4f, 0.6f, 0.4f, 1.0f))) |
| 69 | + confirmed = ImGui.Button("Rename", new Vector2(150, 0)) || enterPressed; |
| 70 | + |
| 71 | + if (confirmed && !string.IsNullOrWhiteSpace(_renameFolderBuffer)) |
| 72 | + { |
| 73 | + try |
| 74 | + { |
| 75 | + C.RenameFolder(_folderToRename, _renameFolderBuffer); |
| 76 | + Close(); |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + Svc.Log.Error(ex, "Error renaming folder"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + ImGui.SameLine(); |
| 85 | + |
| 86 | + using (ImRaii.PushColor(ImGuiCol.Button, new Vector4(0.5f, 0.3f, 0.3f, 1.0f)).Push(ImGuiCol.ButtonHovered, new Vector4(0.6f, 0.4f, 0.4f, 1.0f))) |
| 87 | + if (ImGui.Button("Cancel", new Vector2(150, 0)) || (ImGui.IsKeyPressed(ImGuiKey.Escape) && ImGui.IsWindowFocused())) |
| 88 | + Close(); |
| 89 | + } |
| 90 | +} |
0 commit comments