1

我创建了一个繁忙的图层,在后台 IO 繁忙时显示动画进度指示器。该层被添加到 Gluon mobile 4 中的玻璃窗格中:

BusyLayer extends Layer { ...
    root = new FlowPane(new ProgressIndicator());
    MobileApplication.getInstance().getGlassPane().getLayers().add(this);

DH2FX extends MobileApplication { ...
    addLayerFactory("Busy", () -> new BusyLayer());
    ...
    showLayer("Busy");
    ...
    hideLayer("Busy");

在 Gluon 5 中,getLayers 已被删除,根据迁移指南,可以直接显示层:

BusyLayer extends Layer { ...
    root = new FlowPane(new ProgressIndicator());

DH2FX extends MobileApplication { ...
    BusyLayer busyLayer = new BusyLayer();
    ...
    busyLayer.show();
    ...
    busyLayer.hide();

但是该层没有隐藏。

====

主要播放器是一个单例背景类,因此 BusyLayer 只显示一次:

class BackgroundActivity {
    private final AtomicInteger busyAtomicInteger = new AtomicInteger(0);
    BusyLayer busyLayer = new BusyLayer();
    private long time;

    public BackgroundActivity() {
        busyLayer.setOnShowing(e -> {
            time = System.currentTimeMillis(); 
            System.out.println("Showing busyLayer");
        }); 
        busyLayer.setOnShown(e -> {
            System.out.println("busyLayer shown in: " + (System.currentTimeMillis() - time) + " ms");
        }); 
        busyLayer.setOnHiding(e -> System.out.println("hiding layer at " + (System.currentTimeMillis() - time) + " ms"));
    }

    void start() {
        if (busyAtomicInteger.getAndIncrement() == 0) {
             busyLayer.show();
        }
    }

    void done() {
        if (busyAtomicInteger.decrementAndGet() == 0) {
            busyLayer.hide();
        }
    }

    void failure(Throwable t) {
        t.printStackTrace();
        failure();
    }

    void failure() {
        done();
    }
}
protected final BackgroundActivity backgroundActivity = new BackgroundActivity();

像这样使用 CompletableFutures 执行异步任务的代码:

    // Hours
    backgroundActivity.start();
    CompletableFuture.supplyAsync( () -> entryService().getHours(calendarPickerForHessian))
    .exceptionally( e -> { backgroundActivity.failure(e); return null; } )
    .thenAcceptAsync( (Hour[] hours) -> {
        Platform.runLater( () -> {
            refreshHours(hours);
            backgroundActivity.done();
        });
    });

    // ProjectTotals
    backgroundActivity.start();
    CompletableFuture.supplyAsync( () -> entryService().getProjectTotals(calendarPickerForHessian) )
    .exceptionally( e -> { backgroundActivity.failure(e); return null; } )
    .thenAcceptAsync( (LinkedHashMap<Integer, Double> projectTotals) -> {
        Platform.runLater( () -> {
            refreshProjectTotals(projectTotals);
            backgroundActivity.done();
        });
    });

    // DayTotals
    backgroundActivity.start();
    CompletableFuture.supplyAsync( () -> entryService().getDayTotals(calendarPickerForHessian))
    .exceptionally( e -> { backgroundActivity.failure(e); return null; } )
    .thenAcceptAsync( (SortedMap<String, Double> dayTotals) -> {
        Platform.runLater( () -> {
            refreshDayTotals(dayTotals);
            backgroundActivity.done();
        });
    });

当然还有 BusyLayer 本身:

public class BusyLayer extends Layer {

public BusyLayer() {
    root = new StackPane(new ProgressIndicator());
    root.setAlignment(Pos.CENTER);
    root.getStyleClass().add("semitransparent7");
    getChildren().add(root);
}
private final StackPane root;

@Override
public void layoutChildren() {
    root.setVisible(isShowing());
    if (!isShowing()) {
        return;
    }

    GlassPane glassPane = MobileApplication.getInstance().getGlassPane();
    root.resize(glassPane.getWidth(), glassPane.getHeight());
    resizeRelocate(0, 0, glassPane.getWidth(), glassPane.getHeight());
}

}

4

1 回答 1

1

当您尝试过早隐藏图层时,Charm 5.0 存在一个已知问题。

显示图层时,渲染布局需要一些时间,即使没有动画过渡,显示图层的时间和最终显示的时间之间也存在几毫秒的差距。

如果您Layer::hide在图层显示之前调用,调用将退出,并且图层不会被隐藏。

一个简单的测试如下:

private long time;

BusyLayer busyLayer = new BusyLayer();
busyLayer.setOnShowing(e -> {
    time = System.currentTimeMillis(); 
    System.out.println("Showing busyLayer");
}); 
busyLayer.setOnShown(e -> {
    System.out.println("busyLayer shown in: " + (System.currentTimeMillis() - time) + " ms");
}); 
busyLayer.setOnHiding(e -> System.out.println("hiding layer at " + (System.currentTimeMillis() - time) + " ms"));
busyLayer.show();

现在假设您有一个需要一秒钟的长任务:

PauseTransition p = new PauseTransition(Duration.seconds(1));
p.setOnFinished(f -> busyLayer.hide());
p.play();

然后该层将按预期隐藏。

但是如果任务更快并且需要几毫秒:

PauseTransition p = new PauseTransition(Duration.seconds(0.01));
p.setOnFinished(f -> busyLayer.hide());
p.play();

可能该层尚未显示,hide()调用将失败。

解决方法

虽然已正确修复此问题,但可能的解决方法是侦听图层的LifecycleEvent.SHOWN事件,并执行以下操作:

private BooleanProperty shown = new SimpleBooleanProperty();

BusyLayer busyLayer = new BusyLayer();
busyLayer.setOnShowing(e -> shown.set(false));
busyLayer.setOnShown(e -> shown.set(true));
busyLayer.show();

PauseTransition p = new PauseTransition(taskDuration);
p.setOnFinished(f -> {
    if (shown.get()) {
        // layer was shown, hide it
        busyLayer.hide();
    } else {
        // layer is not shown yet, wait until it does, and hide
        shown.addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable) {
                if (shown.get()) {
                    busyLayer.hide();
                    shown.removeListener(this);
                }
            }
        });
    }
});
p.play();

编辑

我正在添加一个可能的BusyLayer实现:

class BusyLayer extends Layer {

    private final GlassPane glassPane = MobileApplication.getInstance().getGlassPane();
    private final StackPane root;
    private final double size = 150;

    public BusyLayer() {
        root = new StackPane(new ProgressIndicator());
        root.setStyle("-fx-background-color: white;");
        getChildren().add(root);
        setBackgroundFade(0.5);
    }

    @Override
    public void layoutChildren() {
        super.layoutChildren();
        root.setVisible(isShowing());
        if (!isShowing()) {
            return;
        }
        root.resize(size, size);
        resizeRelocate((glassPane.getWidth() - size)/2, (glassPane.getHeight()- size)/2, size, size);
    }

}

编辑

主要问题与如何BusyLayer覆盖该Layer::layoutChildren方法有关。

如您所见,请在此处阅读Layer::layoutChildren

覆盖此方法以添加图层的布局逻辑。应注意在覆盖的方法中调用此方法以使图层正常运行。

这意味着您必须调用super.layoutChildren()才能使图层正常工作。

@Override
public void layoutChildren() {
    super.layoutChildren();
    // add your own implementation
}

这是扩展 JavaFX 内置控件时的常见模式。

于 2018-11-01T16:10:20.250 回答