Skip to content

Take padding into account when scrolling #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2022
Merged
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
19 changes: 12 additions & 7 deletions src/main/java/org/fxmisc/flowless/VirtualizedScrollPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import javafx.beans.DefaultProperty;
import javafx.beans.NamedArg;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.value.ChangeListener;
import javafx.css.PseudoClass;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Node;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Region;
Expand All @@ -21,7 +20,7 @@
import org.reactfx.value.Var;

@DefaultProperty("content")
public class VirtualizedScrollPane<V extends Node & Virtualized> extends Region implements Virtualized {
public class VirtualizedScrollPane<V extends Region & Virtualized> extends Region implements Virtualized {

private static final PseudoClass CONTENT_FOCUSED = PseudoClass.getPseudoClass("content-focused");

Expand Down Expand Up @@ -84,12 +83,14 @@ public VirtualizedScrollPane(
hPosEstimate = Val.combine(
content.estimatedScrollXProperty(),
Val.map(content.layoutBoundsProperty(), Bounds::getWidth),
Val.map(content.paddingProperty(), p -> p.getLeft() + p.getRight()),
content.totalWidthEstimateProperty(),
VirtualizedScrollPane::offsetToScrollbarPosition)
.asVar(this::setHPosition);
vPosEstimate = Val.combine(
content.estimatedScrollYProperty(),
Val.map(content.layoutBoundsProperty(), Bounds::getHeight),
Val.map(content.paddingProperty(), p -> p.getTop() + p.getBottom()),
content.totalHeightEstimateProperty(),
VirtualizedScrollPane::offsetToScrollbarPosition)
.orElseConst(0.0)
Expand Down Expand Up @@ -320,17 +321,21 @@ protected void layoutChildren() {
}

private void setHPosition(double pos) {
Insets padding = content.getPadding();
double offset = scrollbarPositionToOffset(
pos,
content.getLayoutBounds().getWidth(),
padding.getLeft() + padding.getRight(),
content.totalWidthEstimateProperty().getValue());
content.estimatedScrollXProperty().setValue((double) Math.round(offset));
}

private void setVPosition(double pos) {
Insets padding = content.getPadding();
double offset = scrollbarPositionToOffset(
pos,
content.getLayoutBounds().getHeight(),
padding.getTop() + padding.getBottom(),
content.totalHeightEstimateProperty().getValue());
// offset needs rounding otherwise thin lines appear between cells,
// usually only visible when cells have dark backgrounds/borders.
Expand All @@ -353,16 +358,16 @@ protected double computeValue() {
}

private static double offsetToScrollbarPosition(
double contentOffset, double viewportSize, double contentSize) {
double contentOffset, double viewportSize, double padding, double contentSize) {
return contentSize > viewportSize
? contentOffset / (contentSize - viewportSize) * contentSize
? contentOffset / (contentSize - viewportSize + padding) * contentSize
: 0;
}

private static double scrollbarPositionToOffset(
double scrollbarPos, double viewportSize, double contentSize) {
double scrollbarPos, double viewportSize, double padding, double contentSize) {
return contentSize > viewportSize
? scrollbarPos / contentSize * (contentSize - viewportSize)
? scrollbarPos / contentSize * (contentSize - viewportSize + padding)
: 0;
}
}