source: branches/Independent/Os.pas

Last change on this file was 67, checked in by chronos, 3 weeks ago
  • Modified: Apps split into separate units.
  • Modified: SystemApi moved into separate unit.
File size: 3.6 KB
Line 
1unit Os;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, DateUtils, Api, Console, FileSystem;
7
8type
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
78implementation
79
80uses
81 AppCode, AppTest, SystemApi;
82
83{ TAppThread }
84
85procedure TAppThread.Execute;
86begin
87 App.App.Run(App.Context);
88end;
89
90{ TRunningApp }
91
92procedure TRunningApp.SetApp(AValue: TApp);
93begin
94 if FApp = AValue then Exit;
95 FApp := AValue;
96end;
97
98procedure TRunningApp.Run;
99begin
100 AppThread.Start;
101end;
102
103constructor TRunningApp.Create;
104begin
105 Context := TAppContext.Create;
106 AppThread := TAppThread.Create(True);
107 AppThread.App := Self;
108end;
109
110destructor TRunningApp.Destroy;
111begin
112 AppThread.Terminate;
113 AppThread.WaitFor;
114 FreeAndNil(AppThread);
115 FreeAndNil(Context);
116 inherited;
117end;
118
119{ TRunningApps }
120
121function TRunningApps.AddNew(App: TApp; System: TSystem): TRunningApp;
122var
123 SystemApi: TSystemApi;
124begin
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);
132end;
133
134{ TWindows }
135
136function TWindows.AddNew: TWindow;
137begin
138 Result := TWindow.Create;
139 Add(Result);
140end;
141
142function TWindows.SearchByHandle(Handle: THandle): TWindow;
143var
144 I: Integer;
145begin
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;
150end;
151
152{ TSystem }
153
154procedure TSystem.Redraw;
155begin
156 if Assigned(FOnDraw) then FOnDraw(Self);
157end;
158
159procedure TSystem.RunApp(Name: string);
160var
161 App: TApp;
162 RunningApp: TRunningApp;
163begin
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.');
169end;
170
171procedure TSystem.Start;
172var
173 AppCode: TAppCode;
174begin
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');
182end;
183
184constructor TSystem.Create;
185begin
186 Handles := THandles.Create;
187 Windows := TWindows.Create;
188 Console := TConsole.Create;
189 FileSystem := TFileSystem.Create;
190 RunningApps := TRunningApps.Create;
191 Apps := TApps.Create;
192end;
193
194destructor TSystem.Destroy;
195begin
196 FreeAndNil(Apps);
197 FreeAndNil(RunningApps);
198 FreeAndNil(Console);
199 FreeAndNil(FileSystem);
200 FreeAndNil(Windows);
201 FreeAndNil(Handles);
202 inherited;
203end;
204
205end.
206
Note: See TracBrowser for help on using the repository browser.