-
Notifications
You must be signed in to change notification settings - Fork 19
/
NotificationWindows.pas
296 lines (251 loc) · 7.79 KB
/
NotificationWindows.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
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
unit NotificationWindows;
interface
uses
Winapi.Windows,
Winapi.Messages,
System.SysUtils,
System.Contnrs,
System.Variants,
System.Classes,
System.Math,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Generics.Collections,
AnyiQuack,
AQPControlAnimations;
type
TNotificationStack = class;
TNotificationWindow = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
private
const
CloseDelayID = 779;
var
FCloseTimeout: Integer;
FStack: TNotificationStack;
FClosed: Boolean;
procedure UpdateCloseTimeout;
procedure SetCloseTimeout(CloseTimeout: Integer);
protected
function AutoClosePossible: Boolean; virtual;
public
constructor Create(AOwner: TComponent); override;
procedure Close; reintroduce;
property Stack: TNotificationStack read FStack;
{**
* Auto close feature
*
* Assign a value > 0 to enable the feature or 0 to disable it.
* The timeout is in milliseconds. The notification window is getting closed
* automatically, after the defined timeout is expired and until the method
* AutoClosePossible returns True.
*}
property CloseTimeout: Integer read FCloseTimeout write SetCloseTimeout;
end;
TNotificationStack = class
private
const
PositionAnimationID = 123;
AlphaAnimationID = 456;
type
TNotificationList = TObjectList<TNotificationWindow>;
var
FList: TNotificationList;
FInPositionAnimationDuration: Integer;
FInAlphaAnimationDuration: Integer;
FOutPositionAnimationDuration: Integer;
FOutAlphaAnimationDuration: Integer;
procedure Close(NotificationWindow: TNotificationWindow);
procedure UpdatePositions;
property List: TNotificationList read FList;
public
constructor Create;
destructor Destroy; override;
procedure Add(NotificationWindow: TNotificationWindow);
procedure CloseAll(Animate: Boolean = True);
property InPositionAnimationDuration: Integer read FInPositionAnimationDuration
write FInPositionAnimationDuration;
property InAlphaAnimationDuration: Integer read FInAlphaAnimationDuration
write FInAlphaAnimationDuration;
property OutPositionAnimationDuration: Integer read FOutPositionAnimationDuration
write FOutPositionAnimationDuration;
property OutAlphaAnimationDuration: Integer read FOutAlphaAnimationDuration
write FOutAlphaAnimationDuration;
end;
implementation
{$R *.dfm}
{** TNotificationWindow **}
constructor TNotificationWindow.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Win 10 bugfix: TWinControl descendants are sometimes not rendered, but
// the switch AlphaBlend off and on again solve the issue.
AlphaBlend := False;
AlphaBlend := True;
end;
function TNotificationWindow.AutoClosePossible: Boolean;
begin
Result := (Screen.ActiveForm <> Self) and not PtInRect(BoundsRect, Mouse.CursorPos);
end;
procedure TNotificationWindow.Close;
begin
FClosed := True;
Stack.Close(Self);
end;
procedure TNotificationWindow.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caNone;
Close;
end;
procedure TNotificationWindow.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_ESCAPE then
Close;
end;
procedure TNotificationWindow.SetCloseTimeout(CloseTimeout: Integer);
begin
if CloseTimeout = FCloseTimeout then
Exit;
FCloseTimeout := CloseTimeout;
UpdateCloseTimeout;
end;
procedure TNotificationWindow.UpdateCloseTimeout;
begin
Take(Self)
.CancelDelays(CloseDelayID)
.IfThen(CloseTimeout > 0)
{**
* Wait "long", if the auto close feature is possible
*}
.IfThen(AutoClosePossible)
.EachDelay(CloseTimeout,
function(AQ: TAQ; O: TObject): Boolean
begin
if AutoClosePossible then
Close
else
UpdateCloseTimeout;
Result := True;
end, CloseDelayID)
{**
* Wait "short" (polling), if the auto close feature isn't possible
*}
.IfElse
.EachDelay(100,
function(AQ: TAQ; O: TObject): Boolean
begin
UpdateCloseTimeout;
Result := True;
end)
.IfEnd
.IfEnd;
end;
{** TNotificationStack **}
constructor TNotificationStack.Create;
begin
FList := TNotificationList.Create(False);
FInPositionAnimationDuration := 1000;
FInAlphaAnimationDuration := 800;
FOutPositionAnimationDuration := 500;
FOutAlphaAnimationDuration := 300;
end;
destructor TNotificationStack.Destroy;
begin
FList.OwnsObjects := True;
FList.Free;
inherited Destroy;
end;
procedure TNotificationStack.UpdatePositions;
var
Stack: TAQ;
WindowIndex, TopPosition: Integer;
begin
Stack := TAQ.Managed;
for WindowIndex := List.Count - 1 downto 0 do
if not List[WindowIndex].FClosed then
Stack.Add(List[WindowIndex]);
if Stack.Count = 0 then
begin
Stack.Die;
Exit;
end;
TopPosition := Screen.WorkAreaRect.Bottom;
WindowIndex := 0;
Stack
.CancelAnimations(PositionAnimationID)
.Each(
function(AQ: TAQ; O: TObject):Boolean
var
TargetNotf: TNotificationWindow absolute O;
AniPlugin: TAQPControlAnimations;
begin
Dec(TopPosition, TargetNotf.Height);
AniPlugin := Take(O).Plugin<TAQPControlAnimations>;
AniPlugin.BoundsAnimation(
Screen.WorkAreaRect.Right - TargetNotf.Width,
TopPosition, -1, -1,
IfThen(WindowIndex = 0, InPositionAnimationDuration div 2, InPositionAnimationDuration),
PositionAnimationID, TAQ.Ease(etBack, emInInverted));
AniPlugin.AlphaBlendAnimation(MAXBYTE, InAlphaAnimationDuration,
AlphaAnimationID, TAQ.Ease(etSinus));
Inc(WindowIndex);
Result := True;
end);
end;
procedure TNotificationStack.Add(NotificationWindow: TNotificationWindow);
begin
NotificationWindow.FStack := Self;
List.Add(NotificationWindow);
NotificationWindow.Left := Screen.WorkAreaRect.Right - NotificationWindow.Width;
NotificationWindow.Top := Screen.PrimaryMonitor.BoundsRect.Bottom;
ShowWindow(NotificationWindow.WindowHandle, SW_SHOWNOACTIVATE);
NotificationWindow.Visible := True;
NotificationWindow.AlphaBlend := True;
UpdatePositions;
end;
procedure TNotificationStack.Close(NotificationWindow: TNotificationWindow);
var
NextFocusedWindowIndex: Integer;
AniPlugin: TAQPControlAnimations;
begin
AniPlugin := Take(NotificationWindow)
.CancelAnimations
.Plugin<TAQPControlAnimations>;
AniPlugin.BoundsAnimation(Screen.WorkAreaRect.Right, NotificationWindow.Top, -1, -1,
OutPositionAnimationDuration, 0,
TAQ.Ease(etCubic, emInInverted));
AniPlugin.AlphaBlendAnimation(0, OutAlphaAnimationDuration, 0, TAQ.Ease(etSinus),
{**
* Handler for the OnComplete event
*}
procedure(Sender: TObject)
begin
NotificationWindow.Release;
end);
NextFocusedWindowIndex := List.Remove(NotificationWindow);
if (Screen.ActiveForm = NotificationWindow) and (List.Count > 0) then
begin
Dec(NextFocusedWindowIndex);
if NextFocusedWindowIndex < 0 then
NextFocusedWindowIndex:=0;
List[NextFocusedWindowIndex].SetFocus;
end;
UpdatePositions;
end;
procedure TNotificationStack.CloseAll(Animate: Boolean);
var
cc: Integer;
begin
for cc := List.Count - 1 downto 0 do
if Animate then
List[cc].Close
else
begin
List[cc].Release;
List.Delete(cc);
end;
end;
end.