forked from Militereum/Militereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.mac.pas
268 lines (233 loc) · 6.82 KB
/
common.mac.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
unit common.mac;
interface
function autoRunEnabled: Boolean;
procedure enableAutoRun;
procedure disableAutoRun;
function darkModeEnabled: Boolean;
procedure enableDarkMode;
procedure disableDarkMode;
procedure initialize;
procedure beforeShowDialog;
procedure afterShowDialog;
implementation
uses
// Delphi
Macapi.AppKit,
Macapi.CocoaTypes,
Macapi.Foundation,
Macapi.ObjectiveC,
System.Classes,
System.IOUtils,
System.Messaging,
System.SysUtils,
// FireMonkey
FMX.Forms,
FMX.Helpers.Mac,
FMX.Platform,
FMX.Platform.Mac,
FMX.Styles,
// web3
web3.sync,
// Project
common,
main,
thread;
function launchAgents: string;
begin
Result := System.IOUtils.TPath.GetLibraryPath;
if (Result <> '') and (Result[Length(Result)] <> '/') then Result := Result + '/';
Result := Result + 'LaunchAgents';
end;
function launchAgent: string;
begin
Result := launchAgents;
if (Result <> '') and (Result[Length(Result)] <> '/') then Result := Result + '/';
Result := Result + 'com.militereum.agent.plist';
end;
function autoRunEnabled: Boolean;
begin
Result := System.IOUtils.TFile.Exists(launchAgent);
if Result then
begin
const plist = TStringList.Create;
try
for var line in plist do
begin
Result := SameText(line.Trim, System.SysUtils.Format('<string>%s</string>', [ParamStr(0)]));
if Result then EXIT;
end;
finally
plist.Free;
end;
end;
end;
procedure enableAutoRun;
begin
System.IOUtils.TFile.WriteAllText(launchagent, System.SysUtils.Format(
'<?xml version="1.0" encoding="UTF-8"?>' + #10 +
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' + #10 +
'<plist version="1.0">' + #10 +
'<dict>' + #10 +
' <key>Label</key>' + #10 +
' <string>com.militereum.agent</string>' + #10 +
' <key>LimitLoadToSessionType</key>' + #10 +
' <string>Aqua</string>' + #10 +
' <key>ProgramArguments</key>' + #10 +
' <array>' + #10 +
' <string>%s</string>' + #10 +
' <string>-autorun</string>' + #10 +
' </array>' + #10 +
' <key>ProcessType</key>' + #10 +
' <string>Interactive</string>' + #10 +
' <key>RunAtLoad</key>' + #10 +
' <true/>' + #10 +
' <key>KeepAlive</key>' + #10 +
' <false/>' + #10 +
'</dict>' + #10 +
'</plist>', [ParamStr(0)]));
end;
procedure disableAutoRun;
begin
System.IOUtils.TFile.Delete(launchAgent);
end;
function darkModeEnabled: Boolean;
begin
Result := False;
var S: IFMXSystemAppearanceService;
if TPlatformServices.Current.SupportsPlatformService(IFMXSystemAppearanceService, S) then
Result := S.ThemeKind = TSystemThemeKind.Dark;
end;
{$R 'assets\nero_mac.res'}
procedure enableDarkMode;
begin
TStyleManager.TrySetStyleFromResource('nero_mac');
end;
procedure disableDarkMode;
begin
TStyleManager.SetStyle(TStyleStreaming.LoadFromResource(hInstance, 'osxstyle', PChar(10)));
end;
type
IApplicationDelegate = interface(NSApplicationDelegate)
['{452088EB-EFEC-4407-AF08-018C1D03496B}']
procedure onMenuClicked(sender: NSMenuItem); cdecl;
procedure applicationWillBecomeActive(Notification: NSNotification); cdecl;
end;
type
TApplicationDelegate = class(TOCLocal, IApplicationDelegate)
private
FFirstBecomeActive: Boolean;
public
constructor Create;
function applicationShouldTerminate(Notification: NSNotification): NSInteger; cdecl;
procedure applicationWillTerminate(Notification: NSNotification); cdecl;
procedure applicationDidFinishLaunching(Notification: NSNotification); cdecl;
function applicationDockMenu(sender: NSApplication): NSMenu; cdecl;
procedure applicationDidHide(Notification: NSNotification); cdecl;
procedure applicationDidUnhide(Notification: NSNotification); cdecl;
procedure onMenuClicked(sender: NSMenuItem); cdecl;
procedure applicationWillBecomeActive(Notification: NSNotification); cdecl;
end;
var
app: NSApplication;
old: NSApplicationDelegate;
new: TApplicationDelegate;
cnt: ICriticalInt64 = nil;
function counter: ICriticalInt64;
begin
if not Assigned(cnt) then cnt := TCriticalInt64.Create(0);
Result := cnt;
end;
procedure initialize;
begin
if FindCmdLineSwitch('autorun') then
TMessageManager.DefaultManager.SubscribeToMessage(TMainFormChangedMessage, procedure(const Sender: TObject; const Msg: TMessage)
begin
const main = TMainFormChangedMessage(Msg).Value;
if Assigned(main) and main.Visible then
begin
beforeShowDialog;
try
main.Visible := False;
finally
afterShowDialog;
end;
end;
end);
app := TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication);
old := app.delegate;
new := TApplicationDelegate.Create;
app.setDelegate(IApplicationDelegate(new));
end;
procedure beforeShowDialog;
begin
counter.Enter;
try
counter.Inc;
finally
counter.Leave;
end;
end;
procedure afterShowDialog;
begin
counter.Enter;
try
counter.Dec;
finally
counter.Leave;
end;
end;
function SendOSXMessage(const Sender: TObject; const OSXMessageClass: TOSXMessageClass; const NSSender: NSObject): NSObject;
begin
const MessageObject = TOSXMessageObject.Create(NSSender);
try
TMessageManager.DefaultManager.SendMessage(Sender, OSXMessageClass.Create(MessageObject, False), True);
Result := MessageObject.ReturnValue;
finally
MessageObject.Free;
end;
end;
{ TApplicationDelegate }
constructor TApplicationDelegate.Create;
begin
inherited Create;
FFirstBecomeActive := True;
end;
function TApplicationDelegate.applicationDockMenu(sender: NSApplication): NSMenu;
begin
Result := old.applicationDockMenu(sender);
end;
function TApplicationDelegate.applicationShouldTerminate(Notification: NSNotification): NSInteger;
begin
Result := old.applicationShouldTerminate(Notification);
end;
procedure TApplicationDelegate.applicationDidHide(Notification: NSNotification);
begin
old.applicationDidHide(Notification);
end;
procedure TApplicationDelegate.applicationWillTerminate(Notification: NSNotification);
begin
old.applicationWillTerminate(Notification)
end;
procedure TApplicationDelegate.applicationDidUnhide(Notification: NSNotification);
begin
old.applicationDidUnhide(Notification);
end;
procedure TApplicationDelegate.onMenuClicked(sender: NSMenuItem);
begin
SendOSXMessage(Self, TApplicationMenuClickedMessage, sender);
end;
procedure TApplicationDelegate.applicationDidFinishLaunching(Notification: NSNotification);
begin
old.applicationDidFinishLaunching(Notification);
end;
procedure TApplicationDelegate.applicationWillBecomeActive(Notification: NSNotification);
begin
if FFirstBecomeActive then
FFirstBecomeActive := False
else
if (counter.Get = 0) and not common.Debug then thread.synchronize(procedure
begin
if Assigned(FrmMain) and not(FrmMain.Visible) then FrmMain.Show;
end);
end;
end.