-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMain.pas
More file actions
360 lines (314 loc) · 11.4 KB
/
Copy pathMain.pas
File metadata and controls
360 lines (314 loc) · 11.4 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
{****************************************************************************************}
{ PowerOff }
{ ErrorSoft(c) 2016-2017 }
{ }
{ Utility was written, to check the suitability of the FireMonkey technology to develop- }
{ in general - yes, at the moment, under Windows (and to simulate the interface UWP), }
{ it is a workable technology. }
{ }
{ This can be useful utilite or some of the sources, such as FontSizeForBox function. }
{ }
{ PowerOff - Very simple application for auto shutdown the computer. }
{ It is useful app, if you before sleep like watch videos on }
{ YouTube/music/TV shows/movies, but they continue playing all night, causing a headache }
{ in the morning ... }
{ }
{ This project uses MIT license: }
{ -------------------------------------------------------------------------------------- }
{ Copyright (c) 2016 errorsoft }
{ Permission is hereby granted, free of charge, to any person obtaining a copy of this }
{ software and associated documentation files (the "Software"), to deal in the Software }
{ without restriction, including without limitation the rights to use, copy, modify, }
{ merge, publish, distribute, sublicense, and/or sell copies of the Software, and to }
{ permit persons to whom the Software is furnished to do so, subject to the following }
{ conditions: }
{ The above copyright notice and this permission notice shall be included in all copies }
{ or substantial portions of the Software. }
{ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, }
{ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR }
{ A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT }
{ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF }
{ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE }
{ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }
{****************************************************************************************}
unit Main;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.TabControl,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.TextLayout,
WinApi.Windows, WinApi.ShellAPI, System.Math, FMX.Effects, FMX.Objects,
FMX.Ani;
type
TMainForm = class(TForm)
StyleBook: TStyleBook;
SetTitle: TLabel;
TabControl: TTabControl;
TabItemSet: TTabItem;
GridPanelLayout: TGridPanelLayout;
Button1: TButton;
Button2: TButton;
Button4: TButton;
Button3: TButton;
TabItemRun: TTabItem;
RunTitle: TLabel;
Display: TLabel;
Timer: TTimer;
Button5: TButton;
LayoutCustomTime: TLayout;
CalloutPanel: TCalloutPanel;
TrackBarCustom: TTrackBar;
CustomTime: TLabel;
LayoutCustomButton: TLayout;
ButtonCustomOk: TButton;
LayoutCustomTimeInternalRect: TLayout;
FooterEsLayout: TLayout;
FooterEs: TLabel;
FooterEsColorAnimation: TColorAnimation;
BackButton: TButton;
Back_Image: TImage;
LayoutBackButton: TLayout;
procedure ButtonSetClick(Sender: TObject);
procedure TimerTimer(Sender: TObject);
procedure DisplayResize(Sender: TObject);
procedure Button5Resize(Sender: TObject);
procedure Button5MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
Y: Single);
procedure TrackBarCustomChange(Sender: TObject);
procedure ButtonCustomOkClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FooterEsClick(Sender: TObject);
procedure BackButtonClick(Sender: TObject);
procedure TrackBarCustomPainting(Sender: TObject; Canvas: TCanvas; const [Ref] ARect: TRectF);
private
PowerOffTime: DWord;
IsUsedNotification: Boolean;
procedure DoLayoutCustomTime;
procedure Test;
procedure SetTime(Time: Integer);
function PowerOff: Boolean;
procedure ShowNotification;
procedure OnIdle(Sender: TObject; var Done: Boolean);
end;
var
MainForm: TMainForm;
implementation
{$R *.fmx}
{$I 'Config.inc'}
uses
Utils, Tooltip{$ifdef AutoUpdate}, TurboUpdate.Check, TurboUpdate.Types, TurboUpdate.UpdateFMX{$endif};
const
sNotificationBodyRu = 'Âíèìàíèå! ×åðåç 5 ìèíóò ïðîèçîéäåò àâòîìàòè÷åñêîå âûêëþ÷åíèå êîìïüþòåðà';
sNotificationBodyEn = 'Warning! After 5 minutes will automatically shutdown the computer';
sNotificationTitle = 'PowerOff';
sDefaultDisplayRu = '00÷ 00ì 00ñ';
sDefaultDisplayEn = '00h 00m 00s';
sOffTextRu = 'Âûêëþ÷åíèå...';
sOffTextEn = 'Power off...';
sCustomTmeRu = 'Îòêëþ÷èòü ÷åðåç';
sCustomTmeEn = 'Shutdown in';
NotificationTime = 60 * 5;// 5 minutes
NormalDispColor = $FF000000;
AlarmDispColor = $FFFF0000;
function sNotificationBody: string;
begin
if IsRu then
Result := sNotificationBodyRu
else
Result := sNotificationBodyEn;
end;
function sDefaultDisplay: string;
begin
if IsRu then
Result := sDefaultDisplayRu
else
Result := sDefaultDisplayEn;
end;
function sOffText: string;
begin
if IsRu then
Result := sOffTextRu
else
Result := sOffTextEn;
end;
function sCustomTme: string;
begin
if IsRu then
Result := sCustomTmeRu
else
Result := sCustomTmeEn;
end;
procedure TMainForm.BackButtonClick(Sender: TObject);
begin
Timer.Enabled := False;
TabControl.SetActiveTabWithTransition(TabItemSet, TTabTransition.Slide, TTabTransitionDirection.Reversed);
Button5Resize(Button5);
end;
procedure TMainForm.Button5MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
Y: Single);
begin
Button1.Enabled := not Button5.IsPressed;
Button2.Enabled := not Button5.IsPressed;
Button3.Enabled := not Button5.IsPressed;
Button4.Enabled := not Button5.IsPressed;
TrackBarCustomChange(TrackBarCustom);
DoLayoutCustomTime;
LayoutCustomTime.Visible := Button5.IsPressed;
end;
procedure TMainForm.Button5Resize(Sender: TObject);
begin
if TButton(Sender).IsPressed and not Timer.Enabled then
begin
DoLayoutCustomTime;
end;
end;
procedure TMainForm.ButtonCustomOkClick(Sender: TObject);
begin
SetTime(Trunc(TrackBarCustom.Value) * 60);
end;
procedure TMainForm.ButtonSetClick(Sender: TObject);
begin
SetTime(DWord(TControl(Sender).Tag) * 60);
end;
procedure TMainForm.DisplayResize(Sender: TObject);
begin
Display.Font.Size := FontSizeForBox(sDefaultDisplay,
Display.Font, Display.Width, Display.Height, Display.Height);
end;
procedure TMainForm.FooterEsClick(Sender: TObject);
begin
ShellExecute(0, 'open', PChar('https://github.com/errorcalc/PowerOff'), nil, nil, 0);
end;
procedure TMainForm.FormCreate(Sender: TObject);
{$ifdef AutoUpdate}
const
Urls: TStringArray = ['https://raw.githubusercontent.com/errorcalc/PowerOff/master/Update.ini', 'https://errorsoft.org/software/update.ini'];
Name = 'ErrorSoft.PowerOff';
{$endif}
begin
FooterEsLayout.BringToFront;// for link
// Apply rus lang to UI
if IsRu then
begin
SetTitle.Text := 'Îòêëþ÷èòü êîìïüþòåð ÷åðåç:';
Button1.Text := '15 ìèíóò';
Button2.Text := '30 ìèíóò';
Button3.Text := '1 ÷àñ';
Button4.Text := '2 ÷àñà';
Button5.Text := 'Íàñòðîèòü...';
RunTitle.Text := 'Äî âûêëþ÷åíèÿ êîìïüþòåðà îñòàëîñü:';
end;
Application.OnIdle := OnIdle;
// AutoUpdate
{$ifdef AutoUpdate}
FooterEs.Text := FooterEs.Text + ', [Auto Update]';
TurboUpdate.Check.CheckUpdate(
Urls,
Name,
procedure (UpdateAviable: Boolean; Version: TFileVersion)
var
Info: TUpdateInfo;
begin
if UpdateAviable then
begin
Info := Default(TUpdateInfo);
Info.Urls := Urls;
Info.Name := Name;
Info.Description := 'PowerOff Update';
TurboUpdate.UpdateFMX.Update(Info);
end;
end);
{$endif}
end;
// WHAT IS IT?
// This is try fix bug with windows 10 and suspending timers!
// http://www.sql.ru/forum/1203445-a/win-8-1-suspended
procedure TMainForm.OnIdle(Sender: TObject; var Done: Boolean);
begin
if Timer.Enabled then
Test;
end;
procedure TMainForm.DoLayoutCustomTime;
const
MaxWidth = 640;
MinWidth = 370;
CalloutOffset = 40;
var
Width: Integer;
begin
Width := Trunc(ClientWidth * 0.80);
if Width < MinWidth then
Width := MinWidth;
if Width > MaxWidth then
Width := MaxWidth;
LayoutCustomTime.Size.Width := Width;
CalloutPanel.CalloutOffset := CalloutPanel.Width - CalloutOffset;
LayoutCustomTime.Position.X := Trunc(
Button5.LocalToAbsolute(PointF(0, 0)).X - LayoutCustomTime.Size.Width + Button5.Width);
LayoutCustomTime.Position.Y := Trunc(
Button5.LocalToAbsolute(PointF(0, 0)).Y - LayoutCustomTime.Size.Height);
end;
function TMainForm.PowerOff: Boolean;
var
TokenHandle: THandle;
Priv, Prevst: TTokenPrivileges;
rl: DWORD;
begin
if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, TokenHandle) then
begin
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', Priv.Privileges[0].Luid) then
begin
Priv.PrivilegeCount := 1;
Priv.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(TokenHandle, False, Priv, SizeOf(Prevst), Prevst, rl);
end;
end;
Result := ExitWindowsEx(EWX_SHUTDOWN or EWX_POWEROFF, 0);
end;
procedure TMainForm.SetTime(Time: Integer);
begin
Display.TextSettings.FontColor := NormalDispColor;
IsUsedNotification := False;
PowerOffTime := GetTickCount div 1000 + Time;
Test;
TabControl.SetActiveTabWithTransition(TabItemRun, TTabTransition.Slide, TTabTransitionDirection.Normal);
Timer.Enabled := True;
end;
procedure TMainForm.ShowNotification;
begin
WinApi.Windows.MessageBeep(MB_ICONINFORMATION);
ShowTooltip(sNotificationBody);
end;
procedure TMainForm.Test;
var
Time: Integer;
begin
Time := PowerOffTime - GetTickCount div 1000;
if Time >= 0 then
Display.Text := SecondsToString(Time);
if Time <= 0 then
begin
Timer.Enabled := False;
Display.Text := sOffText;
PowerOff;
end;
if not IsUsedNotification and (Time <= NotificationTime) then
begin
IsUsedNotification := True;
Display.TextSettings.FontColor := AlarmDispColor;
ShowNotification;
end;
end;
procedure TMainForm.TimerTimer(Sender: TObject);
begin
Test;
end;
procedure TMainForm.TrackBarCustomChange(Sender: TObject);
begin
CustomTime.Text := sCustomTme + ': ' + SecondsToString(Trunc(TTrackBar(Sender).Value) * 60);
end;
procedure TMainForm.TrackBarCustomPainting(Sender: TObject; Canvas: TCanvas; const [Ref] ARect: TRectF);
begin
DrawTicks(TTrackBar(Sender), 25, 30, True, TLineKind.Both, 6, 8, $66000000);
end;
end.