Skip to content
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

Added auto grow feature #77

Closed
wants to merge 2 commits into from
Closed
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
55 changes: 54 additions & 1 deletion src/main/java/org/fxmisc/flowless/VirtualFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import java.util.Optional;
import java.util.function.Function;

import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.ObservableList;
import javafx.css.CssMetaData;
import javafx.css.StyleConverter;
import javafx.css.Styleable;
import javafx.css.StyleableObjectProperty;
import javafx.css.StyleableProperty;
import javafx.geometry.Bounds;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Point2D;
import javafx.scene.input.ScrollEvent;
Expand Down Expand Up @@ -160,6 +164,21 @@ public Var<Double> lengthOffsetEstimateProperty() {
return lengthOffsetEstimate;
}

private BooleanProperty autoGrowProp = new SimpleBooleanProperty();
/**
* For Horizontal flows the width will auto grow, and for Vertical flows the height will auto grow,
* according to the number and size of each item in the VirtualFlow. The default is false.
*/
public BooleanProperty autoGrowProperty() {
return autoGrowProp;
}
public void setAutoGrow( boolean value ) {
autoGrowProp.set( value );
}
public boolean isAutoGrow() {
return autoGrowProp.get();
}

private VirtualFlow(
ObservableList<T> items,
Function<? super T, ? extends C> cellFactory,
Expand Down Expand Up @@ -303,7 +322,41 @@ private double computePrefBreadth() {
return 100;
}

private double computePrefLength(double breadth) {
private double computePrefLength(double breadth)
{
if ( autoGrowProp.get() )
{
if ( getWidth() == 0.0 || getHeight() == 0 )
{
Platform.runLater( () -> requestLayout() );
}
else
{
double size = 0.0;
Insets in = getInsets();

if ( getContentBias() == Orientation.HORIZONTAL ) // vertical flow
{
for ( int p = 0; p < items.size(); p++ ) {
size += getCell( p ).getNode().getLayoutBounds().getHeight();
}

if ( size > 0.0 ) {
return size + in.getTop() + in.getBottom();
}
}
else // horizontal flow
{
for ( int p = 0; p < items.size(); p++ ) {
size += getCell( p ).getNode().getLayoutBounds().getWidth();
}

if ( size > 0.0 ) {
return size + in.getLeft() + in.getRight();
}
}
}
}
return 100;
}

Expand Down