Code cleanup

This commit is contained in:
Henrik Rydgård
2025-11-22 14:38:58 +01:00
parent 77c7ba1435
commit 8042a57794
2 changed files with 34 additions and 15 deletions
+24 -12
View File
@@ -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<void(UI::ViewGroup *)> createViews, UI::View *sourceView) : PopupScreen(""), createViews_(createViews) {
PopupCallbackScreen::PopupCallbackScreen(std::function<void(UI::ViewGroup *)> 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) {
+10 -3
View File
@@ -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<bool> enabled_;
};
class PopupCallbackScreen : public PopupScreen {
class PopupCallbackScreen : public AbstractContextMenuScreen {
public:
PopupCallbackScreen(std::function<void(UI::ViewGroup *)> createViews, UI::View *sourceView);
const char *tag() const override { return "ContextMenuCallbackPopup"; }