-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainUI.pas
More file actions
1062 lines (861 loc) · 27 KB
/
Copy pathMainUI.pas
File metadata and controls
1062 lines (861 loc) · 27 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit MainUI;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Winapi.WebView2, Winapi.ActiveX,
Vcl.ExtCtrls, Vcl.Menus, Vcl.StdCtrls, Vcl.Imaging.pngimage, Cod.Types,
{Vcl.Edge, } Edge2, Cod.SysUtils, Cod.Files, Cod.Windows, Cod.Internet,
Cod.IniSettings, Winapi.ShlObj, DateUtils, Cod.Version, Cod.StringUtils,
SettingsUI, UITypes, IOUtils, System.SyncObjs, System.TimeSpan, JSON, Winapi.EdgeUtils,
System.Win.TaskbarCore, Vcl.Taskbar, System.Actions, Vcl.ActnList,
System.ImageList, Vcl.ImgList, System.Generics.Collections, Math,
Cod.WindowsRT.AppRegistration, Cod.WindowsRT.NotificationManager,
Cod.WindowsRT, Cod.CodrutSoftware.API.Update, Cod.Forms;
const
WM_RESTOREAPPFROMTRAY = WM_USER + 100;
type
// Define custom class
TMainBrowser = class(TCustomEdgeBrowser)
private
type TScriptCallback = reference to procedure (Status: HResult; ResultObjectJSON: string);
procedure HandleCreateWebViewCompleted(Sender: TCustomEdgeBrowser; AResult: HResult);
protected
procedure OpenDevTools;
// Scripts
procedure ExecuteScript(const JavaScript: string; Callback: TScriptCallback=nil); overload;
function ExecuteScriptAwait(const JavaScript: string; out Output: string; Timeout: cardinal=1000): boolean; overload;
public
constructor Create(AOwner: TComponent); override;
end;
TMainForm = class(TForm)
TrayIcon: TTrayIcon;
TrayMenu: TPopupMenu;
Show1: TMenuItem;
MinimizetoTray1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
N3: TMenuItem;
Givefeedback1: TMenuItem;
StartupLogo: TImage;
DelayedUpdateCheck: TTimer;
DebugPanel: TPanel;
Label5: TLabel;
Label6: TLabel;
Button4: TButton;
Button5: TButton;
Label7: TLabel;
Button6: TButton;
Label8: TLabel;
Edit1: TEdit;
Button7: TButton;
DebugStat: TTimer;
Button8: TButton;
Button9: TButton;
Button10: TButton;
Panel2: TPanel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
Label14: TLabel;
Label15: TLabel;
Label16: TLabel;
Label17: TLabel;
StartURLLoader: TTimer;
Settings1: TMenuItem;
N4: TMenuItem;
PeriodicUpdaterTask: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure TrayIconDblClick(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure MinimizetoTray1Click(Sender: TObject);
procedure Show1Click(Sender: TObject);
procedure YourLibrary1Click(Sender: TObject);
procedure Givefeedback1Click(Sender: TObject);
procedure DelayedUpdateCheckTimer(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure DebugStatTimer(Sender: TObject);
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button10Click(Sender: TObject);
procedure StartURLLoaderTimer(Sender: TObject);
procedure Settings1Click(Sender: TObject);
procedure PeriodicUpdaterTaskTimer(Sender: TObject);
protected
procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND;
procedure WMRestoreAppFromTray(var Message: TMessage); message WM_RESTOREAPPFROMTRAY;
procedure WMActivate(var Msg: TWMActivate); message WM_ACTIVATE;
private
procedure CloseProgram;
// Tray
procedure MinimizeToTray;
procedure RestoreFromTray;
// Update procs
procedure UpdateNetworkState;
// Window
procedure CreateSystemMenu;
// Forms
procedure OpenSettingsForm;
// Utils
procedure ReloadWebViewBase;
// Notifications
procedure FetchNotificationsFromClient;
// Audio
procedure InjectAudioHook;
function GetPlayedAudios: TArray<string>;
// Browser
procedure InitializeSite(FirstLoad: boolean=false);
procedure WaitBrowserNavigation;
procedure WaitBrowserInitialization;
// Main
procedure NavigateHome;
// Events
procedure BrowserExecuteScript(Sender: TCustomEdgeBrowser; AResult: HResult; const AResultObjectAsJson: string);
procedure BrowserNavigationStarting(Sender: TCustomEdgeBrowser; Args: TNavigationStartingEventArgs);
procedure BrowserNavigationCompleted(Sender: TCustomEdgeBrowser;
IsSuccess: Boolean; WebErrorStatus: COREWEBVIEW2_WEB_ERROR_STATUS);
procedure NotifActivated(Sender: TNotification; Arguments: string; UserInput: TUserInputMap);
procedure CreatingWebViewFinalized(Sender: TCustomEdgeBrowser; AResult: HResult);
public
Browser: TMainBrowser;
{ Public declarations }
InTray: boolean;
// Update
procedure DoUpdateCheck(NotifyStatus: boolean);
end;
const
VERSION: TVersion = (Major: 1; Minor: 0; Maintenance: 2);
FLUFFYCHAT_CLIENT_VERSION: TVersion = (Major: 2; Minor: 9; Maintenance: 1);
APP_NAME = 'FluffyChat Desktop';
DEVELOPER_URL = 'https://www.codrutsoft.com/';
SUPPORT_URL = 'https://go.codrutsoft.com/support/';
FEEDBACK_URL = 'https://github.com/Codrax/FluffyChat-Windows-Desktop';
WEBSITE_APP_LINK = 'https://www.codrutsoft.com/apps/fluffy-chat-desktop/';
API_APP_NAME = 'fluffy-chat-desktop';
APP_USER_MODEL_ID = 'com.codrutsoft.fluffychatdesktop';
// Menu (titlebar)
MENU_ACTION = 999;
MENU_ACTION_RELOAD = MENU_ACTION+1;
MENU_ACTION_TRAY = MENU_ACTION+2;
MENU_ACTION_SETTINGS = MENU_ACTION+3;
// Links
SOURCE_BASE = 'source\';
APP_BASE_FILE = SOURCE_BASE+'web\index.html';
HOME_URL = 'https://app.local/web/index.html';
var
MainForm: TMainForm;
// Update
VersionCheck: TStandardVersionCheckerUpdateUrl;
// System
AppData: string;
AppDir: string;
Settings: TSettingsManager;
Status: TSectionSettingsManager;
IsWindowsStoreApp: boolean;
DebugMode: boolean;
AppInitialized: boolean;
TaskbarPlayingState: boolean = false;
// Notifications
Manager: TNotificationManager;
NotificationNotifications: TNotification;
KnownMissedNotificationCount: integer;
NotificationCall: TNotification;
// Browser
BrowserInitialized: boolean;
BrowserNavigating: boolean;
ActionInjectAudioHook: boolean;
AudioHookInjected: boolean;
// Utils
StartURL: string;
ShowWhenLoaded: boolean;
InitializeWhenLoaded: boolean;
// App state
CurrentState: string;
LastNetworkConnectedState: boolean = true;
implementation
{$R *.dfm}
procedure TMainForm.BrowserExecuteScript(Sender: TCustomEdgeBrowser;
AResult: HResult; const AResultObjectAsJson: string);
begin
//
end;
procedure TMainForm.BrowserNavigationCompleted(Sender: TCustomEdgeBrowser;
IsSuccess: Boolean; WebErrorStatus: COREWEBVIEW2_WEB_ERROR_STATUS);
begin
CurrentState := 'Navigation done';
BrowserNavigating := false;
// Hidden
if ShowWhenLoaded then begin
Browser.Align := alClient;
//
ShowWhenLoaded := false;
end;
// Init
if InitializeWhenLoaded then begin
AppInitialized := true;
StartupLogo.Hide;
// Fix fluffy chat not loaded when starting minimized in the system tray
if InTray and not Application.ShowMainForm then begin
AlphaBlend := true;
AlphaBlendValue := 0;
Show;
Hide;
AlphaBlend := false;
AlphaBlendValue := 255;
end;
//
InitializeWhenLoaded := false;
end;
// Error
if not (WebErrorStatus in [COREWEBVIEW2_WEB_ERROR_STATUS_UNKNOWN, COREWEBVIEW2_WEB_ERROR_STATUS_CONNECTION_ABORTED]) then begin
var S := '';
case WebErrorStatus of
COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_COMMON_NAME_IS_INCORRECT: S := 'CERTIFICATE_COMMON_NAME_IS_INCORRECT';
COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_EXPIRED: S := 'CERTIFICATE_EXPIRED';
COREWEBVIEW2_WEB_ERROR_STATUS_CLIENT_CERTIFICATE_CONTAINS_ERRORS: S := 'CLIENT_CERTIFICATE_CONTAINS_ERRORS';
COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_REVOKED: S := 'CERTIFICATE_REVOKED';
COREWEBVIEW2_WEB_ERROR_STATUS_CERTIFICATE_IS_INVALID: S := 'CERTIFICATE_IS_INVALID';
COREWEBVIEW2_WEB_ERROR_STATUS_SERVER_UNREACHABLE: S := 'SERVER_UNREACHABLE';
COREWEBVIEW2_WEB_ERROR_STATUS_TIMEOUT: S := 'TIMEOUT';
COREWEBVIEW2_WEB_ERROR_STATUS_ERROR_HTTP_INVALID_SERVER_RESPONSE: S := 'ERROR_HTTP_INVALID_SERVER_RESPONSE';
//COREWEBVIEW2_WEB_ERROR_STATUS_CONNECTION_ABORTED: S := 'CONNECTION_ABORTED';
COREWEBVIEW2_WEB_ERROR_STATUS_CONNECTION_RESET: S := 'CONNECTION_RESET';
COREWEBVIEW2_WEB_ERROR_STATUS_DISCONNECTED: S := 'DISCONNECTED';
COREWEBVIEW2_WEB_ERROR_STATUS_CANNOT_CONNECT: S := 'CANNOT_CONNECT';
COREWEBVIEW2_WEB_ERROR_STATUS_HOST_NAME_NOT_RESOLVED: S := 'HOST_NAME_NOT_RESOLVED';
COREWEBVIEW2_WEB_ERROR_STATUS_OPERATION_CANCELED: S := 'OPERATION_CANCELED';
COREWEBVIEW2_WEB_ERROR_STATUS_REDIRECT_FAILED: S := 'REDIRECT_FAILED';
COREWEBVIEW2_WEB_ERROR_STATUS_UNEXPECTED_ERROR: S := 'UNEXPECTED_ERROR';
COREWEBVIEW2_WEB_ERROR_STATUS_VALID_AUTHENTICATION_CREDENTIALS_REQUIRED: S := 'VALID_AUTHENTICATION_CREDENTIALS_REQUIRED';
COREWEBVIEW2_WEB_ERROR_STATUS_VALID_PROXY_AUTHENTICATION_REQUIRED: S := 'VALID_PROXY_AUTHENTICATION_REQUIRED';
else S := 'An unknown error has occured';
end;
MessageDLG('Browser error:'#13+S+#13#13'Please report this problem to the developers.', mtError, [mbClose], 0);
// Error panel
Browser.Hide;
end else begin
Browser.Show;
if ActionInjectAudioHook then begin
InjectAudioHook;
AudioHookInjected := true;
//
ActionInjectAudioHook := false;
end;
end;
end;
procedure TMainForm.BrowserNavigationStarting(Sender: TCustomEdgeBrowser;
Args: TNavigationStartingEventArgs);
begin
CurrentState := 'Navigating';
BrowserNavigating := true;
end;
procedure TMainForm.Button10Click(Sender: TObject);
begin
Browser.Navigate('about:blank');
end;
procedure TMainForm.Button4Click(Sender: TObject);
begin
Browser.Show;
// Align
Browser.Align := alClient;
end;
procedure TMainForm.Button5Click(Sender: TObject);
begin
Browser.Hide;
end;
procedure TMainForm.Button6Click(Sender: TObject);
begin
StartupLogo.Hide;
end;
procedure TMainForm.Button7Click(Sender: TObject);
begin
Browser.Navigate(Edit1.Text);
end;
procedure TMainForm.Button8Click(Sender: TObject);
begin
NavigateHome;
end;
procedure TMainForm.Button9Click(Sender: TObject);
begin
Browser.Refresh;
end;
procedure TMainForm.CloseProgram;
begin
InTray := true; // to prevent mimimizing
Close;
end;
function TMainForm.GetPlayedAudios: TArray<string>;
var
JSResult, Clean: string;
Json: TJSONArray;
I: Integer;
begin
Result := [];
if not AudioHookInjected then
Exit;
SetLength(Result, 0);
if not Browser.ExecuteScriptAwait(
'JSON.stringify(window.__getAndClearPlayedAudios() ?? [])',
JSResult
) then
Exit;
// JS returns JSON string like: ["file1.mp3","file2.ogg"]
Clean := JSResult.Trim;
try
const JS = TJSONString.ParseJSONValue(Clean);
Clean := JS.Value;
JS.Free;
except
Exit;
end;
if (Clean = '') or (Clean = 'null') then
Exit;
try
Json := TJSONObject.ParseJSONValue(Clean) as TJSONArray;
except
Exit;
end;
try
SetLength(Result, Json.Count);
for I := 0 to Json.Count - 1 do
Result[I] := Json.Items[I].Value;
finally
Json.Free;
end;
end;
procedure TMainForm.Givefeedback1Click(Sender: TObject);
begin
ShellRun(FEEDBACK_URL, true);
end;
procedure TMainForm.InitializeSite;
begin
CurrentState := 'Initializing browser...';
// UI
if not DebugMode then
StartupLogo.Show;
Browser.Show;
// Align
if not DebugMode then begin
Browser.Align := alNone;
Browser.Width := ClientWidth;
Browser.Height := ClientHeight;
Browser.Top := -Browser.Height;
end;
BrowserInitialized := false;
// Init
Browser.ReinitializeWebView;
Application.ProcessMessages;
// Wait for initializaiton
WaitBrowserInitialization;
// Set host mapping
Browser.SetVirtualHostNameToFolderMapping(
'app.local',
AppDir + SOURCE_BASE,
COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_ALLOW
);
// Navigate
Browser.Navigate('about:blank');
WaitBrowserNavigation;
end;
procedure TMainForm.InjectAudioHook;
begin
Browser.ExecuteScript(
'(function(){' +
'if(window.__audioHookInstalled) return;' +
'window.__audioHookInstalled = true;' +
'window.__playedAudios = [];' +
'const originalPlay = HTMLMediaElement.prototype.play;' +
'HTMLMediaElement.prototype.play = function() {' +
' try {' +
' let src = this.currentSrc || this.src || "";' +
' if(src) {' +
' window.__playedAudios.push(src);' +
' console.log("AUDIO_PLAYED_FULLPATH:" + src);' +
' }' +
' } catch(e) {}' +
' return originalPlay.apply(this, arguments);' +
'};' +
'window.__getAndClearPlayedAudios = function() {' +
' const copy = window.__playedAudios.slice();' +
' window.__playedAudios.length = 0;' +
' return copy;' +
'};' +
'})();'
, nil);
end;
procedure TMainForm.CreateSystemMenu;
var
HhMenu: HMENU;
procedure AddSeparator;
begin
InsertMenu(HhMenu, 999 , MF_BYPOSITION or MF_SEPARATOR, 0, nil);
end;
procedure AddMenu(Name: string; id: integer);
begin
InsertMenu(HhMenu, 999, MF_BYPOSITION, id, PChar(Name));
end;
begin
// Get the handle to the system menu
HhMenu := GetSystemMenu(Handle, False);
// Item
AddSeparator;
AddMenu('Reload web view', MENU_ACTION_RELOAD);
AddSeparator;
AddMenu('Minimize to tray', MENU_ACTION_TRAY);
AddMenu('Settings', MENU_ACTION_SETTINGS);
end;
procedure TMainForm.CreatingWebViewFinalized(Sender: TCustomEdgeBrowser;
AResult: HResult);
begin
// Initialized
BrowserInitialized := true;
// Context menu
if not DebugMode then begin
Browser.DefaultContextMenusEnabled := false;
Browser.BuiltInErrorPageEnabled := false;
Browser.DevToolsEnabled := false;
Browser.StatusBarEnabled := false;
Browser.ZoomControlEnabled := false;
end else
Browser.OpenDevTools;
// Get settings
Browser.ZoomFactor := Settings.Get<double>('zoom', 'accessibility', 1);
end;
procedure TMainForm.Exit1Click(Sender: TObject);
begin
CloseProgram;
end;
procedure TMainForm.FetchNotificationsFromClient;
var
Items: TArray<string>;
CountNotifications: integer;
CountCalls: integer;
begin
Items := GetPlayedAudios;
// Is the window open
try
if not InTray and (GetForegroundWindow = Handle) then begin
KnownMissedNotificationCount := 0;
Manager.DestroyNotification(NotificationNotifications);
Manager.DestroyNotification(NotificationCall);
Exit;
end;
except
Exit;
end;
// Count them
CountNotifications := 0;
CountCalls := 0;
for var I := 0 to High(Items) do
if Items[I].EndsWith('phone.ogg') or Items[I].StartsWith('data:audio/ogg;base64,T2dnUwACAAAAAAAAAAAko0mQAAAAAAben9YBHgF2b3JiaXMAAAAAAkSsAAAAAAAAgLUBAAAAAAC4AU9nZ1MAAAAAAAAAAAAAJKNJkAEAAAAu7GtSEUD') then
Inc(CountCalls)
else
if Items[I].EndsWith('notification.ogg') then
Inc(CountNotifications)
else
ShowMessage(Items[I]);
// Remove notifications that belong to the calls
if (CountCalls > 0) then
CountNotifications := Max(0, CountNotifications-CountCalls);
// Notifications
if (CountNotifications > 0) then begin
KnownMissedNotificationCount := KnownMissedNotificationCount + CountNotifications;
// Create notification
Manager.DestroyNotification(NotificationNotifications);
TToastContentBuilder.Create
.AddText( TToastValueString.Create(Format('You have %d new notifications', [KnownMissedNotificationCount])) )
.AddText( TToastValueString.Create(Format('There are %d more notifications than last time.'#13#13'Click here to open FluffyChat', [CountNotifications])) )
.AddButton('View', TActivationType.Foreground, 'view')
.AddButton('Ignore', TActivationType.Foreground, 'ignore')
.CreateNotificationAndFree(NotificationNotifications);
// Set tag
NotificationNotifications.Tag := 'notif-notifications';
// Events
NotificationNotifications.OnActivated := NotifActivated;
Manager.ShowNotification(NotificationNotifications);
end;
// Calls
if (CountCalls > 0) then begin
// Create notification
Manager.DestroyNotification(NotificationCall);
TToastContentBuilder.Create
.AddText( TToastValueString.Create('Someone is calling you') )
.AddAppLogoOverride(TToastValueString.Create(AppDir+'call.png'), TImageCrop.Circle, 'Call')
.AddText( TToastValueString.Create('Click here to open FluffyChat and answer this call') )
.AddButton('Open', TActivationType.Foreground, 'view')
.AddAudio(TSoundEventValue.NotificationDefault, WinFalse, WinTrue)
.CreateNotificationAndFree(NotificationCall);
// Set tag
NotificationCall.Tag := 'notif-call';
// Events
NotificationCall.OnActivated := NotifActivated;
Manager.ShowNotification(NotificationCall);
end;
end;
procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if AppInitialized and not InTray and not DebugMode and Settings.Get<boolean>('minimize-to-tray', 'general', true) then begin
MinimizeToTray;
CanClose := false;
Exit;
end;
// Save positions
SaveFormPositions(Self, AppData+'form-pos.ini');
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Caption := APP_NAME;
TrayIcon.Hint := APP_NAME;
// Dirs
AppData := GetPathInAppData(APP_NAME, 'Codrut Software', TAppDataType.Roaming, true);
AppDir := IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName));
// UI
FixDelphiXDialogs;
// Settings
Settings := TSettingsManager.Create(AppData + 'settings.ini');
Status := TSectionSettingsManager.Create(AppData + 'status.ini', 'status');
// Create system menu
CreateSystemMenu;
// Load form
LoadFormPositions(Self, AppData+'form-pos.ini');
// Theme (must be after loading position)
DarkModeApplyToWindow(Handle, true);
// Debug Mode
if DebugMode then begin
DebugPanel.BringToFront;
DebugPanel.Show;
DebugStat.Enabled := true;
Label15.Caption := VERSION.ToString;
end else begin
DebugPanel.Destroy;
DebugStat.Destroy;
end;
// Tasks
DelayedUpdateCheck.Enabled := Settings.Get<boolean>('app', 'check-updates', true);
// Player
SetCurrentProcessExplicitAppUserModelID( APP_USER_MODEL_ID );
// Browser
Browser := TMainBrowser.Create(Self);
Browser.Parent := Self;
Browser.Align := alClient;
Browser.SendToBack; // dev
Browser.AreBrowserExtensionsEnabled := false;
Browser.OnExecuteScript := BrowserExecuteScript;
Browser.OnNavigationStarting := BrowserNavigationStarting;
Browser.OnNavigationCompleted := BrowserNavigationCompleted;
Browser.OnCreateWebViewCompleted := CreatingWebViewFinalized;
const AppDataBrowser = AppData + 'Browser\';
Browser.UserDataFolder := AppDataBrowser;
// Init
InitializeSite;
// Init - Updates
UpdateNetworkState;
// Start URL
StartURL := HOME_URL;
if not GetNetworkConnected then // offline mode
StartURL := HOME_URL;
// Begin loading
StartURLLoader.Enabled := true;
end;
procedure TMainForm.MinimizeToTray;
begin
if InTray then
Exit;
InTray := true;
// Set
Hide;
Show1.Visible := true;
MinimizetoTray1.Visible := false;
end;
procedure TMainForm.MinimizetoTray1Click(Sender: TObject);
begin
MinimizeToTray;
end;
procedure TMainForm.NavigateHome;
begin
// Navigate
Browser.Navigate( HOME_URL );
end;
procedure TMainForm.NotifActivated(Sender: TNotification; Arguments: string;
UserInput: TUserInputMap);
begin
if Arguments = 'ignore' then
Exit;
RestoreFromTray;
BringToTopAndFocusWindow(Handle);
end;
procedure TMainForm.OpenSettingsForm;
begin
if SettingsForm <> nil then begin
try
BringToTopAndFocusWindow(SettingsForm.Handle);
except
end;
Exit;
end;
SettingsForm := TSettingsForm.Create(Self);
with SettingsForm do
try
// Show
ShowModal;
finally
Free;
SettingsForm := nil;
end;
end;
procedure TMainForm.ReloadWebViewBase;
begin
InitializeSite;
//
StartURL := HOME_URL;
StartURLLoader.Enabled := true;
end;
procedure TMainForm.RestoreFromTray;
begin
if not InTray then
Exit;
InTray := false;
// Set
Show;
Show1.Visible := false;
MinimizetoTray1.Visible := true;
end;
procedure TMainForm.Settings1Click(Sender: TObject);
begin
RestoreFromTray;
// Settings
OpenSettingsForm;
end;
procedure TMainForm.Show1Click(Sender: TObject);
begin
RestoreFromTray;
end;
procedure TMainForm.StartURLLoaderTimer(Sender: TObject);
begin
TTimer(Sender).Enabled := false;
// Default page
InitializeWhenLoaded := true;
ShowWhenLoaded := true;
// Navigate
ActionInjectAudioHook := true;
AudioHookInjected := false;
Browser.Navigate( StartURL );
end;
procedure TMainForm.DebugStatTimer(Sender: TObject);
begin
Label11.Caption := BooleanToString( BrowserNavigating );
Label13.Caption := Browser.LocationURL;
Label17.Caption := int64(Browser.LastErrorCode).ToString;
end;
procedure TMainForm.DelayedUpdateCheckTimer(Sender: TObject);
begin
if IsWindowsStoreApp then
Exit;
// Is in tray
if InTray then
Exit;
// Disable self
TTimer(Sender).Enabled := false;
// Last check < a day ago
const LastUpdateCheck = Status.Get<double>('last-update-check', 0);
if (LastUpdateCheck <> 0) and (DaysBetween(Now, LastUpdateCheck) < 1) then
Exit;
// Write last update check
Status.Put<double>('last-update-check', Now);
// Update
DoUpdateCheck(false);
end;
procedure TMainForm.DoUpdateCheck(NotifyStatus: boolean);
procedure DoDownloadFailed;
begin
if MessageDLG('The download failed. Open the website?', mtWarning, [mbYes, mbNo], 0) <> mrYes then
Exit;
ShellRun(WEBSITE_APP_LINK, true);
end;
begin
// Start update check
VersionCheck.Load;
if not VersionCheck.Loaded then
Exit; // failed
// New version?
if not VersionCheck.ServerVersion.NewerThan(VersionCheck.ClientVersion) then begin
if NotifyStatus then
MessageDLG('There are no new updates avalabile.', mtWarning, [mbOk], 0);
Exit;
end;
// Alert user
if MessageDLG('There is a new version of '+APP_NAME+' avalabile on the server. Do you wish to download It now? The app will close to update',
mtWarning, [mbYes, mbNo], 0) <> mrYes then
Exit;
// Validate url
if VersionCheck.UpdateUrl = '' then begin
DoDownloadFailed;
Exit;
end;
// Start download
try
const OutputFile = ReplaceWinPath(Format('%%TEMP%%\updateinstall_%S.exe', [
GenerateString(8, [TStrGenFlag.UppercaseLetters, TStrGenFlag.LowercaseLetters, TStrGenFlag.Numbers])
]));
DownloadFile(VersionCheck.UpdateUrl, OutputFile);
// Run
ShellRun( OutputFile, true, '-ad' );
// Close
Application.Terminate;
except
DoDownloadFailed;
Exit;
end;
end;
procedure TMainForm.PeriodicUpdaterTaskTimer(Sender: TObject);
begin
//if not Visible then Exit;
// Reset interval
TTimer(sender).Interval := 2000;
// Fetch notifications
FetchNotificationsFromClient;
// Check internet connectivity
UpdateNetworkState;
end;
procedure TMainForm.TrayIconDblClick(Sender: TObject);
begin
if InTray then
RestoreFromTray
else
BringToTopAndFocusWindow(Handle);
end;
procedure TMainForm.UpdateNetworkState;
begin
const State = GetNetworkConnected;
if State <> LastNetworkConnectedState then begin
if State then
Caption := APP_NAME
else
Caption := APP_NAME + ' (Offline)';
LastNetworkConnectedState := State;
end;
end;
procedure TMainForm.WaitBrowserInitialization;
begin
var I: integer; I := 0;
while not BrowserInitialized and (I < 5000) do begin
Sleep(1);
Application.ProcessMessages;
end;
end;
procedure TMainForm.WaitBrowserNavigation;
begin
var I: integer; I := 0;
while not BrowserNavigating and (I < 5000) do begin
Sleep(1);
Application.ProcessMessages;
end;
end;
procedure TMainForm.WMActivate(var Msg: TWMActivate);
begin
inherited;
if (Msg.Active in [WA_ACTIVE, WA_CLICKACTIVE]) and not Msg.Minimized then
try
// Select webview
if Browser.Visible then
Browser.DoEnter;
//Browser.SetFocus;
except
end;
end;
procedure TMainForm.WMRestoreAppFromTray(var Message: TMessage);
begin
RestoreFromTray;
end;
procedure TMainForm.WMSysCommand(var Message: TWMSysCommand);
begin
// Handle menus
case Message.CmdType of
MENU_ACTION_RELOAD: ReloadWebViewBase;
MENU_ACTION_TRAY: MinimizeToTray;
MENU_ACTION_SETTINGS: OpenSettingsForm;
end;
// Done
inherited;
end;
procedure TMainForm.YourLibrary1Click(Sender: TObject);
begin
end;
{ TMainBrowser }
constructor TMainBrowser.Create(AOwner: TComponent);
begin
inherited;
OnCreateWebViewCompleted := HandleCreateWebViewCompleted;
// Args
AdditionalBrowserArguments := '--autoplay-policy=no-user-gesture-required'; // tried: --disable-features=MediaControls,GlobalMediaControls - does not work!!
end;
procedure TMainBrowser.ExecuteScript(const JavaScript: string;
Callback: TScriptCallback);
begin
if DefaultInterface <> nil then
DefaultInterface.ExecuteScript(PChar(JavaScript),
Callback<HResult, PChar>.CreateAs<ICoreWebView2ExecuteScriptCompletedHandler>(
function(ErrorCode: HResult; ResultObjectAsJson: PWideChar): HResult stdcall
begin
Result := S_OK;
if Assigned(Callback) then
Callback(ErrorCode, string(ResultObjectAsJson));
end));
end;