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

Change: merge duplicate logic to scroll in lists by key into a single location #9053

Merged

Conversation

@rubidium42
Copy link
Contributor

@rubidium42 rubidium42 commented Apr 18, 2021

Motivation / Problem

Reading #9011 I saw the code for handled up/down/page down/page up/home/end in lists in the NewGRF UI getting duplicated. This triggered me to look at other places with the same logic, and there were three with all mostly the same code with slight variations where they did the validations.
Code that is copied twice, and might be copied for a third time is not what we should aim for.

Description

Introduce a new function in Scrollbar to handle the list position update logic, and convert the existing versions to that single new one.

Limitations

None?

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')
src/widget.cpp Outdated Show resolved Hide resolved
src/widget.cpp Outdated
@@ -1974,6 +1974,66 @@ int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, in
return (pos >= this->GetCount()) ? INT_MAX : pos;
}

/**
* Update the given list position as if it were on this scroll bar when the given key_code was pressed.
Copy link
Contributor

@nielsmh nielsmh Apr 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This took me several tries to understand but I can't think of a better way to word it either.

@rubidium42 rubidium42 force-pushed the unduplicated_key_list_position_logic branch from a88d766 to eee6848 Apr 18, 2021
src/widget.cpp Outdated Show resolved Hide resolved
@rubidium42 rubidium42 force-pushed the unduplicated_key_list_position_logic branch from eee6848 to e673b62 Apr 20, 2021
@LordAro
Copy link
Member

@LordAro LordAro commented Apr 20, 2021

This is actually a codechange rather than a change, no? (Should have noticed before, my bad)

@rubidium42 rubidium42 force-pushed the unduplicated_key_list_position_logic branch from e673b62 to 2dca768 Apr 20, 2021
@rubidium42 rubidium42 merged commit bf4fe19 into OpenTTD:master Apr 21, 2021
12 checks passed
@rubidium42 rubidium42 deleted the unduplicated_key_list_position_logic branch Apr 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants