Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scrolling PopupMenu on keyboard/controller input #80271

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions scene/gui/popup_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1803,14 +1803,16 @@ int PopupMenu::get_item_count() const {
void PopupMenu::scroll_to_item(int p_idx) {
ERR_FAIL_INDEX(p_idx, items.size());

// Scroll item into view (upwards).
if (items[p_idx]._ofs_cache - scroll_container->get_v_scroll() < -control->get_position().y) {
scroll_container->set_v_scroll(items[p_idx]._ofs_cache + control->get_position().y);
}

// Scroll item into view (downwards).
if (items[p_idx]._ofs_cache + items[p_idx]._height_cache - scroll_container->get_v_scroll() > -control->get_position().y + scroll_container->get_size().height) {
scroll_container->set_v_scroll(items[p_idx]._ofs_cache + items[p_idx]._height_cache + control->get_position().y);
// Calculate the position of the item relative to the visible area.
int item_y = items[p_idx]._ofs_cache;
int visible_height = scroll_container->get_size().height;
int relative_y = item_y - scroll_container->get_v_scroll();

// If item is not fully visible, adjust scroll.
if (relative_y < 0) {
scroll_container->set_v_scroll(item_y);
} else if (relative_y + items[p_idx]._height_cache > visible_height) {
scroll_container->set_v_scroll(item_y + items[p_idx]._height_cache - visible_height);
}
}

Expand Down
Loading