summaryrefslogtreecommitdiff
path: root/src/macos.m
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-09-21 11:51:55 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-09-21 11:51:55 +0300
commit282e1376091e850565b5c5166755ae94afe56ad2 (patch)
tree8c4334f0dbe18bb1fa23939ff800ce9051c070d6 /src/macos.m
parente48a9a27bd11dbef9531bd12d3c0c60cc771b2c3 (diff)
macOS: Improving native menu behavior
Selected items and dynamic label updates.
Diffstat (limited to 'src/macos.m')
-rw-r--r--src/macos.m35
1 files changed, 31 insertions, 4 deletions
diff --git a/src/macos.m b/src/macos.m
index cec53a7d..d9ee71e7 100644
--- a/src/macos.m
+++ b/src/macos.m
@@ -524,26 +524,53 @@ void removeMenu_MacOS(int atIndex) {
524 [appMenu removeItemAtIndex:atIndex]; 524 [appMenu removeItemAtIndex:atIndex];
525} 525}
526 526
527enum iColorId removeColorEscapes_String(iString *d) {
528 enum iColorId color = none_ColorId;
529 for (;;) {
530 const char *esc = strchr(cstr_String(d), '\v');
531 if (esc) {
532 const char *ptr = esc + 1;
533 color = 0;
534 if (*ptr == '\v') {
535 color += asciiExtended_ColorEscape;
536 ptr++;
537 }
538 color += *ptr - asciiBase_ColorEscape;
539 ptr++;
540 remove_Block(&d->chars, esc - cstr_String(d), ptr - esc);
541 }
542 else break;
543 }
544 return color;
545}
546
527static void makeMenuItems_(NSMenu *menu, MenuCommands *commands, const iMenuItem *items, size_t n) { 547static void makeMenuItems_(NSMenu *menu, MenuCommands *commands, const iMenuItem *items, size_t n) {
528 for (size_t i = 0; i < n && items[i].label; ++i) { 548 for (size_t i = 0; i < n && items[i].label; ++i) {
529 const char *label = translateCStr_Lang(items[i].label); 549 const char *label = translateCStr_Lang(items[i].label);
530 if (label[0] == '\v') {
531 /* Skip the formatting escape. */
532 label += 2;
533 }
534 if (equal_CStr(label, "---")) { 550 if (equal_CStr(label, "---")) {
535 [menu addItem:[NSMenuItem separatorItem]]; 551 [menu addItem:[NSMenuItem separatorItem]];
536 } 552 }
537 else { 553 else {
538 const iBool hasCommand = (items[i].command && items[i].command[0]); 554 const iBool hasCommand = (items[i].command && items[i].command[0]);
555 iBool isChecked = iFalse;
556 if (startsWith_CStr(label, "###")) {
557 isChecked = iTrue;
558 label += 3;
559 }
539 iString itemTitle; 560 iString itemTitle;
540 initCStr_String(&itemTitle, label); 561 initCStr_String(&itemTitle, label);
541 removeIconPrefix_String(&itemTitle); 562 removeIconPrefix_String(&itemTitle);
563 if (removeColorEscapes_String(&itemTitle) == uiTextCaution_ColorId) {
564// prependCStr_String(&itemTitle, "\u26a0\ufe0f ");
565 }
542 NSMenuItem *item = [menu addItemWithTitle:[NSString stringWithUTF8String:cstr_String(&itemTitle)] 566 NSMenuItem *item = [menu addItemWithTitle:[NSString stringWithUTF8String:cstr_String(&itemTitle)]
543 action:(hasCommand ? @selector(postMenuItemCommand:) : nil) 567 action:(hasCommand ? @selector(postMenuItemCommand:) : nil)
544 keyEquivalent:@""]; 568 keyEquivalent:@""];
545 deinit_String(&itemTitle); 569 deinit_String(&itemTitle);
546 [item setTarget:commands]; 570 [item setTarget:commands];
571 if (isChecked) {
572 [item setState:NSControlStateValueOn];
573 }
547 int key = items[i].key; 574 int key = items[i].key;
548 int kmods = items[i].kmods; 575 int kmods = items[i].kmods;
549 if (hasCommand) { 576 if (hasCommand) {