diff options
author | Adam Scott <ascott.ca@gmail.com> | 2024-11-04 15:50:05 -0500 |
---|---|---|
committer | Adam Scott <ascott.ca@gmail.com> | 2024-11-05 12:50:34 -0500 |
commit | 49f918e59612a18b8e2a58dfb7b43a851d2dfb45 (patch) | |
tree | 890e5ecb30c8794047997cb5748092aa3de3378a | |
parent | 1bffd6c73b44b85e5889f54e14b2193940cf5bb1 (diff) | |
download | redot-engine-49f918e59612a18b8e2a58dfb7b43a851d2dfb45.tar.gz |
Fix issue where scrolling to item center would overflow on top
-rw-r--r-- | scene/gui/tree.cpp | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index aab6f672f0..cfd93a5317 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -5362,14 +5362,16 @@ void Tree::scroll_to_item(TreeItem *p_item, bool p_center_on_item) { int y_offset = get_item_offset(p_item); if (y_offset != -1) { - const int tbh = _get_title_button_height(); - y_offset -= tbh; + const int title_button_height = _get_title_button_height(); + y_offset -= title_button_height; const int cell_h = compute_item_height(p_item) + theme_cache.v_separation; - int screen_h = area_size.height - tbh; + int screen_h = area_size.height - title_button_height; if (p_center_on_item) { - v_scroll->set_value(y_offset - (screen_h - cell_h) / 2.0f); + // This makes sure that centering the offset doesn't overflow. + const double v_scroll_value = y_offset - MAX((screen_h - cell_h) / 2.0, 0.0); + v_scroll->set_value(v_scroll_value); } else { if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet. v_scroll->set_value(y_offset); |