1 | unit Os;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Generics.Collections, DateUtils, Api, Console, FileSystem;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TSystem = class;
|
---|
10 |
|
---|
11 | TRunningApp = class;
|
---|
12 |
|
---|
13 | { TAppThread }
|
---|
14 |
|
---|
15 | TAppThread = class(TThread)
|
---|
16 | App: TRunningApp;
|
---|
17 | procedure Execute; override;
|
---|
18 | end;
|
---|
19 |
|
---|
20 | { TRunningApp }
|
---|
21 |
|
---|
22 | TRunningApp = class
|
---|
23 | private
|
---|
24 | FApp: TApp;
|
---|
25 | procedure SetApp(AValue: TApp);
|
---|
26 | public
|
---|
27 | Context: TAppContext;
|
---|
28 | AppThread: TAppThread;
|
---|
29 | procedure Run;
|
---|
30 | constructor Create;
|
---|
31 | destructor Destroy; override;
|
---|
32 | property App: TApp read FApp write SetApp;
|
---|
33 | end;
|
---|
34 |
|
---|
35 | { TRunningApps }
|
---|
36 |
|
---|
37 | TRunningApps = class(TObjectList<TRunningApp>)
|
---|
38 | function AddNew(App: TApp; System: TSystem): TRunningApp;
|
---|
39 | end;
|
---|
40 |
|
---|
41 | { TWindow }
|
---|
42 |
|
---|
43 | TWindow = class
|
---|
44 | Handle: THandle;
|
---|
45 | Name: string;
|
---|
46 | Visible: Boolean;
|
---|
47 | Rect: TRect;
|
---|
48 | end;
|
---|
49 |
|
---|
50 | { TWindows }
|
---|
51 |
|
---|
52 | TWindows = class(TObjectList<TWindow>)
|
---|
53 | function AddNew: TWindow;
|
---|
54 | function SearchByHandle(Handle: THandle): TWindow;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | { TSystem }
|
---|
58 |
|
---|
59 | TSystem = class
|
---|
60 | private
|
---|
61 | FOnDraw: TNotifyEvent;
|
---|
62 | public
|
---|
63 | Console: TConsole;
|
---|
64 | FileSystem: TFileSystem;
|
---|
65 | Apps: TApps;
|
---|
66 | RunningApps: TRunningApps;
|
---|
67 | Windows: TWindows;
|
---|
68 | Handles: THandles;
|
---|
69 | procedure Redraw;
|
---|
70 | procedure RunApp(Name: string);
|
---|
71 | procedure Start;
|
---|
72 | constructor Create;
|
---|
73 | destructor Destroy; override;
|
---|
74 | property OnDraw: TNotifyEvent read FOnDraw write FOnDraw;
|
---|
75 | end;
|
---|
76 |
|
---|
77 |
|
---|
78 | implementation
|
---|
79 |
|
---|
80 | uses
|
---|
81 | AppCode, AppTest, SystemApi;
|
---|
82 |
|
---|
83 | { TAppThread }
|
---|
84 |
|
---|
85 | procedure TAppThread.Execute;
|
---|
86 | begin
|
---|
87 | App.App.Run(App.Context);
|
---|
88 | end;
|
---|
89 |
|
---|
90 | { TRunningApp }
|
---|
91 |
|
---|
92 | procedure TRunningApp.SetApp(AValue: TApp);
|
---|
93 | begin
|
---|
94 | if FApp = AValue then Exit;
|
---|
95 | FApp := AValue;
|
---|
96 | end;
|
---|
97 |
|
---|
98 | procedure TRunningApp.Run;
|
---|
99 | begin
|
---|
100 | AppThread.Start;
|
---|
101 | end;
|
---|
102 |
|
---|
103 | constructor TRunningApp.Create;
|
---|
104 | begin
|
---|
105 | Context := TAppContext.Create;
|
---|
106 | AppThread := TAppThread.Create(True);
|
---|
107 | AppThread.App := Self;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | destructor TRunningApp.Destroy;
|
---|
111 | begin
|
---|
112 | AppThread.Terminate;
|
---|
113 | AppThread.WaitFor;
|
---|
114 | FreeAndNil(AppThread);
|
---|
115 | FreeAndNil(Context);
|
---|
116 | inherited;
|
---|
117 | end;
|
---|
118 |
|
---|
119 | { TRunningApps }
|
---|
120 |
|
---|
121 | function TRunningApps.AddNew(App: TApp; System: TSystem): TRunningApp;
|
---|
122 | var
|
---|
123 | SystemApi: TSystemApi;
|
---|
124 | begin
|
---|
125 | Result := TRunningApp.Create;
|
---|
126 | SystemApi := TSystemApi.Create;
|
---|
127 | SystemApi.App := Result;
|
---|
128 | SystemApi.System := System;
|
---|
129 | Result.Context.Api := SystemApi;
|
---|
130 | Result.App := App;
|
---|
131 | Add(Result);
|
---|
132 | end;
|
---|
133 |
|
---|
134 | { TWindows }
|
---|
135 |
|
---|
136 | function TWindows.AddNew: TWindow;
|
---|
137 | begin
|
---|
138 | Result := TWindow.Create;
|
---|
139 | Add(Result);
|
---|
140 | end;
|
---|
141 |
|
---|
142 | function TWindows.SearchByHandle(Handle: THandle): TWindow;
|
---|
143 | var
|
---|
144 | I: Integer;
|
---|
145 | begin
|
---|
146 | I := 0;
|
---|
147 | while (I < Count) and (Items[I].Handle <> Handle) do Inc(I);
|
---|
148 | if I < Count then Result := Items[I]
|
---|
149 | else Result := nil;
|
---|
150 | end;
|
---|
151 |
|
---|
152 | { TSystem }
|
---|
153 |
|
---|
154 | procedure TSystem.Redraw;
|
---|
155 | begin
|
---|
156 | if Assigned(FOnDraw) then FOnDraw(Self);
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure TSystem.RunApp(Name: string);
|
---|
160 | var
|
---|
161 | App: TApp;
|
---|
162 | RunningApp: TRunningApp;
|
---|
163 | begin
|
---|
164 | App := Apps.SearchByName(Name);
|
---|
165 | if Assigned(App) then begin
|
---|
166 | RunningApp := RunningApps.AddNew(App, Self);
|
---|
167 | RunningApp.Run;
|
---|
168 | end else raise Exception.Create('App ' + Name + ' not found.');
|
---|
169 | end;
|
---|
170 |
|
---|
171 | procedure TSystem.Start;
|
---|
172 | var
|
---|
173 | AppCode: TAppCode;
|
---|
174 | begin
|
---|
175 | Apps.AddNew(TAppTest);
|
---|
176 | AppCode := TAppCode(Apps.AddNew(TAppCode));
|
---|
177 | AppCode.Code.Text := FileSystem.LoadFile('Code.exe');
|
---|
178 |
|
---|
179 | RunApp('Test');
|
---|
180 | RunApp('Test');
|
---|
181 | RunApp('Code');
|
---|
182 | end;
|
---|
183 |
|
---|
184 | constructor TSystem.Create;
|
---|
185 | begin
|
---|
186 | Handles := THandles.Create;
|
---|
187 | Windows := TWindows.Create;
|
---|
188 | Console := TConsole.Create;
|
---|
189 | FileSystem := TFileSystem.Create;
|
---|
190 | RunningApps := TRunningApps.Create;
|
---|
191 | Apps := TApps.Create;
|
---|
192 | end;
|
---|
193 |
|
---|
194 | destructor TSystem.Destroy;
|
---|
195 | begin
|
---|
196 | FreeAndNil(Apps);
|
---|
197 | FreeAndNil(RunningApps);
|
---|
198 | FreeAndNil(Console);
|
---|
199 | FreeAndNil(FileSystem);
|
---|
200 | FreeAndNil(Windows);
|
---|
201 | FreeAndNil(Handles);
|
---|
202 | inherited;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | end.
|
---|
206 |
|
---|