feat(fx): add resizable split groups and asynchronous tab close

This commit is contained in:
2026-11-15 21:07:15 +01:00
parent af5a4278fd
commit 1a3aea6e09
40 changed files with 2724 additions and 93 deletions
@@ -0,0 +1,70 @@
package de.neitzel.fx.example.docking;
import de.neitzel.fx.docking.CloseConsent;
import de.neitzel.fx.docking.CloseResult;
import de.neitzel.fx.docking.DockGroupDefinition;
import de.neitzel.fx.docking.DockPane;
import de.neitzel.fx.docking.DockTarget;
import de.neitzel.fx.docking.Dockable;
import de.neitzel.fx.docking.DockableId;
import de.neitzel.fx.docking.DockingManager;
import de.neitzel.fx.docking.DockingOptions;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import java.util.concurrent.CompletableFuture;
/** Demonstrates a pending close decision while the editor remains open. */
public final class CloseDockingApp extends Application {
@Override
public void start(Stage stage) {
DockPane pane = new DockPane();
pane.getInitialLayout().setCenter(new DockGroupDefinition("editors"));
Label notice = new Label("Close Notes, then allow the close or keep the editor open.");
Button close = new Button("Close Notes");
Button allow = new Button("Allow close");
Button veto = new Button("Keep open");
var pending = new SimpleObjectProperty<CompletableFuture<CloseConsent>>();
allow.disableProperty().bind(pending.isNull());
veto.disableProperty().bind(pending.isNull());
BorderPane shell = new BorderPane(pane);
shell.setTop(new ToolBar(close, allow, veto));
shell.setBottom(notice);
stage.setScene(new Scene(shell, 760, 420));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
Dockable editor = new Dockable(new DockableId("notes"), new TextArea("Edit these notes before closing."));
editor.setTitle("Notes");
editor.setCloseCheck(request -> {
notice.setText("Notes remains open while you decide.");
CompletableFuture<CloseConsent> answer = new CompletableFuture<>();
pending.set(answer);
return answer;
});
editor.setOnClosed(() -> {
close.setDisable(true);
notice.setText("Notes closed. Its resources can now be released.");
});
manager.open(editor, DockTarget.area("editors"));
close.setOnAction(event -> manager.requestTabClose(editor.getId()).thenAcceptAsync(result -> {
if (result instanceof CloseResult.Vetoed) notice.setText("Close canceled. Notes remains editable.");
}, Platform::runLater));
allow.setOnAction(event -> answer(pending, CloseConsent.ALLOW));
veto.setOnAction(event -> answer(pending, CloseConsent.VETO));
stage.setTitle("NeitzelLib — asynchronous tab close");
stage.show();
}
private static void answer(SimpleObjectProperty<CompletableFuture<CloseConsent>> pending, CloseConsent consent) {
CompletableFuture<CloseConsent> answer = pending.get();
pending.set(null);
if (answer != null) answer.complete(consent);
}
}
@@ -0,0 +1,12 @@
package de.neitzel.fx.example.docking;
import javafx.application.Application;
/** Plain Java launcher for the asynchronous close example. */
public final class CloseDockingMain {
private CloseDockingMain() { }
public static void main(String[] args) {
Application.launch(CloseDockingApp.class, args);
}
}
@@ -1,6 +1,9 @@
package de.neitzel.fx.example.docking;
import de.neitzel.fx.docking.DockGroupDefinition;
import de.neitzel.fx.docking.DockSplitDefinition;
import de.neitzel.fx.docking.SplitDirections;
import javafx.geometry.Orientation;
import de.neitzel.fx.docking.ActionResult;
import de.neitzel.fx.docking.DockLayout;
import de.neitzel.fx.docking.DockPane;
@@ -24,14 +27,17 @@ import javafx.stage.Stage;
public final class DockingApp extends Application {
@Override
public void start(Stage stage) throws Exception {
boolean splits = getParameters().getRaw().contains("--splits");
DockPane pane = getParameters().getRaw().contains("--fxml")
? FXMLLoader.load(DockingApp.class.getResource("Docking.fxml")) : initialPane();
? FXMLLoader.load(DockingApp.class.getResource(splits ? "SplitDocking.fxml" : "Docking.fxml"))
: initialPane(splits);
Button activate = new Button("Activate Notes");
Button toTools = new Button("Move Notes to tools");
Button toEditors = new Button("Move Notes to editors");
Label status = new Label("Drag a tab or use its context menu to move it.");
Button splitNotes = new Button("Split Notes right");
Label status = new Label("Drag a tab to an edge to split; drag dividers to resize.");
BorderPane shell = new BorderPane(pane);
shell.setTop(new ToolBar(activate, toTools, toEditors));
shell.setTop(new ToolBar(activate, toTools, toEditors, splitNotes));
shell.setBottom(status);
stage.setScene(new Scene(shell, 900, 600));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
@@ -42,7 +48,12 @@ public final class DockingApp extends Application {
manager.open(editor, DockTarget.area("editors"));
Dockable other = new Dockable(new DockableId("readme"), new TextArea("Readme"));
other.setTitle("Readme");
manager.open(other, DockTarget.area("editors"));
manager.open(other, DockTarget.area(splits ? "preview" : "editors"));
if (splits) {
Dockable output = new Dockable(new DockableId("output"), new TextArea("Build output"));
output.setTitle("Output");
manager.open(output, DockTarget.area("output"));
}
Dockable project = new Dockable(new DockableId("project"), new Label("Project files"));
project.setTitle("Project");
project.setIcon(new Label("▦"));
@@ -50,6 +61,10 @@ public final class DockingApp extends Application {
activate.setOnAction(event -> manager.activate(notesId));
toTools.setOnAction(event -> report(status, manager.move(notesId, DockTarget.area("tools"))));
toEditors.setOnAction(event -> report(status, manager.move(notesId, DockTarget.area("editors"))));
splitNotes.setOnAction(event -> manager.layout().windows().stream()
.flatMap(window -> window.areas().stream()).flatMap(area -> area.groups().stream())
.filter(group -> group.dockableIds().contains(notesId)).findFirst()
.ifPresent(group -> report(status, manager.move(notesId, DockTarget.split(group.id(), Side.RIGHT)))));
stage.setTitle("NeitzelLib docking — Java or ordinary FXML");
stage.show();
manager.activate(notesId);
@@ -63,12 +78,28 @@ public final class DockingApp extends Application {
});
}
private static DockPane initialPane() {
private static DockPane initialPane(boolean splits) {
DockLayout layout = new DockLayout();
layout.setCenter(new DockGroupDefinition("editors"));
if (splits) {
DockSplitDefinition secondary = new DockSplitDefinition();
secondary.setDestinationId("secondary");
secondary.setOrientation(Orientation.VERTICAL);
secondary.setFirst(new DockGroupDefinition("preview"));
secondary.setSecond(new DockGroupDefinition("output"));
DockSplitDefinition editors = new DockSplitDefinition();
editors.setDestinationId("editors");
editors.setFirst(new DockGroupDefinition("primary"));
editors.setSecond(secondary);
layout.setCenter(editors);
} else {
layout.setCenter(new DockGroupDefinition("editors"));
}
DockGroupDefinition tools = new DockGroupDefinition("tools");
tools.setTabSide(Side.RIGHT);
tools.setPreferredWidth(200);
tools.setSplitDirections(SplitDirections.VERTICAL);
tools.setVisibleWhenEmpty(false);
tools.setReleaseSpaceWhenEmpty(true);
layout.setRight(tools);
DockPane pane = new DockPane();
pane.setInitialLayout(layout);
@@ -6,7 +6,8 @@
<initialLayout>
<DockLayout>
<center><DockGroupDefinition destinationId="editors"/></center>
<right><DockGroupDefinition destinationId="tools" tabSide="RIGHT" preferredWidth="200"/></right>
<right><DockGroupDefinition destinationId="tools" tabSide="RIGHT" preferredWidth="200" splitDirections="VERTICAL"
visibleWhenEmpty="false" releaseSpaceWhenEmpty="true"/></right>
</DockLayout>
</initialLayout>
</DockPane>
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import de.neitzel.fx.docking.DockPane?>
<?import de.neitzel.fx.docking.DockLayout?>
<?import de.neitzel.fx.docking.DockGroupDefinition?>
<?import de.neitzel.fx.docking.DockSplitDefinition?>
<DockPane xmlns:fx="http://javafx.com/fxml/1">
<initialLayout>
<DockLayout>
<center>
<DockSplitDefinition destinationId="editors" orientation="HORIZONTAL">
<first><DockGroupDefinition destinationId="primary"/></first>
<second>
<DockSplitDefinition destinationId="secondary" orientation="VERTICAL">
<first><DockGroupDefinition destinationId="preview"/></first>
<second><DockGroupDefinition destinationId="output"/></second>
</DockSplitDefinition>
</second>
</DockSplitDefinition>
</center>
<right><DockGroupDefinition destinationId="tools" tabSide="RIGHT" preferredWidth="200" splitDirections="VERTICAL"
visibleWhenEmpty="false" releaseSpaceWhenEmpty="true"/></right>
</DockLayout>
</initialLayout>
</DockPane>
@@ -0,0 +1,47 @@
package de.neitzel.fx.example.docking;
import de.neitzel.fx.docking.*;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import org.junit.jupiter.api.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.*;
/** Checks the example resource through the same ordinary FXML/public manager path as clients. */
class SplitDockingExampleTest {
@Test
void theNestedFxmlExampleOpensEachNamedDestinationWithoutMvvm() throws InterruptedException {
CountDownLatch started = new CountDownLatch(1);
try { Platform.startup(started::countDown); }
catch (IllegalStateException alreadyStarted) { started.countDown(); }
assertTrue(started.await(10, TimeUnit.SECONDS));
CountDownLatch done = new CountDownLatch(1);
AtomicReference<Throwable> failure = new AtomicReference<>();
Platform.runLater(() -> {
try {
DockPane pane = FXMLLoader.load(DockingApp.class.getResource("SplitDocking.fxml"));
Stage stage = new Stage();
stage.setScene(new Scene(pane, 900, 600));
DockingManager manager = DockingManager.attach(stage, pane, new DockingOptions());
for (String target : new String[]{"primary", "preview", "output", "tools"}) {
assertInstanceOf(ActionResult.Applied.class,
manager.open(new Dockable(new DockableId(target), new TextArea(target)), DockTarget.area(target)));
}
DockLayoutView.Area editors = manager.layout().windows().getFirst().areas().getFirst();
assertEquals(3, editors.groups().size());
assertInstanceOf(DockLayoutView.Split.class,
assertInstanceOf(DockLayoutView.Split.class, editors.root()).second());
} catch (Throwable thrown) { failure.set(thrown); }
finally { done.countDown(); }
});
assertTrue(done.await(10, TimeUnit.SECONDS));
if (failure.get() != null) fail("Nested docking example failed", failure.get());
}
}