forked from blaisexen/Camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDW.NativeControl.Android.pas
170 lines (146 loc) · 4.08 KB
/
DW.NativeControl.Android.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
unit DW.NativeControl.Android;
{$I DW.GlobalDefines.inc}
interface
uses
// RTL
System.Types, System.Classes,
// Android
Androidapi.JNI.Embarcadero, Androidapi.JNI.GraphicsContentViewText,
// FMX
FMX.Controls;
type
TNativeControl = class(TControl)
private
FBounds: TRect;
FNativeLayout: JNativeLayout;
FScale: Single;
procedure DoHide;
procedure DoResize;
procedure DoShow;
procedure FinaliseLayout;
procedure Initialise;
procedure InitialiseLayout;
procedure UpdateBounds;
function GetNativeControl: Pointer;
protected
FNativeControl: JView;
function CreateNativeControl: JView; virtual;
function GetIsVisible: Boolean;
procedure Resize; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Hide; override;
procedure Show; override;
property NativeControl: Pointer read GetNativeControl;
end;
implementation
uses
// RTL
System.SysUtils,
// Android
Androidapi.Helpers, Androidapi.JNI.App,
// FMX
FMX.Helpers.Android, FMX.Types, FMX.Forms, FMX.Platform, FMX.Platform.Android;
type
TOpenControl = class(TControl);
{ TNativeControl }
constructor TNativeControl.Create(AOwner: TComponent);
begin
inherited;
Initialise;
end;
destructor TNativeControl.Destroy;
begin
if (FNativeControl <> nil) and (FNativeLayout <> nil) then
FinaliseLayout;
inherited;
end;
procedure TNativeControl.FinaliseLayout;
begin
TUIThreadCaller.Call<JView, JNativeLayout>(
procedure (ANativeControl: JView; ANativeLayout: JNativeLayout)
begin
ANativeControl.setVisibility(TJView.JavaClass.INVISIBLE);
ANativeLayout.setControl(nil);
end, FNativeControl, FNativeLayout);
end;
function TNativeControl.CreateNativeControl: JView;
begin
Result := nil;
end;
procedure TNativeControl.Initialise;
var
LScreenService: IFMXScreenService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, LScreenService) then
FScale := LScreenService.GetScreenScale
else
FScale := 1;
CallInUIThreadAndWaitFinishing(InitialiseLayout);
end;
procedure TNativeControl.InitialiseLayout;
begin
FNativeControl := CreateNativeControl;
if FNativeControl <> nil then
begin
FNativeLayout := TJNativeLayout.JavaClass.init(TAndroidHelper.Activity, MainActivity.getWindow.getDecorView.getWindowToken);
FNativeLayout.setControl(FNativeControl);
end;
end;
procedure TNativeControl.UpdateBounds;
var
LPoint: TPointF;
LControl: IControl;
begin
if Parent is TCommonCustomForm then
LPoint := Position.Point
else if Supports(ParentControl, IControl, LControl) then
LPoint := LControl.LocalToScreen(Position.Point)
else
Exit; // <======
FBounds := Rect(Round(LPoint.X * FScale), Round(LPoint.Y * FScale), Round(Width * FScale), Round(Height * FScale));
end;
procedure TNativeControl.DoResize;
begin
UpdateBounds;
FNativeLayout.setPosition(FBounds.Left, FBounds.Top);
FNativeLayout.setSize(FBounds.Right, FBounds.Bottom);
end;
procedure TNativeControl.Resize;
begin
inherited;
CallInUIThread(DoResize);
end;
function TNativeControl.GetIsVisible: Boolean;
begin
Result := (FNativeControl <> nil) and (FNativeControl.getVisibility = TJView.JavaClass.VISIBLE);
end;
function TNativeControl.GetNativeControl: Pointer;
begin
Result := FNativeControl;
end;
procedure TNativeControl.DoHide;
begin
if (FNativeControl <> nil) and (FNativeControl.getVisibility <> TJView.JavaClass.INVISIBLE) then
FNativeControl.setVisibility(TJView.JavaClass.INVISIBLE);
end;
procedure TNativeControl.Hide;
begin
inherited;
CallInUIThread(DoHide);
end;
procedure TNativeControl.DoShow;
begin
if (FNativeControl <> nil) and (FNativeControl.getVisibility <> TJView.JavaClass.VISIBLE) then
begin
DoResize;
FNativeControl.setVisibility(TJView.JavaClass.VISIBLE);
end;
end;
procedure TNativeControl.Show;
begin
inherited;
CallInUIThread(DoShow);
end;
end.