/
UiContext.macOS.mm
229 lines (204 loc) 路 7.52 KB
/
UiContext.macOS.mm
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
/*****************************************************************************
* Copyright (c) 2014-2022 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#if defined(__APPLE__) && defined(__MACH__)
# include "UiContext.h"
# include <openrct2/common.h>
# include <openrct2/core/String.hpp>
# include <openrct2/ui/UiContext.h>
// undefine `interface` and `abstract`, because it's causing conflicts with Objective-C's keywords
# undef interface
# undef abstract
# include <ApplicationServices/ApplicationServices.h>
# import <Cocoa/Cocoa.h>
# include <CoreFoundation/CFBundle.h>
# include <SDL.h>
# include <mach-o/dyld.h>
# include <string>
namespace OpenRCT2::Ui
{
class macOSContext final : public IPlatformUiContext
{
private:
public:
macOSContext()
{
@autoreleasepool
{
if ([NSWindow respondsToSelector:@selector(setAllowsAutomaticWindowTabbing:)])
{
[NSWindow setAllowsAutomaticWindowTabbing:NO];
}
}
}
void SetWindowIcon(SDL_Window* window) override
{
}
bool IsSteamOverlayAttached() override
{
STUB();
return false;
}
void ShowMessageBox(SDL_Window* window, const std::string& message) override
{
@autoreleasepool
{
NSAlert* alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"OK"];
alert.messageText = [NSString stringWithUTF8String:message.c_str()];
[alert runModal];
}
}
bool HasMenuSupport() override
{
return false;
}
int32_t ShowMenuDialog(
const std::vector<std::string>& options, const std::string& title, const std::string& text) override
{
return -1;
}
void OpenFolder(const std::string& path) override
{
@autoreleasepool
{
NSString* nsPath = [NSString stringWithUTF8String:path.c_str()];
NSURL* folderURL = [NSURL fileURLWithPath:nsPath];
[[NSWorkspace sharedWorkspace] openURL:folderURL];
}
}
void OpenURL(const std::string& url) override
{
CFURLRef urlRef = CFURLCreateWithBytes(
nullptr, reinterpret_cast<const UInt8*>(url.c_str()), url.length(), kCFStringEncodingUTF8, nullptr);
LSOpenCFURLRef(urlRef, 0);
CFRelease(urlRef);
}
std::string ShowFileDialog(SDL_Window* window, const FileDialogDesc& desc) override
{
@autoreleasepool
{
NSMutableArray* extensions = [NSMutableArray new];
for (const OpenRCT2::Ui::FileDialogDesc::Filter& filter : desc.Filters)
{
if (filter.Pattern != "")
{
NSString* fp = [NSString stringWithUTF8String:filter.Pattern.c_str()];
fp = [fp stringByReplacingOccurrencesOfString:@"*." withString:@""];
[extensions addObjectsFromArray:[fp componentsSeparatedByString:@";"]];
}
}
NSString* directory;
NSSavePanel* panel;
if (desc.Type == FileDialogType::Save)
{
NSString* filePath = [NSString stringWithUTF8String:desc.DefaultFilename.c_str()];
directory = filePath.stringByDeletingLastPathComponent;
NSString* basename = filePath.lastPathComponent;
panel = [NSSavePanel savePanel];
panel.nameFieldStringValue = [NSString stringWithFormat:@"%@.%@", basename, extensions.firstObject];
}
else if (desc.Type == FileDialogType::Open)
{
directory = [NSString stringWithUTF8String:desc.InitialDirectory.c_str()];
NSOpenPanel* open = [NSOpenPanel openPanel];
open.canChooseDirectories = false;
open.canChooseFiles = true;
open.allowsMultipleSelection = false;
panel = open;
}
else
{
return std::string();
}
panel.title = [NSString stringWithUTF8String:desc.Title.c_str()];
panel.allowedFileTypes = extensions;
panel.directoryURL = [NSURL fileURLWithPath:directory];
if ([panel runModal] == NSModalResponseCancel)
{
return std::string();
}
else
{
return panel.URL.path.UTF8String;
}
}
}
std::string ShowDirectoryDialog(SDL_Window* window, const std::string& title) override
{
@autoreleasepool
{
NSOpenPanel* panel = [NSOpenPanel openPanel];
panel.canChooseFiles = false;
panel.canChooseDirectories = true;
panel.allowsMultipleSelection = false;
if ([panel runModal] == NSModalResponseOK)
{
NSString* selectedPath = panel.URL.path;
const char* path = selectedPath.UTF8String;
return path;
}
else
{
return "";
}
}
}
bool HasFilePicker() const override
{
return true;
}
private:
static int32_t Execute(const std::string& command, std::string* output = nullptr)
{
log_verbose("executing \"%s\"...", command.c_str());
FILE* fpipe = popen(command.c_str(), "r");
if (fpipe == nullptr)
{
return -1;
}
if (output != nullptr)
{
// Read output into buffer
std::vector<char> outputBuffer;
char buffer[1024];
size_t readBytes;
while ((readBytes = fread(buffer, 1, sizeof(buffer), fpipe)) > 0)
{
outputBuffer.insert(outputBuffer.begin(), buffer, buffer + readBytes);
}
// Trim line breaks
size_t outputLength = outputBuffer.size();
for (size_t i = outputLength - 1; i != SIZE_MAX; i--)
{
if (outputBuffer[i] == '\n')
{
outputLength = i;
}
else
{
break;
}
}
// Convert to string
*output = std::string(outputBuffer.data(), outputLength);
}
else
{
fflush(fpipe);
}
// Return exit code
return pclose(fpipe);
}
};
std::unique_ptr<IPlatformUiContext> CreatePlatformUiContext()
{
return std::make_unique<macOSContext>();
}
} // namespace OpenRCT2::Ui
#endif // __APPLE__ && __MACH__