-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathDN.PackageOverview.pas
358 lines (325 loc) · 10.5 KB
/
DN.PackageOverview.pas
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
{
#########################################################
# Copyright by Alexander Benikowski #
# This unit is part of the Delphinus project hosted on #
# https://github.com/Memnarch/Delphinus #
#########################################################
}
unit DN.PackageOverview;
interface
uses
Classes,
Types,
Messages,
Windows,
Graphics,
Controls,
Forms,
Generics.Collections,
ImgList,
DN.Package.Intf,
DN.Preview,
DN.PackageFilter,
DN.Version;
type
TCheckIsPackageInstalled = reference to function(const APackage: IDNPackage): TDNVersion;
TPackageEvent = reference to procedure(const APackage: IDNPackage);
TPackageOverView = class(TScrollBox)
private
FPreviews: TList<TPreview>;
FUnusedPreviews: TObjectList<TPreview>;
FPackages: TList<IDNPackage>;
FSelectedPackage: IDNPackage;
FOnSelectedPackageChanged: TNotifyEvent;
FOnCheckIsPackageInstalled: TCheckIsPackageInstalled;
FOnCheckHasPackageUpdate: TCheckIsPackageInstalled;
FOnInstallPackage: TPackageEvent;
FOnUninstallPackage: TPackageEvent;
FOnUpdatePackage: TPackageEvent;
FColumns: Integer;
FOnFilter: TPackageFilter;
FOSIcons: TImageList;
FButtonIcons: TImageList;
FRemovedSelectedPackage: TGUID;
FDummyPic: TGraphic;
procedure HandlePackagesChanged(Sender: TObject; const Item: IDNPackage; Action: TCollectionNotification);
procedure AddPreview(const APackage: IDNPackage);
procedure RemovePreview(const APackage: IDNPackage);
procedure HandlePreviewClicked(Sender: TObject);
procedure ChangeSelectedPackage(const APackage: IDNPackage);
function GetPreviewForPackage(const APackage: IDNPackage): TPreview;
function GetInstalledVersion(const APackage: IDNPackage): TDNVersion;
function GetUpdateVersion(const APackage: IDNPackage): TDNVersion;
procedure InstallPackage(const APackage: IDNPackage);
procedure UninstallPackage(const APackage: IDNPackage);
procedure UpdatePackage(const APackage: IDNPackage);
procedure LoadIcons;
protected
procedure Resize; override;
procedure UpdateElements(AColumns: Integer);
procedure SetPreviewPosition(APreview: TPreview; AIndex: Integer; AColumns: Integer);
procedure ClearPreviews;
function IsAccepted(const APackage: IDNPackage): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
procedure Clear();
procedure Refresh();
procedure ApplyFilter;
property Packages: TList<IDNPackage> read FPackages;
property DummyPic: TGraphic read FDummyPic write FDummyPic;
property SelectedPackage: IDNPackage read FSelectedPackage;
property OnSelectedPackageChanged: TNotifyEvent read FOnSelectedPackageChanged write FOnSelectedPackageChanged;
property OnCheckIsPackageInstalled: TCheckIsPackageInstalled read FOnCheckIsPackageInstalled write FOnCheckIsPackageInstalled;
property OnCheckHasPackageUpdate: TCheckIsPackageInstalled read FOnCheckHasPackageUpdate write FOnCheckHasPackageUpdate;
property OnInstallPackage: TPackageEvent read FOnInstallPackage write FOnInstallPackage;
property OnUninstallPackage: TPackageEvent read FOnUninstallPackage write FOnUninstallPackage;
property OnUpdatePackage: TPackageEvent read FOnUpdatePackage write FOnUpdatePackage;
property OnFilter: TPackageFilter read FOnFilter write FOnFilter;
end;
implementation
{ TPackageOverView }
uses
Delphinus.Resources.Names,
Delphinus.Resources;
const
CColumns = 1;
CSpace = 0;
procedure TPackageOverView.AddPreview(const APackage: IDNPackage);
var
LPreview: TPreview;
begin
if IsAccepted(APackage) then
begin
if FUnusedPreviews.Count > 0 then
LPreview := FUnusedPreviews.Extract(FUnusedPreviews[0])
else
LPreview := TPreview.Create(nil, FOSIcons, FButtonIcons);
LPreview.DummyPic := FDummyPic;
LPreview.Package := APackage;
LPreview.Parent := Self;
SetPreviewPosition(LPreview, FPreviews.Count, FColumns);
LPreview.InstalledVersion := GetInstalledVersion(APackage);
LPreview.UpdateVersion := GetUpdateVersion(APackage);
LPreview.OnClick := HandlePreviewClicked;
LPreview.OnInstall := procedure(Sender: TObject) begin InstallPackage(TPreview(Sender).Package) end;
LPreview.OnUninstall := procedure(Sender: TObject) begin UninstallPackage(TPreview(Sender).Package) end;
LPreview.OnUpdate := procedure(Sender: TObject) begin UpdatePackage(TPreview(Sender).Package) end;
FPreviews.Add(LPreview);
if (not Assigned(SelectedPackage)) and (FRemovedSelectedPackage = APackage.ID) then
ChangeSelectedPackage(APackage);
end;
end;
procedure TPackageOverView.Clear;
begin
FPackages.Clear();
end;
procedure TPackageOverView.ClearPreviews;
var
LPreview: TPreview;
begin
for LPreview in FPreviews do
begin
LPreview.Parent := nil;
LPreview.Package := nil;
end;
FUnusedPreviews.AddRange(FPreviews);
FPreviews.Clear;
end;
constructor TPackageOverView.Create(AOwner: TComponent);
begin
inherited;
FPreviews := TList<TPreview>.Create();
FUnusedPreviews := TObjectList<TPreview>.Create(True);
FPackages := TList<IDNPackage>.Create();
FPackages.OnNotify := HandlePackagesChanged;
BorderStyle := bsNone;
VertScrollBar.Smooth := True;
VertScrollBar.Tracking := True;
VertScrollBar.Visible := True;
FColumns := CColumns;
Self.ControlStyle := Self.ControlStyle + [csOpaque];
HorzScrollBar.Visible := False;
FOSIcons := TImageList.Create(Self);
FOSIcons.Width := 32;
FOSIcons.Height := 32;
FOSIcons.ColorDepth := cd32Bit;
FButtonIcons := TImageList.Create(Self);
FButtonIcons.Width := 32;
FButtonIcons.Height := 32;
FButtonIcons.ColorDepth := cd32Bit;
LoadIcons();
ParentColor := False;
Color := clWindow;
end;
destructor TPackageOverView.Destroy;
begin
FPreviews.Free();
FUnusedPreviews.Free;
FPackages.Free();
inherited;
end;
function TPackageOverView.GetPreviewForPackage(
const APackage: IDNPackage): TPreview;
var
LPreview: TPreview;
begin
Result := nil;
for LPreview in FPreviews do
begin
if LPreview.Package = APackage then
begin
Result := LPreview;
Break;
end;
end;
end;
procedure TPackageOverView.HandlePackagesChanged(Sender: TObject;
const Item: IDNPackage; Action: TCollectionNotification);
begin
case Action of
cnAdded: AddPreview(Item);
cnRemoved, cnExtracted: RemovePreview(Item);
end;
end;
procedure TPackageOverView.HandlePreviewClicked(Sender: TObject);
begin
ChangeSelectedPackage((Sender as TPreview).Package);
end;
function TPackageOverView.GetUpdateVersion(const APackage: IDNPackage): TDNVersion;
begin
if Assigned(FOnCheckHasPackageUpdate) then
begin
Result := FOnCheckHasPackageUpdate(APackage);
end
else
begin
Result := TDNVersion.Create();
end;
end;
procedure TPackageOverView.InstallPackage(const APackage: IDNPackage);
begin
if Assigned(FOnInstallPackage) then
FOnInstallPackage(APackage);
end;
function TPackageOverView.IsAccepted(const APackage: IDNPackage): Boolean;
begin
Result := True;
if Assigned(FOnFilter) then
FOnFilter(APackage, Result);
end;
procedure TPackageOverView.LoadIcons;
begin
AddIconToImageList(FOSIcons, Ico_Windows);
AddIconToImageList(FOSIcons, Ico_Mac);
AddIconToImageList(FOSIcons, Ico_Android);
AddIconToImageList(FOSIcons, Ico_IOS);
AddIconToImageList(FOSIcons, Ico_Linux);
AddIconToImageList(FButtonIcons, Ico_Install);
AddIconToImageList(FButtonIcons, Ico_Delete);
AddIconToImageList(FButtonIcons, Ico_Update);
end;
function TPackageOverView.GetInstalledVersion(
const APackage: IDNPackage): TDNVersion;
begin
if Assigned(FOnCheckIsPackageInstalled) then
begin
Result := FOnCheckIsPackageInstalled(APackage);
end
else
begin
Result := TDNVersion.Create();
end;
end;
procedure TPackageOverView.Refresh;
var
LPreview: TPreview;
begin
for LPreview in FPreviews do
begin
LPreview.InstalledVersion := GetInstalledVersion(LPreview.Package);
LPreview.UpdateVersion := GetUpdateVersion(LPreview.Package);
end;
end;
procedure TPackageOverView.RemovePreview(const APackage: IDNPackage);
var
i: Integer;
begin
for i := FPreviews.Count - 1 downto 0 do
begin
if FPreviews[i].Package = APackage then
begin
FPreviews[i].Parent := nil;
FPreviews[i].Package := nil;
if FPreviews[i].Selected then
begin
FRemovedSelectedPackage := APackage.ID;
FPreviews[i].Selected := False;
end;
FUnusedPreviews.Add(FPreviews.Extract(FPreviews[i]));
Break;
end;
end;
if FSelectedPackage = APackage then
ChangeSelectedPackage(nil);
end;
procedure TPackageOverView.Resize;
begin
inherited;
UpdateElements(FColumns);
end;
procedure TPackageOverView.SetPreviewPosition(APreview: TPreview;
AIndex: Integer; AColumns: Integer);
begin
APreview.Top := (AIndex div AColumns) * (APreview.Height + CSpace) - VertScrollBar.Position;
APreview.Left := (AIndex mod AColumns) * (APreview.Width + CSpace);
APreview.Width := ClientWidth;
end;
procedure TPackageOverView.UninstallPackage(const APackage: IDNPackage);
begin
if Assigned(FOnUninstallPackage) then
FOnUninstallPackage(APackage);
end;
procedure TPackageOverView.UpdateElements(AColumns: Integer);
var
i: Integer;
begin
for i := 0 to FPreviews.Count - 1 do
SetPreviewPosition(FPreviews[i], i, AColumns);
end;
procedure TPackageOverView.UpdatePackage(const APackage: IDNPackage);
begin
if Assigned(FOnUpdatePackage) then
FOnUpdatePackage(APackage);
end;
procedure TPackageOverView.ApplyFilter;
var
LPackage: IDNPackage;
begin
ClearPreviews();
for LPackage in FPackages do
begin
AddPreview(LPackage);
end;
end;
procedure TPackageOverView.ChangeSelectedPackage;
var
LPreview: TPreview;
begin
if Assigned(FSelectedPackage) then
begin
LPreview := GetPreviewForPackage(FSelectedPackage);
if Assigned(LPreview) then
LPreview.Selected := False;
end;
FSelectedPackage := APackage;
if Assigned(FSelectedPackage) then
begin
LPreview := GetPreviewForPackage(FSelectedPackage);
if Assigned(LPreview) then
LPreview.Selected := True;
end;
if Assigned(FOnSelectedPackageChanged) then
FOnSelectedPackageChanged(Self);
end;
end.