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

Code before:
                         fromView:nil];
  } else {
    position = NSMakePoint(x, [view frame].size.height - y);
  }

  // If no preferred item is specified, try to show all of the menu items.
  if (!item) {
    CGFloat windowBottom = CGRectGetMinY([view window].frame);
    CGFloat lowestMenuPoint = windowBottom + position.y - [menu size].height;
    CGFloat screenBottom = CGRectGetMinY([view window].screen.visibleFrame);
    CGFloat distanceFromBottom = lowestMenuPoint - screenBottom;
    if (distanceFromBottom < 0)
      position.y = position.y - distanceFromBottom + 4;
  }

  // Place the menu left of cursor if it is overflowing off right of screen.
  CGFloat windowLeft = CGRectGetMinX([view window].frame);
  CGFloat rightmostMenuPoint = windowLeft + position.x + [menu size].width;
  CGFloat screenRight = CGRectGetMaxX([view window].screen.visibleFrame);
  if (rightmostMenuPoint > screenRight)
    position.x = position.x - [menu size].width;

  [popup_controllers_[window_id] setCloseCallback:std::move(close_callback)];

  if (frame) {
    auto* rfh = frame->render_frame_host()->GetOutermostMainFrameOrEmbedder();
    if (rfh->IsRenderFrameLive()) {
      auto* rwhvm =
          static_cast<content::RenderWidgetHostViewMac*>(rfh->GetView());
      RenderWidgetHostViewCocoa* cocoa_view = rwhvm->GetInProcessNSView();
      view = cocoa_view;
    }
  }

  NSEvent* dummy_event = [NSEvent mouseEventWithType:NSEventTypeRightMouseDown
                                            location:position
                                       modifierFlags:0
                                           timestamp:0
                                        windowNumber:nswindow.windowNumber
                                             context:nil
                                         eventNumber:0
                                          clickCount:1
                                            pressure:0];
  ui::ShowContextMenu(menu, dummy_event, view, true);
}

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));

Review comment (bug):
Probably worth checking this since it can be a nullptr
```suggestion
    auto* frame_rfh = frame->render_frame_host();
    auto* rfh = frame_rfh ? frame_rfh->GetOutermostMainFrameOrEmbedder() : nullptr;
```

Code after:
                         fromView:nil];
  } else {
    position = NSMakePoint(x, [view frame].size.height - y);
  }

  // If no preferred item is specified, try to show all of the menu items.
  if (!item) {
    CGFloat windowBottom = CGRectGetMinY([view window].frame);
    CGFloat lowestMenuPoint = windowBottom + position.y - [menu size].height;
    CGFloat screenBottom = CGRectGetMinY([view window].screen.visibleFrame);
    CGFloat distanceFromBottom = lowestMenuPoint - screenBottom;
    if (distanceFromBottom < 0)
      position.y = position.y - distanceFromBottom + 4;
  }

  // Place the menu left of cursor if it is overflowing off right of screen.
  CGFloat windowLeft = CGRectGetMinX([view window].frame);
  CGFloat rightmostMenuPoint = windowLeft + position.x + [menu size].width;
  CGFloat screenRight = CGRectGetMaxX([view window].screen.visibleFrame);
  if (rightmostMenuPoint > screenRight)
    position.x = position.x - [menu size].width;

  [popup_controllers_[window_id] setCloseCallback:std::move(close_callback)];

  if (frame && frame->render_frame_host()) {
    auto* rfh = frame->render_frame_host()->GetOutermostMainFrameOrEmbedder();
    if (rfh && rfh->IsRenderFrameLive()) {
      auto* rwhvm =
          static_cast<content::RenderWidgetHostViewMac*>(rfh->GetView());
      RenderWidgetHostViewCocoa* cocoa_view = rwhvm->GetInProcessNSView();
      view = cocoa_view;
    }
  }

  NSEvent* dummy_event = [NSEvent mouseEventWithType:NSEventTypeRightMouseDown
                                            location:position
                                       modifierFlags:0
                                           timestamp:0
                                        windowNumber:nswindow.windowNumber
                                             context:nil
                                         eventNumber:0
                                          clickCount:1
                                            pressure:0];
  ui::ShowContextMenu(menu, dummy_event, view, true);
}

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));