-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLightCore.MRU.pas
More file actions
198 lines (149 loc) · 4.98 KB
/
LightCore.MRU.pas
File metadata and controls
198 lines (149 loc) · 4.98 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
UNIT LightCore.MRU;
{=============================================================================================================
2026.01.30
www.GabrielMoraru.com
==============================================================================================================
MRU (Most Recently Used) File List
Maintains a list of recently accessed files, automatically persisted to disk.
- Duplicate files are moved to the top (not added twice)
- Duplicate detection is case-insensitive (Windows behavior)
- List size is capped at MaxItems (default 16)
- OnChanged event fires when list is modified
- Only existing files are added (verified via FileExists)
Usage:
MRU:= TMRUList.Create;
MRU.FileName:= AppDataFolder + 'RecentFiles.mru'; // Loads existing list if file exists
MRU.OnChanged:= UpdateMenu;
MRU.AddToMRU(FullPath); // Add file after user opens it
Also see:
AddFile2TaskbarMRU in LightVcl.Common.Shell.pas
=============================================================================================================}
INTERFACE
USES
System.SysUtils, System.Classes;
TYPE
TMRUList = class(TObject)
private
FMaxItems: Integer;
FIniFile: string;
FChanged: TNotifyEvent;
FList: TStringList;
procedure SetMaxItems(Value: Integer);
procedure SetFileName(Value: string);
procedure TrimToMaxItems;
procedure DoChanged;
public
constructor Create;
destructor Destroy; override;
function AddToMRU(CONST aFileName: string): Boolean;
procedure Clear;
function Count: Integer;
function GetItem(Index: Integer): string;
property FileName: string read FIniFile write SetFileName; { File path for persistence. Setting this loads existing data. }
property MaxItems: Integer read FMaxItems write SetMaxItems; { Maximum entries (default 16). Minimum is 1. }
property OnChanged: TNotifyEvent read FChanged write FChanged; { Fires on list modification }
property Items: TStringList read FList; { Direct access for iteration. Treat as read-only - use AddToMRU/Clear to modify. }
end;
IMPLEMENTATION
{--------------------------------------------------------------------------------------------------
CONSTRUCTOR / DESTRUCTOR
--------------------------------------------------------------------------------------------------}
constructor TMRUList.Create;
begin
inherited Create;
FMaxItems:= 16;
FList:= TStringList.Create;
end;
destructor TMRUList.Destroy;
begin
FreeAndNil(FList);
inherited;
end;
{--------------------------------------------------------------------------------------------------
INTERNAL HELPERS
--------------------------------------------------------------------------------------------------}
procedure TMRUList.DoChanged;
begin
if Assigned(FChanged)
then FChanged(Self);
end;
procedure TMRUList.TrimToMaxItems;
begin
while FList.Count > FMaxItems do
FList.Delete(FList.Count - 1);
end;
{--------------------------------------------------------------------------------------------------
PROPERTIES
--------------------------------------------------------------------------------------------------}
procedure TMRUList.SetFileName(Value: string);
begin
FIniFile:= Value;
if FileExists(FIniFile)
then begin
FList.LoadFromFile(FIniFile);
TrimToMaxItems;
DoChanged;
end;
end;
procedure TMRUList.SetMaxItems(Value: Integer);
VAR
WasChanged: Boolean;
begin
if Value < 1 then Value:= 1;
FMaxItems:= Value;
WasChanged:= FList.Count > FMaxItems;
TrimToMaxItems;
if WasChanged then
begin
if FIniFile <> ''
then FList.SaveToFile(FIniFile);
DoChanged;
end;
end;
{--------------------------------------------------------------------------------------------------
PUBLIC METHODS
--------------------------------------------------------------------------------------------------}
{ Adds a file to the MRU list (at the top).
If the file already exists in the list, it's moved to the top.
Returns True if the file was already in the list (moved), False if newly added. }
function TMRUList.AddToMRU(CONST aFileName: string): Boolean;
VAR i: Integer;
begin
Result:= FALSE;
if NOT FileExists(aFileName) then EXIT;
{ Insert at top }
FList.Insert(0, aFileName);
{ Remove any duplicates (case-insensitive comparison) }
for i:= FList.Count - 1 downto 1 do
begin
if SameText(FList[i], aFileName)
then begin
FList.Delete(i);
Result:= TRUE; { Was already in list }
end;
end;
{ Enforce max items limit }
TrimToMaxItems;
{ Persist to file }
if FIniFile <> ''
then FList.SaveToFile(FIniFile);
DoChanged;
end;
procedure TMRUList.Clear;
begin
FList.Clear;
if FIniFile <> ''
then FList.SaveToFile(FIniFile);
DoChanged;
end;
function TMRUList.Count: Integer;
begin
Result:= FList.Count;
end;
function TMRUList.GetItem(Index: Integer): string;
begin
if (Index >= 0) AND (Index < FList.Count)
then Result:= FList[Index]
else Result:= '';
end;
end.