Skip to content

Commit

Permalink
GUI2/Listbox: move some common code for key handling to its own function
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Aug 22, 2017
1 parent 13717da commit 06f8696
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 56 deletions.
83 changes: 27 additions & 56 deletions src/gui/widgets/listbox.cpp
Expand Up @@ -449,27 +449,37 @@ 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_);

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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions src/gui/widgets/listbox.hpp
Expand Up @@ -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:
Expand Down

0 comments on commit 06f8696

Please sign in to comment.