-
Notifications
You must be signed in to change notification settings - Fork 14
/
Icons.Manager.pas
138 lines (112 loc) · 3.57 KB
/
Icons.Manager.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
unit Icons.Manager;
interface
uses
Winapi.Windows,
System.SysUtils, System.Classes,
System.Generics.Collections, System.Generics.Defaults,
Power, Power.WinApi.PowrProf,
Battery.Mode,
Icon.Renderers,
Icon.Renderers.Default, Icon.Renderers.Percent, Icon.Renderers.Simple,
Helpers.Images.Generator,
Versions.Helpers;
type
TIconsManager = class
strict private
class function IsFlag(b: Byte; Flag: Byte): Boolean; inline; static;
strict private
FRenderer: IIconRenderer;
FTextRenderer: IIconRenderer;
FOptions: TIconsOptions;
function GetRenderer: IIconRenderer;
function GetTextRenderer: IIconRenderer;
procedure OptionsChange(Sender: TObject);
property Renderer: IIconRenderer read GetRenderer;
property TextRenderer: IIconRenderer read GetTextRenderer;
public
constructor Create; reintroduce;
destructor Destroy; override;
function GetIcon(Dpi: Integer): HICON;
function GetImage(Dpi: Integer): HBITMAP;
function GetImageAsIcon(Dpi: Integer): HICON;
property Options: TIconsOptions read FOptions;
end;
implementation
{ TIconsManager }
class function TIconsManager.IsFlag(b, Flag: Byte): Boolean;
begin
Result:= b and Flag = Flag;
end;
constructor TIconsManager.Create;
begin
FOptions := TIconsOptions.Create;
with FOptions do
begin
IconStyle := isAuto;
IconColorType := DefaultIconColorType;
IconBehavior := DefaultIconBehavior;
IconTheme := DefaultIconTheme;
ExplicitMissingBattery := True;
OnChange := OptionsChange;
end;
end;
destructor TIconsManager.Destroy;
begin
FOptions.Free;
end;
function TIconsManager.GetIcon(Dpi: Integer): HICON;
var
PowerStatus: TSystemPowerStatus;
State: TBatteryState;
EffectiveRenderer: IIconRenderer;
begin
if not GetSystemPowerStatus(PowerStatus) then
ZeroMemory(@PowerStatus, SizeOf(PowerStatus));
State := TBatteryMode.State;
if (FOptions.IconBehavior = TIconBehavior.ibPercent) and
State.BatteryPresent and
(not IsFlag(PowerStatus.BatteryFlag, BATTERY_FLAG_NO_BATTERY)) and
((State.PowerCondition = PoAc) or (State.PowerCondition = PoDc)) then
EffectiveRenderer := TextRenderer
else
EffectiveRenderer := Renderer;
Result := EffectiveRenderer.GenerateIcon(TIconParams.Create(PowerStatus, State), Dpi);
end;
function TIconsManager.GetImage(Dpi: Integer): HBITMAP;
var
PowerStatus: TSystemPowerStatus;
begin
if not GetSystemPowerStatus(PowerStatus) then
ZeroMemory(@PowerStatus, SizeOf(PowerStatus));
Result := Renderer.GenerateImage(TIconParams.Create(PowerStatus, TBatteryMode.State), Dpi);
end;
function TIconsManager.GetImageAsIcon(Dpi: Integer): HICON;
var
hBmp: HBITMAP;
begin
hBmp := GetImage(Dpi);
Result := HBitmapToHIcon(hBmp);
DeleteObject(hBmp);
end;
function TIconsManager.GetRenderer: IIconRenderer;
begin
if FRenderer <> nil then Exit(FRenderer);
case FOptions.EffectiveIconStyle of
isWin8..isWinVista: FRenderer := TDefaultIconRenderer.Create(FOptions);
isWin11..isWin11Light: FRenderer := TSimpleIconRenderer.Create(FOptions);
else raise Exception.Create('Not supported icon style');
end;
Result := FRenderer;
end;
function TIconsManager.GetTextRenderer: IIconRenderer;
begin
if FTextRenderer <> nil then Exit(FTextRenderer);
FTextRenderer := TPercentIconRenderer.Create(FOptions);
Result := FTextRenderer;
end;
procedure TIconsManager.OptionsChange(Sender: TObject);
begin
FRenderer := nil;
FTextRenderer := nil;
end;
end.