Skip to content

Commit

Permalink
#80 (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeSimcoe authored Oct 17, 2024
1 parent 845a14c commit 785df9f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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);

Expand Down
19 changes: 19 additions & 0 deletions docs/modules/ROOT/pages/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
54 changes: 35 additions & 19 deletions runtime/src/main/java/io/quarkiverse/fx/views/FxViewRepository.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -41,13 +41,15 @@ public class FxViewRepository {
@Inject
Instance<FXMLLoader> fxmlLoader;

private final Map<String, FxViewData> viewDataMap = new HashMap<>();

@Inject
FxViewConfig config;

private final Map<String, FxViewData> viewDataMap = new HashMap<>();

private List<String> viewNames;

private Stage primaryStage;

public void setViewNames(final List<String> views) {
this.viewNames = views;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -186,4 +195,11 @@ private static ObservableList<String> 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;
}
}

0 comments on commit 785df9f

Please sign in to comment.