-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathMainForm.pas
164 lines (144 loc) · 5.68 KB
/
MainForm.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
(**************************************************************************)
(* *)
(* Module: Unit 'MainForm' Copyright (c) 2021 *)
(* *)
(* Lucas Moura Belo - lmbelo *)
(* lucas.belo@live.com *)
(* Brazil *)
(* *)
(**************************************************************************)
(* Functionality: MainForm of PyFun *)
(* PyFun on Android *)
(* *)
(**************************************************************************)
(* This source code is distributed with no WARRANTY, for no reason or use.*)
(* Everyone is allowed to use and change this code free for his own tasks *)
(* and projects, as long as this header and its copyright text is intact. *)
(* For changed versions of this code, which are public distributed the *)
(* following additional conditions have to be fullfilled: *)
(* 1) The header has to contain a comment on the change and the author of *)
(* it. *)
(* 2) A copy of the changed source has to be sent to the above E-Mail *)
(* address or my then valid address, if this is possible to the *)
(* author. *)
(* The second condition has the target to maintain an up to date central *)
(* version of the component. If this condition is not acceptable for *)
(* confidential or legal reasons, everyone is free to derive a component *)
(* or to generate a diff file to my or other original sources. *)
(**************************************************************************)
unit MainForm;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Memo.Types,
FMX.Ani, FMX.StdCtrls, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo,
PythonEngine, FMX.PythonGUIInputOutput, System.Actions, FMX.ActnList,
FMX.Objects, FMX.Layouts, FMX.Platform, AppEnvironment, ProgressFrame,
System.Threading, FMX.ListBox, WrapDelphi, WrapDelphiFMX, FMX.ExtCtrls,
FMX.DateTimeCtrls;
type
TPyMainForm = class(TForm)
tbTop: TToolBar;
PythonEngine1: TPythonEngine;
ActionList1: TActionList;
actRun: TAction;
Label1: TLabel;
StyleBook1: TStyleBook;
PyDelphiWrapper1: TPyDelphiWrapper;
PythonModule1: TPythonModule;
tbBottom: TToolBar;
btnRun: TButton;
mmMainScript: TMemo;
mmOutput: TMemo;
spInputOutput: TSplitter;
RoundRect1: TRoundRect;
PythonGUIInputOutput1: TPythonGUIInputOutput;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure actRunExecute(Sender: TObject);
private
FAppEnv: TAppEnvironment;
function AppEventHandler(AAppEvent: TApplicationEvent; AContext: TObject): boolean;
procedure ConfigurePython();
procedure DisableComponents();
procedure EnableComponents();
function TryLoadingMainScript(): boolean;
procedure RunMainScript();
public
{ Public declarations }
end;
var
PyMainForm: TPyMainForm;
implementation
uses
PythonLoad, System.IOUtils;
{$R *.fmx}
{ TPyMainForm }
procedure TPyMainForm.FormCreate(Sender: TObject);
begin
DisableComponents();
FAppEnv := TAppEnvironment.Create(TProgressViewFrame.Create(Self, Self));
var LAppEventService := IFMXApplicationEventService(nil);
if TPlatformServices.Current.SupportsPlatformService(
IFMXApplicationEventService, IInterface(LAppEventService)) then
LAppEventService.SetApplicationEventHandler(AppEventHandler)
else begin
Log.d('Platform service "IFMXApplicationEventService" not supported.');
Halt(1);
end;
end;
procedure TPyMainForm.FormDestroy(Sender: TObject);
begin
FAppEnv.Free();
end;
procedure TPyMainForm.RunMainScript;
begin
PythonEngine1.ExecString(AnsiString(mmMainScript.Lines.Text));
end;
function TPyMainForm.TryLoadingMainScript: boolean;
begin
var LScript := TPath.Combine(TPath.GetDocumentsPath(), 'MobileControlsFrm.py');
Result := TFile.Exists(LScript);
if Result then begin
mmMainScript.Lines.Text := TFile.ReadAllText(LScript)
.Replace('%PATH%', TPath.GetDocumentsPath());
end;
end;
procedure TPyMainForm.actRunExecute(Sender: TObject);
begin
PythonEngine1.ExecString(AnsiString(mmMainScript.Lines.Text));
end;
function TPyMainForm.AppEventHandler(AAppEvent: TApplicationEvent;
AContext: TObject): boolean;
begin
case AAppEvent of
TApplicationEvent.FinishedLaunching: ConfigurePython();
end;
Result := true;
end;
procedure TPyMainForm.ConfigurePython;
begin
FAppEnv.InitializeEnvironmentAsync(PythonEngine1, true,
procedure(const AInitialized: boolean; const ALastErrorMsg: string) begin
TThread.Synchronize(nil,
procedure begin
if AInitialized then begin
if TryLoadingMainScript() then
RunMainScript()
else
ShowMessage('Unabled to run the script.');
EnableComponents();
end else
ShowMessage(ALastErrorMsg);
end);
end);
end;
procedure TPyMainForm.DisableComponents;
begin
btnRun.Enabled := false;
end;
procedure TPyMainForm.EnableComponents;
begin
btnRun.Enabled := true;
end;
end.