scrollbar optimization

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-06-15 14:42:35 +02:00
parent ea0208083f
commit d9a0dfb8bd
4 changed files with 46 additions and 29 deletions

View File

@@ -67,6 +67,7 @@ void Menu::setItemSize(const size_t size)
m_items.erase(m_items.begin() + static_cast<int>(size + 1), m_items.end());
}
}
void Menu::toggle(const MenuItem &menuItem)
{
const auto item =
@@ -312,7 +313,7 @@ void Menu::addSelection(uint8_t id, const std::string &text, const std::vector<s
m_items.emplace_back(id, MenuItemTypes::SELECTION, text, values, index, callback);
}
void Menu::addToggle(uint8_t id, const std::string &text, bool selected)
void Menu::addToggle(uint8_t id, const std::string &text, const bool selected)
{
auto callback = [this](const MenuItem &menuItem, const ButtonType button) -> void {
onButtonPressed(menuItem, button);

View File

@@ -9,17 +9,16 @@ ScrollBar::ScrollBar(const menu_options_t *options, const size_t x, const size_t
void ScrollBar::render()
{
// Early return if scrollbar is not needed (only one item)
if (m_max <= 1) {
if (m_max <= 1)
{
return;
}
// Draw dotted track line for visual reference
for (size_t y = m_y; y < m_y + m_height; y += 2) {
for (size_t y = m_y; y < m_y + m_height; y += 2)
{
u8g2_DrawPixel(u8g2, m_x, y);
}
// Draw scroll thumb to indicate current position
u8g2_DrawBox(u8g2, u8g2->width - 4, m_thumbY, 3, m_thumbHeight);
}
@@ -29,6 +28,14 @@ void ScrollBar::refresh(const size_t value, const size_t max, const size_t min)
m_max = max;
m_min = min;
m_thumbHeight = m_height / m_max;
m_thumbY = m_y + (m_value - m_min) * m_thumbHeight;
}
if (m_max <= 1)
{
return;
}
m_thumbHeight = std::max(m_height / 4, static_cast<size_t>(3));
const size_t trackLength = m_height - m_thumbHeight;
m_thumbY = m_y + (m_value * trackLength) / (m_max - 1);
}