Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/TrayIcon.cpp (C++)

Code before:

    nid.hIcon = static_cast<HICON>(GetActiveAppIconHandle(true));
    StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), appNameLoc.c_str());
    nid.uFlags = NIF_MESSAGE | NIF_SHOWTIP | NIF_TIP | NIF_ICON;
    Shell_NotifyIcon(NIM_ADD, &nid);

    // For whatever reason, the NIM_ADD call doesn't seem to set the version
    // properly, resulting in us being unable to receive the expected notification
    // events. We actually have to make a separate NIM_SETVERSION call for it to
    // work properly.
    nid.uVersion = NOTIFYICON_VERSION_4;
    Shell_NotifyIcon(NIM_SETVERSION, &nid);

    _trayIconData = nid;
}

// Method Description:
// - This creates our context menu and displays it at the given
//   screen coordinates.
// Arguments:
// - coord: The coordinates where we should be showing the context menu.
// - peasants: The map of all peasants that should be available in the context menu.
// Return Value:
// - <none>
void TrayIcon::ShowTrayContextMenu(const til::point coord,
                                   IVectorView<winrt::Microsoft::Terminal::Remoting::PeasantInfo> peasants)
{
    if (const auto hMenu = _CreateTrayContextMenu(peasants))
    {
        // We'll need to set our window to the foreground before calling
        // TrackPopupMenuEx or else the menu won't dismiss when clicking away.
        SetForegroundWindow(_trayIconHwnd.get());

        // User can select menu items with the left and right buttons.
        UINT uFlags = TPM_RIGHTBUTTON;

        // Nonzero if drop-down menus are right-aligned with the corresponding menu-bar item
        // 0 if the menus are left-aligned.
        if (GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0)
        {
            uFlags |= TPM_RIGHTALIGN;
        }
        else
        {
            uFlags |= TPM_LEFTALIGN;
        }

        TrackPopupMenuEx(hMenu, uFlags, gsl::narrow_cast<int>(coord.x()), gsl::narrow_cast<int>(coord.y()), _owningHwnd, NULL);
    }
}


Review comment (suggestion):
You could add a `const T&` to prevent the extra copy here and below.

Code after:

    nid.hIcon = static_cast<HICON>(GetActiveAppIconHandle(true));
    StringCchCopy(nid.szTip, ARRAYSIZE(nid.szTip), appNameLoc.c_str());
    nid.uFlags = NIF_MESSAGE | NIF_SHOWTIP | NIF_TIP | NIF_ICON;
    Shell_NotifyIcon(NIM_ADD, &nid);

    // For whatever reason, the NIM_ADD call doesn't seem to set the version
    // properly, resulting in us being unable to receive the expected notification
    // events. We actually have to make a separate NIM_SETVERSION call for it to
    // work properly.
    nid.uVersion = NOTIFYICON_VERSION_4;
    Shell_NotifyIcon(NIM_SETVERSION, &nid);

    _trayIconData = nid;
}

// Method Description:
// - This creates our context menu and displays it at the given
//   screen coordinates.
// Arguments:
// - coord: The coordinates where we should be showing the context menu.
// - peasants: The map of all peasants that should be available in the context menu.
// Return Value:
// - <none>
void TrayIcon::ShowTrayContextMenu(const til::point& coord,
                                   const IVectorView<winrt::Microsoft::Terminal::Remoting::PeasantInfo>& peasants)
{
    if (const auto hMenu = _CreateTrayContextMenu(peasants))
    {
        // We'll need to set our window to the foreground before calling
        // TrackPopupMenuEx or else the menu won't dismiss when clicking away.
        SetForegroundWindow(_trayIconHwnd.get());

        // User can select menu items with the left and right buttons.
        UINT uFlags = TPM_RIGHTBUTTON;

        // Nonzero if drop-down menus are right-aligned with the corresponding menu-bar item
        // 0 if the menus are left-aligned.
        if (GetSystemMetrics(SM_MENUDROPALIGNMENT) != 0)
        {
            uFlags |= TPM_RIGHTALIGN;
        }
        else
        {
            uFlags |= TPM_LEFTALIGN;
        }

        TrackPopupMenuEx(hMenu, uFlags, gsl::narrow_cast<int>(coord.x()), gsl::narrow_cast<int>(coord.y()), _owningHwnd, NULL);
    }
}