-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathFrmSettingsFrame.pas
More file actions
583 lines (500 loc) · 17.1 KB
/
FrmSettingsFrame.pas
File metadata and controls
583 lines (500 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
unit FrmSettingsFrame;
interface
uses
System.Classes,
System.Generics.Collections,
Winapi.Messages,
Vcl.Controls,
Vcl.Forms,
Vcl.StdCtrls,
Vcl.ComCtrls,
Vcl.ExtCtrls,
Vcl.Dialogs,
PluginSettings,
ToolsAPI;
const
WM_REBUILD_CUSTOM_EDITOR_ROWS = WM_APP + 1;
type
TCustomEditorRowControls = class
public
Panel: TPanel;
chkEnabled: TCheckBox;
edDisplayName: TEdit;
hkShortcut: THotKey;
btnClearShortcut: TButton;
btnCustomize: TButton;
btnRemove: TButton;
EditorSettings: TEditorSettings;
destructor Destroy; override;
end;
TFrmSettingsFrame = class(TFrame)
chkVSCodeEnabled: TCheckBox;
lnkVSCodeHome: TLinkLabel;
hkVSCodeShortcut: THotKey;
btnVSCodeClearShortcut: TButton;
btnVSCodeAdvanced: TButton;
chkCursorEnabled: TCheckBox;
lnkCursorHome: TLinkLabel;
hkCursorShortcut: THotKey;
btnCursorClearShortcut: TButton;
btnCursorAdvanced: TButton;
chkWindsurfEnabled: TCheckBox;
lnkWindsurfHome: TLinkLabel;
hkWindsurfShortcut: THotKey;
btnWindsurfClearShortcut: TButton;
btnWindsurfAdvanced: TButton;
chkTraeEnabled: TCheckBox;
lnkTraeHome: TLinkLabel;
hkTraeShortcut: THotKey;
btnTraeClearShortcut: TButton;
btnTraeAdvanced: TButton;
chkVSCodiumEnabled: TCheckBox;
lnkVSCodiumHome: TLinkLabel;
hkVSCodiumShortcut: THotKey;
btnVSCodiumClearShortcut: TButton;
btnVSCodiumAdvanced: TButton;
btnAddCustomEditor: TButton;
lblBuiltInHint: TLabel;
lblCustomEditorsHint: TLabel;
lnkProjectHome: TLinkLabel;
sbCustomEditors: TScrollBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
procedure BuiltInAdvancedSettingsClick(Sender: TObject);
procedure btnAddCustomEditorClick(Sender: TObject);
procedure BuiltInClearShortcutClick(Sender: TObject);
procedure CustomEditorClearShortcutClick(Sender: TObject);
procedure CustomEditorCustomizeClick(Sender: TObject);
procedure CustomEditorRemoveClick(Sender: TObject);
procedure HomePageLinkClick(Sender: TObject; const Link: string; LinkType: TSysLinkType);
private
FBuiltInEditorDrafts: TObjectList<TEditorSettings>;
FCustomEditorDrafts: TObjectList<TEditorSettings>;
FCustomEditorRows: TObjectList<TCustomEditorRowControls>;
FFrameInitialized: Boolean;
FPendingRemovedCustomEditor: TEditorSettings;
FCustomEditorRowsRebuildQueued: Boolean;
function FindBuiltInEditorDraft(const EditorId: string): TEditorSettings;
function FindBuiltInEditor(const EditorId: string): TEditorSettings;
function FindCustomEditorRow(Sender: TObject): TCustomEditorRowControls;
procedure QueueCustomEditorRowsRebuild(RemovedEditor: TEditorSettings = nil);
procedure ApplyCustomEditorDrafts;
procedure CreateBuiltInEditorDrafts;
procedure CreateCustomEditorDrafts;
procedure RebuildCustomEditorRows;
procedure LoadBuiltInEditorControls;
procedure LoadBuiltInEditorControl(const EditorId: string; CheckBox: TCheckBox; HotKey: THotKey);
procedure ApplyBuiltInEditorDrafts;
procedure SaveCustomEditorControls;
procedure SaveBuiltInEditorControls;
procedure SaveBuiltInEditorControl(const EditorId: string; CheckBox: TCheckBox; HotKey: THotKey);
procedure InitializeFrame;
procedure WMRebuildCustomEditorRows(var Message: TMessage); message WM_REBUILD_CUSTOM_EDITOR_ROWS;
public
destructor Destroy; override;
end;
/// <summary>
/// Registers the plugin's settings into the IDE Tools > Options... dialog.
/// The IDE calls GetFrameClass to instantiate TFrmSettingsFrame, then
/// FrameCreated to populate it, and DialogClosed to apply changes.
/// The object is NOT reference-counted (_AddRef/_Release return -1):
/// lifetime is managed explicitly by the plugin via FreeAndNil.
/// </summary>
TEditInVSCodeOptions = class(TInterfacedObject, INTAAddInOptions)
strict private
FFrame: TFrmSettingsFrame;
protected
// IInterface - non-reference-counted: lifetime managed by owner
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
function GetArea: string;
function GetCaption: string;
function GetFrameClass: TCustomFrameClass;
procedure FrameCreated(AFrame: TCustomFrame);
procedure DialogClosed(Accepted: Boolean);
function ValidateContents: Boolean;
function GetHelpContext: Integer;
function IncludeInIDEInsight: Boolean;
end;
implementation
{$R *.dfm}
uses
System.SysUtils,
Vcl.Menus,
Winapi.Windows,
Winapi.ShellAPI,
FrmEditorAdvancedSettings;
{ TFrmSettingsFrame }
destructor TCustomEditorRowControls.Destroy;
begin
if Panel <> nil then
Panel.Parent := nil;
FreeAndNil(Panel);
inherited;
end;
procedure TFrmSettingsFrame.BuiltInAdvancedSettingsClick(Sender: TObject);
var
EditorDraft: TEditorSettings;
begin
SaveBuiltInEditorControls;
SaveCustomEditorControls;
if Sender = btnVSCodeAdvanced then
EditorDraft := FindBuiltInEditorDraft('vscode')
else if Sender = btnCursorAdvanced then
EditorDraft := FindBuiltInEditorDraft('cursor')
else if Sender = btnWindsurfAdvanced then
EditorDraft := FindBuiltInEditorDraft('windsurf')
else if Sender = btnTraeAdvanced then
EditorDraft := FindBuiltInEditorDraft('trae')
else if Sender = btnVSCodiumAdvanced then
EditorDraft := FindBuiltInEditorDraft('vscodium')
else
EditorDraft := nil;
if EditorDraft = nil then
Exit;
TFrmEditorAdvancedSettings.EditEditorSettings(EditorDraft);
end;
procedure TFrmSettingsFrame.btnAddCustomEditorClick(Sender: TObject);
begin
SaveCustomEditorControls;
var DisplayName := 'Custom Editor';
if FCustomEditorDrafts.Count > 0 then
DisplayName := DisplayName + ' ' + IntToStr(FCustomEditorDrafts.Count + 1);
var EditorDraft := TPluginSettings.CreateNewCustomEditor(DisplayName);
FCustomEditorDrafts.Add(EditorDraft);
RebuildCustomEditorRows;
TFrmEditorAdvancedSettings.EditEditorSettings(EditorDraft);
RebuildCustomEditorRows;
end;
procedure TFrmSettingsFrame.BuiltInClearShortcutClick(Sender: TObject);
begin
if Sender = btnVSCodeClearShortcut then
hkVSCodeShortcut.HotKey := 0
else if Sender = btnCursorClearShortcut then
hkCursorShortcut.HotKey := 0
else if Sender = btnWindsurfClearShortcut then
hkWindsurfShortcut.HotKey := 0
else if Sender = btnTraeClearShortcut then
hkTraeShortcut.HotKey := 0
else if Sender = btnVSCodiumClearShortcut then
hkVSCodiumShortcut.HotKey := 0;
end;
procedure TFrmSettingsFrame.CustomEditorClearShortcutClick(Sender: TObject);
begin
var Row := FindCustomEditorRow(Sender);
if Row = nil then
Exit;
Row.hkShortcut.HotKey := 0;
end;
procedure TFrmSettingsFrame.CustomEditorCustomizeClick(Sender: TObject);
begin
SaveCustomEditorControls;
var Row := FindCustomEditorRow(Sender);
if (Row = nil) or (Row.EditorSettings = nil) then
Exit;
TFrmEditorAdvancedSettings.EditEditorSettings(Row.EditorSettings);
end;
procedure TFrmSettingsFrame.CustomEditorRemoveClick(Sender: TObject);
begin
SaveCustomEditorControls;
var Row := FindCustomEditorRow(Sender);
if (Row = nil) or (Row.EditorSettings = nil) then
Exit;
Row.Panel.Visible := False;
Row.Panel.Enabled := False;
QueueCustomEditorRowsRebuild(Row.EditorSettings);
end;
procedure TFrmSettingsFrame.CreateCustomEditorDrafts;
begin
FreeAndNil(FCustomEditorDrafts);
FCustomEditorDrafts := TObjectList<TEditorSettings>.Create(True);
for var Editor in TPluginSettings.CustomEditors do
FCustomEditorDrafts.Add(Editor.Clone);
end;
procedure TFrmSettingsFrame.CreateBuiltInEditorDrafts;
begin
FreeAndNil(FBuiltInEditorDrafts);
FBuiltInEditorDrafts := TObjectList<TEditorSettings>.Create(True);
for var Editor in TPluginSettings.BuiltInEditors do
FBuiltInEditorDrafts.Add(Editor.Clone);
end;
destructor TFrmSettingsFrame.Destroy;
begin
FPendingRemovedCustomEditor := nil;
FCustomEditorRowsRebuildQueued := False;
FCustomEditorRows.Free;
FCustomEditorDrafts.Free;
FBuiltInEditorDrafts.Free;
inherited;
end;
function TFrmSettingsFrame.FindBuiltInEditor(const EditorId: string): TEditorSettings;
begin
Result := nil;
for var Editor in TPluginSettings.BuiltInEditors do
if SameText(Editor.Id, EditorId) then
Exit(Editor);
end;
function TFrmSettingsFrame.FindBuiltInEditorDraft(const EditorId: string): TEditorSettings;
begin
Result := nil;
if FBuiltInEditorDrafts = nil then
Exit;
for var Editor in FBuiltInEditorDrafts do
if SameText(Editor.Id, EditorId) then
Exit(Editor);
end;
function TFrmSettingsFrame.FindCustomEditorRow(Sender: TObject): TCustomEditorRowControls;
begin
Result := nil;
if FCustomEditorRows = nil then
Exit;
for var Row in FCustomEditorRows do begin
if Sender = Row.btnCustomize then
Exit(Row);
if Sender = Row.btnClearShortcut then
Exit(Row);
if Sender = Row.btnRemove then
Exit(Row);
end;
end;
procedure TFrmSettingsFrame.HomePageLinkClick(Sender: TObject; const Link: string; LinkType: TSysLinkType);
begin
if (LinkType <> sltURL) or (Link = '') then
Exit;
ShellExecute(Handle, 'open', PChar(Link), nil, nil, SW_SHOWNORMAL);
end;
procedure TFrmSettingsFrame.LoadBuiltInEditorControl(const EditorId: string; CheckBox: TCheckBox; HotKey: THotKey);
begin
var Editor := FindBuiltInEditorDraft(EditorId);
if Editor = nil then begin
CheckBox.Checked := False;
CheckBox.Enabled := False;
HotKey.HotKey := 0;
HotKey.Enabled := False;
Exit;
end;
CheckBox.Enabled := True;
CheckBox.Checked := Editor.Enabled;
HotKey.Enabled := True;
HotKey.HotKey := Editor.Shortcut;
end;
procedure TFrmSettingsFrame.LoadBuiltInEditorControls;
begin
LoadBuiltInEditorControl('vscode', chkVSCodeEnabled, hkVSCodeShortcut);
LoadBuiltInEditorControl('cursor', chkCursorEnabled, hkCursorShortcut);
LoadBuiltInEditorControl('windsurf', chkWindsurfEnabled, hkWindsurfShortcut);
LoadBuiltInEditorControl('trae', chkTraeEnabled, hkTraeShortcut);
LoadBuiltInEditorControl('vscodium', chkVSCodiumEnabled, hkVSCodiumShortcut);
end;
procedure TFrmSettingsFrame.RebuildCustomEditorRows;
begin
if FCustomEditorRows = nil then
FCustomEditorRows := TObjectList<TCustomEditorRowControls>.Create(True)
else
FCustomEditorRows.Clear;
if FCustomEditorDrafts.Count = 0 then begin
sbCustomEditors.VertScrollBar.Range := 0;
lblCustomEditorsHint.Visible := True;
Exit;
end;
SendMessage(sbCustomEditors.Handle, WM_SETREDRAW, 0, 0);
try
sbCustomEditors.DisableAlign;
var RowTop := 0;
for var EditorDraft in FCustomEditorDrafts do begin
var Row := TCustomEditorRowControls.Create;
Row.EditorSettings := EditorDraft;
Row.Panel := TPanel.Create(nil);
Row.Panel.Parent := sbCustomEditors;
Row.Panel.Left := 0;
Row.Panel.Top := RowTop;
Row.Panel.Width := sbCustomEditors.ClientWidth - 8;
Row.Panel.Height := 38;
Row.Panel.BevelOuter := bvNone;
Row.chkEnabled := TCheckBox.Create(Row.Panel);
Row.chkEnabled.Parent := Row.Panel;
Row.chkEnabled.Left := 8;
Row.chkEnabled.Top := 10;
Row.chkEnabled.Width := 17;
Row.chkEnabled.Caption := '';
Row.chkEnabled.Checked := EditorDraft.Enabled;
Row.edDisplayName := TEdit.Create(Row.Panel);
Row.edDisplayName.Parent := Row.Panel;
Row.edDisplayName.Left := 32;
Row.edDisplayName.Top := 8;
Row.edDisplayName.Width := 210;
Row.edDisplayName.Height := 21;
Row.edDisplayName.Text := EditorDraft.DisplayName;
Row.hkShortcut := THotKey.Create(Row.Panel);
Row.hkShortcut.Parent := Row.Panel;
Row.hkShortcut.Left := 250;
Row.hkShortcut.Top := 8;
Row.hkShortcut.Width := 110;
Row.hkShortcut.Height := 21;
Row.hkShortcut.HotKey := EditorDraft.Shortcut;
Row.btnClearShortcut := TButton.Create(Row.Panel);
Row.btnClearShortcut.Parent := Row.Panel;
Row.btnClearShortcut.Left := 366;
Row.btnClearShortcut.Top := 6;
Row.btnClearShortcut.Width := 44;
Row.btnClearShortcut.Height := 25;
Row.btnClearShortcut.Caption := 'Clear';
Row.btnClearShortcut.OnClick := CustomEditorClearShortcutClick;
Row.btnCustomize := TButton.Create(Row.Panel);
Row.btnCustomize.Parent := Row.Panel;
Row.btnCustomize.Left := 416;
Row.btnCustomize.Top := 6;
Row.btnCustomize.Width := 84;
Row.btnCustomize.Height := 25;
Row.btnCustomize.Caption := 'Customize';
Row.btnCustomize.OnClick := CustomEditorCustomizeClick;
Row.btnRemove := TButton.Create(Row.Panel);
Row.btnRemove.Parent := Row.Panel;
Row.btnRemove.Left := 506;
Row.btnRemove.Top := 6;
Row.btnRemove.Width := 60;
Row.btnRemove.Height := 25;
Row.btnRemove.Caption := 'Remove';
Row.btnRemove.OnClick := CustomEditorRemoveClick;
FCustomEditorRows.Add(Row);
Inc(RowTop, Row.Panel.Height + 4);
end;
sbCustomEditors.VertScrollBar.Range := RowTop;
lblCustomEditorsHint.Visible := False;
finally
sbCustomEditors.EnableAlign;
SendMessage(sbCustomEditors.Handle, WM_SETREDRAW, 1, 0);
RedrawWindow(sbCustomEditors.Handle, nil, 0, RDW_INVALIDATE or RDW_ERASE or RDW_ALLCHILDREN);
end;
end;
procedure TFrmSettingsFrame.InitializeFrame;
begin
if FFrameInitialized then
Exit;
CreateBuiltInEditorDrafts;
CreateCustomEditorDrafts;
LoadBuiltInEditorControls;
RebuildCustomEditorRows;
FFrameInitialized := True;
end;
procedure TFrmSettingsFrame.QueueCustomEditorRowsRebuild(RemovedEditor: TEditorSettings = nil);
begin
if RemovedEditor <> nil then
FPendingRemovedCustomEditor := RemovedEditor;
if FCustomEditorRowsRebuildQueued then
Exit;
FCustomEditorRowsRebuildQueued := True;
PostMessage(Handle, WM_REBUILD_CUSTOM_EDITOR_ROWS, 0, 0);
end;
procedure TFrmSettingsFrame.ApplyBuiltInEditorDrafts;
begin
for var EditorDraft in FBuiltInEditorDrafts do begin
var Editor := FindBuiltInEditor(EditorDraft.Id);
if Editor = nil then
Continue;
Editor.AssignFrom(EditorDraft);
end;
end;
procedure TFrmSettingsFrame.ApplyCustomEditorDrafts;
begin
SaveCustomEditorControls;
TPluginSettings.ReplaceCustomEditors(FCustomEditorDrafts);
end;
procedure TFrmSettingsFrame.SaveCustomEditorControls;
begin
if FCustomEditorRows = nil then
Exit;
for var Row in FCustomEditorRows do begin
if Row.EditorSettings = nil then
Continue;
Row.EditorSettings.Enabled := Row.chkEnabled.Checked;
Row.EditorSettings.Shortcut := Row.hkShortcut.HotKey;
Row.EditorSettings.DisplayName := Trim(Row.edDisplayName.Text);
if Row.EditorSettings.DisplayName = '' then
Row.EditorSettings.DisplayName := 'Custom Editor';
end;
end;
procedure TFrmSettingsFrame.SaveBuiltInEditorControl(const EditorId: string; CheckBox: TCheckBox; HotKey: THotKey);
begin
var Editor := FindBuiltInEditorDraft(EditorId);
if Editor = nil then
Exit;
Editor.Enabled := CheckBox.Checked;
Editor.Shortcut := HotKey.HotKey;
end;
procedure TFrmSettingsFrame.SaveBuiltInEditorControls;
begin
SaveBuiltInEditorControl('vscode', chkVSCodeEnabled, hkVSCodeShortcut);
SaveBuiltInEditorControl('cursor', chkCursorEnabled, hkCursorShortcut);
SaveBuiltInEditorControl('windsurf', chkWindsurfEnabled, hkWindsurfShortcut);
SaveBuiltInEditorControl('trae', chkTraeEnabled, hkTraeShortcut);
SaveBuiltInEditorControl('vscodium', chkVSCodiumEnabled, hkVSCodiumShortcut);
end;
procedure TFrmSettingsFrame.WMRebuildCustomEditorRows(var Message: TMessage);
begin
FCustomEditorRowsRebuildQueued := False;
if (FPendingRemovedCustomEditor <> nil) and (FCustomEditorDrafts <> nil) then begin
var EditorToRemove := FPendingRemovedCustomEditor;
FPendingRemovedCustomEditor := nil;
FCustomEditorDrafts.Remove(EditorToRemove);
end;
RebuildCustomEditorRows;
Message.Result := 0;
end;
function TEditInVSCodeOptions._AddRef: Integer;
begin
Result := -1; // not reference-counted
end;
function TEditInVSCodeOptions._Release: Integer;
begin
Result := -1; // not reference-counted
end;
{ TEditInVSCodeOptions }
function TEditInVSCodeOptions.GetArea: string;
begin
Result := 'Third Party';
end;
function TEditInVSCodeOptions.GetCaption: string;
begin
Result := 'Edit in VS Code';
end;
function TEditInVSCodeOptions.GetFrameClass: TCustomFrameClass;
begin
Result := TFrmSettingsFrame;
end;
procedure TEditInVSCodeOptions.FrameCreated(AFrame: TCustomFrame);
begin
FFrame := AFrame as TFrmSettingsFrame;
FFrame.InitializeFrame;
end;
procedure TEditInVSCodeOptions.DialogClosed(Accepted: Boolean);
begin
if not Accepted then
Exit;
FFrame.SaveBuiltInEditorControls;
FFrame.SaveCustomEditorControls;
FFrame.ApplyBuiltInEditorDrafts;
FFrame.ApplyCustomEditorDrafts;
TPluginSettings.Save;
TPluginSettings.NotifyShortcutChanged;
TPluginSettings.NotifySettingsChanged;
end;
function TEditInVSCodeOptions.ValidateContents: Boolean;
begin
Result := True;
end;
function TEditInVSCodeOptions.GetHelpContext: Integer;
begin
Result := 0;
end;
function TEditInVSCodeOptions.IncludeInIDEInsight: Boolean;
begin
// This page is small and self-contained. Keeping it out of IDE Insight
// avoids extra indexing work when the IDE builds preferences metadata.
Result := False;
end;
end.