utoolbox.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-3.0-only
  2. unit UToolbox;
  3. {$mode objfpc}{$H+}
  4. interface
  5. uses
  6. Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  7. ComCtrls, LazPaintType;
  8. type
  9. { TFToolbox }
  10. TFToolbox = class(TForm)
  11. ToolBar1: TToolBar;
  12. ToolBar2: TToolBar;
  13. ToolBar3: TToolBar;
  14. ToolBar4: TToolBar;
  15. procedure FormCreate(Sender: TObject);
  16. procedure FormShow(Sender: TObject);
  17. private
  18. FDarkTheme: boolean;
  19. FLazPaintInstance: TLazPaintCustomInstance;
  20. procedure SetDarkTheme(AValue: boolean);
  21. { private declarations }
  22. procedure SetImages(AToolBar: TToolBar; AImages: TImageList);
  23. procedure SetLazPaintInstance(AValue: TLazPaintCustomInstance);
  24. procedure ThemeChanged(Sender: TObject);
  25. public
  26. destructor Destroy; override;
  27. { public declarations }
  28. procedure AddButton(AToolBar: TToolBar; AAction: TBasicAction);
  29. procedure SetImages(AImages: TImageList);
  30. property LazPaintInstance: TLazPaintCustomInstance read FLazPaintInstance write SetLazPaintInstance;
  31. property DarkTheme: boolean read FDarkTheme write SetDarkTheme;
  32. end;
  33. implementation
  34. uses math, UDarkTheme;
  35. { TFToolbox }
  36. procedure TFToolbox.FormShow(Sender: TObject);
  37. begin
  38. self.EnsureVisible(False);
  39. end;
  40. procedure TFToolbox.SetDarkTheme(AValue: boolean);
  41. begin
  42. if FDarkTheme=AValue then Exit;
  43. FDarkTheme:=AValue;
  44. DarkThemeInstance.Apply(ToolBar1, AValue);
  45. DarkThemeInstance.Apply(ToolBar2, AValue);
  46. DarkThemeInstance.Apply(ToolBar3, AValue);
  47. DarkThemeInstance.Apply(ToolBar4, AValue);
  48. Color := DarkThemeInstance.GetColorButtonFace(AValue);
  49. end;
  50. procedure TFToolbox.AddButton(AToolBar: TToolBar; AAction: TBasicAction);
  51. var button: TToolButton;
  52. begin
  53. button := TToolButton.Create(AToolBar);
  54. button.Parent := AToolbar;
  55. button.Action := AAction;
  56. button.Style := tbsButton;
  57. end;
  58. procedure TFToolbox.SetImages(AImages: TImageList);
  59. var w: integer;
  60. begin
  61. Toolbar1.Top := 0;
  62. SetImages(ToolBar1, AImages);
  63. Toolbar2.Top := ToolBar1.Top+ToolBar1.Height;
  64. SetImages(ToolBar2, AImages);
  65. Toolbar3.Top := ToolBar2.Top+ToolBar2.Height;
  66. SetImages(ToolBar3, AImages);
  67. Toolbar4.Top := ToolBar3.Top+ToolBar3.Height;
  68. SetImages(ToolBar4, AImages);
  69. ClientHeight := ToolBar4.Top+ToolBar4.Height+2;
  70. w := 0;
  71. w := Max(w,Toolbar1.Width);
  72. w := Max(w,Toolbar2.Width);
  73. w := Max(w,Toolbar3.Width);
  74. w := Max(w,Toolbar4.Width);
  75. ClientWidth := w;
  76. end;
  77. procedure TFToolbox.SetImages(AToolBar: TToolBar; AImages: TImageList);
  78. begin
  79. AToolBar.Images := AImages;
  80. AToolBar.ButtonWidth := AImages.Width + 4;
  81. AToolBar.ButtonHeight := AImages.Height + 4;
  82. AToolBar.Height := AToolBar.ButtonHeight+4;
  83. AToolBar.Width := AToolBar.ButtonWidth*AToolBar.ButtonCount+4;
  84. end;
  85. procedure TFToolbox.SetLazPaintInstance(AValue: TLazPaintCustomInstance);
  86. begin
  87. if FLazPaintInstance=AValue then Exit;
  88. if Assigned(FLazPaintInstance) then
  89. FLazPaintInstance.RegisterThemeListener(@ThemeChanged, false);
  90. FLazPaintInstance:=AValue;
  91. if Assigned(FLazPaintInstance) then
  92. FLazPaintInstance.RegisterThemeListener(@ThemeChanged, true);
  93. end;
  94. procedure TFToolbox.ThemeChanged(Sender: TObject);
  95. begin
  96. DarkTheme := LazPaintInstance.DarkTheme;
  97. end;
  98. destructor TFToolbox.Destroy;
  99. begin
  100. if Assigned(FLazPaintInstance) then
  101. FLazPaintInstance.RegisterThemeListener(@ThemeChanged, false);
  102. inherited Destroy;
  103. end;
  104. procedure TFToolbox.FormCreate(Sender: TObject);
  105. begin
  106. BorderStyle := ToolWindowFixedSize;
  107. FormStyle := ToolWindowStyle;
  108. Position := poDesigned;
  109. end;
  110. {$R *.lfm}
  111. end.