-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacOSApplication.cpp
More file actions
213 lines (163 loc) · 7.89 KB
/
Copy pathMacOSApplication.cpp
File metadata and controls
213 lines (163 loc) · 7.89 KB
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
#include "MacOSApplication.h"
#include "SystemDataPool.h"
#include "SystemFunctions.h"
#include "SystemLogging.h"
#include "SystemMemory.h"
#include "SystemPlatformFunctions.h"
MemoryArena ApplicationMemoryArena;
bool ApplicationExited = false;
uint64_t ApplePerformanceCounterStart;
uint64_t ApplePerformanceCounterFrequencyInSeconds;
void InitApplicationMemory()
{
if (ApplicationMemoryArena.Storage == nullptr)
{
ApplicationMemoryArena = SystemAllocateMemoryArena();
SystemLogDebugMessage(ElemLogMessageCategory_Application, "Init OK");
#ifdef _DEBUG
SystemLogDebugMessage(ElemLogMessageCategory_Application, "Debug Mode");
#endif
ApplePerformanceCounterStart = SystemPlatformGetHighPerformanceCounter();
ApplePerformanceCounterFrequencyInSeconds = SystemPlatformGetHighPerformanceCounterFrequencyInSeconds();
}
}
void CreateApplicationMenu(ReadOnlySpan<char> applicationName)
{
using NS::StringEncoding::UTF8StringEncoding;
auto applicationNameString = NS::String::string(applicationName.Pointer, UTF8StringEncoding);
auto mainMenu = NS::RetainPtr(NS::Menu::alloc()->init(MTLSTR("MainMenu")));
auto applicationMenuItem = NS::RetainPtr(mainMenu->addItem(MTLSTR("ApplicationMenu"), nullptr, MTLSTR("")));
auto applicationSubMenu = NS::RetainPtr(NS::Menu::alloc()->init(MTLSTR("Application")));
applicationMenuItem->setSubmenu(applicationSubMenu.get());
auto aboutSelector = NS::MenuItem::registerActionCallback("about", [](void*, SEL, const NS::Object* pSender)
{
NS::Application::sharedApplication()->orderFrontStandardAboutPanel(nullptr);
});
applicationSubMenu->addItem(NS::String::string("About ", UTF8StringEncoding)->stringByAppendingString(applicationNameString), aboutSelector, MTLSTR(""));
applicationSubMenu->addItem(NS::MenuItem::separatorItem());
auto serviceMenuItem = applicationSubMenu->addItem(MTLSTR("Service"), nullptr, MTLSTR(""));
auto serviceMenu = NS::RetainPtr(NS::Menu::alloc()->init(MTLSTR("Service")));
serviceMenuItem->setSubmenu(serviceMenu.get());
applicationSubMenu->addItem(NS::MenuItem::separatorItem());
auto hideSelector = NS::MenuItem::registerActionCallback("appHide", [](void*,SEL,const NS::Object* pSender)
{
NS::Application::sharedApplication()->hide(pSender);
});
applicationSubMenu->addItem(NS::String::string("Hide ", UTF8StringEncoding)->stringByAppendingString(applicationNameString), hideSelector, MTLSTR("h"));
auto hideOthersSelector = NS::MenuItem::registerActionCallback("appHideOthers", [](void*,SEL,const NS::Object* pSender)
{
NS::Application::sharedApplication()->hideOtherApplications(pSender);
});
auto hideOthersMenuItem = applicationSubMenu->addItem(NS::String::string("Hide Others", UTF8StringEncoding), hideOthersSelector, MTLSTR("h"));
hideOthersMenuItem->setKeyEquivalentModifierMask(NS::EventModifierFlagCommand | NS::EventModifierFlagOption);
auto showAllSelector = NS::MenuItem::registerActionCallback("appShowall", [](void*,SEL,const NS::Object* pSender)
{
NS::Application::sharedApplication()->unhideAllApplications(pSender);
});
applicationSubMenu->addItem(NS::String::string("Show All", UTF8StringEncoding), showAllSelector, MTLSTR(""));
applicationSubMenu->addItem(NS::MenuItem::separatorItem());
auto quitSelector = NS::MenuItem::registerActionCallback("appQuit", [](void*,SEL,const NS::Object* pSender)
{
NS::Application::sharedApplication()->terminate(pSender);
});
applicationSubMenu->addItem(NS::String::string("Quit ", UTF8StringEncoding)->stringByAppendingString(applicationNameString), quitSelector, MTLSTR("q"));
auto windowMenuItem = NS::RetainPtr(mainMenu->addItem(MTLSTR("WindowMenu"), nullptr, MTLSTR("")));
auto windowSubMenu = NS::RetainPtr(NS::Menu::alloc()->init(MTLSTR("Window")));
windowMenuItem->setSubmenu(windowSubMenu.get());
// TODO: The 2 items here should be on the top
auto minimizeSelector = NS::MenuItem::registerActionCallback("minimize", [](void*,SEL,const NS::Object* pSender)
{
NS::Application::sharedApplication()->mainWindow()->miniaturize(nullptr);
});
windowSubMenu->addItem(NS::String::string("Minimize", UTF8StringEncoding), minimizeSelector, MTLSTR("m"));
auto zoomSelector = NS::MenuItem::registerActionCallback("zoom", [](void*,SEL,const NS::Object* pSender)
{
NS::Application::sharedApplication()->mainWindow()->zoom(nullptr);
});
windowSubMenu->addItem(NS::String::string("Zoom", UTF8StringEncoding), zoomSelector, MTLSTR(""));
NS::Application::sharedApplication()->setMainMenu(mainMenu.get());
NS::Application::sharedApplication()->setServicesMenu(serviceMenu.get());
NS::Application::sharedApplication()->setWindowsMenu(windowSubMenu.get());
}
ElemAPI void ElemConfigureLogHandler(ElemLogHandlerPtr logHandler)
{
if (logHandler)
{
SystemRegisterLogHandler(logHandler);
}
}
ElemAPI ElemSystemInfo ElemGetSystemInfo()
{
auto stackMemoryArena = SystemGetStackMemoryArena();
auto executablePath = SystemPlatformGetExecutablePath(stackMemoryArena);
auto environment = SystemPlatformGetEnvironment(stackMemoryArena);
auto lastIndex = SystemLastIndexOf(executablePath, environment->PathSeparator);
SystemAssert(lastIndex != -1);
auto applicationPath = SystemPushArrayZero<char>(stackMemoryArena, lastIndex + 1);
SystemCopyBuffer(applicationPath, executablePath.Slice(0, lastIndex + 1));
return
{
.Platform = ElemPlatform_MacOS,
.ApplicationPath = applicationPath.Pointer,
.SupportMultiWindows = true
};
}
ElemAPI int32_t ElemRunApplication(const ElemRunApplicationParameters* parameters)
{
InitApplicationMemory();
auto applicationDelegate = MacOSApplicationDelegate(parameters);
auto sharedApplication = NS::Application::sharedApplication();
sharedApplication->setDelegate(&applicationDelegate);
sharedApplication->run();
return 0;
}
ElemAPI void ElemExitApplication(int32_t exitCode)
{
if (exitCode == 0)
{
NS::Application::sharedApplication()->terminate(nullptr);
}
else
{
exit(exitCode);
}
}
MacOSApplicationDelegate::MacOSApplicationDelegate(const ElemRunApplicationParameters* parameters)
{
_runParameters = parameters;
}
void MacOSApplicationDelegate::applicationDidFinishLaunching(NS::Notification* pNotification)
{
auto sharedApplication = NS::Application::sharedApplication();
sharedApplication->setActivationPolicy(NS::ActivationPolicy::ActivationPolicyRegular);
sharedApplication->activateIgnoringOtherApps(true);
sharedApplication->finishLaunching();
auto applicationName = "Elemental Application";
if (_runParameters && _runParameters->ApplicationName)
{
applicationName = _runParameters->ApplicationName;
}
CreateApplicationMenu(applicationName);
if (_runParameters->InitHandler)
{
_runParameters->InitHandler(_runParameters->Payload);
}
}
bool MacOSApplicationDelegate::applicationShouldTerminateAfterLastWindowClosed(NS::Application* pSender)
{
return true;
}
NS::TerminateReply MacOSApplicationDelegate::applicationShouldTerminate(NS::Application* pSender)
{
auto stackMemoryArena = SystemGetStackMemoryArena();
auto allocationInfos = SystemGetAllocationInfos();
SystemLogDebugMessage(ElemLogMessageCategory_Application, "Allocated lib memory before releasing: %s/%s",
SystemFormatMemorySize(stackMemoryArena, allocationInfos.CommittedBytes).Pointer,
SystemFormatMemorySize(stackMemoryArena, allocationInfos.ReservedBytes).Pointer);
ApplicationExited = true;
if (_runParameters && _runParameters->FreeHandler)
{
_runParameters->FreeHandler(_runParameters->Payload);
}
return NS::TerminateReplyTerminateNow;
}