From 06f86960d5db2b94d299fd49fd4766c9bd46a899 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 22 Aug 2017 17:45:58 +1100 Subject: [PATCH] GUI2/Listbox: move some common code for key handling to its own function --- src/gui/widgets/listbox.cpp | 83 ++++++++++++------------------------- src/gui/widgets/listbox.hpp | 3 ++ 2 files changed, 30 insertions(+), 56 deletions(-) diff --git a/src/gui/widgets/listbox.cpp b/src/gui/widgets/listbox.cpp index 2b7d8944a4b3..8e848347529e 100644 --- a/src/gui/widgets/listbox.cpp +++ b/src/gui/widgets/listbox.cpp @@ -449,6 +449,29 @@ listbox::child_populate_dirty_list(window& caller, generator_->populate_dirty_list(caller, child_call_stack); } +void listbox::update_visible_area_on_key_event(const bool key_direction_vertical) +{ + const SDL_Rect& visible = content_visible_area(); + SDL_Rect rect = generator_->item(generator_->get_selected_item()).get_rectangle(); + + // When scrolling make sure the new items are visible... + if(key_direction_vertical) { + // ...but leave the horizontal scrollbar position. + rect.x = visible.x; + rect.w = visible.w; + } else { + // ...but leave the vertical scrollbar position. + rect.y = visible.y; + rect.h = visible.h; + } + + show_content_rect(rect); + + if(callback_value_changed_) { + callback_value_changed_(*this); + } +} + void listbox::handle_key_up_arrow(SDL_Keymod modifier, bool& handled) { assert(generator_); @@ -456,20 +479,7 @@ void listbox::handle_key_up_arrow(SDL_Keymod modifier, bool& handled) generator_->handle_key_up_arrow(modifier, handled); if(handled) { - // When scrolling make sure the new items is visible but leave the - // horizontal scrollbar position. - const SDL_Rect& visible = content_visible_area(); - SDL_Rect rect = generator_->item(generator_->get_selected_item()) - .get_rectangle(); - - rect.x = visible.x; - rect.w = visible.w; - - show_content_rect(rect); - - if(callback_value_changed_) { - callback_value_changed_(*this); - } + update_visible_area_on_key_event(true); } else { // Inherited. scrollbar_container::handle_key_up_arrow(modifier, handled); @@ -483,20 +493,7 @@ void listbox::handle_key_down_arrow(SDL_Keymod modifier, bool& handled) generator_->handle_key_down_arrow(modifier, handled); if(handled) { - // When scrolling make sure the new items is visible but leave the - // horizontal scrollbar position. - const SDL_Rect& visible = content_visible_area(); - SDL_Rect rect = generator_->item(generator_->get_selected_item()) - .get_rectangle(); - - rect.x = visible.x; - rect.w = visible.w; - - show_content_rect(rect); - - if(callback_value_changed_) { - callback_value_changed_(*this); - } + update_visible_area_on_key_event(true); } else { // Inherited. scrollbar_container::handle_key_up_arrow(modifier, handled); @@ -511,20 +508,7 @@ void listbox::handle_key_left_arrow(SDL_Keymod modifier, bool& handled) // Inherited. if(handled) { - // When scrolling make sure the new items is visible but leave the - // vertical scrollbar position. - const SDL_Rect& visible = content_visible_area(); - SDL_Rect rect = generator_->item(generator_->get_selected_item()) - .get_rectangle(); - - rect.y = visible.y; - rect.h = visible.h; - - show_content_rect(rect); - - if(callback_value_changed_) { - callback_value_changed_(*this); - } + update_visible_area_on_key_event(false); } else { scrollbar_container::handle_key_left_arrow(modifier, handled); } @@ -538,20 +522,7 @@ void listbox::handle_key_right_arrow(SDL_Keymod modifier, bool& handled) // Inherited. if(handled) { - // When scrolling make sure the new items is visible but leave the - // vertical scrollbar position. - const SDL_Rect& visible = content_visible_area(); - SDL_Rect rect = generator_->item(generator_->get_selected_item()) - .get_rectangle(); - - rect.y = visible.y; - rect.h = visible.h; - - show_content_rect(rect); - - if(callback_value_changed_) { - callback_value_changed_(*this); - } + update_visible_area_on_key_event(false); } else { scrollbar_container::handle_key_left_arrow(modifier, handled); } diff --git a/src/gui/widgets/listbox.hpp b/src/gui/widgets/listbox.hpp index ff56d7764c95..79f5a6723946 100644 --- a/src/gui/widgets/listbox.hpp +++ b/src/gui/widgets/listbox.hpp @@ -325,6 +325,9 @@ class listbox : public scrollbar_container void handle_key_right_arrow(SDL_Keymod modifier, bool& handled) override; private: + /** Helper to update visible area after a key event. */ + void update_visible_area_on_key_event(const bool key_direction_vertical); + /** * @todo A listbox must have the following config parameters in the * instantiation: