Skip to content

Commit 53adc32

Browse files
committed
Make Preferences dialogue remember last tab used
Add new Preferences "meta" section to store name of tab that was displayed when dialogue box was last closed. Preferences dialogue now uses that preference to make the last tab current when it is opened.
1 parent 300936f commit 53adc32

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

Src/FmPreferencesDlg.pas

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ TPreferencesDlg = class(TGenericOKDlg, INoPublicConstruct)
3939
/// <summary>Handles event triggered when list box is clicked or changed
4040
/// via keyboard.</summary>
4141
procedure lbPagesClick(Sender: TObject);
42+
procedure FormDestroy(Sender: TObject);
4243
strict private
4344
class var
4445
/// <summary>List of registered page frames</summary>
@@ -76,6 +77,9 @@ TPreferencesDlg = class(TGenericOKDlg, INoPublicConstruct)
7677
/// <remarks>Stores state of tab being closed and restores state of tab
7778
/// being opened.</remarks>
7879
procedure SelectTab(TS: TTabSheet);
80+
/// <summary>Returns index of tab selected when dialogue box was last
81+
/// closed or -1 if no tab recorded or tab doesn't exist.</summary>
82+
function GetLastTabIdx: Integer;
7983
strict protected
8084
/// <summary>Gets the help A-link keyword to be used when help button
8185
/// clicked.</summary>
@@ -170,7 +174,8 @@ implementation
170174

171175
uses
172176
// Project
173-
IntfCommon;
177+
IntfCommon,
178+
UStrUtils;
174179

175180

176181
{$R *.dfm}
@@ -292,14 +297,36 @@ class function TPreferencesDlg.Execute(AOwner: TComponent;
292297
Result := Execute(AOwner, [FrameClass], UpdateUI);
293298
end;
294299

300+
procedure TPreferencesDlg.FormDestroy(Sender: TObject);
301+
begin
302+
// Save current tab
303+
if Assigned(pcMain.ActivePage) then
304+
Preferences.LastTab := MapTabSheetToPage(pcMain.ActivePage).DisplayName;
305+
inherited;
306+
end;
307+
308+
function TPreferencesDlg.GetLastTabIdx: Integer;
309+
var
310+
TabName: string;
311+
ListIdx: Integer;
312+
begin
313+
TabName := Preferences.LastTab;
314+
if TabName = '' then
315+
Exit(-1);
316+
for ListIdx := 0 to Pred(lbPages.Count) do
317+
if StrSameText(TabName, lbPages.Items[ListIdx]) then
318+
Exit(ListIdx);
319+
Result := -1;
320+
end;
321+
295322
function TPreferencesDlg.GetSelectedPage: TPrefsBaseFrame;
296323
begin
297324
Result := MapTabSheetToPage(pcMain.ActivePage);
298325
end;
299326

300327
procedure TPreferencesDlg.InitForm;
301328
var
302-
TabIdx: Integer; // loops thru tabs in page control
329+
TabIdx: Integer; // loops thru tabs in page control
303330
begin
304331
inherited;
305332
// Take local copy of global preferences. This local copy will be updated as
@@ -309,8 +336,10 @@ procedure TPreferencesDlg.InitForm;
309336
// Display and initialise required pages
310337
for TabIdx := 0 to Pred(pcMain.PageCount) do
311338
MapTabSheetToPage(TabIdx).LoadPrefs(fLocalPrefs);
312-
// Select first TabSheet
313-
fCurrentPageIdx := 0;
339+
// Select last use tab sheet (or 1st if last not known)
340+
fCurrentPageIdx := GetLastTabIdx;
341+
if fCurrentPageIdx < 0 then
342+
fCurrentPageIdx := 0;
314343
pcMain.ActivePageIndex := fCurrentPageIdx;
315344
lbPages.ItemIndex := fCurrentPageIdx;
316345
end;

Src/UPreferences.pas

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,23 @@ interface
3737
IPreferences = interface(IInterface)
3838
['{381B9A92-B528-47E1-AC04-90E1FFFDADA7}']
3939

40+
/// <summary>Gets last tab displayed by Preferences dialogue box when it
41+
/// was last closed, or empty string if the tab is not known.
42+
/// </summary>
43+
/// <remarks>This is meta data about the dialogue box itself, not about
44+
/// user preferences.</remarks>
45+
function GetLastTab: string;
46+
/// <summary>Sets last tab displayed by Preferences dialogue box when it
47+
/// was last closed.</summary>
48+
/// <remarks>This is meta data about the dialogue box itself, not about
49+
/// user preferences.</remarks>
50+
procedure SetLastTab(const Value: string);
51+
/// <summary>Last tab displayed by Preferences dialogue box when it was
52+
/// last closed, or empty string if the tab is not known.</summary>
53+
/// <remarks>This is meta data about the dialogue box itself, not about
54+
/// user preferences.</remarks>
55+
property LastTab: string read GetLastTab write SetLastTab;
56+
4057
/// <summary>Gets style of commenting used to describe snippets in
4158
/// generated code.</summary>
4259
function GetSourceCommentStyle: TCommentStyle;
@@ -283,6 +300,7 @@ TPreferences = class(TInterfacedObject,
283300
)
284301
strict protected
285302
var
303+
fLastTab: string;
286304
/// <summary>Default file extension / type used when writing code
287305
/// snippets file.</summary>
288306
fSourceDefaultFileType: TSourceFileType;
@@ -344,6 +362,24 @@ TPreferences = class(TInterfacedObject,
344362
/// <summary>Destroys object instance.</summary>
345363
destructor Destroy; override;
346364

365+
/// <summary>Gets last tab displayed by Preferences dialogue box when it
366+
/// was last closed, or empty string if the tab is not known.
367+
/// </summary>
368+
/// <remarks>
369+
/// <para>This is meta data about the dialogue box itself, not about
370+
/// user preferences.</para>
371+
/// <para>Method of IPreferences.</para>
372+
/// </remarks>
373+
function GetLastTab: string;
374+
/// <summary>Sets last tab displayed by Preferences dialogue box when it
375+
/// was last closed.</summary>
376+
/// <remarks>
377+
/// <para>This is meta data about the dialogue box itself, not about user
378+
/// preferences.</para>
379+
/// <para>Method of IPreferences.</para>
380+
/// </remarks>
381+
procedure SetLastTab(const Value: string);
382+
347383
/// <summary>Gets style of commenting used to describe snippets in
348384
/// generated code.</summary>
349385
/// <remarks>Method of IPreferences.</remarks>
@@ -608,6 +644,7 @@ procedure TPreferences.Assign(const Src: IInterface);
608644
if not Supports(Src, IPreferences, SrcPref) then
609645
raise EBug.Create(ClassName + '.Assign: Src is wrong type');
610646
// Copy the data
647+
Self.fLastTab := SrcPref.LastTab;
611648
Self.fSourceDefaultFileType := SrcPref.SourceDefaultFileType;
612649
Self.fSourceCommentStyle := SrcPref.SourceCommentStyle;
613650
Self.fTruncateSourceComments := SrcPref.TruncateSourceComments;
@@ -671,6 +708,11 @@ function TPreferences.GetHiliteAttrs: IHiliteAttrs;
671708
Result := fHiliteAttrs;
672709
end;
673710

711+
function TPreferences.GetLastTab: string;
712+
begin
713+
Result := fLastTab;
714+
end;
715+
674716
function TPreferences.GetMeasurementUnits: TMeasurementUnits;
675717
begin
676718
Result := fMeasurementUnits;
@@ -768,6 +810,11 @@ procedure TPreferences.SetHiliteAttrs(const Attrs: IHiliteAttrs);
768810
(fHiliteAttrs as IAssignable).Assign(Attrs);
769811
end;
770812

813+
procedure TPreferences.SetLastTab(const Value: string);
814+
begin
815+
fLastTab := Value;
816+
end;
817+
771818
procedure TPreferences.SetMeasurementUnits(const Value: TMeasurementUnits);
772819
begin
773820
fMeasurementUnits := Value;
@@ -854,6 +901,7 @@ function TPreferencesPersist.Clone: IInterface;
854901
Result := TPreferences.Create;
855902
// Copy properties to it
856903
NewPref := Result as IPreferences;
904+
NewPref.LastTab := Self.fLastTab;
857905
NewPref.SourceDefaultFileType := Self.fSourceDefaultFileType;
858906
NewPref.SourceCommentStyle := Self.fSourceCommentStyle;
859907
NewPref.TruncateSourceComments := Self.fTruncateSourceComments;
@@ -886,6 +934,10 @@ constructor TPreferencesPersist.Create;
886934
begin
887935
inherited Create;
888936

937+
// Read meta data section (no sub-section name)
938+
Storage := Settings.ReadSection(ssPreferences);
939+
fLastTab := Storage.GetString('LastTab');
940+
889941
// Read general section
890942
Storage := Settings.ReadSection(ssPreferences, cGeneral);
891943
fMeasurementUnits := TMeasurementUnits(
@@ -969,6 +1021,11 @@ destructor TPreferencesPersist.Destroy;
9691021
var
9701022
Storage: ISettingsSection; // object used to access persistent storage
9711023
begin
1024+
// Wreite meta section (no sub-section name)
1025+
Storage := Settings.EmptySection(ssPreferences);
1026+
Storage.SetString('LastTab', fLastTab);
1027+
Storage.Save;
1028+
9721029
// Write general section
9731030
Storage := Settings.EmptySection(ssPreferences, cGeneral);
9741031
Storage.SetInteger('Units', Ord(fMeasurementUnits));

0 commit comments

Comments
 (0)