Skip to content

Commit

Permalink
Considered changes in UiScheduler implementation (WebFX Platform)
Browse files Browse the repository at this point in the history
  • Loading branch information
salmonb committed Nov 1, 2023
1 parent 97f551c commit 38e75b3
Showing 1 changed file with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package dev.webfx.platform.uischeduler.spi.impl.openjfx;

import dev.webfx.platform.scheduler.spi.SchedulerProviderBase;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.util.Duration;
import dev.webfx.kit.launcher.WebFxKitLauncher;
import dev.webfx.platform.scheduler.spi.SchedulerProviderBase;
import dev.webfx.platform.uischeduler.spi.impl.UiSchedulerProviderBase;
import javafx.animation.AnimationTimer;
import javafx.application.Platform;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -19,6 +16,10 @@ public final class FxUiSchedulerProvider extends UiSchedulerProviderBase {

private final ExecutorService executor = Executors.newCachedThreadPool();

public FxUiSchedulerProvider() {
WebFxKitLauncher.onReady(this::executeAnimationPipe);
}

@Override
protected SchedulerProviderBase.ScheduledBase scheduledImpl(SchedulerProviderBase.WrappedRunnable wrappedRunnable, long delayMs) {
// Redirecting to specific Java or JavaFX API in the following cases:
Expand Down Expand Up @@ -47,26 +48,22 @@ public boolean isUiThread() {
return WebFxKitLauncher.isReady() && Platform.isFxApplicationThread();
}

private Timeline timeline;
private Runnable animationRunnable;
private final AnimationTimer animationTimer = new AnimationTimer() {
@Override
public void handle(long now) {
animationRunnable.run();
}
};

@Override
protected void checkExecuteAnimationPipeIsScheduledForNextAnimationFrame() {
synchronized (this) {
if (timeline == null) {
timeline = new Timeline(new KeyFrame(Duration.millis(1), event -> this.executeAnimationPipe()));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
}
protected void requestAnimationFrame(Runnable runnable) {
animationRunnable = runnable;
animationTimer.start();
}

@Override
protected void onExecuteAnimationPipeFinished(boolean noMoreAnimationScheduled) {
synchronized (this) {
if (noMoreAnimationScheduled && timeline != null) {
timeline.stop();
timeline = null;
}
}
protected void cancelAnimationFrame() {
animationTimer.stop();
}
}

0 comments on commit 38e75b3

Please sign in to comment.