uloading.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. unit ULoading;
  3. {$mode objfpc}{$H+}
  4. interface
  5. uses
  6. Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  7. ExtCtrls, BCPanel, types, BGRABitmap;
  8. type
  9. { TFLoading }
  10. TFLoading = class(TForm)
  11. BGRAPanel1: TBCPanel;
  12. Timer1: TTimer;
  13. procedure BGRAPanel1AfterRenderBCPanel(Sender: TObject;
  14. const ABGRA: TBGRABitmap; {%H-}ARect: TRect);
  15. procedure FormCreate(Sender: TObject);
  16. procedure Timer1Timer(Sender: TObject);
  17. { private declarations }
  18. public
  19. LoadingStatus: string;
  20. WantedTimeOut: integer;
  21. { public declarations }
  22. procedure SetTimeOut(AMillisecond: integer);
  23. procedure ShowMessage(AMessage: string; AMillisecond: integer = 0);
  24. procedure HideMessage;
  25. end;
  26. procedure MessagePopup(AMessage: string; AMillisecond: integer);
  27. procedure MessagePopupForever(AMessage: string);
  28. procedure MessagePopupHide;
  29. implementation
  30. uses BGRALayers, BGRAReadLzp, LCScaleDPI, LazPaintType, BGRABitmapTypes;
  31. const MarginTopBottom = 3;
  32. MarginLeftRight = 3;
  33. var PopupWindow: TFLoading;
  34. PopupFontFullHeight: integer;
  35. procedure MessagePopup(AMessage: string; AMillisecond: integer);
  36. begin
  37. if AMillisecond <= 0 then AMillisecond:= 1000;
  38. if PopupWindow= nil then
  39. PopupWindow := TFLoading.Create(nil);
  40. PopupWindow.ShowMessage(AMessage, AMillisecond);
  41. end;
  42. procedure MessagePopupForever(AMessage: string);
  43. begin
  44. if PopupWindow= nil then
  45. PopupWindow := TFLoading.Create(nil);
  46. PopupWindow.ShowMessage(AMessage, 0);
  47. PopupWindow.SetTimeOut(0);
  48. end;
  49. procedure MessagePopupHide;
  50. begin
  51. if PopupWindow <> nil then FreeAndNil(PopupWindow);
  52. end;
  53. { TFLoading }
  54. procedure TFLoading.FormCreate(Sender: TObject);
  55. begin
  56. if PopupFontFullHeight = 0 then
  57. begin
  58. PopupFontFullHeight := DoScaleY(20,OriginalDPI);
  59. end;
  60. end;
  61. procedure TFLoading.BGRAPanel1AfterRenderBCPanel(Sender: TObject;
  62. const ABGRA: TBGRABitmap; ARect: TRect);
  63. begin
  64. {$IFDEF LINUX}
  65. ABGRA.FontQuality := fqSystemClearType;
  66. {$ELSE}
  67. ABGRA.FontQuality := fqFineAntialiasing;
  68. {$ENDIF}
  69. ABGRA.FontFullHeight:= PopupFontFullHeight;
  70. ABGRA.TextOut(MarginLeftRight,MarginTopBottom,LoadingStatus,BGRABlack);
  71. if WantedTimeOut <> 0 then SetTimeOut(WantedTimeOut);
  72. end;
  73. procedure TFLoading.Timer1Timer(Sender: TObject);
  74. begin
  75. Timer1.Enabled:= false;
  76. HideMessage;
  77. end;
  78. procedure TFLoading.ShowMessage(AMessage: string; AMillisecond: integer);
  79. var bmp: TBGRABitmap;
  80. begin
  81. bmp := TBGRABitmap.Create(0,0);
  82. {$IFDEF LINUX}
  83. bmp.FontQuality := fqSystemClearType;
  84. {$ELSE}
  85. bmp.FontQuality := fqFineAntialiasing;
  86. {$ENDIF}
  87. bmp.FontFullHeight:= PopupFontFullHeight;
  88. self.LoadingStatus := AMessage;
  89. with bmp.TextSize(AMessage) do
  90. begin
  91. self.ClientWidth := cx+2*MarginLeftRight;
  92. self.ClientHeight := cy+2*MarginTopBottom;
  93. end;
  94. bmp.Free;
  95. self.Left := (Screen.Width-self.Width) div 2;
  96. self.Top := (Screen.Height-self.Height) div 2;
  97. if not self.Visible then self.Show else BGRAPanel1.UpdateControl;
  98. if AMillisecond <> 0 then
  99. SetTimeOut(AMillisecond);
  100. WantedTimeOut := AMillisecond;
  101. end;
  102. procedure TFLoading.HideMessage;
  103. begin
  104. if self.Visible then self.Hide;
  105. Update;
  106. end;
  107. procedure TFLoading.SetTimeOut(AMillisecond: integer);
  108. begin
  109. if AMillisecond = 0 then
  110. Timer1.Enabled:= false
  111. else
  112. begin
  113. Timer1.Enabled := false;
  114. Timer1.Interval := AMillisecond;
  115. Timer1.Enabled := true;
  116. end;
  117. end;
  118. {$R *.lfm}
  119. initialization
  120. finalization
  121. PopupWindow.Free;
  122. end.