Repository: electron/electron
File: shell/browser/api/electron_api_menu_mac.mm (Objective-C++)

Code before:
                             location:position
                        modifierFlags:0
                            timestamp:0
                         windowNumber:nswindow.windowNumber
                              context:nil
                          eventNumber:0
                           clickCount:1
                             pressure:0];
      ui::ShowContextMenu(menu, dummy_event, view, true);
    }
    // If no render frame host was found, return early.
    return;
  } else {
    // Make sure events can be pumped while the menu is up.
    base::CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop allow;

    // One of the events that could be pumped is |window.close()|.
    // User-initiated event-tracking loops protect against this by
    // setting flags in -[CrApplication sendEvent:], but since
    // web-content menus are initiated by IPC message the setup has to
    // be done manually.
    base::mac::ScopedSendingEvent sendingEventScoper;

    // Don't emit unresponsive event when showing menu.
    [menu popUpMenuPositioningItem:item atLocation:position inView:view];
  }
}

void MenuMac::ClosePopupAt(int32_t window_id) {
  auto close_popup = base::BindOnce(&MenuMac::ClosePopupOnUI,
                                    weak_factory_.GetWeakPtr(), window_id);
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, std::move(close_popup));
}

std::u16string MenuMac::GetAcceleratorTextAtForTesting(int index) const {
  // A least effort to get the real shortcut text of NSMenuItem, the code does
  // not need to be perfect since it is test only.
  ElectronMenuController* controller =
      [[ElectronMenuController alloc] initWithModel:model()
                              useDefaultAccelerator:NO];
  NSMenuItem* item = [[controller menu] itemAtIndex:index];
  std::u16string text;
  NSEventModifierFlags modifiers = [item keyEquivalentModifierMask];
  if (modifiers & NSEventModifierFlagControl)
    text += u"Ctrl";
  if (modifiers & NSEventModifierFlagShift) {
    if (!text.empty())
      text += u"+";
    text += u"Shift";
  }

Review comment (suggestion):
We still want to show a menu when `frame` isn't provided, or if things don't work out in the `frame` case.

```suggestion
      return;
    }
  }

  // Make sure events can be pumped while the menu is up.
  base::CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop allow;

  // One of the events that could be pumped is |window.close()|.
  // User-initiated event-tracking loops protect against this by
  // setting flags in -[CrApplication sendEvent:], but since
  // web-content menus are initiated by IPC message the setup has to
  // be done manually.
  base::mac::ScopedSendingEvent sendingEventScoper;

  // Don't emit unresponsive event when showing menu.
  [menu popUpMenuPositioningItem:item atLocation:position inView:view];
```

Code after:
                             location:position
                        modifierFlags:0
                            timestamp:0
                         windowNumber:nswindow.windowNumber
                              context:nil
                          eventNumber:0
                           clickCount:1
                             pressure:0];
      ui::ShowContextMenu(menu, dummy_event, view, true);
      return;
    }
  }

  // Make sure events can be pumped while the menu is up.
  base::CurrentThread::ScopedAllowApplicationTasksInNativeNestedLoop allow;

  // One of the events that could be pumped is |window.close()|.
  // User-initiated event-tracking loops protect against this by
  // setting flags in -[CrApplication sendEvent:], but since
  // web-content menus are initiated by IPC message the setup has to
  // be done manually.
  base::mac::ScopedSendingEvent sendingEventScoper;

  // Don't emit unresponsive event when showing menu.
  [menu popUpMenuPositioningItem:item atLocation:position inView:view];
}

void MenuMac::ClosePopupAt(int32_t window_id) {
  auto close_popup = base::BindOnce(&MenuMac::ClosePopupOnUI,
                                    weak_factory_.GetWeakPtr(), window_id);
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, std::move(close_popup));
}

std::u16string MenuMac::GetAcceleratorTextAtForTesting(int index) const {
  // A least effort to get the real shortcut text of NSMenuItem, the code does
  // not need to be perfect since it is test only.
  ElectronMenuController* controller =
      [[ElectronMenuController alloc] initWithModel:model()
                              useDefaultAccelerator:NO];
  NSMenuItem* item = [[controller menu] itemAtIndex:index];
  std::u16string text;
  NSEventModifierFlags modifiers = [item keyEquivalentModifierMask];
  if (modifiers & NSEventModifierFlagControl)
    text += u"Ctrl";
  if (modifiers & NSEventModifierFlagShift) {
    if (!text.empty())
      text += u"+";
    text += u"Shift";
  }
  if (modifiers & NSEventModifierFlagOption) {