Java Code Examples for org.reactfx.value.Val#combine()

The following examples show how to use org.reactfx.value.Val#combine() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ScaledVirtualized.java    From Flowless with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public ScaledVirtualized(V content) {
    super();
    this.content = content;
    getChildren().add(content);
    getTransforms().add(zoom);

    estHeight = Val.combine(
            content.totalHeightEstimateProperty(),
            zoom.yProperty(),
            (estHeight, scaleFactor) -> estHeight * scaleFactor.doubleValue()
    );
    estWidth = Val.combine(
            content.totalWidthEstimateProperty(),
            zoom.xProperty(),
            (estWidth, scaleFactor) -> estWidth * scaleFactor.doubleValue()
    );
    estScrollX = Var.mapBidirectional(
            content.estimatedScrollXProperty(),
            scrollX -> scrollX * zoom.getX(),
            scrollX -> scrollX / zoom.getX()
    );
    estScrollY = Var.mapBidirectional(
            content.estimatedScrollYProperty(),
            scrollY -> scrollY * zoom.getY(),
            scrollY -> scrollY / zoom.getY()
    );

    zoom.xProperty()     .addListener((obs, ov, nv) -> requestLayout());
    zoom.yProperty()     .addListener((obs, ov, nv) -> requestLayout());
    zoom.zProperty()     .addListener((obs, ov, nv) -> requestLayout());
    zoom.pivotXProperty().addListener((obs, ov, nv) -> requestLayout());
    zoom.pivotYProperty().addListener((obs, ov, nv) -> requestLayout());
    zoom.pivotZProperty().addListener((obs, ov, nv) -> requestLayout());
}
 
Example 2
Source File: ParagraphBox.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
ParagraphBox(Paragraph<PS, SEG, S> par, BiConsumer<TextFlow, PS> applyParagraphStyle,
             Function<StyledSegment<SEG, S>, Node> nodeFactory) {
    this.getStyleClass().add("paragraph-box");
    this.text = new ParagraphText<>(par, nodeFactory);
    applyParagraphStyle.accept(this.text, par.getParagraphStyle());
    
    // start at -1 so that the first time it is displayed, the caret at pos 0 is not
    // accidentally removed from its parent and moved to this node's ParagraphText
    // before this node gets updated to its real index and therefore removes
    // caret from the SceneGraph completely
    this.index = Var.newSimpleVar(-1);

    getChildren().add(text);
    graphic = Val.combine(
            graphicFactory,
            this.index,
            (f, i) -> f != null && i > -1 ? f.apply(i) : null);
    graphic.addListener((obs, oldG, newG) -> {
        if(oldG != null) {
            getChildren().remove(oldG);
        }
        if(newG != null) {
            getChildren().add(newG);
        }
    });
    graphicOffset.addListener(obs -> requestLayout());
}
 
Example 3
Source File: SizeTracker.java    From Flowless with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Constructs a SizeTracker
 *
 * @param orientation if vertical, breadth = width and length = height;
 *                    if horizontal, breadth = height and length = width
 */
public SizeTracker(
        OrientationHelper orientation,
        ObservableObjectValue<Bounds> viewportBounds,
        MemoizationList<? extends Cell<?, ?>> lazyCells) {
    this.orientation = orientation;
    this.viewportBounds = viewportBounds;
    this.cells = lazyCells;
    this.breadths = lazyCells.map(orientation::minBreadth).memoize();
    this.maxKnownMinBreadth = breadths.memoizedItems()
            .reduce(Math::max)
            .orElseConst(0.0);
    this.breadthForCells = Val.combine(
            maxKnownMinBreadth,
            viewportBounds,
            (a, b) -> Math.max(a, orientation.breadth(b)));

    Val<Function<Cell<?, ?>, Double>> lengthFn = avoidFalseInvalidations(breadthForCells).map(
            breadth -> cell -> orientation.prefLength(cell, breadth));

    this.lengths = cells.mapDynamic(lengthFn).memoize();

    LiveList<Double> knownLengths = this.lengths.memoizedItems();
    Val<Double> sumOfKnownLengths = knownLengths.reduce((a, b) -> a + b).orElseConst(0.0);
    Val<Integer> knownLengthCount = knownLengths.sizeProperty();

    this.averageLengthEstimate = Val.create(
            () -> {
                // make sure to use pref lengths of all present cells
                for(int i = 0; i < cells.getMemoizedCount(); ++i) {
                    int j = cells.indexOfMemoizedItem(i);
                    lengths.force(j, j + 1);
                }

                int count = knownLengthCount.getValue();
                return count == 0
                        ? null
                        : sumOfKnownLengths.getValue() / count;
            },
            sumOfKnownLengths, knownLengthCount);

    this.totalLengthEstimate = Val.combine(
            averageLengthEstimate, cells.sizeProperty(),
            (avg, n) -> n * avg);

    Val<Integer> firstVisibleIndex = Val.create(
            () -> cells.getMemoizedCount() == 0 ? null : cells.indexOfMemoizedItem(0),
            cells, cells.memoizedItems()); // need to observe cells.memoizedItems()
            // as well, because they may change without a change in cells.

    Val<? extends Cell<?, ?>> firstVisibleCell = cells.memoizedItems()
            .collapse(visCells -> visCells.isEmpty() ? null : visCells.get(0));

    Val<Integer> knownLengthCountBeforeFirstVisibleCell = Val.create(() -> {
        return firstVisibleIndex.getOpt()
                .map(i -> lengths.getMemoizedCountBefore(Math.min(i, lengths.size())))
                .orElse(0);
    }, lengths, firstVisibleIndex);

    Val<Double> totalKnownLengthBeforeFirstVisibleCell = knownLengths.reduceRange(
            knownLengthCountBeforeFirstVisibleCell.map(n -> new IndexRange(0, n)),
            (a, b) -> a + b).orElseConst(0.0);

    Val<Double> unknownLengthEstimateBeforeFirstVisibleCell = Val.combine(
            firstVisibleIndex,
            knownLengthCountBeforeFirstVisibleCell,
            averageLengthEstimate,
            (firstIdx, knownCnt, avgLen) -> (firstIdx - knownCnt) * avgLen);

    Val<Double> firstCellMinY = firstVisibleCell.flatMap(orientation::minYProperty);

    lengthOffsetEstimate = Val.combine(
            totalKnownLengthBeforeFirstVisibleCell,
            unknownLengthEstimateBeforeFirstVisibleCell,
            firstCellMinY,
            (a, b, minY) -> a + b - minY).orElseConst(0.0);

    // pinning totalLengthEstimate and lengthOffsetEstimate
    // binds it all together and enables memoization
    this.subscription = Subscription.multi(
            totalLengthEstimate.pin(),
            lengthOffsetEstimate.pin());
}
 
Example 4
Source File: OrientationHelper.java    From Flowless with BSD 2-Clause "Simplified" License 4 votes vote down vote up
default Val<Double> minYProperty(Node node) {
    return Val.combine(
            layoutYProperty(node),
            node.layoutBoundsProperty(),
            (layoutY, layoutBounds) -> layoutY.doubleValue() + minY(layoutBounds));
}