Skip to content

Commit

Permalink
[GuiScrollbar] Enable full range on large lists
Browse files Browse the repository at this point in the history
* Problems with range calculations prevented access to full range in large lists
* These adjustments allow drag-scrolling to the end without any slop
* Tested on:
** Comms smaller than the window
** Comms larger than the window
** A log smaller than the window
** A log with 10,000 entries
  • Loading branch information
csibbitt committed Oct 12, 2023
1 parent 494fd70 commit b6909c4
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/gui/gui2_scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ void GuiScrollbar::onDraw(sp::RenderTarget& renderer)
renderer.drawStretched(rect, back.texture, back.color);

// Update the bar's range, size, and draggable bar size.
range = max_value - min_value;
range = max_value - min_value - value_size;
arrow_size = rect.size.x / 2.0f;
move_height = rect.size.y - arrow_size * 2;
// Clamp the size of the draggable bar to no less than 20px.
bar_size = std::clamp(move_height * value_size / range, 20.0F, move_height);
bar_size = std::clamp(move_height * value_size / (range + value_size), 20.0F, move_height);
// Set the bottom of the draggable bar no lower than the top of the bottom arrow.
const float bar_y = std::min(rect.position.y + arrow_size + move_height * getValue() / range, rect.position.y + rect.size.y - arrow_size - bar_size);
const float bar_y = std::min((rect.position.y + arrow_size + (move_height - bar_size) * getValue() / range), rect.position.y + rect.size.y - arrow_size - bar_size);

renderer.drawStretched(sp::Rect(rect.position.x, bar_y, rect.size.x, bar_size), front.texture, front.color);
}

bool GuiScrollbar::onMouseDown(sp::io::Pointer::Button button, glm::vec2 position, sp::io::Pointer::ID id)
{
float bar_y = rect.position.y + arrow_size + move_height * getValue() / range;
float bar_y = rect.position.y + arrow_size + (move_height - bar_size) * getValue() / range;
if (position.y >= bar_y && position.y <= bar_y + bar_size)
{
drag_scrollbar = true;
Expand All @@ -58,7 +58,7 @@ void GuiScrollbar::onMouseDrag(glm::vec2 position, sp::io::Pointer::ID id)
target_y_offset = std::min(target_y_offset, move_height - bar_size);

if (bar_size < move_height)
setValue(int(target_y_offset / move_height * range + 0.5f));
setValue(int(target_y_offset / (move_height - bar_size) * range + 0.5f));
}
}

Expand All @@ -71,7 +71,7 @@ void GuiScrollbar::onMouseUp(glm::vec2 position, sp::io::Pointer::ID id)
target_y_offset = std::min(target_y_offset, move_height - bar_size);

if (bar_size < move_height)
setValue(int(target_y_offset / move_height * range + 0.5f));
setValue(int(target_y_offset / (move_height - bar_size) * range + 0.5f));
}
}

Expand Down

0 comments on commit b6909c4

Please sign in to comment.