bool CContextMenuManager::OnClick(unsigned int id, const CFileItemPtr& item) { if (!item) return false; auto it = std::find_if(m_items.begin(), m_items.end(), [id](const Item& kv){ return kv.first == id; }); if (it == m_items.end()) { CLog::Log(LOGERROR, "CContextMenuManager: unknown button id '%u'", id); return false; } CContextMenuItem menuItem = it->second; if (menuItem.IsGroup()) { CLog::Log(LOGDEBUG, "CContextMenuManager: showing group '%s'", menuItem.ToString().c_str()); CContextButtons choices; AddVisibleItems(item, choices, menuItem); if (choices.empty()) { CLog::Log(LOGERROR, "CContextMenuManager: no items in group '%s'", menuItem.ToString().c_str()); return false; } int choice = CGUIDialogContextMenu::ShowAndGetChoice(choices); if (choice == -1) return false; return OnClick(choice, item); } return menuItem.Execute(item); }
int CGUIDialogContextMenu::ShowAndGetChoice(const CContextButtons &choices) { if (choices.empty()) return -1; CGUIDialogContextMenu *pMenu = g_windowManager.GetWindow<CGUIDialogContextMenu>(WINDOW_DIALOG_CONTEXT_MENU); if (pMenu) { pMenu->m_buttons = choices; pMenu->Initialize(); pMenu->SetInitialVisibility(); pMenu->SetupButtons(); pMenu->PositionAtCurrentFocus(); pMenu->Open(); int idx = pMenu->m_clickedButton; if (idx != -1) return choices[idx].first; } return -1; }
bool CGUIDialogPVRItemsViewBase::ContextMenu(int itemIdx) { auto InRange = [](size_t i, std::pair<size_t, size_t> range){ return i >= range.first && i < range.second; }; if (itemIdx < 0 || itemIdx >= m_vecItems->Size()) return false; const CFileItemPtr item = m_vecItems->Get(itemIdx); if (!item) return false; CContextButtons buttons; // Add the global menu const ContextMenuView globalMenu = CServiceBroker::GetContextMenuManager().GetItems(*item, CContextMenuManager::MAIN); auto globalMenuRange = std::make_pair(buttons.size(), buttons.size() + globalMenu.size()); for (const auto& menu : globalMenu) buttons.emplace_back(~buttons.size(), menu->GetLabel(*item)); // Add addon menus const ContextMenuView addonMenu = CServiceBroker::GetContextMenuManager().GetAddonItems(*item, CContextMenuManager::MAIN); auto addonMenuRange = std::make_pair(buttons.size(), buttons.size() + addonMenu.size()); for (const auto& menu : addonMenu) buttons.emplace_back(~buttons.size(), menu->GetLabel(*item)); if (buttons.empty()) return true; int idx = CGUIDialogContextMenu::Show(buttons); if (idx < 0 || idx >= static_cast<int>(buttons.size())) return false; Close(); if (InRange(idx, globalMenuRange)) return CONTEXTMENU::LoopFrom(*globalMenu[idx - globalMenuRange.first], item); return CONTEXTMENU::LoopFrom(*addonMenu[idx - addonMenuRange.first], item); }