* feat: add experimental Apple Silicon support with Nivfold Integrate the standalone Nivfold protocol 1 runner; tested against Nivfold f436fb6. The Windows Reims vGPU runtime remains independent. * fix: open QQ invites and VM SSH on macOS * fix: move VM, View and Help to the macOS menu bar * feat(macos): add resumable Apple restore image downloads Expose the compatible restore image in the VM wizard and Images page, validate downloads before caching, and fix macOS memory reporting and plugin SVG rendering. * fix(macos): localize the native application menu * fix(macos): address native desktop review findings
121 lines
4.7 KiB
Objective-C
121 lines
4.7 KiB
Objective-C
//go:build darwin && cgo
|
|
|
|
#import <AppKit/AppKit.h>
|
|
|
|
extern void reimsApplicationMenuClicked(int token);
|
|
|
|
@interface ReimsMenuTarget : NSObject
|
|
- (void)invoke:(NSMenuItem *)sender;
|
|
@end
|
|
|
|
@implementation ReimsMenuTarget
|
|
- (void)invoke:(NSMenuItem *)sender {
|
|
reimsApplicationMenuClicked((int)sender.tag);
|
|
}
|
|
@end
|
|
|
|
static ReimsMenuTarget *menuTarget;
|
|
static NSArray<NSMenuItem *> *managedItems;
|
|
static NSArray<NSMenuItem *> *standardItems;
|
|
|
|
static void setMenuTitle(NSMenuItem *item, NSString *title) {
|
|
if (title.length == 0) return;
|
|
item.title = title;
|
|
item.submenu.title = title;
|
|
}
|
|
|
|
static void localizeStandardItems(NSMenu *menu, NSDictionary<NSString *, NSString *> *labels) {
|
|
for (NSMenuItem *item in menu.itemArray) {
|
|
if (item.isSeparatorItem) continue;
|
|
if (item.hasSubmenu) {
|
|
// AppKit can also inject its own submenus, such as AutoFill.
|
|
// Only relabel the speech submenu created by Wails.
|
|
for (NSMenuItem *child in item.submenu.itemArray) {
|
|
if (child.action == @selector(startSpeaking:)) {
|
|
setMenuTitle(item, labels[@"speech"]);
|
|
localizeStandardItems(item.submenu, labels);
|
|
break;
|
|
}
|
|
}
|
|
} else if (item.action) {
|
|
NSString *action = NSStringFromSelector(item.action);
|
|
// Wails sends both Undo and Redo to undo:, with Shift for Redo.
|
|
if (item.action == @selector(undo:) && (item.keyEquivalentModifierMask & NSEventModifierFlagShift)) {
|
|
action = @"redo:";
|
|
}
|
|
setMenuTitle(item, labels[action]);
|
|
}
|
|
}
|
|
}
|
|
|
|
static NSMenu *buildMenu(NSDictionary *group) {
|
|
NSMenu *menu = [[[NSMenu alloc] initWithTitle:group[@"label"]] autorelease];
|
|
menu.autoenablesItems = NO;
|
|
for (NSDictionary *row in group[@"rows"]) {
|
|
if (!row[@"label"]) {
|
|
[menu addItem:[NSMenuItem separatorItem]];
|
|
continue;
|
|
}
|
|
NSInteger token = [row[@"token"] integerValue];
|
|
NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:row[@"label"]
|
|
action:token ? @selector(invoke:) : NULL
|
|
keyEquivalent:row[@"key"] ?: @""] autorelease];
|
|
item.keyEquivalentModifierMask = NSEventModifierFlagCommand;
|
|
item.tag = token;
|
|
item.target = menuTarget;
|
|
item.enabled = token != 0;
|
|
item.state = [row[@"checked"] boolValue] ? NSControlStateValueOn : NSControlStateValueOff;
|
|
[menu addItem:item];
|
|
}
|
|
return menu;
|
|
}
|
|
|
|
static BOOL setMenuGroups(NSDictionary *snapshot) {
|
|
NSMenu *main = NSApp.mainMenu;
|
|
NSArray<NSDictionary *> *groups = snapshot[@"groups"];
|
|
if (!main || groups.count != 3) return NO;
|
|
if (!menuTarget) menuTarget = [ReimsMenuTarget new];
|
|
|
|
if (!managedItems || managedItems.firstObject.menu != main) {
|
|
// Wails initializes App, Edit and Window in this order. Keep the
|
|
// original items so localization does not change targets or shortcuts.
|
|
if (main.numberOfItems != 3) return NO;
|
|
[standardItems release];
|
|
standardItems = [main.itemArray copy];
|
|
NSMutableArray *items = [NSMutableArray arrayWithCapacity:3];
|
|
NSInteger index = 2;
|
|
for (NSDictionary *group in groups) {
|
|
[items addObject:[[[NSMenuItem alloc] initWithTitle:group[@"label"] action:NULL keyEquivalent:@""] autorelease]];
|
|
}
|
|
[main insertItem:items[0] atIndex:index];
|
|
[main insertItem:items[1] atIndex:index + 1];
|
|
[main addItem:items[2]];
|
|
[managedItems release];
|
|
managedItems = [items copy];
|
|
}
|
|
NSDictionary<NSString *, NSString *> *labels = snapshot[@"standard"];
|
|
setMenuTitle(standardItems[0], @"Reims-Go");
|
|
setMenuTitle(standardItems[1], labels[@"edit"]);
|
|
setMenuTitle(standardItems[2], labels[@"window"]);
|
|
for (NSMenuItem *item in standardItems) localizeStandardItems(item.submenu, labels);
|
|
|
|
for (NSUInteger i = 0; i < groups.count; i++) {
|
|
NSMenuItem *item = managedItems[i];
|
|
item.title = groups[i][@"label"];
|
|
item.submenu = buildMenu(groups[i]);
|
|
}
|
|
NSApp.helpMenu = managedItems.lastObject.submenu;
|
|
return YES;
|
|
}
|
|
|
|
int reimsSyncApplicationMenu(const char *snapshot) {
|
|
@autoreleasepool {
|
|
NSData *data = [NSData dataWithBytes:snapshot length:strlen(snapshot)];
|
|
NSDictionary *menu = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
|
|
if (NSThread.isMainThread) return setMenuGroups(menu);
|
|
__block BOOL updated = NO;
|
|
dispatch_sync(dispatch_get_main_queue(), ^{ updated = setMenuGroups(menu); });
|
|
return updated;
|
|
}
|
|
}
|