Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/renderer/GraphicalDisplayRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,26 @@ void GraphicalDisplayRenderer::drawScrollBar() {
return;
}

uint8_t x = gDisplay->getDisplayWidth() - scrollbarWidth;
uint8_t areaHeight = rows * rowHeight();
if (areaHeight > gDisplay->getDisplayHeight()) {
areaHeight = gDisplay->getDisplayHeight();
uint8_t displayWidth = gDisplay->getDisplayWidth();
uint16_t areaHeight16 = static_cast<uint16_t>(rows) * rowHeight();
if (areaHeight16 > gDisplay->getDisplayHeight()) {
areaHeight16 = gDisplay->getDisplayHeight();
}
uint8_t areaHeight = static_cast<uint8_t>(areaHeight16);

uint8_t x = displayWidth - scrollbarWidth;

gDisplay->setDrawColor(0);
gDisplay->drawBox(x, 0, scrollbarWidth, areaHeight);
gDisplay->setDrawColor(1);

uint8_t handleHeight = (static_cast<uint16_t>(rows) * areaHeight) / totalItems;
if (handleHeight < 2) {
handleHeight = 2;
}

uint16_t scrollRange = totalItems - rows;
uint16_t trackRange = areaHeight - handleHeight;
uint8_t y = scrollRange == 0 ? 0 : (static_cast<uint16_t>(viewStart) * trackRange) / scrollRange;
uint16_t trackRange = areaHeight > handleHeight ? areaHeight - handleHeight : 0;
uint16_t scrollRange = totalItems > rows ? totalItems - rows : 1;
uint8_t y = (static_cast<uint16_t>(viewStart) * trackRange) / scrollRange;
gDisplay->drawBox(x, y, scrollbarWidth, handleHeight);
}
19 changes: 18 additions & 1 deletion test/GraphicalDisplayRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class StubGraphicalDisplay : public GraphicalDisplayInterface {
uint8_t drawColorCount = 0;
uint8_t drawColors[4] = {0, 0, 0, 0};
uint8_t lastDrawBoxHeight = 0;
uint8_t lastDrawBoxY = 0;

void begin() override {}
void clear() override {}
Expand Down Expand Up @@ -43,8 +44,9 @@ class StubGraphicalDisplay : public GraphicalDisplayInterface {
}
void clearBuffer() override {}
void sendBuffer() override {}
void drawBox(uint8_t, uint8_t, uint8_t, uint8_t h) override {
void drawBox(uint8_t, uint8_t y, uint8_t, uint8_t h) override {
drawBoxCount++;
lastDrawBoxY = y;
lastDrawBoxHeight = h;
}
void drawFrame(uint8_t, uint8_t, uint8_t, uint8_t) override {}
Expand Down Expand Up @@ -84,6 +86,21 @@ unittest(graphical_renderer_uses_tight_row_height) {
assertEqual(8, display.lastDrawBoxHeight);
}

unittest(graphical_renderer_clears_scrollbar_track_before_handle) {
StubGraphicalDisplay display;
GraphicalDisplayRenderer renderer(&display);

renderer.setViewportContext(0, 9);
renderer.drawItem("Label", NULL);

assertEqual(3, display.drawBoxCount);
assertEqual(0, display.drawColors[0]);
assertEqual(1, display.drawColors[1]);
assertEqual(0, display.drawColors[2]);
assertEqual(1, display.drawColors[3]);
assertEqual(0, display.lastDrawBoxY);
}

unittest(graphical_renderer_colors_indicators_when_focused) {
StubGraphicalDisplay display;
FocusableGraphicalDisplayRenderer renderer(&display);
Expand Down
Loading