-
Notifications
You must be signed in to change notification settings - Fork 64
/
DN.Preview.pas
347 lines (309 loc) · 9.25 KB
/
DN.Preview.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
{
#########################################################
# Copyright by Alexander Benikowski #
# This unit is part of the Delphinus project hosted on #
# https://github.com/Memnarch/Delphinus #
#########################################################
}
unit DN.Preview;
interface
uses
Classes,
Types,
Windows,
Messages,
Delphinus.UITypes,
Controls,
Graphics,
Math,
DN.Package.Intf,
DN.Version,
ImgList,
StdCtrls;
type
TNotifyEvent = reference to procedure(Sender: TObject);
TPreview = class(TCustomControl)
private
FTarget: TBitmap;
FPackage: IDNPackage;
FSelected: Boolean;
FButton: TButton;
FUpdateButton: TButton;
FInstalledVersion: TDNVersion;
FUpdateVersion: TDNVersion;
FOnUpdate: TNotifyEvent;
FOnInstall: TNotifyEvent;
FOnUninstall: TNotifyEvent;
FOSImages: TImageList;
FDummyPic: TGraphic;
procedure SetSelected(const Value: Boolean);
procedure SetPackage(const Value: IDNPackage);
procedure SetInstalledVersion(const Value: TDNVersion);
procedure SetUpdateVersion(const Value: TDNVersion);
procedure DoInstall();
procedure DoUninstall();
procedure DoUpdate();
procedure HandleButtonClick(Sender: TObject);
procedure DownSample;
procedure UpdateControls;
procedure PaintOsImages;
protected
procedure Paint; override;
procedure SetupControls(AImages: TImageList);
procedure Resize; override;
public
constructor Create(AOwner: TComponent; AOsImages: TImageList; AButtonImages: TImageList); reintroduce;
destructor Destroy(); override;
property Package: IDNPackage read FPackage write SetPackage;
property DummyPic: TGraphic read FDummyPic write FDummyPic;
property Selected: Boolean read FSelected write SetSelected;
property InstalledVersion: TDNVersion read FInstalledVersion write SetInstalledVersion;
property UpdateVersion: TDNVersion read FUpdateVersion write SetUpdateVersion;
property OnClick;
property OnInstall: TNotifyEvent read FOnInstall write FOnInstall;
property OnUninstall: TNotifyEvent read FOnUninstall write FOnUninstall;
property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
end;
const
CPreviewWidth = 256;
CPreviewImageSize = 80;
CPreviewHeight = CPreviewImageSize;
CButtonHeight = 21;
CButtonWidth = 100;
CPadding = 3;
CMargin = 3;
CLeftMargin = CPadding + CMargin*2 + CPreviewImageSize;
implementation
uses
DN.Graphics,
DN.Types;
{ TPreview }
constructor TPreview.Create(AOwner: TComponent; AOsImages: TImageList; AButtonImages: TImageList);
begin
inherited Create(AOwner);
FOSImages := AOsImages;
FTarget := TBitmap.Create();
FTarget.PixelFormat := pf32bit;
Width := CPreviewWidth;
Height := CPreviewHeight + CPadding * 2;
ShowHint := True;
SetupControls(AButtonImages);
end;
destructor TPreview.Destroy;
begin
FTarget.Free();
inherited;
end;
procedure TPreview.DoInstall;
begin
if Assigned(FOnInstall) then
FOnInstall(Self);
end;
procedure TPreview.DoUninstall;
begin
if Assigned(FOnUninstall) then
FOnUninstall(Self);
end;
procedure TPreview.DoUpdate;
begin
if Assigned(FOnUpdate) then
FOnUpdate(Self);
end;
procedure TPreview.DownSample;
var
LTemp: TBitmap;
LOldMode: Integer;
begin
LTemp := TBitmap.Create();
try
LTemp.PixelFormat := pf32bit;
if Assigned(FPackage.Picture.Graphic) then
begin
LTemp.SetSize(FPackage.Picture.Width, FPackage.Picture.Height);
LTemp.Canvas.Brush.Color := clWhite;
LTemp.Canvas.FillRect(LTemp.Canvas.ClipRect);
LTemp.Canvas.Draw(0, 0, FPackage.Picture.Graphic);
end
else if Assigned(FDummyPic) then
begin
LTemp.SetSize(FDummyPic.Width, FDummyPic.Height);
LTemp.Canvas.Brush.Color := clWhite;
LTemp.Canvas.FillRect(LTemp.Canvas.ClipRect);
LTemp.Canvas.Draw(0, 0, FDummyPic);
end;
FTarget.SetSize(CPreviewImageSize, CPreviewImageSize);
FTarget.Canvas.FillRect(FTarget.Canvas.ClipRect);
LOldMode := GetStretchBltMode(FTarget.Handle);
SetStretchBltMode(FTarget.Canvas.Handle, HALFTONE);
StretchBlt(FTarget.Canvas.Handle, 0, 0, FTarget.Width, FTarget.Height,
LTemp.Canvas.Handle, 0, 0, LTemp.Width, LTemp.Height, SRCCOPY);
SetStretchBltMode(FTarget.Canvas.Handle, LOldMode);
finally
LTemp.Free;
end;
end;
procedure TPreview.HandleButtonClick(Sender: TObject);
begin
if Sender = FButton then
begin
if not InstalledVersion.IsEmpty then
begin
DoUninstall();
end
else
begin
DoInstall();
end;
end
else
begin
if Sender = FUpdateButton then
DoUpdate()
end;
end;
procedure TPreview.Paint;
var
LVersionString, LDescription, LLicenseType: string;
LRect: TRect;
begin
inherited;
if Assigned(FPackage) then
begin
Canvas.Brush.Style := bsSolid;
if Selected then
Canvas.Brush.Color := clSkyBlue
else
Canvas.Brush.Color := clWindow;
Canvas.FillRect(Canvas.ClipRect);
Canvas.Brush.Style := bsClear;
Canvas.Draw(CPadding, CPadding, FTarget);
Canvas.Font.Style := [TFontStyle.fsBold];
Canvas.Font.Color := clWindowText;
Canvas.TextOut(CLeftMargin, CMargin, FPackage.Name);
Canvas.Font.Style := [];
Canvas.Font.Color := clGrayText;
Canvas.TextOut(CLeftMargin, (CMargin + Abs(Canvas.Font.Height)), FPackage.Author);
LLicenseType := FPackage.LicenseTypes;
if LLicenseType = '' then
LLicenseType := 'No license';
Canvas.TextOut(CLeftMargin, (CMargin + Abs(Canvas.Font.Height))*2, LLicenseType);
Canvas.Font.Style := [];
if not InstalledVersion.IsEmpty then
begin
LVersionString := InstalledVersion.ToString;
if not UpdateVersion.IsEmpty then
begin
LVersionString := LVersionString + ' -> ' + UpdateVersion.ToString;
end;
Canvas.TextOut(CLeftMargin, (CMargin + Abs(Canvas.Font.Height))*3, LVersionString);
end;
LDescription := FPackage.Description;
LRect.Left := CLeftMargin;
LRect.Top := (CMargin + Abs(Canvas.Font.Height))*4;
LRect.Right := Width - CMargin - FOSImages.Width*3 - (Width - FButton.Left);
LRect.Bottom := Height - CPadding;
Canvas.TextRect(LRect, LDescription, [tfWordBreak, tfEndEllipsis]);
Canvas.Pen.Color := clBtnFace;
Canvas.MoveTo(0, Height - 1);
Canvas.LineTo(Width, Height - 1);
PaintOsImages();
end;
end;
procedure TPreview.PaintOsImages;
var
LOffset: Integer;
LTopOffset: Integer;
begin
LOffset := FButton.Left - FOSImages.Width - CMargin;
LTopOffset := Height - FOSImages.Height - CMargin;
if cpLinux64 in FPackage.Platforms then
begin
FOSImages.Draw(Canvas, LOffset, LTopOffset, 4);
Dec(LOffset, FOSImages.Width);
end;
if ([cpIOSDevice32, cpIOSDevice64] * FPackage.Platforms) <> [] then
begin
FOSImages.Draw(Canvas, LOffset, LTopOffset, 3);
Dec(LOffset, FOSImages.Width);
end;
if cpAndroid in FPackage.Platforms then
begin
FOSImages.Draw(Canvas, LOffset, LTopOffset, 2);
Dec(LOffset, FOSImages.Width);
end;
if cpOSX32 in FPackage.Platforms then
begin
FOSImages.Draw(Canvas, LOffset, LTopOffset, 1);
Dec(LOffset, FOSImages.Width);
end;
if ([cpWin32, cpWin64] * FPackage.Platforms) <> [] then
begin
FOSImages.Draw(Canvas, LOffset, LTopOffset, 0);
end;
end;
procedure TPreview.Resize;
begin
inherited;
UpdateControls();
end;
procedure TPreview.SetUpdateVersion(const Value: TDNVersion);
begin
FUpdateVersion := Value;
FUpdateButton.Visible := not FUpdateVersion.IsEmpty;
UpdateControls();
InvalidateRect(Handle, Rect(Left, Top, Width, Height), False);
end;
procedure TPreview.UpdateControls;
begin
FButton.Left := Width - FButton.Width - CMargin;
FUpdateButton.Left := Width - FUpdateButton.Width - CMargin;
end;
procedure TPreview.SetInstalledVersion(const Value: TDNVersion);
begin
FInstalledVersion := Value;
if not FInstalledVersion.IsEmpty then
begin
FButton.Hint := 'Uninstall';
FButton.ImageIndex := 1;
end
else
begin
FButton.Hint := 'Install';
FButton.ImageIndex := 0;
end;
end;
procedure TPreview.SetPackage(const Value: IDNPackage);
begin
FPackage := Value;
if Assigned(FPackage) then
DownSample();
end;
procedure TPreview.SetSelected(const Value: Boolean);
begin
if FSelected <> Value then
begin
FSelected := Value;
Invalidate;
end;
end;
procedure TPreview.SetupControls(AImages: TImageList);
begin
FButton := TButton.Create(Self);
FButton.Width := 38;
FButton.Height := 38;
FButton.Top := Height - FButton.Height - CMargin;
FButton.OnClick := HandleButtonClick;
FButton.Images := AImages;
FButton.Parent := Self;
FUpdateButton := TButton.Create(Self);
FUpdateButton.Width := 38;
FUpdateButton.Height := 38;
FUpdateButton.Top := Height - FUpdateButton.Height*2 - CMargin*2;
FUpdateButton.Visible := False;
FUpdateButton.Hint := 'Update';
FUpdateButton.Images := AImages;
FUpdateButton.ImageIndex := 2;
FUpdateButton.OnClick := HandleButtonClick;
FUpdateButton.Parent := Self;
end;
end.