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
@@ -3,6 +3,7 @@ package org.saturnclient.ui;
import org.saturnclient.ui.components.Sidebar; import org.saturnclient.ui.components.Sidebar;
import org.saturnclient.ui.elements.ImageTexture; import org.saturnclient.ui.elements.ImageTexture;
import org.saturnclient.ui.elements.Panel; import org.saturnclient.ui.elements.Panel;
import org.saturnclient.ui.elements.Scroll;
import org.saturnclient.ui.resources.Textures; import org.saturnclient.ui.resources.Textures;
/** /**
@@ -18,6 +19,9 @@ public abstract class PanelScreen extends SaturnScreen {
protected static final int PADDING = 20; protected static final int PADDING = 20;
/** How far a scroll reaches past its content area, into the padding, for its bar */
private static final int SCROLL_OVERHANG = 14;
private final int sidebarItem; private final int sidebarItem;
protected int panelX, panelY, panelWidth, panelHeight; protected int panelX, panelY, panelWidth, panelHeight;
@@ -61,4 +65,18 @@ public abstract class PanelScreen extends SaturnScreen {
* Draws the panel's content. * Draws the panel's content.
*/ */
protected abstract void panel(); protected abstract void panel();
/**
* The width of a scroll filling {@code width} of the content area.
*/
protected static int scrollWidth(int width) {
return width + SCROLL_OVERHANG;
}
/**
* The width left for a scroll's children, clear of its scrollbar.
*/
protected static int scrollContentWidth(int width) {
return scrollWidth(width) - Scroll.SCROLLBAR_SPACE;
}
} }
@@ -1,5 +1,8 @@
package org.saturnclient.ui.components.inputs; package org.saturnclient.ui.components.inputs;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.saturnclient.config.ConfigManager; import org.saturnclient.config.ConfigManager;
import org.saturnclient.config.property.FloatProperty; import org.saturnclient.config.property.FloatProperty;
import org.saturnclient.ui.elements.Field; import org.saturnclient.ui.elements.Field;
@@ -11,7 +14,16 @@ public class FloatInput extends Input {
this.width = 96; this.width = 96;
this.height = Field.HEIGHT; this.height = Field.HEIGHT;
this.prop = prop; this.prop = prop;
this.text = String.valueOf(prop.value); this.text = format(prop.value);
}
/**
* The value with at most three decimals and no trailing zeros, since floats
* like 1.2000000476837158 can't be stored exactly.
*/
private static String format(float value) {
return new BigDecimal(Float.toString(value)).setScale(3, RoundingMode.HALF_UP).stripTrailingZeros()
.toPlainString();
} }
@Override @Override
@@ -51,7 +63,7 @@ public class FloatInput extends Input {
@Override @Override
public void checkReset() { public void checkReset() {
if (prop.isReset) { if (prop.isReset) {
this.text = String.valueOf(prop.value); this.text = format(prop.value);
this.cursorPosition = 0; this.cursorPosition = 0;
prop.isReset = false; prop.isReset = false;
} }
@@ -23,6 +23,7 @@ public class Slider extends Element {
private static final int KNOB = 16; private static final int KNOB = 16;
private static final int VALUE_WIDTH = 72; private static final int VALUE_WIDTH = 72;
private static final int VALUE_GAP = 14; private static final int VALUE_GAP = 14;
private static final int MAX_DECIMALS = 3;
private final double min, max, step; private final double min, max, step;
private final int decimals; private final int decimals;
@@ -32,25 +33,28 @@ public class Slider extends Element {
private boolean dragging = false; private boolean dragging = false;
private Slider(double min, double max, double step, String unit, DoubleSupplier value, DoubleConsumer onChange) { /**
* @param step as text, so a float step like 0.1 isn't read as 0.10000000149
*/
private Slider(double min, double max, String step, String unit, DoubleSupplier value, DoubleConsumer onChange) {
this.min = min; this.min = min;
this.max = max; this.max = max;
this.step = step; this.step = Double.parseDouble(step);
this.unit = unit; this.unit = unit;
this.value = value; this.value = value;
this.onChange = onChange; this.onChange = onChange;
this.decimals = Math.max(0, BigDecimal.valueOf(step).stripTrailingZeros().scale()); this.decimals = Math.min(MAX_DECIMALS, Math.max(0, new BigDecimal(step).stripTrailingZeros().scale()));
this.width = 280; this.width = 280;
this.height = Field.HEIGHT; this.height = Field.HEIGHT;
} }
public static Slider of(IntProperty prop) { public static Slider of(IntProperty prop) {
return new Slider(prop.min, prop.max, prop.step, prop.unit, () -> prop.value, return new Slider(prop.min, prop.max, Integer.toString(prop.step), prop.unit, () -> prop.value,
v -> prop.value = (int) Math.round(v)); v -> prop.value = (int) Math.round(v));
} }
public static Slider of(FloatProperty prop) { public static Slider of(FloatProperty prop) {
return new Slider(prop.min, prop.max, prop.step, prop.unit, () -> prop.value, return new Slider(prop.min, prop.max, Float.toString(prop.step), prop.unit, () -> prop.value,
v -> prop.value = (float) v); v -> prop.value = (float) v);
} }
@@ -4,24 +4,40 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.saturnclient.config.Theme; 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.Element;
import org.saturnclient.ui.ElementContext; import org.saturnclient.ui.ElementContext;
import org.saturnclient.ui.ElementRenderer; import org.saturnclient.ui.ElementRenderer;
import org.saturnclient.ui.RenderScope; 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 { public class Scroll extends Element {
private static IntProperty scrollBarRadius = Property.integer(10); /** Units scrolled per mouse wheel notch */
private static IntProperty scrollBarWidth = Property.integer(5); private static final double SCROLL_SPEED = 24;
private static IntProperty scrollBarPadding = Property.integer(5); /** 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; int padding = 0;
boolean background = true; boolean background = true;
protected List<Element> children = new ArrayList<>(); 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) { public Scroll(int padding) {
this.padding = padding; this.padding = padding;
@@ -50,7 +66,8 @@ public class Scroll extends Element {
@Override @Override
public void render(RenderScope renderScope, ElementContext ctx) { public void render(RenderScope renderScope, ElementContext ctx) {
calculateMaxScroll(); updateMaxScroll();
int offset = getOffset();
if (background) { if (background) {
renderScope.drawRoundedRectangle(0, 0, width, height, renderScope.drawRoundedRectangle(0, 0, width, height,
@@ -59,75 +76,27 @@ public class Scroll extends Element {
renderScope.enableScissor(padding, padding, width - padding, height - padding); renderScope.enableScissor(padding, padding, width - padding, height - padding);
renderScope.getMatrixStack().push(); renderScope.getMatrixStack().push();
renderScope.getMatrixStack().translate(padding, -scroll + padding); renderScope.getMatrixStack().translate(padding, -offset + padding);
ElementRenderer.render(children, ctx.elapsed, renderScope, ElementRenderer.render(children, ctx.elapsed, renderScope,
ctx.mouseX - padding, ctx.mouseX - padding,
ctx.mouseY - padding + scroll); ctx.mouseY - padding + offset);
renderScope.getMatrixStack().pop(); renderScope.getMatrixStack().pop();
renderScope.disableScissor(); renderScope.disableScissor();
if (maxScroll > 0) { if (maxScroll > 0) {
renderScope.drawRoundedRectangle( boolean active = thumbGrab != null || ctx.isHovering(getBarX() - BAR_INSET, 0, BAR_WIDTH + BAR_INSET * 2,
width - scrollBarWidth.value - scrollBarPadding.value, height);
calculateScrollBarY(),
scrollBarWidth.value, renderScope.drawRoundedRectangle(getBarX(), getThumbY(), BAR_WIDTH, getThumbHeight(), BAR_WIDTH / 2,
calculateScrollBarHeight(), active ? Theme.SCROLL.value : Theme.withAlpha(0.7f, Theme.SCROLL.value));
scrollBarRadius.value,
Theme.SCROLL.value);
} }
} }
@Override @Override
public void scroll(int mouseX, int mouseY, double horizontalAmount, double verticalAmount) { public void scroll(int mouseX, int mouseY, double horizontalAmount, double verticalAmount) {
scroll -= verticalAmount; setScroll(scroll - verticalAmount * SCROLL_SPEED);
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;
}
} }
@Override @Override
@@ -146,17 +115,80 @@ public class Scroll extends Element {
return; 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 @Override
public void mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { 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); deltaY);
} }
@Override @Override
public void mouseReleased(double mouseX, double mouseY, int button) { 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));
} }
} }
@@ -52,7 +52,6 @@ public class ClientSettings extends PanelScreen {
private static final int LIST_Y = 58; private static final int LIST_Y = 58;
private static final int SECTION_GAP = 22; private static final int SECTION_GAP = 22;
private static final int PRESET_GAP = 10; private static final int PRESET_GAP = 10;
private static final int SCROLLBAR_SPACE = 14;
private Section section = Section.GENERAL; private Section section = Section.GENERAL;
@@ -88,14 +87,15 @@ public class ClientSettings extends PanelScreen {
// The section's settings // The section's settings
Scroll scroll = new Scroll(0).noBackground(); Scroll scroll = new Scroll(0).noBackground();
int listWidth = scrollContentWidth(width);
switch (section) { switch (section) {
case GENERAL -> PropertyList.draw(scroll, getGeneralProperties(), Set.of(), width, 0); case GENERAL -> PropertyList.draw(scroll, getGeneralProperties(), Set.of(), listWidth, 0);
case UI_STYLE -> drawUiStyle(scroll, width); case UI_STYLE -> drawUiStyle(scroll, listWidth);
case ANIMATIONS -> PropertyList.draw(scroll, AnimationConfig.config.getProperties(), Set.of(), width, 0); case ANIMATIONS -> PropertyList.draw(scroll, AnimationConfig.config.getProperties(), Set.of(), listWidth, 0);
} }
draw(scroll.dimensions(width + SCROLLBAR_SPACE, contentHeight - LIST_Y).position(x, contentY + LIST_Y)); draw(scroll.dimensions(scrollWidth(width), contentHeight - LIST_Y).position(x, contentY + LIST_Y));
} }
private void drawUiStyle(Scroll scroll, int width) { private void drawUiStyle(Scroll scroll, int width) {
@@ -101,7 +101,6 @@ public class CosmeticsMenu extends PanelScreen {
private static final int COLUMNS = 4; private static final int COLUMNS = 4;
private static final int TILE_GAP = 10; private static final int TILE_GAP = 10;
private static final int GRID_Y = 58; private static final int GRID_Y = 58;
private static final int SCROLLBAR_SPACE = 14;
private static final float SMALL_SCALE = 0.6f; private static final float SMALL_SCALE = 0.6f;
private Kind kind = Kind.CLOAKS; private Kind kind = Kind.CLOAKS;
@@ -139,10 +138,10 @@ public class CosmeticsMenu extends PanelScreen {
scroll.draw(new Label(() -> message, 0.7f, Theme::getBodyFont, Theme::getMutedFg) scroll.draw(new Label(() -> message, 0.7f, Theme::getBodyFont, Theme::getMutedFg)
.position(messageX, 60)); .position(messageX, 60));
} else { } else {
scroll.draw(createGrid(width)); scroll.draw(createGrid(scrollContentWidth(width)));
} }
draw(scroll.dimensions(width + SCROLLBAR_SPACE, gridHeight).position(x, contentY + GRID_Y)); draw(scroll.dimensions(scrollWidth(width), gridHeight).position(x, contentY + GRID_Y));
} }
/** /**
@@ -24,7 +24,6 @@ public class ModMenu extends PanelScreen {
private static final int COLUMNS = 3; private static final int COLUMNS = 3;
private static final int CARD_GAP = 10; private static final int CARD_GAP = 10;
private static final int SCROLLBAR_SPACE = 14;
private String category = ALL; private String category = ALL;
private String query = ""; private String query = "";
@@ -40,7 +39,7 @@ public class ModMenu extends PanelScreen {
protected void panel() { protected void panel() {
// The search box starts empty after a resize, so the query does too. // The search box starts empty after a resize, so the query does too.
query = ""; query = "";
gridWidth = contentWidth; gridWidth = scrollContentWidth(contentWidth);
// Header // Header
draw(new Label(() -> "Mods", 1.15f, () -> Theme.FONT.value, () -> Theme.FOREGROUND.value) draw(new Label(() -> "Mods", 1.15f, () -> Theme.FONT.value, () -> Theme.FOREGROUND.value)
@@ -66,7 +65,7 @@ public class ModMenu extends PanelScreen {
// Mods // Mods
int scrollY = chipsY + categories.height + 14; int scrollY = chipsY + categories.height + 14;
scroll = new Scroll(0).noBackground(); scroll = new Scroll(0).noBackground();
scroll.dimensions(gridWidth + SCROLLBAR_SPACE, panelY + panelHeight - 12 - scrollY); scroll.dimensions(scrollWidth(contentWidth), panelY + panelHeight - 12 - scrollY);
refresh(true); refresh(true);
draw(scroll.position(contentX, scrollY)); draw(scroll.position(contentX, scrollY));
} }
@@ -33,7 +33,6 @@ public class ModSettings extends PanelScreen {
private static final int PREVIEW_WIDTH = 280; private static final int PREVIEW_WIDTH = 280;
private static final int PREVIEW_MAX_HEIGHT = 220; private static final int PREVIEW_MAX_HEIGHT = 220;
private static final int COLUMN_GAP = 22; private static final int COLUMN_GAP = 22;
private static final int SCROLLBAR_SPACE = 14;
private final Mod mod; private final Mod mod;
@@ -77,8 +76,9 @@ public class ModSettings extends PanelScreen {
} }
Scroll scroll = new Scroll(0).noBackground(); Scroll scroll = new Scroll(0).noBackground();
PropertyList.draw(scroll, mod.getConfig().getProperties(), Set.of(ENABLED), listWidth, 0); PropertyList.draw(scroll, mod.getConfig().getProperties(), Set.of(ENABLED), scrollContentWidth(listWidth),
draw(scroll.dimensions(listWidth + SCROLLBAR_SPACE, bodyHeight).position(contentX, bodyY)); 0);
draw(scroll.dimensions(scrollWidth(listWidth), bodyHeight).position(contentX, bodyY));
} }
/** /**