Skip to content

Commit

Permalink
Refactor scroll indicator methods and text padding function
Browse files Browse the repository at this point in the history
- Updated scroll indicator methods in MenuScreen class
- Refactored padding function in CharacterDisplayRenderer class
  • Loading branch information
forntoh committed Oct 6, 2024
1 parent 52a2436 commit 7b5c4c9
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/MenuItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class MenuItem {
/**
* @brief Draw this menu item on specified display on specified row.
* @param renderer The renderer to use for drawing.
* @param itemIndex The index of the item in the menu.
* @param screenRow The row on the screen where the item should be drawn.
*/
virtual void draw(MenuRenderer* renderer, uint8_t screenRow) {
Expand Down
6 changes: 3 additions & 3 deletions src/MenuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ void MenuScreen::draw(MenuRenderer* renderer) {
if (item == nullptr) {
break;
}
markScroll(i, renderer);
updateScrollIndicators(i, renderer);
item->draw(renderer, i);
}
}

void MenuScreen::markScroll(uint8_t index, MenuRenderer* renderer) {
void MenuScreen::updateScrollIndicators(uint8_t index, MenuRenderer* renderer) {
renderer->hasHiddenItemsAbove = index == 0 && view > 0;
renderer->hasHiddenItemsBelow = index == renderer->maxRows - 1 && (view + renderer->maxRows) < itemCount;
}

bool MenuScreen::process(LcdMenu* menu, const unsigned char command) {
MenuRenderer* renderer = menu->getRenderer();
markScroll(cursor - view, renderer);
updateScrollIndicators(cursor - view, renderer);
if (items[cursor]->process(menu, command)) {
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/MenuScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ class MenuScreen {
*/
void draw(MenuRenderer* renderer);
/**
* @brief Mark scroll indicators.
* @brief Update scroll indicators.
*/
void markScroll(uint8_t i, MenuRenderer* renderer);
void updateScrollIndicators(uint8_t index, MenuRenderer* renderer);
/**
* @brief Process the command.
* @return `true` if the command was processed, `false` otherwise.
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/CharacterDisplayRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void CharacterDisplayRenderer::drawItem(uint8_t screenRow, const char* text) {
buf[calculateAvailableLength()] = '\0';
uint8_t cursorCol = strlen(buf);

padText(buf, maxCols, buf);
padText(buf, buf);
appendIndicatorToText(screenRow, buf, buf);

display->setCursor(0, screenRow);
Expand Down Expand Up @@ -83,7 +83,7 @@ void CharacterDisplayRenderer::appendIndicatorToText(uint8_t screenRow, const ch
}
}

void CharacterDisplayRenderer::padText(const char* text, uint8_t itemIndex, char* buf) {
void CharacterDisplayRenderer::padText(const char* text, char* buf) {
uint8_t textLength = strlen(text);
uint8_t spaces = (textLength > calculateAvailableLength()) ? 0 : calculateAvailableLength() - textLength;
spaces = constrain(spaces, 0, maxCols);
Expand Down
4 changes: 1 addition & 3 deletions src/renderer/CharacterDisplayRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ class CharacterDisplayRenderer : public MenuRenderer {
* text is longer than the available length, no padding is added.
*
* @param text The input text to be padded.
* @param itemIndex The index of the item (not used in the current implementation).
* @param buf The buffer where the padded text will be stored. It should be large
* enough to hold the padded text.
*/
void padText(const char* text, uint8_t itemIndex, char* buf);
void padText(const char* text, char* buf);

/**
* @brief Calculates the available length for display.
Expand Down Expand Up @@ -107,7 +106,6 @@ class CharacterDisplayRenderer : public MenuRenderer {
* truncating the text if it's too long, padding the text with spaces,
* appending an indicator to the text, and finally drawing the text on the display.
*
* @param itemIndex The index of the item in the menu.
* @param screenRow The row on the screen where the item should be drawn.
* @param text The text of the menu item to be drawn.
*/
Expand Down

0 comments on commit 7b5c4c9

Please sign in to comment.