Fix scrolling and keep lists clear of the scrollbar

Scroll kept its position as an int, so wheel and trackpad deltas were
truncated: small ones were lost in one direction and a wheel notch moved
one unit. It now keeps a fractional position, scrolls 24 units a notch,
sizes and places its thumb consistently, and the thumb can be dragged or
the track clicked. PanelScreen's scrollWidth and scrollContentWidth keep
every scrolling list clear of the bar.

Sliders read a float step from its text, so 0.1 no longer shows 17
decimals, and show at most three; number boxes show at most three too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-09-27 17:54:12 +02:00
co-authored by claude
parent 33c09b6922
commit 351507d60d
8 changed files with 152 additions and 88 deletions
@@ -4,24 +4,40 @@ import java.util.ArrayList;
import java.util.List;
import org.saturnclient.config.Theme;
import org.saturnclient.config.property.IntProperty;
import org.saturnclient.config.property.Property;
import org.saturnclient.ui.Element;
import org.saturnclient.ui.ElementContext;
import org.saturnclient.ui.ElementRenderer;
import org.saturnclient.ui.RenderScope;
/**
* A vertically scrolling container. It scrolls with the mouse wheel or by
* dragging its scrollbar, which shows along its right edge when the children
* don't fit.
*/
public class Scroll extends Element {
private static IntProperty scrollBarRadius = Property.integer(10);
private static IntProperty scrollBarWidth = Property.integer(5);
private static IntProperty scrollBarPadding = Property.integer(5);
/** Units scrolled per mouse wheel notch */
private static final double SCROLL_SPEED = 24;
/** Space left below the last child when scrolled to the end */
private static final int END_SPACE = 10;
private static final int BAR_WIDTH = 5;
private static final int BAR_INSET = 5;
private static final int MIN_THUMB = 20;
/** Width to keep clear at the right of the content for the scrollbar and its click area */
public static final int SCROLLBAR_SPACE = BAR_WIDTH + BAR_INSET * 2 + 4;
int padding = 0;
boolean background = true;
protected List<Element> children = new ArrayList<>();
int scroll = 0;
int maxScroll = 0;
/** Kept fractional so small trackpad deltas add up instead of being dropped */
private double scroll = 0;
private int maxScroll = 0;
/** Where in the thumb it was grabbed, while it's being dragged */
private Integer thumbGrab = null;
public Scroll(int padding) {
this.padding = padding;
@@ -50,7 +66,8 @@ public class Scroll extends Element {
@Override
public void render(RenderScope renderScope, ElementContext ctx) {
calculateMaxScroll();
updateMaxScroll();
int offset = getOffset();
if (background) {
renderScope.drawRoundedRectangle(0, 0, width, height,
@@ -59,75 +76,27 @@ public class Scroll extends Element {
renderScope.enableScissor(padding, padding, width - padding, height - padding);
renderScope.getMatrixStack().push();
renderScope.getMatrixStack().translate(padding, -scroll + padding);
renderScope.getMatrixStack().translate(padding, -offset + padding);
ElementRenderer.render(children, ctx.elapsed, renderScope,
ctx.mouseX - padding,
ctx.mouseY - padding + scroll);
ctx.mouseY - padding + offset);
renderScope.getMatrixStack().pop();
renderScope.disableScissor();
if (maxScroll > 0) {
renderScope.drawRoundedRectangle(
width - scrollBarWidth.value - scrollBarPadding.value,
calculateScrollBarY(),
scrollBarWidth.value,
calculateScrollBarHeight(),
scrollBarRadius.value,
Theme.SCROLL.value);
boolean active = thumbGrab != null || ctx.isHovering(getBarX() - BAR_INSET, 0, BAR_WIDTH + BAR_INSET * 2,
height);
renderScope.drawRoundedRectangle(getBarX(), getThumbY(), BAR_WIDTH, getThumbHeight(), BAR_WIDTH / 2,
active ? Theme.SCROLL.value : Theme.withAlpha(0.7f, Theme.SCROLL.value));
}
}
@Override
public void scroll(int mouseX, int mouseY, double horizontalAmount, double verticalAmount) {
scroll -= verticalAmount;
if (scroll < 0) {
scroll = 0;
} else if (scroll > maxScroll) {
scroll = maxScroll;
}
}
@Override
public Element dimensions(int width, int height) {
super.dimensions(width, height);
maxScroll = 0;
for (Element element : children) {
maxScroll = Math.max(maxScroll, (element.y + element.height + padding) - (height - padding * 2));
}
return this;
}
int calculateScrollBarHeight() {
if (maxScroll <= 0)
return height - (scrollBarPadding.value * 2);
return Math.max(20, (height * height) / (height + maxScroll + 10)) - (scrollBarPadding.value * 2);
}
int calculateScrollBarY() {
int scrollBarHeight = calculateScrollBarHeight();
if (maxScroll <= 0)
return scrollBarPadding.value;
int y = (scroll * (height - scrollBarHeight - scrollBarPadding.value * 2)) / maxScroll + scrollBarPadding.value;
return Math.min(y, height - scrollBarHeight - scrollBarPadding.value);
}
private void calculateMaxScroll() {
maxScroll = 0;
for (Element element : children) {
int bottom = element.y + element.height;
maxScroll = Math.max(maxScroll, bottom - height + padding);
}
if (maxScroll > 0) {
maxScroll = maxScroll + 10;
} else {
maxScroll = 0;
}
setScroll(scroll - verticalAmount * SCROLL_SPEED);
}
@Override
@@ -146,17 +115,80 @@ public class Scroll extends Element {
return;
}
ElementRenderer.mouseClicked(children, mouseX - padding, mouseY - padding + scroll, button);
if (maxScroll > 0 && mouseX >= getBarX() - BAR_INSET) {
int thumbY = getThumbY();
int thumbHeight = getThumbHeight();
// Grab the thumb where it was clicked, or center it on a click in the track.
boolean onThumb = mouseY >= thumbY && mouseY <= thumbY + thumbHeight;
thumbGrab = onThumb ? (int) mouseY - thumbY : thumbHeight / 2;
dragThumb(mouseY);
return;
}
ElementRenderer.mouseClicked(children, mouseX - padding, mouseY - padding + getOffset(), button);
}
@Override
public void mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
ElementRenderer.mouseDragged(children, mouseX - padding, mouseY - padding + scroll, button, deltaX,
if (thumbGrab != null) {
dragThumb(mouseY);
return;
}
ElementRenderer.mouseDragged(children, mouseX - padding, mouseY - padding + getOffset(), button, deltaX,
deltaY);
}
@Override
public void mouseReleased(double mouseX, double mouseY, int button) {
ElementRenderer.mouseReleased(children, mouseX - padding, mouseY - padding + scroll, button);
thumbGrab = null;
ElementRenderer.mouseReleased(children, mouseX - padding, mouseY - padding + getOffset(), button);
}
private void dragThumb(double mouseY) {
int travel = getTrackHeight() - getThumbHeight();
if (travel > 0) {
setScroll((mouseY - thumbGrab - BAR_INSET) / travel * maxScroll);
}
}
private void setScroll(double value) {
scroll = Math.max(0, Math.min(maxScroll, value));
}
private int getOffset() {
return (int) Math.round(scroll);
}
private void updateMaxScroll() {
int bottom = 0;
for (Element element : children) {
bottom = Math.max(bottom, element.y + element.height);
}
int overflow = bottom + padding * 2 - height;
maxScroll = overflow > 0 ? overflow + END_SPACE : 0;
// The content may have shrunk, such as after filtering.
setScroll(scroll);
}
private int getBarX() {
return width - BAR_WIDTH - BAR_INSET;
}
private int getTrackHeight() {
return height - BAR_INSET * 2;
}
private int getThumbHeight() {
int track = getTrackHeight();
return Math.max(MIN_THUMB, (int) ((long) track * height / (height + maxScroll)));
}
private int getThumbY() {
int travel = getTrackHeight() - getThumbHeight();
return BAR_INSET + (int) (travel * (scroll / maxScroll));
}
}