@geastack/host 0.1.27 → 0.1.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/audio_runtime.cpp +131 -29
- package/bluetooth.cpp +22 -0
- package/host/audio.cpp +132 -4
- package/host/ble.cpp +44 -0
- package/host/bluetooth_central_apple.mm +412 -0
- package/host/crypto.cpp +318 -0
- package/host/crypto_apple.mm +69 -0
- package/host/fetch.cpp +47 -2
- package/host/files.cpp +194 -0
- package/host/files_apple.mm +25 -0
- package/host/image.cpp +17 -6
- package/host/media.cpp +8 -7
- package/host/menu.cpp +49 -0
- package/host/menu_apple.mm +249 -0
- package/host/menu_bar.cpp +31 -0
- package/host/menu_bar_apple.mm +220 -0
- package/host/opus.cpp +31 -0
- package/host/opus_apple.mm +134 -0
- package/host/tile_loader.cpp +5 -3
- package/host/websocket.cpp +29 -2
- package/host/websocket_apple.mm +30 -9
- package/host/websocket_curl.cpp +15 -4
- package/include/gea/embedded-host.h +5 -0
- package/include/host/audio.h +12 -0
- package/include/host/backends.h +25 -0
- package/include/host/crypto.h +46 -0
- package/include/host/files.h +42 -0
- package/include/host/menu.h +93 -0
- package/include/host/menu_bar.h +48 -0
- package/include/host/navigator.h +67 -1
- package/include/host/opus.h +35 -0
- package/include/host/websocket.h +2 -0
- package/package.json +1 -1
package/host/image.cpp
CHANGED
|
@@ -57,7 +57,14 @@ namespace {
|
|
|
57
57
|
bool (*g_mount_provider)() = nullptr;
|
|
58
58
|
bool (*g_fallback_mount_provider)() = nullptr;
|
|
59
59
|
std::string (*g_read_path_provider)(const std::string &) = nullptr;
|
|
60
|
+
std::string (*g_path_provider)(const std::string &) = nullptr;
|
|
60
61
|
} // namespace
|
|
62
|
+
void setPathProvider(std::string (*provider)(const std::string &)) {
|
|
63
|
+
g_path_provider = provider;
|
|
64
|
+
}
|
|
65
|
+
std::string hostPath(const std::string &path) {
|
|
66
|
+
return g_path_provider ? g_path_provider(path) : path;
|
|
67
|
+
}
|
|
61
68
|
void setMountProvider(bool (*provider)()) { g_mount_provider = provider; }
|
|
62
69
|
void setFallbackMountProvider(bool (*provider)()) {
|
|
63
70
|
g_fallback_mount_provider = provider;
|
|
@@ -66,9 +73,11 @@ void setReadPathProvider(std::string (*provider)(const std::string &)) {
|
|
|
66
73
|
g_read_path_provider = provider;
|
|
67
74
|
}
|
|
68
75
|
static std::FILE *openRead(const std::string &path) {
|
|
76
|
+
if (std::FILE *file = std::fopen(hostPath(path).c_str(), "rb"))
|
|
77
|
+
return file;
|
|
69
78
|
if (g_read_path_provider)
|
|
70
79
|
return std::fopen(g_read_path_provider(path).c_str(), "rb");
|
|
71
|
-
return
|
|
80
|
+
return nullptr;
|
|
72
81
|
}
|
|
73
82
|
bool ensureMounted() {
|
|
74
83
|
auto provider =
|
|
@@ -286,9 +295,10 @@ bool ImageService::writeFile(const std::string &path,
|
|
|
286
295
|
if (path.empty() || bytes.empty() ||
|
|
287
296
|
!storageReadyFor(path))
|
|
288
297
|
return;
|
|
289
|
-
|
|
298
|
+
const std::string host = gea::platform::storage::hostPath(path);
|
|
299
|
+
makeParentDirs(host);
|
|
290
300
|
gea::platform::storage::FileOperations files;
|
|
291
|
-
result = gea::platform::storage::writeFileAtomically(
|
|
301
|
+
result = gea::platform::storage::writeFileAtomically(host, bytes, files);
|
|
292
302
|
});
|
|
293
303
|
return result;
|
|
294
304
|
}
|
|
@@ -297,7 +307,7 @@ bool ImageService::removeFile(const std::string &path) const {
|
|
|
297
307
|
bool result = false;
|
|
298
308
|
gea::platform::onInternalStack([&] {
|
|
299
309
|
if (!path.empty() && storageReadyFor(path))
|
|
300
|
-
result = std::remove(path.c_str()) == 0;
|
|
310
|
+
result = std::remove(gea::platform::storage::hostPath(path).c_str()) == 0;
|
|
301
311
|
});
|
|
302
312
|
return result;
|
|
303
313
|
}
|
|
@@ -350,7 +360,8 @@ std::string ImageService::listFiles(const std::string &path) const {
|
|
|
350
360
|
if (path.empty() || !storageReadyFor(path))
|
|
351
361
|
return;
|
|
352
362
|
#ifdef GEA_HOST_HAS_DIRENT
|
|
353
|
-
|
|
363
|
+
const std::string host = gea::platform::storage::hostPath(path);
|
|
364
|
+
DIR *dir = opendir(host.c_str());
|
|
354
365
|
if (!dir)
|
|
355
366
|
return;
|
|
356
367
|
while (const dirent *entry = readdir(dir)) {
|
|
@@ -359,7 +370,7 @@ std::string ImageService::listFiles(const std::string &path) const {
|
|
|
359
370
|
bool regular = entry->d_type == DT_REG;
|
|
360
371
|
if (entry->d_type == DT_UNKNOWN) {
|
|
361
372
|
struct stat st{};
|
|
362
|
-
const std::string full =
|
|
373
|
+
const std::string full = host + "/" + entry->d_name;
|
|
363
374
|
regular = stat(full.c_str(), &st) == 0 && S_ISREG(st.st_mode);
|
|
364
375
|
}
|
|
365
376
|
if (!regular)
|
package/host/media.cpp
CHANGED
|
@@ -245,9 +245,10 @@ bool patchWavHeader(FILE *file, std::uint32_t dataBytes) {
|
|
|
245
245
|
double writeWavFileOnInternalStack(const std::string &path, const std::vector<std::int16_t> &samples, int sampleRate, int channels) {
|
|
246
246
|
if (path.empty()) return 0.0;
|
|
247
247
|
gea::platform::storage::ensureMounted();
|
|
248
|
-
|
|
248
|
+
const std::string host = gea::platform::storage::hostPath(path);
|
|
249
|
+
makeParentDirs(host);
|
|
249
250
|
|
|
250
|
-
FILE *file = std::fopen(
|
|
251
|
+
FILE *file = std::fopen(host.c_str(), "wb");
|
|
251
252
|
if (!file) {
|
|
252
253
|
#ifdef ESP_PLATFORM
|
|
253
254
|
ESP_LOGE(kMediaLogTag, "failed to open WAV for write path=%s errno=%d", path.c_str(), errno);
|
|
@@ -314,7 +315,7 @@ std::string nextDefaultRecordingPath(NativeMediaRecorderHandle handle) {
|
|
|
314
315
|
if (recordingPathAssigned(buf)) continue;
|
|
315
316
|
|
|
316
317
|
struct stat info;
|
|
317
|
-
if (stat(buf, &info) != 0 && errno == ENOENT) return buf;
|
|
318
|
+
if (stat(gea::platform::storage::hostPath(buf).c_str(), &info) != 0 && errno == ENOENT) return buf;
|
|
318
319
|
}
|
|
319
320
|
return {};
|
|
320
321
|
}
|
|
@@ -322,10 +323,10 @@ std::string nextDefaultRecordingPath(NativeMediaRecorderHandle handle) {
|
|
|
322
323
|
// A default name is claimed by creating the file ("x" fails if it exists), so it never replaces a file that
|
|
323
324
|
// appeared after the name was picked, or this recorder's previous recording; it moves on to the next name.
|
|
324
325
|
std::FILE *openRecorderOutput(RecorderState &recorder, NativeMediaRecorderHandle handle) {
|
|
325
|
-
if (!recorder.defaultPath) return std::fopen(recorder.path.c_str(), "wb");
|
|
326
|
+
if (!recorder.defaultPath) return std::fopen(gea::platform::storage::hostPath(recorder.path).c_str(), "wb");
|
|
326
327
|
|
|
327
328
|
for (;;) {
|
|
328
|
-
std::FILE *file = std::fopen(recorder.path.c_str(), "wbx");
|
|
329
|
+
std::FILE *file = std::fopen(gea::platform::storage::hostPath(recorder.path).c_str(), "wbx");
|
|
329
330
|
if (file || errno != EEXIST) return file;
|
|
330
331
|
|
|
331
332
|
recorder.path = nextDefaultRecordingPath(handle);
|
|
@@ -336,7 +337,7 @@ std::FILE *openRecorderOutput(RecorderState &recorder, NativeMediaRecorderHandle
|
|
|
336
337
|
bool openRecorderFileOnInternalStack(RecorderState &recorder, NativeMediaRecorderHandle handle) {
|
|
337
338
|
if (recorder.path.empty()) return false;
|
|
338
339
|
gea::platform::storage::ensureMounted();
|
|
339
|
-
makeParentDirs(recorder.path);
|
|
340
|
+
makeParentDirs(gea::platform::storage::hostPath(recorder.path));
|
|
340
341
|
recorder.file = openRecorderOutput(recorder, handle);
|
|
341
342
|
recorder.samplesWritten = 0;
|
|
342
343
|
recorder.writeFailed = false;
|
|
@@ -442,7 +443,7 @@ double finalizeRecorderFile(RecorderState &recorder) {
|
|
|
442
443
|
std::vector<std::uint8_t> readFileBytesOnInternalStack(const std::string &path) {
|
|
443
444
|
if (path.empty()) return {};
|
|
444
445
|
gea::platform::storage::ensureMounted();
|
|
445
|
-
FILE *file = std::fopen(path.c_str(), "rb");
|
|
446
|
+
FILE *file = std::fopen(gea::platform::storage::hostPath(path).c_str(), "rb");
|
|
446
447
|
if (!file) return {};
|
|
447
448
|
std::fseek(file, 0, SEEK_END);
|
|
448
449
|
const long size = std::ftell(file);
|
package/host/menu.cpp
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The `Menu` host API (host/menu.h). Menus themselves are the platform's
|
|
3
|
+
// (host/menu_apple.mm on macOS); these fallbacks make every other host a
|
|
4
|
+
// no-op that hands out dummy handles, keeps no callback and never fires.
|
|
5
|
+
#include "host/menu.h"
|
|
6
|
+
|
|
7
|
+
#include <utility>
|
|
8
|
+
|
|
9
|
+
namespace gea::host {
|
|
10
|
+
|
|
11
|
+
double MenuFacade::create() const { return menu_platform::create(); }
|
|
12
|
+
double MenuFacade::item(const std::string &label, const std::string &key, double modifiers, const std::string &role,
|
|
13
|
+
std::function<void()> onClick) const
|
|
14
|
+
{
|
|
15
|
+
return menu_platform::item(label, key, static_cast<int>(modifiers), role, std::move(onClick));
|
|
16
|
+
}
|
|
17
|
+
double MenuFacade::separator() const { return menu_platform::separator(); }
|
|
18
|
+
void MenuFacade::append(double menu, double item) const { menu_platform::append(menu, item); }
|
|
19
|
+
void MenuFacade::clear(double menu) const { menu_platform::clear(menu); }
|
|
20
|
+
void MenuFacade::setLabel(double item, const std::string &label) const { menu_platform::setLabel(item, label); }
|
|
21
|
+
void MenuFacade::setEnabled(double item, bool enabled) const { menu_platform::setEnabled(item, enabled); }
|
|
22
|
+
void MenuFacade::setChecked(double item, bool checked) const { menu_platform::setChecked(item, checked); }
|
|
23
|
+
void MenuFacade::setVisible(double item, bool visible) const { menu_platform::setVisible(item, visible); }
|
|
24
|
+
void MenuFacade::setSymbol(double item, const std::string &symbol) const { menu_platform::setSymbol(item, symbol); }
|
|
25
|
+
void MenuFacade::setSubmenu(double item, double menu) const { menu_platform::setSubmenu(item, menu); }
|
|
26
|
+
void MenuFacade::release(double handle) const { menu_platform::release(handle); }
|
|
27
|
+
|
|
28
|
+
namespace menu_platform {
|
|
29
|
+
|
|
30
|
+
__attribute__((weak)) double create() { return 0; }
|
|
31
|
+
__attribute__((weak)) double item(const std::string &, const std::string &, int, const std::string &,
|
|
32
|
+
std::function<void()>)
|
|
33
|
+
{
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
__attribute__((weak)) double separator() { return 0; }
|
|
37
|
+
__attribute__((weak)) void append(double, double) {}
|
|
38
|
+
__attribute__((weak)) void clear(double) {}
|
|
39
|
+
__attribute__((weak)) void setLabel(double, const std::string &) {}
|
|
40
|
+
__attribute__((weak)) void setEnabled(double, bool) {}
|
|
41
|
+
__attribute__((weak)) void setChecked(double, bool) {}
|
|
42
|
+
__attribute__((weak)) void setVisible(double, bool) {}
|
|
43
|
+
__attribute__((weak)) void setSymbol(double, const std::string &) {}
|
|
44
|
+
__attribute__((weak)) void setSubmenu(double, double) {}
|
|
45
|
+
__attribute__((weak)) void release(double) {}
|
|
46
|
+
__attribute__((weak)) void *nativeMenu(double) { return nullptr; }
|
|
47
|
+
|
|
48
|
+
} // namespace menu_platform
|
|
49
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// macOS menus for the `Menu` host API (host/menu.h): one NSMenu per menu
|
|
3
|
+
// handle, one NSMenuItem per item handle. Every item targets one shared
|
|
4
|
+
// GeaMenuTarget with its handle as the tag; a click only queues the handle,
|
|
5
|
+
// and menu::runCallbacks runs the item's onClick on the next frame. Frames
|
|
6
|
+
// run on the main thread on macOS, so the setters write AppKit directly.
|
|
7
|
+
#if defined(__APPLE__)
|
|
8
|
+
#include <TargetConditionals.h>
|
|
9
|
+
#endif
|
|
10
|
+
#if defined(__APPLE__) && TARGET_OS_OSX
|
|
11
|
+
|
|
12
|
+
#import <AppKit/AppKit.h>
|
|
13
|
+
|
|
14
|
+
#include <cstdlib>
|
|
15
|
+
#include <functional>
|
|
16
|
+
#include <string>
|
|
17
|
+
#include <unordered_map>
|
|
18
|
+
#include <utility>
|
|
19
|
+
#include <vector>
|
|
20
|
+
|
|
21
|
+
#include "host/menu.h"
|
|
22
|
+
|
|
23
|
+
__asm__(".linker_option \"-framework\", \"AppKit\"");
|
|
24
|
+
|
|
25
|
+
namespace {
|
|
26
|
+
|
|
27
|
+
struct Slot {
|
|
28
|
+
NSMenu *menu = nil;
|
|
29
|
+
NSMenuItem *item = nil;
|
|
30
|
+
std::function<void()> onClick;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Handles count up and are never reused, so a click queued for an item that
|
|
34
|
+
// was released (or a MenuBar holding a destroyed menu) finds nothing.
|
|
35
|
+
long gNextHandle = 1;
|
|
36
|
+
std::unordered_map<long, Slot> gSlots;
|
|
37
|
+
std::vector<long> gPending;
|
|
38
|
+
|
|
39
|
+
Slot *lookup(double handle) {
|
|
40
|
+
auto found = gSlots.find(static_cast<long>(handle));
|
|
41
|
+
return found == gSlots.end() ? nullptr : &found->second;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
NSMenuItem *itemFor(double handle) {
|
|
45
|
+
Slot *slot = lookup(handle);
|
|
46
|
+
return slot ? slot->item : nil;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
NSMenu *menuFor(double handle) {
|
|
50
|
+
Slot *slot = lookup(handle);
|
|
51
|
+
return slot ? slot->menu : nil;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
NSString *nsString(const std::string &text) { return [NSString stringWithUTF8String:text.c_str()] ?: @""; }
|
|
55
|
+
|
|
56
|
+
void runPending() {
|
|
57
|
+
if (gPending.empty()) return;
|
|
58
|
+
std::vector<long> clicked;
|
|
59
|
+
clicked.swap(gPending);
|
|
60
|
+
for (long handle : clicked) {
|
|
61
|
+
Slot *slot = lookup(handle);
|
|
62
|
+
if (!slot) continue;
|
|
63
|
+
// The callback may destroy this menu (or rebuild it into the table); run
|
|
64
|
+
// a copy so its captures outlive the call.
|
|
65
|
+
auto onClick = slot->onClick;
|
|
66
|
+
if (onClick) onClick();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
void discardPending() { gPending.clear(); }
|
|
71
|
+
|
|
72
|
+
NSString *keyEquivalent(const std::string &key) {
|
|
73
|
+
if (key.empty()) return @"";
|
|
74
|
+
if (key.size() == 1) return nsString(key);
|
|
75
|
+
static const std::unordered_map<std::string, unichar> named = {
|
|
76
|
+
{"enter", '\r'},
|
|
77
|
+
{"return", '\r'},
|
|
78
|
+
{"tab", '\t'},
|
|
79
|
+
{"space", ' '},
|
|
80
|
+
{"escape", 0x1b},
|
|
81
|
+
{"backspace", 0x08},
|
|
82
|
+
{"delete", NSDeleteFunctionKey},
|
|
83
|
+
{"up", NSUpArrowFunctionKey},
|
|
84
|
+
{"down", NSDownArrowFunctionKey},
|
|
85
|
+
{"left", NSLeftArrowFunctionKey},
|
|
86
|
+
{"right", NSRightArrowFunctionKey},
|
|
87
|
+
{"home", NSHomeFunctionKey},
|
|
88
|
+
{"end", NSEndFunctionKey},
|
|
89
|
+
{"pageup", NSPageUpFunctionKey},
|
|
90
|
+
{"pagedown", NSPageDownFunctionKey},
|
|
91
|
+
};
|
|
92
|
+
unichar character = 0;
|
|
93
|
+
auto found = named.find(key);
|
|
94
|
+
if (found != named.end()) {
|
|
95
|
+
character = found->second;
|
|
96
|
+
} else if (key[0] == 'f' && key.size() <= 3) {
|
|
97
|
+
const int number = std::atoi(key.c_str() + 1);
|
|
98
|
+
if (number >= 1 && number <= 12) character = static_cast<unichar>(NSF1FunctionKey + number - 1);
|
|
99
|
+
}
|
|
100
|
+
if (!character) return nsString(key);
|
|
101
|
+
return [NSString stringWithCharacters:&character length:1];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
NSEventModifierFlags modifierFlags(int modifiers) {
|
|
105
|
+
NSEventModifierFlags flags = 0;
|
|
106
|
+
if (modifiers & gea::host::MenuFacade::kCommandModifier) flags |= NSEventModifierFlagCommand;
|
|
107
|
+
if (modifiers & gea::host::MenuFacade::kShiftModifier) flags |= NSEventModifierFlagShift;
|
|
108
|
+
if (modifiers & gea::host::MenuFacade::kAltModifier) flags |= NSEventModifierFlagOption;
|
|
109
|
+
if (modifiers & gea::host::MenuFacade::kControlModifier) flags |= NSEventModifierFlagControl;
|
|
110
|
+
return flags;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
NSString *appName() {
|
|
114
|
+
return [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]
|
|
115
|
+
?: [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleName"]
|
|
116
|
+
?: NSProcessInfo.processInfo.processName;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// A submenu hangs off one item at a time.
|
|
120
|
+
void detachFromSupermenu(NSMenu *menu) {
|
|
121
|
+
NSMenu *super = menu.supermenu;
|
|
122
|
+
if (!super) return;
|
|
123
|
+
for (NSMenuItem *item in super.itemArray) {
|
|
124
|
+
if (item.submenu == menu) item.submenu = nil;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
long insert(Slot slot) {
|
|
129
|
+
const long handle = gNextHandle++;
|
|
130
|
+
gSlots.emplace(handle, std::move(slot));
|
|
131
|
+
return handle;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
} // namespace
|
|
135
|
+
|
|
136
|
+
@interface GeaMenuTarget : NSObject
|
|
137
|
+
- (void)fire:(NSMenuItem *)sender;
|
|
138
|
+
@end
|
|
139
|
+
|
|
140
|
+
@implementation GeaMenuTarget
|
|
141
|
+
- (void)fire:(NSMenuItem *)sender {
|
|
142
|
+
gPending.push_back(static_cast<long>(sender.tag));
|
|
143
|
+
}
|
|
144
|
+
@end
|
|
145
|
+
|
|
146
|
+
namespace {
|
|
147
|
+
// NSMenuItem.target is weak.
|
|
148
|
+
GeaMenuTarget *target() {
|
|
149
|
+
static GeaMenuTarget *shared = [[GeaMenuTarget alloc] init];
|
|
150
|
+
return shared;
|
|
151
|
+
}
|
|
152
|
+
} // namespace
|
|
153
|
+
|
|
154
|
+
namespace gea::host::menu_platform {
|
|
155
|
+
|
|
156
|
+
double create() {
|
|
157
|
+
Slot slot;
|
|
158
|
+
slot.menu = [[NSMenu alloc] init];
|
|
159
|
+
// The app owns enabled: AppKit must not re-validate items against the
|
|
160
|
+
// responder chain.
|
|
161
|
+
slot.menu.autoenablesItems = NO;
|
|
162
|
+
return insert(std::move(slot));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
double item(const std::string &label, const std::string &key, int modifiers, const std::string &role,
|
|
166
|
+
std::function<void()> onClick) {
|
|
167
|
+
const bool quit = role == "quit";
|
|
168
|
+
NSString *title = nsString(label);
|
|
169
|
+
if (quit && title.length == 0) title = [@"Quit " stringByAppendingString:appName()];
|
|
170
|
+
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title
|
|
171
|
+
action:quit ? @selector(terminate:) : @selector(fire:)
|
|
172
|
+
keyEquivalent:keyEquivalent(key)];
|
|
173
|
+
item.keyEquivalentModifierMask = modifierFlags(modifiers);
|
|
174
|
+
item.target = quit ? (id)NSApp : (id)target();
|
|
175
|
+
Slot slot;
|
|
176
|
+
slot.item = item;
|
|
177
|
+
if (!quit) slot.onClick = std::move(onClick);
|
|
178
|
+
const long handle = insert(std::move(slot));
|
|
179
|
+
item.tag = handle;
|
|
180
|
+
gea::host::menu::pendingCallbacks = runPending;
|
|
181
|
+
gea::host::menu::discardCallbacks = discardPending;
|
|
182
|
+
return handle;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
double separator() {
|
|
186
|
+
Slot slot;
|
|
187
|
+
slot.item = [NSMenuItem separatorItem];
|
|
188
|
+
return insert(std::move(slot));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
void append(double menuHandle, double itemHandle) {
|
|
192
|
+
NSMenu *menu = menuFor(menuHandle);
|
|
193
|
+
NSMenuItem *item = itemFor(itemHandle);
|
|
194
|
+
if (!menu || !item) return;
|
|
195
|
+
if (item.menu) [item.menu removeItem:item];
|
|
196
|
+
[menu addItem:item];
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
void clear(double menuHandle) { [menuFor(menuHandle) removeAllItems]; }
|
|
200
|
+
|
|
201
|
+
void setLabel(double handle, const std::string &label) { itemFor(handle).title = nsString(label); }
|
|
202
|
+
|
|
203
|
+
void setEnabled(double handle, bool enabled) { itemFor(handle).enabled = enabled; }
|
|
204
|
+
|
|
205
|
+
void setChecked(double handle, bool checked) {
|
|
206
|
+
itemFor(handle).state = checked ? NSControlStateValueOn : NSControlStateValueOff;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
void setVisible(double handle, bool visible) { itemFor(handle).hidden = !visible; }
|
|
210
|
+
|
|
211
|
+
void setSymbol(double handle, const std::string &symbol) {
|
|
212
|
+
NSMenuItem *item = itemFor(handle);
|
|
213
|
+
if (!item) return;
|
|
214
|
+
item.image = symbol.empty() ? nil : [NSImage imageWithSystemSymbolName:nsString(symbol) accessibilityDescription:nil];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
void setSubmenu(double itemHandle, double menuHandle) {
|
|
218
|
+
NSMenuItem *item = itemFor(itemHandle);
|
|
219
|
+
if (!item) return;
|
|
220
|
+
NSMenu *menu = menuFor(menuHandle);
|
|
221
|
+
if (menu && item.submenu == menu) return;
|
|
222
|
+
if (menu) detachFromSupermenu(menu);
|
|
223
|
+
item.submenu = menu;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
void release(double handle) {
|
|
227
|
+
Slot *slot = lookup(handle);
|
|
228
|
+
if (!slot) return;
|
|
229
|
+
if (slot->item) {
|
|
230
|
+
if (slot->item.menu) [slot->item.menu removeItem:slot->item];
|
|
231
|
+
slot->item.submenu = nil;
|
|
232
|
+
slot->item.target = nil;
|
|
233
|
+
slot->item.action = nil;
|
|
234
|
+
}
|
|
235
|
+
if (slot->menu) {
|
|
236
|
+
detachFromSupermenu(slot->menu);
|
|
237
|
+
[slot->menu removeAllItems];
|
|
238
|
+
}
|
|
239
|
+
// Destroying the callback releases its captures; when release() runs inside
|
|
240
|
+
// that callback, runPending holds the copy that is executing.
|
|
241
|
+
auto released = std::move(slot->onClick);
|
|
242
|
+
gSlots.erase(static_cast<long>(handle));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
void *nativeMenu(double handle) { return (__bridge void *)menuFor(handle); }
|
|
246
|
+
|
|
247
|
+
} // namespace gea::host::menu_platform
|
|
248
|
+
|
|
249
|
+
#endif
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The `MenuBar` host API (host/menu_bar.h). The menu bar itself is the
|
|
3
|
+
// platform's (host/menu_bar_apple.mm on macOS); these fallbacks make every
|
|
4
|
+
// other host a no-op.
|
|
5
|
+
#include "host/menu_bar.h"
|
|
6
|
+
|
|
7
|
+
namespace gea::host {
|
|
8
|
+
|
|
9
|
+
bool MenuBarFacade::supported() const { return menu_bar_platform::supported(); }
|
|
10
|
+
bool MenuBarFacade::install(const std::string &symbol) const { return menu_bar_platform::install(symbol); }
|
|
11
|
+
bool MenuBarFacade::active() const { return menu_bar_platform::active(); }
|
|
12
|
+
void MenuBarFacade::setSymbol(const std::string &symbol) const { menu_bar_platform::setSymbol(symbol); }
|
|
13
|
+
bool MenuBarFacade::shown() const { return menu_bar_platform::shown(); }
|
|
14
|
+
void MenuBarFacade::show() const { menu_bar_platform::show(); }
|
|
15
|
+
void MenuBarFacade::hide() const { menu_bar_platform::hide(); }
|
|
16
|
+
void MenuBarFacade::setMenu(double menu) const { menu_bar_platform::setMenu(menu); }
|
|
17
|
+
|
|
18
|
+
namespace menu_bar_platform {
|
|
19
|
+
|
|
20
|
+
__attribute__((weak)) bool supported() { return false; }
|
|
21
|
+
__attribute__((weak)) bool install(const std::string &) { return false; }
|
|
22
|
+
__attribute__((weak)) bool active() { return false; }
|
|
23
|
+
__attribute__((weak)) void setSymbol(const std::string &) {}
|
|
24
|
+
__attribute__((weak)) bool shown() { return true; }
|
|
25
|
+
__attribute__((weak)) void show() {}
|
|
26
|
+
__attribute__((weak)) void hide() {}
|
|
27
|
+
__attribute__((weak)) void setMenu(double) {}
|
|
28
|
+
__attribute__((weak)) void *nativeSecondaryMenu() { return nullptr; }
|
|
29
|
+
|
|
30
|
+
} // namespace menu_bar_platform
|
|
31
|
+
} // namespace gea::host
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// macOS menu bar mode for the `MenuBar` host API (host/menu_bar.h): a status
|
|
3
|
+
// item with an SF Symbol, the app's window restyled as a borderless panel that
|
|
4
|
+
// drops down under it, accessory activation (no Dock icon), the right-click
|
|
5
|
+
// menu (the app's `Menu` from setMenu, else Quit), and a timer that
|
|
6
|
+
// keeps the app delegate's frames coming while the panel is hidden -- the
|
|
7
|
+
// window's display link stops with the window.
|
|
8
|
+
#if defined(__APPLE__)
|
|
9
|
+
#include <TargetConditionals.h>
|
|
10
|
+
#endif
|
|
11
|
+
#if defined(__APPLE__) && TARGET_OS_OSX
|
|
12
|
+
|
|
13
|
+
#import <AppKit/AppKit.h>
|
|
14
|
+
|
|
15
|
+
#include <cstdlib>
|
|
16
|
+
|
|
17
|
+
#include "host/menu.h"
|
|
18
|
+
#include "host/menu_bar.h"
|
|
19
|
+
|
|
20
|
+
static const CGFloat kPanelGap = 6;
|
|
21
|
+
static const NSTimeInterval kHiddenTick = 1.0 / 20.0;
|
|
22
|
+
|
|
23
|
+
// The app's right-click menu handle (host/menu.h), -1 for the built-in one.
|
|
24
|
+
// Kept as a handle and resolved per click, so setMenu() before install() just
|
|
25
|
+
// works and a destroyed menu falls back to Quit.
|
|
26
|
+
static double gCustomMenu = -1;
|
|
27
|
+
|
|
28
|
+
@interface GeaMenuBar : NSObject
|
|
29
|
+
@property(nonatomic, strong) NSStatusItem *item;
|
|
30
|
+
@property(nonatomic, weak) NSWindow *window;
|
|
31
|
+
@property(nonatomic, strong) NSTimer *timer;
|
|
32
|
+
@property(nonatomic, strong) NSMenu *menu;
|
|
33
|
+
@end
|
|
34
|
+
|
|
35
|
+
@implementation GeaMenuBar
|
|
36
|
+
|
|
37
|
+
- (NSWindow *)appWindow {
|
|
38
|
+
id delegate = NSApp.delegate;
|
|
39
|
+
if ([delegate respondsToSelector:@selector(window)]) {
|
|
40
|
+
id window = [delegate performSelector:@selector(window)];
|
|
41
|
+
if ([window isKindOfClass:[NSWindow class]]) return window;
|
|
42
|
+
}
|
|
43
|
+
for (NSWindow *window in NSApp.windows) {
|
|
44
|
+
if (window.canBecomeMainWindow) return window;
|
|
45
|
+
}
|
|
46
|
+
return NSApp.windows.firstObject;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
- (void)installWithSymbol:(NSString *)symbol {
|
|
50
|
+
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
|
|
51
|
+
|
|
52
|
+
NSWindow *window = [self appWindow];
|
|
53
|
+
self.window = window;
|
|
54
|
+
if (window) {
|
|
55
|
+
const NSSize size = window.contentView.bounds.size;
|
|
56
|
+
window.styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskFullSizeContentView;
|
|
57
|
+
window.titlebarAppearsTransparent = YES;
|
|
58
|
+
window.titleVisibility = NSWindowTitleHidden;
|
|
59
|
+
for (NSWindowButton button : {NSWindowCloseButton, NSWindowMiniaturizeButton, NSWindowZoomButton}) {
|
|
60
|
+
[window standardWindowButton:button].hidden = YES;
|
|
61
|
+
}
|
|
62
|
+
window.movable = NO;
|
|
63
|
+
window.level = NSPopUpMenuWindowLevel;
|
|
64
|
+
window.collectionBehavior = NSWindowCollectionBehaviorCanJoinAllSpaces |
|
|
65
|
+
NSWindowCollectionBehaviorFullScreenAuxiliary | NSWindowCollectionBehaviorTransient;
|
|
66
|
+
[window setContentSize:size];
|
|
67
|
+
[window orderOut:nil];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
self.item = [NSStatusBar.systemStatusBar statusItemWithLength:NSVariableStatusItemLength];
|
|
71
|
+
[self setSymbol:symbol];
|
|
72
|
+
self.item.button.target = self;
|
|
73
|
+
self.item.button.action = @selector(clicked:);
|
|
74
|
+
[self.item.button sendActionOn:NSEventMaskLeftMouseUp | NSEventMaskRightMouseUp];
|
|
75
|
+
|
|
76
|
+
NSString *name = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]
|
|
77
|
+
?: [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleName"]
|
|
78
|
+
?: NSProcessInfo.processInfo.processName;
|
|
79
|
+
self.menu = [[NSMenu alloc] init];
|
|
80
|
+
NSMenuItem *quit = [[NSMenuItem alloc] initWithTitle:[@"Quit " stringByAppendingString:name]
|
|
81
|
+
action:@selector(terminate:)
|
|
82
|
+
keyEquivalent:@"q"];
|
|
83
|
+
quit.target = NSApp;
|
|
84
|
+
[self.menu addItem:quit];
|
|
85
|
+
|
|
86
|
+
[NSNotificationCenter.defaultCenter addObserver:self
|
|
87
|
+
selector:@selector(resigned:)
|
|
88
|
+
name:NSApplicationDidResignActiveNotification
|
|
89
|
+
object:nil];
|
|
90
|
+
|
|
91
|
+
// Another of the app's windows (a `<window>` element) taking the keyboard
|
|
92
|
+
// closes the panel, the way clicking into another app does.
|
|
93
|
+
[NSNotificationCenter.defaultCenter addObserver:self
|
|
94
|
+
selector:@selector(otherWindowBecameKey:)
|
|
95
|
+
name:NSWindowDidBecomeKeyNotification
|
|
96
|
+
object:nil];
|
|
97
|
+
|
|
98
|
+
self.timer = [NSTimer timerWithTimeInterval:kHiddenTick
|
|
99
|
+
target:self
|
|
100
|
+
selector:@selector(tickWhileHidden:)
|
|
101
|
+
userInfo:nil
|
|
102
|
+
repeats:YES];
|
|
103
|
+
self.timer.tolerance = kHiddenTick / 4;
|
|
104
|
+
[NSRunLoop.mainRunLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
|
|
105
|
+
|
|
106
|
+
// A scripted replay (the macOS target's GEA_MACOS_REPLAY_PLAN) drives the
|
|
107
|
+
// panel's window, so it opens the panel the way a click on the icon does.
|
|
108
|
+
if (std::getenv("GEA_MACOS_REPLAY_PLAN")) dispatch_async(dispatch_get_main_queue(), ^{ [self show]; });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
- (void)setSymbol:(NSString *)symbol {
|
|
112
|
+
NSImage *image = [NSImage imageWithSystemSymbolName:symbol accessibilityDescription:nil];
|
|
113
|
+
if (!image) image = [NSImage imageWithSystemSymbolName:@"circle" accessibilityDescription:nil];
|
|
114
|
+
[image setTemplate:YES];
|
|
115
|
+
self.item.button.image = image;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
- (BOOL)shown {
|
|
119
|
+
return self.window.isVisible;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// The menu a right-click or control-click shows: the app's from setMenu(),
|
|
123
|
+
// else the built-in Quit menu.
|
|
124
|
+
- (NSMenu *)secondaryMenu {
|
|
125
|
+
NSMenu *custom = gCustomMenu >= 0 ? (__bridge NSMenu *)gea::host::menu_platform::nativeMenu(gCustomMenu) : nil;
|
|
126
|
+
return custom ?: self.menu;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
- (void)clicked:(id)sender {
|
|
130
|
+
NSEvent *event = NSApp.currentEvent;
|
|
131
|
+
const BOOL secondary = event.type == NSEventTypeRightMouseUp || (event.modifierFlags & NSEventModifierFlagControl);
|
|
132
|
+
if (secondary) {
|
|
133
|
+
self.item.menu = [self secondaryMenu];
|
|
134
|
+
[self.item.button performClick:nil];
|
|
135
|
+
self.item.menu = nil;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (self.shown) [self hide];
|
|
139
|
+
else [self show];
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
- (void)show {
|
|
143
|
+
NSWindow *window = self.window;
|
|
144
|
+
NSWindow *bar = self.item.button.window;
|
|
145
|
+
if (!window || !bar) return;
|
|
146
|
+
const NSRect anchor = bar.frame;
|
|
147
|
+
const NSSize size = window.frame.size;
|
|
148
|
+
const NSRect visible = (bar.screen ?: NSScreen.mainScreen).visibleFrame;
|
|
149
|
+
CGFloat x = NSMidX(anchor) - size.width / 2;
|
|
150
|
+
x = MAX(NSMinX(visible) + kPanelGap, MIN(x, NSMaxX(visible) - size.width - kPanelGap));
|
|
151
|
+
const CGFloat y = NSMinY(anchor) - size.height - kPanelGap;
|
|
152
|
+
[window setFrameOrigin:NSMakePoint(x, y)];
|
|
153
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
154
|
+
[window makeKeyAndOrderFront:nil];
|
|
155
|
+
// Keys go to the app as soon as the panel opens, without a click first.
|
|
156
|
+
NSView *content = window.contentView;
|
|
157
|
+
if (window.firstResponder == window && content.acceptsFirstResponder) [window makeFirstResponder:content];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
- (void)hide {
|
|
161
|
+
[self.window orderOut:nil];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
- (void)resigned:(NSNotification *)notification {
|
|
165
|
+
[self hide];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
- (void)otherWindowBecameKey:(NSNotification *)notification {
|
|
169
|
+
NSWindow *window = notification.object;
|
|
170
|
+
if (window != self.window && window.canBecomeMainWindow && self.shown) [self hide];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
- (void)tickWhileHidden:(NSTimer *)timer {
|
|
174
|
+
if (self.shown) return;
|
|
175
|
+
id delegate = NSApp.delegate;
|
|
176
|
+
// An open `<window>` element's own display link drives frames then.
|
|
177
|
+
const SEL drives = @selector(geaAuxiliaryWindowDrivesFrames);
|
|
178
|
+
if ([delegate respondsToSelector:drives] &&
|
|
179
|
+
reinterpret_cast<BOOL (*)(id, SEL)>([delegate methodForSelector:drives])(delegate, drives))
|
|
180
|
+
return;
|
|
181
|
+
if ([delegate respondsToSelector:@selector(tick)]) [delegate performSelector:@selector(tick)];
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
@end
|
|
185
|
+
|
|
186
|
+
namespace gea::host::menu_bar_platform {
|
|
187
|
+
namespace {
|
|
188
|
+
|
|
189
|
+
GeaMenuBar *installed = nil;
|
|
190
|
+
|
|
191
|
+
NSString *nsString(const std::string &text) { return [NSString stringWithUTF8String:text.c_str()] ?: @""; }
|
|
192
|
+
|
|
193
|
+
} // namespace
|
|
194
|
+
|
|
195
|
+
bool supported() { return true; }
|
|
196
|
+
|
|
197
|
+
bool install(const std::string &symbol) {
|
|
198
|
+
if (installed) return true;
|
|
199
|
+
installed = [[GeaMenuBar alloc] init];
|
|
200
|
+
[installed installWithSymbol:nsString(symbol)];
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
bool active() { return installed != nil; }
|
|
205
|
+
|
|
206
|
+
void setSymbol(const std::string &symbol) { [installed setSymbol:nsString(symbol)]; }
|
|
207
|
+
|
|
208
|
+
bool shown() { return installed ? installed.shown : true; }
|
|
209
|
+
|
|
210
|
+
void show() { [installed show]; }
|
|
211
|
+
|
|
212
|
+
void hide() { [installed hide]; }
|
|
213
|
+
|
|
214
|
+
void setMenu(double menu) { gCustomMenu = menu; }
|
|
215
|
+
|
|
216
|
+
void *nativeSecondaryMenu() { return (__bridge void *)[installed secondaryMenu]; }
|
|
217
|
+
|
|
218
|
+
} // namespace gea::host::menu_bar_platform
|
|
219
|
+
|
|
220
|
+
#endif
|