Added find functionality

This commit is contained in:
lieght
2025-12-05 12:28:06 +01:00
parent 61f03fab26
commit ea179eb0e2
3 changed files with 39 additions and 8 deletions

View File

@@ -31,6 +31,8 @@ import javafx.scene.Scene;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import java.util.Objects;
public final class App extends Application { public final class App extends Application {
private static Stage stage; private static Stage stage;
private static Scene scene; private static Scene scene;
@@ -138,11 +140,11 @@ public final class App extends Application {
} }
public void escapePopup() { public void escapePopup() {
if (WidgetContainer.getAllWidgets().stream().anyMatch( if (!Objects.requireNonNull(
e -> e instanceof QuitPopup || e instanceof EscapePopup WidgetContainer.find(widget -> widget instanceof QuitPopup || widget instanceof EscapePopup)
)) { ).isEmpty()) {
WidgetContainer.findRemoveFirst(QuitPopup.class); WidgetContainer.removeFirst(QuitPopup.class);
WidgetContainer.findRemoveFirst(EscapePopup.class); WidgetContainer.removeFirst(EscapePopup.class);
return; return;
} }

View File

@@ -1,5 +1,6 @@
package org.toop.app.widget; package org.toop.app.widget;
import javafx.collections.ObservableList;
import javafx.scene.Node; import javafx.scene.Node;
import org.toop.app.widget.complex.PopupWidget; import org.toop.app.widget.complex.PopupWidget;
import org.toop.app.widget.complex.ViewWidget; import org.toop.app.widget.complex.ViewWidget;
@@ -10,6 +11,7 @@ import javafx.scene.layout.StackPane;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Predicate;
public final class WidgetContainer { public final class WidgetContainer {
private static StackPane root; private static StackPane root;
@@ -63,7 +65,7 @@ public final class WidgetContainer {
}); });
} }
public static void findRemove(Class<? extends Widget> widgetClass) { public static void remove(Class<? extends Widget> widgetClass) {
if (root == null || currentView == null) return; if (root == null || currentView == null) return;
Platform.runLater(() -> Platform.runLater(() ->
@@ -71,7 +73,7 @@ public final class WidgetContainer {
); );
} }
public static void findRemoveFirst(Class<? extends Widget> widgetClass) { public static void removeFirst(Class<? extends Widget> widgetClass) {
if (root == null || currentView == null) return; if (root == null || currentView == null) return;
Platform.runLater(() -> { Platform.runLater(() -> {
@@ -84,6 +86,33 @@ public final class WidgetContainer {
}); });
} }
public static List<Widget> find(Class<? extends Widget> widgetClass) {
if (root == null || currentView == null) return null;
return getAllWidgets()
.stream()
.filter(widget -> widget.getClass().isAssignableFrom(widgetClass))
.toList();
}
public static List<Widget> find(Predicate<Widget> predicate) {
if (root == null || currentView == null) return null;
return getAllWidgets()
.stream()
.filter(predicate)
.toList();
}
public static Widget findFirst(Class<? extends Widget> widgetClass) {
if (root == null || currentView == null) return null;
return getAllWidgets()
.stream()
.filter(widget -> widget.getClass().isAssignableFrom(widgetClass))
.findFirst().orElse(null);
}
public static ViewWidget getCurrentView() { public static ViewWidget getCurrentView() {
return currentView; return currentView;
} }

View File

@@ -10,7 +10,7 @@ import org.toop.app.widget.view.OptionsView;
public class EscapePopup extends PopupWidget { public class EscapePopup extends PopupWidget {
public EscapePopup() { public EscapePopup() {
var con = Primitive.button("Continue", () -> { hide(); }, false); // TODO, localize var con = Primitive.button("Continue", this::hide, false); // TODO, localize
var qui = Primitive.button("quit", () -> { var qui = Primitive.button("quit", () -> {
hide(); hide();