-
Notifications
You must be signed in to change notification settings - Fork 33
/
HGM.FMX.SmoothScroll.pas
227 lines (199 loc) · 5.77 KB
/
HGM.FMX.SmoothScroll.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
unit HGM.FMX.SmoothScroll;
interface
uses
System.Classes, System.Types, FMX.Types, FMX.Layouts;
type
TSmoothScroll = class(TComponent)
private
FTimerUpdateScroll: TTimer;
FTimerAutoScroll: TTimer;
FScrollImpulse: Single;
FScroll: TCustomScrollBox;
FMaxSpeed: Integer;
FScrollDelta: Integer;
FIncrement: Single;
FAutoScrollDown: Boolean;
FAutoScrollOldPos: Single;
FEnableSmoothScroll: Boolean;
procedure FDoScroll(Delta: Single);
procedure TimerUpdateScrollTimer(Sender: TObject);
procedure TimerAutoScrollTimer(Sender: TObject);
procedure SetScroll(const Value: TCustomScrollBox);
procedure SetMaxSpeed(const Value: Integer);
procedure SetScrollDelta(const Value: Integer);
procedure FOverMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean);
procedure SetIncrement(const Value: Single);
procedure SetUpdateInterval(const Value: Cardinal);
function GetUpdateInterval: Cardinal;
function GetIsEnd: Boolean;
procedure SetEnableSmoothScroll(const Value: Boolean);
public
constructor Create(AOwner: TComponent); override;
constructor CreateFor(AScroll: TCustomScrollBox);
procedure ScrollEvent(WheelDelta: Single);
procedure Boost(Value: Single);
procedure ScrollDown;
procedure ToEnd;
procedure Stop;
property Scroll: TCustomScrollBox read FScroll write SetScroll;
property MaxSpeed: Integer read FMaxSpeed write SetMaxSpeed;
property Increment: Single read FIncrement write SetIncrement;
property UpdateInterval: Cardinal read GetUpdateInterval write SetUpdateInterval;
property ScrollDelta: Integer read FScrollDelta write SetScrollDelta;
property IsEnd: Boolean read GetIsEnd;
property EnableSmoothScroll: Boolean read FEnableSmoothScroll write SetEnableSmoothScroll;
end;
implementation
uses
System.Math;
{ TSmoothScroll }
procedure TSmoothScroll.Boost(Value: Single);
begin
ScrollEvent(Value);
end;
constructor TSmoothScroll.Create(AOwner: TComponent);
begin
inherited;
FEnableSmoothScroll := True;
FAutoScrollDown := False;
FAutoScrollOldPos := 0;
FMaxSpeed := 80;
FScrollImpulse := 0;
FScrollDelta := 9;
FIncrement := 1;
FTimerUpdateScroll := TTimer.Create(Self);
with FTimerUpdateScroll do
begin
Enabled := False;
Interval := 10;
OnTimer := TimerUpdateScrollTimer;
end;
FTimerAutoScroll := TTimer.Create(Self);
with FTimerAutoScroll do
begin
Enabled := False;
Interval := 100;
OnTimer := TimerAutoScrollTimer;
end;
end;
constructor TSmoothScroll.CreateFor(AScroll: TCustomScrollBox);
begin
Create(AScroll);
FScroll := AScroll;
AScroll.OnMouseWheel := FOverMouseWheel;
end;
procedure TSmoothScroll.FOverMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer; var Handled: Boolean);
begin
FTimerAutoScroll.Enabled := False;
Handled := True;
ScrollEvent(WheelDelta);
end;
function TSmoothScroll.GetIsEnd: Boolean;
begin
Result := (Abs(FScroll.ViewportPosition.Y + FScroll.ClientHeight - FScroll.ContentBounds.Height) < 2) or
(FScroll.ContentBounds.Height < FScroll.ClientHeight);
end;
function TSmoothScroll.GetUpdateInterval: Cardinal;
begin
Result := FTimerUpdateScroll.Interval;
end;
procedure TSmoothScroll.FDoScroll(Delta: Single);
begin
if FEnableSmoothScroll or FTimerAutoScroll.Enabled then
begin
if not FTimerUpdateScroll.Enabled then
FTimerUpdateScroll.Enabled := True;
FScrollImpulse := Max(-FMaxSpeed, Min(FScrollImpulse - Delta, FMaxSpeed));
end
else
begin
FScroll.ViewportPosition := TPointF.Create(0, FScroll.ViewportPosition.Y - Delta);
end;
end;
procedure TSmoothScroll.ScrollDown;
begin
FAutoScrollDown := True;
FTimerAutoScroll.Enabled := True;
FTimerUpdateScroll.Enabled := True;
TimerAutoScrollTimer(nil);
end;
procedure TSmoothScroll.ToEnd;
begin
FScroll.ViewportPosition := TPointF.Create(FScroll.ViewportPosition.X, FScroll.ContentBounds.Bottom + 100);
end;
procedure TSmoothScroll.ScrollEvent(WheelDelta: Single);
begin
FDoScroll(WheelDelta / FScrollDelta);
end;
procedure TSmoothScroll.SetEnableSmoothScroll(const Value: Boolean);
begin
FEnableSmoothScroll := Value;
end;
procedure TSmoothScroll.SetIncrement(const Value: Single);
begin
FIncrement := Value;
end;
procedure TSmoothScroll.SetMaxSpeed(const Value: Integer);
begin
FMaxSpeed := Value;
end;
procedure TSmoothScroll.SetScroll(const Value: TCustomScrollBox);
begin
FScroll := Value;
end;
procedure TSmoothScroll.SetScrollDelta(const Value: Integer);
begin
FScrollDelta := Value;
end;
procedure TSmoothScroll.SetUpdateInterval(const Value: Cardinal);
begin
FTimerUpdateScroll.Interval := Value;
end;
procedure TSmoothScroll.Stop;
begin
FTimerUpdateScroll.Enabled := False;
FTimerAutoScroll.Enabled := False;
FScrollImpulse := 0;
end;
procedure TSmoothScroll.TimerAutoScrollTimer(Sender: TObject);
begin
if not FTimerUpdateScroll.Enabled then
begin
FTimerAutoScroll.Enabled := False;
Exit;
end;
if FAutoScrollDown then
ScrollEvent(-100)
else
ScrollEvent(+100);
end;
procedure TSmoothScroll.TimerUpdateScrollTimer(Sender: TObject);
var
Old: TPointF;
begin
if not Assigned(FScroll) then
begin
FTimerUpdateScroll.Enabled := False;
Exit;
end;
if Abs(FScrollImpulse) > (FIncrement * 2) then
begin
if FScrollImpulse < 0 then
FScrollImpulse := FScrollImpulse + FIncrement
else
FScrollImpulse := FScrollImpulse - FIncrement;
Old := FScroll.ViewportPosition;
FScroll.ViewportPosition := TPointF.Create(0, FScroll.ViewportPosition.Y + FScrollImpulse);
if FScroll.ViewportPosition = Old then
begin
FScrollImpulse := 0;
FTimerUpdateScroll.Enabled := False;
end;
end
else
begin
FScrollImpulse := 0;
FTimerUpdateScroll.Enabled := False;
end;
end;
end.