Merge pull request #80271 from Ymanawat/optionmenu-scroll-test

Fix scrolling on keyboard/controller input
This commit is contained in:
Rémi Verschelde 2023-10-09 15:30:57 +02:00
commit c1fed53943
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -2170,14 +2170,16 @@ int PopupMenu::get_item_count() const {
void PopupMenu::scroll_to_item(int p_idx) { void PopupMenu::scroll_to_item(int p_idx) {
ERR_FAIL_INDEX(p_idx, items.size()); ERR_FAIL_INDEX(p_idx, items.size());
// Scroll item into view (upwards). // Calculate the position of the item relative to the visible area.
if (items[p_idx]._ofs_cache - scroll_container->get_v_scroll() < -control->get_position().y) { int item_y = items[p_idx]._ofs_cache;
scroll_container->set_v_scroll(items[p_idx]._ofs_cache + control->get_position().y); int visible_height = scroll_container->get_size().height;
} int relative_y = item_y - scroll_container->get_v_scroll();
// Scroll item into view (downwards). // If item is not fully visible, adjust scroll.
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) { if (relative_y < 0) {
scroll_container->set_v_scroll(items[p_idx]._ofs_cache + items[p_idx]._height_cache + control->get_position().y); 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);
} }
} }