diff --git a/Common/UI/PopupScreens.cpp b/Common/UI/PopupScreens.cpp index 3ce55f874f..3905746872 100644 --- a/Common/UI/PopupScreens.cpp +++ b/Common/UI/PopupScreens.cpp @@ -231,8 +231,27 @@ void ListPopupScreen::OnListChoice(UI::EventParams &e) { OnChoice.Dispatch(e); } +void AbstractContextMenuScreen::AlignPopup(UI::View *parent) { + if (!sourceView_) { + // No menu-like arrangement + return; + } + + // Hacky: Override the position to look like a popup menu. + + AnchorLayoutParams *ap = (AnchorLayoutParams *)parent->GetLayoutParams(); + ap->centering = Centering::None; + if (sourceView_->GetBounds().x2() > g_display.pixel_xres - 300) { + ap->right = sourceView_->GetBounds().x2(); + } else { + ap->left = sourceView_->GetBounds().x; + } + ap->top = sourceView_->GetBounds().y2(); + +} + PopupContextMenuScreen::PopupContextMenuScreen(const ContextMenuItem *items, size_t itemCount, I18NCat category, UI::View *sourceView) - : PopupScreen("", "", ""), items_(items), itemCount_(itemCount), category_(category), sourceView_(sourceView) + : AbstractContextMenuScreen(sourceView), items_(items), itemCount_(itemCount), category_(category) { enabled_.resize(itemCount, true); SetPopupOrigin(sourceView); @@ -260,19 +279,10 @@ void PopupContextMenuScreen::CreatePopupContents(UI::ViewGroup *parent) { } } - // Hacky: Override the position to look like a popup menu. - - AnchorLayoutParams *ap = (AnchorLayoutParams *)parent->GetLayoutParams(); - ap->centering = Centering::None; - if (sourceView_->GetBounds().x2() > g_display.pixel_xres - 300) { - ap->right = sourceView_->GetBounds().x2(); - } else { - ap->left = sourceView_->GetBounds().x; - } - ap->top = sourceView_->GetBounds().y2(); + AlignPopup(parent); } -PopupCallbackScreen::PopupCallbackScreen(std::function createViews, UI::View *sourceView) : PopupScreen(""), createViews_(createViews) { +PopupCallbackScreen::PopupCallbackScreen(std::function createViews, UI::View *sourceView) : AbstractContextMenuScreen(sourceView), createViews_(createViews) { if (sourceView) { SetPopupOrigin(sourceView); } @@ -283,6 +293,8 @@ void PopupCallbackScreen::CreatePopupContents(ViewGroup *parent) { for (int i = 0; i < parent->GetNumSubviews(); i++) { parent->GetViewByIndex(i)->SetAutoResult(DialogResult::DR_OK); } + + AlignPopup(parent); } std::string ChopTitle(const std::string &title) { diff --git a/Common/UI/PopupScreens.h b/Common/UI/PopupScreens.h index 3507a9707a..b16f7d28e9 100644 --- a/Common/UI/PopupScreens.h +++ b/Common/UI/PopupScreens.h @@ -237,8 +237,16 @@ struct ContextMenuItem { const char *imageID; }; +class AbstractContextMenuScreen : public PopupScreen { +public: + AbstractContextMenuScreen(UI::View *sourceView) : PopupScreen("", "", ""), sourceView_(sourceView) {} +protected: + UI::View *sourceView_; + void AlignPopup(UI::View *parent); +}; + // Once a selection has been made, -class PopupContextMenuScreen : public PopupScreen { +class PopupContextMenuScreen : public AbstractContextMenuScreen { public: PopupContextMenuScreen(const ContextMenuItem *items, size_t itemCount, I18NCat category, UI::View *sourceView); const char *tag() const override { return "ContextMenuPopup"; } @@ -254,11 +262,10 @@ private: const ContextMenuItem *items_; size_t itemCount_; I18NCat category_; - UI::View *sourceView_; std::vector enabled_; }; -class PopupCallbackScreen : public PopupScreen { +class PopupCallbackScreen : public AbstractContextMenuScreen { public: PopupCallbackScreen(std::function createViews, UI::View *sourceView); const char *tag() const override { return "ContextMenuCallbackPopup"; }