Skip to content

Commit ceb23a9

Browse files
Issue #103:
We auto generate IDs for new commands now. The ID that is auto-generated should be the same one that would be used if the compiler would auto generate it. Also added menu item "Auto generate IDs for all commands". The benefit of explicitly setting IDs is, that items in the quickstart menu can be persisted by their ID now, without having to worry that the ID might change (e.g. because the list was re-sorted)
1 parent 66d03fd commit ceb23a9

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

Designer/Bin/RibbonDesigner.exe

1.5 KB
Binary file not shown.

Designer/FCommands.pas

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ TFrameCommands = class(TFrame)
171171
procedure ClearDocument;
172172
procedure RefreshSelection;
173173
procedure ShowDocument(const Document: TRibbonDocument);
174+
function FindSmallestUnusedID(const pMinId: Integer = 2): Integer;
174175
end;
175176

176177
const
@@ -185,8 +186,10 @@ implementation
185186

186187
uses
187188
UITypes,
188-
FMain,
189-
System.Generics.Collections, FCommandsSearch;
189+
FMain,
190+
System.Generics.Collections,
191+
FCommandsSearch,
192+
System.Math;
190193

191194
{ TFrameCommands }
192195

@@ -200,6 +203,7 @@ procedure TFrameCommands.ActionAddCommandExecute(Sender: TObject);
200203
ListViewCommands.Selected.Focused := True;
201204
ListViewCommands.Selected.MakeVisible(False);
202205
EditName.SetFocus;
206+
BtnGenerateIDClick(Sender);
203207
Modified;
204208
end;
205209

@@ -304,16 +308,54 @@ constructor TFrameCommands.Create(AOwner: TComponent);
304308
FFrameLargeHCImages.Parent := PanelLargeHCImages;
305309
end;
306310

311+
function TFrameCommands.FindSmallestUnusedID(const pMinId: Integer = 2): Integer;
312+
var
313+
lCommand: TRibbonCommand;
314+
lIDs: TDictionary<Integer, TRibbonCommand>;
315+
I: Integer;
316+
const
317+
cMaxValidID = 59999;
318+
319+
begin
320+
lIDs := TDictionary<Integer, TRibbonCommand>.Create();
321+
322+
try
323+
// Gather all IDs that are already taken in a dictionary
324+
for lCommand in FDocument.Application.Commands do
325+
if lCommand.Id > 0 then
326+
lIDs.Add(lCommand.Id, lCommand);
327+
328+
// Iterate all allowed IDs, starting with the smallest. Return the first one that hasn't been used yet
329+
for I := pMinId to cMaxValidID do
330+
if not lIDs.ContainsKey(I) then
331+
Exit(I);
332+
333+
raise ERangeError.Create('No valid, unused ID could be found within the range between ' + pMinId.ToString + ' and ' + cMaxValidID.ToString);
334+
finally
335+
lIDs.Free;
336+
end;
337+
end;
338+
307339
procedure TFrameCommands.BtnGenerateIDClick(Sender: TObject);
308340
var
309341
command: TRibbonCommand;
310342
highID : integer;
343+
lMinID: Integer;
311344
begin
312345
highID := 1;
313346
for command in FDocument.Application.Commands do
314347
if command.Id > highID then
315348
highID := command.Id;
316-
EditId.Text := IntToStr(highID + 1);
349+
350+
lMinID := 0;
351+
352+
if Assigned(ListViewCommands.Selected) then
353+
lMinID := ListViewCommands.Selected.Index;
354+
355+
lMinID := lMinID + 2;
356+
357+
// By using at least the index of the item, we mimic the behavior of the ribbon compiler's ID auto generation as closely as possible.
358+
EditId.Text := FindSmallestUnusedID(lMinID).ToString;
317359
end;
318360

319361
procedure TFrameCommands.Deactivate;

Designer/FMain.dfm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ object FormMain: TFormMain
146146
ShortCut = 16462
147147
OnExecute = ActionNewExecute
148148
end
149+
object ActionGenerateCommandIDs: TAction
150+
Category = 'Project'
151+
Caption = 'Auto generate IDs for all commands'
152+
Hint = 'Generates and sets IDs for all commands in this markup.'
153+
OnExecute = ActionGenerateCommandIDsExecute
154+
end
149155
object ActionSaveAs: TAction
150156
Category = 'File'
151157
Caption = 'Save As'
@@ -700,6 +706,9 @@ object FormMain: TFormMain
700706
object AutogenerateIDsforallresources1: TMenuItem
701707
Action = ActionGenerateResourceIDs
702708
end
709+
object AutogenerateIDsforallcommands1: TMenuItem
710+
Action = ActionGenerateCommandIDs
711+
end
703712
object Setresourcename1: TMenuItem
704713
Action = ActionSetResourceName
705714
end

Designer/FMain.pas

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ TFormMain = class(TForm)
8888
ActionSetResourceName: TAction;
8989
Setresourcename1: TMenuItem;
9090
ActionGenerateResourceIDs: TAction;
91+
AutogenerateIDsforallcommands1: TMenuItem;
92+
ActionGenerateCommandIDs: TAction;
9193
AutogenerateIDsforallresources1: TMenuItem;
9294
procedure FormActivate(Sender: TObject);
9395
procedure ActionPreviewExecute(Sender: TObject);
@@ -109,6 +111,7 @@ TFormMain = class(TForm)
109111
procedure ActionMSDNExecute(Sender: TObject);
110112
procedure ActionSetResourceNameExecute(Sender: TObject);
111113
procedure ActionGenerateResourceIDsExecute(Sender: TObject);
114+
procedure ActionGenerateCommandIDsExecute(Sender: TObject);
112115
private
113116
{ Private declarations }
114117
FInitialized: Boolean;
@@ -166,7 +169,7 @@ TFormMain = class(TForm)
166169
implementation
167170

168171
uses
169-
UITypes, System.Win.Registry, UIRibbonUtils;
172+
UITypes, System.Win.Registry, UIRibbonUtils, System.Math;
170173

171174
{$R *.dfm}
172175

@@ -184,6 +187,22 @@ procedure TFormMain.ActionExitExecute(Sender: TObject);
184187
Close;
185188
end;
186189

190+
procedure TFormMain.ActionGenerateCommandIDsExecute(Sender: TObject);
191+
var
192+
lCommand: TRibbonCommand;
193+
I: Integer;
194+
begin
195+
for I := 0 to FFrameCommands.ListViewCommands.Items.Count - 1 do
196+
begin
197+
lCommand := FFrameCommands.ListViewCommands.Items[i].Data;
198+
if lCommand.ID = 0 then
199+
// Try to mimic the auto ID generation of the ribbon compiler.
200+
lCommand.ID := FFrameCommands.FindSmallestUnusedID(I + 2)
201+
end;
202+
203+
FFrameCommands.RefreshSelection;
204+
end;
205+
187206
procedure TFormMain.ActionGenerateResourceIDsExecute(Sender: TObject);
188207

189208
var

0 commit comments

Comments
 (0)