-
Notifications
You must be signed in to change notification settings - Fork 0
/
UfrmMain.pas
392 lines (335 loc) · 10.2 KB
/
UfrmMain.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
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
unit UfrmMain;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
Menus, JSONPropStorage, IDEWindowIntf, Types, fgl;
type
TGitmoji = class(TCollectionItem)
private
FName: UTF8String;
FDescription: UTF8String;
FCode: UTF8String;
FEmoji: UTF8String;
published
property name: UTF8String read FName write FName;
property description: UTF8String read FDescription write FDescription;
property code: UTF8String read FCode write FCode;
property emoji: UTF8String read FEmoji write FEmoji;
end;
{ TGitmojiData }
TGitmojiData = class(TPersistent)
private
FGitmojis: TCollection;
published
property gitmojis: TCollection read FGitmojis;
public
constructor Create;
destructor Destroy; override;
end;
TTagData = array of record
Tags: array of string;
Gitmoji: TGitmoji;
end;
{ TfrmMain }
TfrmMain = class(TForm)
ApplicationProperties1: TApplicationProperties;
cbCopyEmoji: TCheckBox;
edSearch: TEdit;
JSONPropStorage1: TJSONPropStorage;
lbGitmojis: TListBox;
mnuShow: TMenuItem;
mnuExit: TMenuItem;
PopupMenu1: TPopupMenu;
TrayIcon1: TTrayIcon;
procedure ApplicationProperties1Deactivate(Sender: TObject);
procedure edSearchChange(Sender: TObject);
procedure edSearchKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure FormShow(Sender: TObject);
procedure JSONPropStorage1RestoreProperties(Sender: TObject);
procedure JSONPropStorage1SavingProperties(Sender: TObject);
procedure lbGitmojisDblClick(Sender: TObject);
procedure lbGitmojisDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
procedure lbGitmojisKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure mnuExitClick(Sender: TObject);
procedure mnuShowClick(Sender: TObject);
procedure TrayIcon1Click(Sender: TObject);
private
FGitmojiData: TGitmojiData;
FTagData: TTagData;
FIconSize: Integer;
FBigSize: Integer;
FSmallSize: Integer;
function DetermineFontSize(ACanvas: TCanvas; AHeight: Integer): Integer;
public
end;
var
frmMain: TfrmMain;
implementation
{$R *.lfm}
uses
fpjson, jsonparser, jsonscanner, fpjsonrtti, LCLType, Clipbrd;
{ TGitmojiData }
constructor TGitmojiData.Create;
begin
inherited Create;
FGitmojis := TCollection.Create(TGitmoji);
end;
destructor TGitmojiData.Destroy;
begin
FreeAndNil(FGitmojis);
inherited Destroy;
end;
{ TfrmMain }
procedure TfrmMain.FormCreate(Sender: TObject);
var
fs: TResourceStream;
parser: TJSONParser;
jsondata: TJSONData;
destreamer: TJSONDeStreamer;
gitmoji: TGitmoji;
i, j, off: Integer;
tags1, tags2: array of String;
configDir: String;
begin
JSONPropStorage1.JSONFileName := GetAppConfigFile(False);
configDir := ExtractFilePath(JSONPropStorage1.JSONFileName);
if not DirectoryExists(configDir) then
begin
ForceDirectories(configDir);
end;
try
fs := TResourceStream.Create(HINSTANCE, 'GITMOJIS', RT_RCDATA);
parser := TJSONParser.Create(fs, DefaultOptions);
jsondata := parser.Parse;
destreamer := TJSONDeStreamer.Create(nil);
FGitmojiData := TGitmojiData.Create;
destreamer.JSONToObject(jsondata as TJSONObject, FGitmojiData);
finally
fs.Free;
parser.Free;
jsondata.Free;
destreamer.Free;
end;
SetLength(FTagData, FGitmojiData.FGitmojis.Count);
for i := 0 to FGitmojiData.FGitmojis.Count - 1 do
begin
gitmoji := TGitmoji(FGitmojiData.FGitmojis.Items[i]);
FTagData[i].Gitmoji := gitmoji;
tags1 := Trim(gitmoji.FName).Split(' ');
tags2 := Trim(gitmoji.FDescription).Split(' ');
SetLength(FTagData[i].Tags, Length(tags1) + Length(tags2));
off := 0;
for j := 0 to Length(tags1) - 1 do
FTagData[i].Tags[off + j] := tags1[j].ToLower;
off := Length(tags1);
for j := 0 to Length(tags2) - 1 do
FTagData[i].Tags[off + j] := tags2[j].ToLower;
end;
FIconSize := DetermineFontSize(lbGitmojis.Canvas, lbGitmojis.ItemHeight);
FBigSize := DetermineFontSize(lbGitmojis.Canvas, lbGitmojis.ItemHeight div 2);
FSmallSize := DetermineFontSize(lbGitmojis.Canvas, lbGitmojis.ItemHeight div 3);
{$IFDEF WithTrayIcon}
TrayIcon1.Visible := True;
{$ENDIF WithTrayIcon}
end;
type
TMatch = class
Count: Integer;
Gitmoji: TGitmoji;
end;
function CompareMatches(const AMatch1, AMatch2: TMatch): Integer;
begin
Result := AMatch1.Count - AMatch2.Count;
if Result = 0 then
Result := CompareStr(AMatch1.Gitmoji.FName, AMatch2.Gitmoji.FName);
end;
type
TMatches = specialize TFPGObjectList<TMatch>;
procedure TfrmMain.edSearchChange(Sender: TObject);
var
s: String;
token: array of string;
i, j, k, matchCount: Integer;
matches: TMatches;
match: TMatch;
begin
s := Trim(AnsiLowerCase(edSearch.Text));
token := s.Split(' ');
lbGitmojis.Items.BeginUpdate;
lbGitmojis.Items.Clear;
if s = '' then
begin
for i := 0 to Length(FTagData) - 1 do
lbGitmojis.Items.AddObject('', FTagData[i].Gitmoji);
end else
begin
matches := TMatches.Create;
for i := 0 to Length(FTagData) - 1 do
begin
matchCount := 0;
for j := 0 to Length(FTagData[i].Tags) - 1 do
for k := 0 to Length(token) - 1 do
if Pos(token[k], FTagData[i].Tags[j]) > 0 then
Inc(matchCount);
if matchCount > 0 then
begin
match := TMatch.Create;
match.Count := matchCount;
match.Gitmoji := FTagData[i].Gitmoji;
matches.Add(match);
end;
end;
matches.Sort(@CompareMatches);
for match in matches do
lbGitmojis.Items.AddObject('', match.Gitmoji);
matches.Free;
end;
lbGitmojis.Items.EndUpdate;
end;
procedure TfrmMain.ApplicationProperties1Deactivate(Sender: TObject);
begin
Close;
end;
procedure TfrmMain.edSearchKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_DOWN then
begin
lbGitmojis.SetFocus;
if lbGitmojis.Items.Count > 0 then
lbGitmojis.ItemIndex := 0;
Key := 0;
end;
end;
procedure TfrmMain.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
{$IFDEF WithTrayIcon}
CloseAction := caHide;
{$ENDIF}
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
FreeAndNil(FGitmojiData);
end;
procedure TfrmMain.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_ESCAPE then
begin
Key := 0;
Close;
end;
end;
procedure TfrmMain.FormShow(Sender: TObject);
begin
Left := Mouse.CursorPos.X - (Width div 2);
Top := Mouse.CursorPos.Y - (Height div 2);
if Left + Width > Screen.DesktopLeft + Screen.DesktopWidth then
Left := Screen.DesktopLeft + Screen.DesktopWidth - Width
else if Left < Screen.DesktopLeft then
Left := Screen.DesktopLeft;
if Top + Height > Screen.DesktopTop + Screen.DesktopHeight then
Top := Screen.DesktopTop + Screen.DesktopHeight - Height
else if Top < Screen.DesktopTop then
Top := Screen.DesktopTop;
edSearch.Text := '';
edSearchChange(Sender);
BringToFront;
Activate;
end;
procedure TfrmMain.JSONPropStorage1RestoreProperties(Sender: TObject);
begin
Width := StrToInt(JSONPropStorage1.StoredValue['Width']);
Height := StrToInt(JSONPropStorage1.StoredValue['Height']);
cbCopyEmoji.Checked := StrToBool(JSONPropStorage1.StoredValue['CopyEmoji']);
end;
procedure TfrmMain.JSONPropStorage1SavingProperties(Sender: TObject);
begin
JSONPropStorage1.StoredValue['Width'] := IntToStr(Width);
JSONPropStorage1.StoredValue['Height'] := IntToStr(Height);
JSONPropStorage1.StoredValue['CopyEmoji'] := BoolToStr(cbCopyEmoji.Checked, True);
end;
procedure TfrmMain.lbGitmojisDblClick(Sender: TObject);
begin
if cbCopyEmoji.Checked then
Clipboard.AsText := TGitmoji(lbGitmojis.Items.Objects[lbGitmojis.ItemIndex]).FEmoji
else
Clipboard.AsText := TGitmoji(lbGitmojis.Items.Objects[lbGitmojis.ItemIndex]).FCode;
Close;
end;
procedure TfrmMain.lbGitmojisDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
var
gitmoji: TGitmoji;
textStyle: TTextStyle;
begin
if Index > -1 then
begin
gitmoji := TGitmoji(lbGitmojis.Items.Objects[Index]);
textStyle := lbGitmojis.Canvas.TextStyle;
textStyle.Opaque := True;
lbGitmojis.Canvas.Font.Size := FIconSize;
lbGitmojis.Canvas.Font.Style := [];
lbGitmojis.Canvas.TextRect(ARect, ARect.Left, arect.top, gitmoji.FEmoji, textStyle);
lbGitmojis.Canvas.Font.Size := FBigSize;
lbGitmojis.Canvas.Font.Style := [fsBold];
lbGitmojis.Canvas.TextRect(ARect, ARect.Left + lbGitmojis.ItemHeight + 8, ARect.Top, gitmoji.code, textStyle);
lbGitmojis.Canvas.Font.Size := FSmallSize;
lbGitmojis.Canvas.Font.Style := [];
lbGitmojis.Canvas.TextRect(ARect, ARect.Left + lbGitmojis.ItemHeight + 8, ARect.Top + lbGitmojis.ItemHeight div 2, gitmoji.FDescription, textStyle);
end;
end;
procedure TfrmMain.lbGitmojisKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key = VK_UP) and (lbGitmojis.ItemIndex = 0) then
edSearch.SetFocus
else if (Key = VK_RETURN) and (lbGitmojis.ItemIndex > -1) then
begin
lbGitmojisDblClick(Sender);
end;
end;
procedure TfrmMain.mnuExitClick(Sender: TObject);
begin
Application.Terminate;
end;
procedure TfrmMain.mnuShowClick(Sender: TObject);
begin
Show;
end;
procedure TfrmMain.TrayIcon1Click(Sender: TObject);
begin
if Visible then
Close
else
Show;
end;
function TfrmMain.DetermineFontSize(ACanvas: TCanvas; AHeight: Integer): Integer;
var
currentHeight: Integer;
begin
currentHeight := ACanvas.TextHeight('M');
if currentHeight < AHeight then
repeat
Result := ACanvas.Font.Size;
ACanvas.Font.Size := ACanvas.Font.Size + 1;
currentHeight := ACanvas.TextHeight('M');
until currentHeight > AHeight
else if currentHeight > AHeight then
repeat
Result := ACanvas.Font.Size;
ACanvas.Font.Size := ACanvas.Font.Size - 1;
currentHeight := ACanvas.TextHeight('M');
until currentHeight < AHeight
else
Result := ACanvas.Font.Size;
end;
end.