diff --git a/deployment/src/test/java/io/quarkiverse/fx/deployment/fxviews/FxViewTest.java b/deployment/src/test/java/io/quarkiverse/fx/deployment/fxviews/FxViewTest.java index fca1b2c..ac026b7 100644 --- a/deployment/src/test/java/io/quarkiverse/fx/deployment/fxviews/FxViewTest.java +++ b/deployment/src/test/java/io/quarkiverse/fx/deployment/fxviews/FxViewTest.java @@ -1,20 +1,5 @@ package io.quarkiverse.fx.deployment.fxviews; -import static org.awaitility.Awaitility.await; - -import java.net.URI; -import java.nio.file.Path; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -import jakarta.enterprise.event.Observes; -import jakarta.inject.Inject; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; - import io.quarkiverse.fx.FxPostStartupEvent; import io.quarkiverse.fx.FxStartupLatch; import io.quarkiverse.fx.QuarkusFxApplication; @@ -23,8 +8,22 @@ import io.quarkiverse.fx.views.FxViewRepository; import io.quarkus.runtime.Quarkus; import io.quarkus.test.QuarkusUnitTest; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; import javafx.collections.ObservableList; import javafx.scene.Parent; +import javafx.stage.Stage; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.net.URI; +import java.nio.file.Path; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.awaitility.Awaitility.await; class FxViewTest { @@ -78,6 +77,9 @@ void testFxView() throws InterruptedException { .atMost(FxTestConstants.LAUNCH_TIMEOUT_MS, TimeUnit.MILLISECONDS) .until(eventObserved::get); + Stage primaryStage = this.viewRepository.getPrimaryStage(); + Assertions.assertNotNull(primaryStage); + FxViewData viewData = this.viewRepository.getViewData("SampleTest"); Assertions.assertNotNull(viewData); diff --git a/docs/modules/ROOT/pages/index.adoc b/docs/modules/ROOT/pages/index.adoc index 1a81c62..2c68257 100644 --- a/docs/modules/ROOT/pages/index.adoc +++ b/docs/modules/ROOT/pages/index.adoc @@ -123,6 +123,25 @@ public class MyBean { } ---- +== Accessing the primary stage + +The primary `Stage` can be accessed by injecting `FxViewRepository`. + +[source,java] +---- +@ApplicationScoped +public class MyBean { + + @Inject + FxViewRepository viewRepository; + + void doSomething() { + Stage primaryStage = this.viewRepository.getPrimaryStage(); + primaryStage.setTitle("App Title"); + } +} +---- + == Properties include::includes/quarkus-fx.adoc[] \ No newline at end of file diff --git a/runtime/src/main/java/io/quarkiverse/fx/views/FxViewRepository.java b/runtime/src/main/java/io/quarkiverse/fx/views/FxViewRepository.java index 87d0372..ea073e4 100644 --- a/runtime/src/main/java/io/quarkiverse/fx/views/FxViewRepository.java +++ b/runtime/src/main/java/io/quarkiverse/fx/views/FxViewRepository.java @@ -1,5 +1,22 @@ package io.quarkiverse.fx.views; +import io.quarkiverse.fx.FxPostStartupEvent; +import io.quarkiverse.fx.FxViewLoadEvent; +import io.quarkiverse.fx.style.StylesheetWatchService; +import io.quarkus.runtime.LaunchMode; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.enterprise.inject.Instance; +import jakarta.inject.Inject; +import javafx.collections.ObservableList; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.Dialog; +import javafx.stage.Stage; +import javafx.stage.Window; +import org.jboss.logging.Logger; + import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -13,23 +30,6 @@ import java.util.Objects; import java.util.ResourceBundle; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.event.Observes; -import jakarta.enterprise.inject.Instance; -import jakarta.inject.Inject; - -import org.jboss.logging.Logger; - -import io.quarkiverse.fx.FxViewLoadEvent; -import io.quarkiverse.fx.style.StylesheetWatchService; -import io.quarkus.runtime.LaunchMode; -import javafx.collections.ObservableList; -import javafx.fxml.FXMLLoader; -import javafx.scene.Parent; -import javafx.scene.Scene; -import javafx.scene.control.Dialog; -import javafx.stage.Window; - @ApplicationScoped public class FxViewRepository { @@ -41,13 +41,15 @@ public class FxViewRepository { @Inject Instance fxmlLoader; - private final Map viewDataMap = new HashMap<>(); - @Inject FxViewConfig config; + private final Map viewDataMap = new HashMap<>(); + private List viewNames; + private Stage primaryStage; + public void setViewNames(final List views) { this.viewNames = views; } @@ -142,6 +144,13 @@ void setupViews(@Observes final FxViewLoadEvent event) { } } + /** + * Listens for startup event then store primary Stage instance + */ + void setupPrimaryStage(@Observes final FxPostStartupEvent event) { + this.primaryStage = event.getPrimaryStage(); + } + private static URL lookupResource(final ClassLoader classLoader, final String name) { URL url = classLoader.getResource(name); if (url == null) { @@ -186,4 +195,11 @@ private static ObservableList getFxmlObjectStyleSheets(final Object root public FxViewData getViewData(final String viewName) { return this.viewDataMap.get(viewName); } + + /** + * Retrieve primary {@link Stage} instance (only available after {@link FxPostStartupEvent} is triggered) + */ + public Stage getPrimaryStage() { + return this.primaryStage; + } }