Skip to content

Commit

Permalink
Allow scheduling FX tasks before FX is initialized via FxThreadUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Oct 25, 2024
1 parent 910ff0b commit 236cbf2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class RecafApplication extends Application implements WorkspaceOpenListen

@Override
public void start(Stage stage) {
// Notify the thread util that FX has been initialized
FxThreadUtil.onInitialize();

// Setup global style
setUserAgentStylesheet(new RecafTheme().getUserAgentStylesheet());

Expand Down
21 changes: 21 additions & 0 deletions recaf-ui/src/main/java/software/coley/recaf/util/FxThreadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import jakarta.annotation.Nonnull;
import javafx.application.Platform;
import software.coley.recaf.RecafApplication;
import software.coley.recaf.util.threading.ThreadPoolFactory;
import software.coley.recaf.util.threading.ThreadUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/**
Expand All @@ -17,6 +20,8 @@
*/
public class FxThreadUtil {
private static final Executor jfxExecutor = FxThreadUtil::run;
private static final List<Runnable> preInitQueue = new ArrayList<>();
private static boolean initialized;

/**
* Run action in JavaFX thread.
Expand All @@ -28,6 +33,12 @@ public static void run(@Nonnull Runnable action) {
// Skip under test environment.
if (TestEnvironment.isTestEnv()) return;

// Hold for later if FX has not been initialized.
if (!initialized) {
preInitQueue.add(action);
return;
}

// Wrap action so that if it fails we don't explode and kill the FX thread.
action = ThreadUtil.wrap(action);

Expand Down Expand Up @@ -55,4 +66,14 @@ public static void delayedRun(long delayMs, @Nonnull Runnable action) {
public static Executor executor() {
return jfxExecutor;
}

/**
* Called by {@link RecafApplication} when it is first initialized.
*/
public static void onInitialize() {
initialized = true;
for (Runnable runnable : preInitQueue)
run(runnable);
preInitQueue.clear();
}
}

0 comments on commit 236cbf2

Please sign in to comment.