Repository navigation
Expand file tree
/
Copy pathVertScrollBoxWithKBHandling.pas
More file actions
88 lines (75 loc) · 3.23 KB
/
Copy pathVertScrollBoxWithKBHandling.pas
File metadata and controls
88 lines (75 loc) · 3.23 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
unit VertScrollBoxWithKBHandling;
interface
uses
System.Types, System.Classes,FMX.Layouts, FMX.Controls, System.Math, FMX.forms, FMX.types;
type
TVertScrollBox = class(FMX.Layouts.TVertScrollBox)
private
FOldCalcContentBoundsProc:TOnCalcContentBoundsEvent;
FOldVKShow,FOldVKHidd:TVirtualKeyboardEvent;
FOldFocusChanged:TNotifyEvent;
FOwnerForm:TCommonCustomForm;
protected
FKBBounds: TRectF;
function DoCalcContentBounds: TRectF; override;
procedure UpdateKBBounds;
procedure FormVirtualKeyboardHidden(Sender: TObject;KeyboardVisible: Boolean;const Bounds: TRect);
procedure FormVirtualKeyboardShown(Sender: TObject;KeyboardVisible: Boolean;const Bounds: TRect);
procedure FormFocusChanged(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TVertScrollBox }
constructor TVertScrollBox.Create(AOwner: TComponent);
begin
inherited;
FOwnerForm:=(Owner as TCommonCustomForm);
FOldVKShow:=FOwnerForm.OnVirtualKeyboardShown;
FOwnerForm.OnVirtualKeyboardShown:=FormVirtualKeyboardShown;
FOldVKHidd:=FOwnerForm.OnVirtualKeyboardHidden;
FOwnerForm.OnVirtualKeyboardHidden :=FormVirtualKeyboardHidden;
FOldCalcContentBoundsProc:=OnCalcContentBounds;
FOldFocusChanged:=FOwnerForm.OnFocusChanged;
FOwnerForm.OnFocusChanged:=FormFocusChanged;
end;
function TVertScrollBox.DoCalcContentBounds: TRectF;
begin
if Assigned(FOldCalcContentBoundsProc) then FOldCalcContentBoundsProc(Self,Result);
if FKBBounds.Top>0 then Result.Bottom := Max(ContentBounds.Bottom, 2*ClientHeight - FKBBounds.Top);
end;
procedure TVertScrollBox.UpdateKBBounds;
var LFocusRect: TRectF;
Y:Single;
begin
Y:=0;
if Assigned(FOwnerForm.Focused) then begin
LFocusRect := TControl(FOwnerForm.Focused.GetObject).AbsoluteRect;
LFocusRect.Offset(Self.ViewportPosition);
if (LFocusRect.IntersectsWith(TRectF.Create(FKBBounds)))
and (LFocusRect.Bottom > FKBBounds.Top) then Y:=LFocusRect.Bottom - FKBBounds.Top;
end;
Self.RealignContent;
Application.ProcessMessages;
Self.ViewportPosition := PointF(Self.ViewportPosition.X, Y);
end;
procedure TVertScrollBox.FormFocusChanged(Sender: TObject);
begin
if Assigned(FOldFocusChanged) then FOldFocusChanged(Sender);
UpdateKBBounds;
end;
procedure TVertScrollBox.FormVirtualKeyboardHidden(Sender: TObject;KeyboardVisible: Boolean;const Bounds: TRect);
begin
if assigned(FOldVKHidd) then FOldVKHidd(Self,KeyboardVisible,Bounds);
FKBBounds.Create(0, 0, 0, 0);
UpdateKBBounds;
end;
procedure TVertScrollBox.FormVirtualKeyboardShown(Sender: TObject;KeyboardVisible: Boolean;const Bounds: TRect);
begin
if assigned(FOldVKShow) then FOldVKShow(Self,KeyboardVisible,Bounds);
FKBBounds := TRectF.Create(Bounds);
FKBBounds.TopLeft := FOwnerForm.ScreenToClient(FKBBounds.TopLeft);
FKBBounds.BottomRight := FOwnerForm.ScreenToClient(FKBBounds.BottomRight);
UpdateKBBounds;
end;
end.