Skip to content

Commit 66d5499

Browse files
committed
resolve #16
1 parent 0cfe104 commit 66d5499

File tree

6 files changed

+42
-88
lines changed

6 files changed

+42
-88
lines changed

SomethingNeedDoing/Config.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,10 @@ public string GetMacroParentFolder(string macroId)
217217
/// <summary>
218218
/// Validates if a macro name is valid for the given folder.
219219
/// </summary>
220-
public bool IsValidMacroName(string name, string folderPath)
220+
public bool IsValidMacroName(string name, string folderPath, string? excludeMacroId = null)
221221
{
222222
if (string.IsNullOrWhiteSpace(name)) return false;
223-
return !GetMacrosInFolder(folderPath)
224-
.Any(m => m.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
223+
return !Macros.Any(m => m.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && m.Id != excludeMacroId);
225224
}
226225

227226
/// <summary>
@@ -328,6 +327,21 @@ public IEnumerable<ConfigMacro> SearchMacros(string searchText, bool caseSensiti
328327
/// </summary>
329328
public IEnumerable<ConfigMacro> GetAllNodes() => Macros;
330329

330+
/// <summary>
331+
/// Gets a unique macro name by appending a number if the base name already exists.
332+
/// </summary>
333+
public string GetUniqueMacroName(string baseName, string? excludeMacroId = null)
334+
{
335+
var name = baseName;
336+
var counter = 1;
337+
while (!IsValidMacroName(name, string.Empty, excludeMacroId))
338+
{
339+
name = $"{baseName} ({counter})";
340+
counter++;
341+
}
342+
return name;
343+
}
344+
331345
/// <summary>
332346
/// Sets a property value by name.
333347
/// </summary>

SomethingNeedDoing/Core/Macros/ConfigMacro.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SomethingNeedDoing.Core;
66
/// </summary>
77
public class ConfigMacro : MacroBase
88
{
9-
public const string Root = "/";
9+
public const string Root = "";
1010

1111
/// <inheritdoc/>
1212
public override string Id { get; } = Guid.NewGuid().ToString();

SomethingNeedDoing/Gui/Modals/CreateFolderModal.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,17 @@ public static void DrawModal()
6363

6464
if (!folderExists && !string.IsNullOrWhiteSpace(_newFolderName))
6565
{
66-
// Create a dummy macro in the folder to ensure it exists
66+
// Create a dummy macro in the folder to ensure it exists (TODO: find a way around this?)
6767
var dummyMacro = new ConfigMacro
6868
{
69-
Name = $"{_newFolderName} Template",
69+
Name = C.GetUniqueMacroName($"{_newFolderName} Template"),
7070
Content = "// Add your macro commands here",
7171
Type = MacroType.Native,
7272
FolderPath = _newFolderName
7373
};
7474

7575
C.Macros.Add(dummyMacro);
76+
C.Save();
7677
Close();
7778
}
7879
}

SomethingNeedDoing/Gui/Modals/CreateMacroModal.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void DrawModal()
6262

6363
ImGuiUtils.CenteredButtons(("Create", () =>
6464
{
65-
var uniqueName = GetUniqueMacroName(_newMacroName);
65+
var uniqueName = C.GetUniqueMacroName(_newMacroName);
6666
var newMacro = new ConfigMacro
6767
{
6868
Name = uniqueName,
@@ -72,20 +72,9 @@ public static void DrawModal()
7272
};
7373

7474
C.Macros.Add(newMacro);
75+
C.Save();
7576
Close();
7677
}
7778
), ("Cancel", Close));
7879
}
79-
80-
private static string GetUniqueMacroName(string baseName)
81-
{
82-
var name = baseName;
83-
var counter = 1;
84-
while (!C.IsValidMacroName(name, ConfigMacro.Root))
85-
{
86-
name = $"{baseName} ({counter})";
87-
counter++;
88-
}
89-
return name;
90-
}
9180
}

SomethingNeedDoing/Gui/Tabs/MacrosTab.cs

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ private class State
2626
public float LeftPanelWidth { get; set; } = UiConstants.DefaultLeftPanelWidth;
2727
public bool IsFolderSectionCollapsed { get; set; }
2828
public HashSet<string> ExpandedFolders { get; } = [];
29-
public DateTime LastCacheUpdate { get; set; }
30-
public Dictionary<string, int> FolderMacroCounts { get; } = [];
3129
}
3230

3331
private readonly State _state = new();
@@ -80,7 +78,6 @@ private void DrawLeftPanel()
8078

8179
private void DrawMacroTree()
8280
{
83-
UpdateMacroCache();
8481
DrawMacroTreeHeader();
8582
if (!_state.IsFolderSectionCollapsed)
8683
DrawFolderTree();
@@ -158,7 +155,7 @@ private void DrawCustomFolders()
158155
private void DrawFolderNode(string folderPath)
159156
{
160157
var isSelected = _state.SelectedFolderId == folderPath;
161-
var folderCount = _state.FolderMacroCounts.GetValueOrDefault(folderPath, 0);
158+
var folderCount = C.GetMacroCount(folderPath);
162159

163160
var flags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.SpanAvailWidth;
164161
if (isSelected) flags |= ImGuiTreeNodeFlags.Selected;
@@ -178,7 +175,6 @@ private void DrawFolderNode(string folderPath)
178175
}
179176

180177
DrawFolderContextMenu(folderPath);
181-
182178
ImGui.Indent(10);
183179
DrawFolderContents(folderPath);
184180
ImGui.Unindent(10);
@@ -226,18 +222,14 @@ private void DrawFolderContextMenu(string folderPath)
226222

227223
if (ImGui.MenuItem("Delete Folder"))
228224
{
229-
if (ShowDeleteFolderConfirmation(folderPath))
225+
try
230226
{
231-
try
232-
{
233-
DeleteFolder(folderPath);
234-
}
235-
catch (Exception ex)
236-
{
237-
Svc.Log.Error(ex, "Error deleting folder");
238-
}
227+
DeleteFolder(folderPath);
228+
}
229+
catch (Exception ex)
230+
{
231+
Svc.Log.Error(ex, "Error deleting folder");
239232
}
240-
ImGui.CloseCurrentPopup();
241233
}
242234
ImGuiEx.Tooltip("Delete this folder and move all macros to root folder");
243235
}
@@ -254,7 +246,7 @@ private void DrawSearchResults()
254246
if (folderPath.Contains(_state.SearchText, StringComparison.OrdinalIgnoreCase))
255247
{
256248
foundAnyFolders = true;
257-
var folderCount = _state.FolderMacroCounts.GetValueOrDefault(folderPath, 0);
249+
var folderCount = C.GetMacroCount(folderPath);
258250
var isSelected = _state.SelectedFolderId == folderPath;
259251

260252
if (ImGui.Selectable($"📁 {folderPath} ({folderCount})", isSelected))
@@ -425,56 +417,6 @@ private void HandleMacroContextMenu(ConfigMacro macro)
425417
}
426418
}
427419

428-
private void UpdateMacroCache()
429-
{
430-
if ((DateTime.Now - _state.LastCacheUpdate).TotalSeconds <= 5) return;
431-
432-
try
433-
{
434-
_state.FolderMacroCounts.Clear();
435-
foreach (var folderPath in C.GetFolderPaths().Where(f => !string.IsNullOrEmpty(f)))
436-
_state.FolderMacroCounts[folderPath] = C.GetMacroCount(folderPath);
437-
438-
_state.LastCacheUpdate = DateTime.Now;
439-
}
440-
catch (Exception ex)
441-
{
442-
Svc.Log.Error(ex, "Error updating macro cache");
443-
}
444-
}
445-
446-
private bool ShowDeleteFolderConfirmation(string folderPath)
447-
{
448-
var macroCount = _state.FolderMacroCounts.GetValueOrDefault(folderPath, 0);
449-
var message = macroCount > 0
450-
? $"Are you sure you want to delete folder '{folderPath}' and move {macroCount} macro(s) to root folder?"
451-
: $"Are you sure you want to delete empty folder '{folderPath}'?";
452-
453-
if (ImGui.GetIO().KeyCtrl)
454-
return true;
455-
456-
var confirmed = false;
457-
ImGui.OpenPopup("Delete Folder");
458-
var isOpen = true;
459-
using var popup = ImRaii.PopupModal("Delete Folder", ref isOpen, ImGuiWindowFlags.AlwaysAutoResize);
460-
if (popup)
461-
{
462-
ImGui.TextWrapped(message);
463-
ImGui.Separator();
464-
465-
if (ImGui.Button("Yes", new Vector2(120, 0)))
466-
{
467-
confirmed = true;
468-
ImGui.CloseCurrentPopup();
469-
}
470-
471-
ImGui.SameLine();
472-
if (ImGui.Button("No", new Vector2(120, 0)))
473-
ImGui.CloseCurrentPopup();
474-
}
475-
return confirmed;
476-
}
477-
478420
private void MoveMacroToFolder(string macroId, string folderPath)
479421
{
480422
if (C.GetMacro(macroId) is ConfigMacro configMacro)

SomethingNeedDoing/Utils/ConfigMacroExtensions.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,23 @@ public static string ContentSansMetadata(this IMacro macro)
2222

2323
public static void Rename(this ConfigMacro macro, string newName)
2424
{
25-
macro.Name = newName;
26-
C.Save();
25+
if (C.IsValidMacroName(newName, string.Empty, macro.Id))
26+
{
27+
macro.Name = newName;
28+
C.Save();
29+
}
30+
else
31+
{
32+
macro.Name = C.GetUniqueMacroName(newName, macro.Id);
33+
C.Save();
34+
}
2735
}
2836

2937
public static void Duplicate(this ConfigMacro macro, string? newName = null)
3038
{
3139
var duplicate = new ConfigMacro
3240
{
33-
Name = newName ?? $"{macro.Name} (Copy)",
41+
Name = C.GetUniqueMacroName(newName ?? $"{macro.Name} (Copy)"),
3442
Type = macro.Type,
3543
Content = macro.Content,
3644
FolderPath = macro.FolderPath

0 commit comments

Comments
 (0)