feat(fx): add resizable split groups and asynchronous tab close
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
/** Application consent callback, invoked on the JavaFX Application Thread. */
|
||||
@FunctionalInterface
|
||||
public interface CloseCheck {
|
||||
/**
|
||||
* Asks whether the content can close, without closing it.
|
||||
*
|
||||
* @param request affected scope and owning window
|
||||
* @return eventual consent; background saves may complete it from another thread
|
||||
*/
|
||||
CompletionStage<CloseConsent> check(CloseRequest request);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** A Dockable's answer after any application prompt or save has finished. */
|
||||
public enum CloseConsent { ALLOW, VETO }
|
||||
@@ -0,0 +1,13 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.stage.Window;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** Consent context, including the actual hosting window to use as dialog owner. */
|
||||
public record CloseRequest(CloseScope scope, Window owner) {
|
||||
public CloseRequest {
|
||||
Objects.requireNonNull(scope, "scope");
|
||||
Objects.requireNonNull(owner, "owner");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** Final close outcome, kept separate from consent and resource-release errors. */
|
||||
public sealed interface CloseResult {
|
||||
record Closed(List<DockFailure> cleanupFailures) implements CloseResult {
|
||||
public Closed { cleanupFailures = List.copyOf(cleanupFailures); }
|
||||
}
|
||||
|
||||
record NoChange() implements CloseResult { }
|
||||
|
||||
record Vetoed(List<DockableId> vetoingIds, List<DockFailure> checkFailures) implements CloseResult {
|
||||
public Vetoed {
|
||||
vetoingIds = List.copyOf(vetoingIds);
|
||||
checkFailures = List.copyOf(checkFailures);
|
||||
}
|
||||
}
|
||||
|
||||
record Canceled() implements CloseResult { }
|
||||
|
||||
record Rejected(DockFailure failure) implements CloseResult { }
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Immutable description of the Dockables covered by the running close request. */
|
||||
public record CloseScope(Kind kind, List<DockableId> dockableIds) {
|
||||
public CloseScope {
|
||||
Objects.requireNonNull(kind, "kind");
|
||||
dockableIds = List.copyOf(dockableIds);
|
||||
}
|
||||
|
||||
/** The entry point that requested consent. */
|
||||
public enum Kind { TAB }
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.ReadOnlyObjectProperty;
|
||||
import javafx.beans.property.ReadOnlyObjectWrapper;
|
||||
import javafx.stage.Window;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
/** Keeps consent, live-layout removal and application resource release in order. */
|
||||
final class DockCloseCoordinator {
|
||||
interface Host {
|
||||
Optional<Dockable> find(DockableId id);
|
||||
Window owner(DockableId id);
|
||||
void remove(DockableId id);
|
||||
}
|
||||
|
||||
private final Host host;
|
||||
private final ReadOnlyObjectWrapper<CloseScope> scope = new ReadOnlyObjectWrapper<>(this, "closingScope");
|
||||
private CompletableFuture<CloseResult> completion;
|
||||
private CompletionStage<CloseResult> outcome;
|
||||
private final LinkedHashMap<DockableId, CompletableFuture<CloseResult>> queued = new LinkedHashMap<>();
|
||||
|
||||
DockCloseCoordinator(Host host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
ReadOnlyObjectProperty<CloseScope> closingScopeProperty() {
|
||||
return scope.getReadOnlyProperty();
|
||||
}
|
||||
|
||||
CloseScope scope() {
|
||||
return scope.get();
|
||||
}
|
||||
|
||||
CompletionStage<CloseResult> requestTabClose(DockableId id) {
|
||||
if (!Platform.isFxApplicationThread()) {
|
||||
throw new IllegalStateException("Docking operations require the JavaFX Application Thread");
|
||||
}
|
||||
Objects.requireNonNull(id, "id");
|
||||
if (completion != null) {
|
||||
if (scope.get().dockableIds().contains(id)) return outcome;
|
||||
return enqueue(id);
|
||||
}
|
||||
if (!queued.isEmpty()) {
|
||||
CompletionStage<CloseResult> result = enqueue(id);
|
||||
Platform.runLater(this::nextQueued);
|
||||
return result;
|
||||
}
|
||||
return begin(id);
|
||||
}
|
||||
|
||||
private CompletionStage<CloseResult> enqueue(DockableId id) {
|
||||
return queued.computeIfAbsent(id, ignored -> new CompletableFuture<>()).minimalCompletionStage();
|
||||
}
|
||||
|
||||
private void nextQueued() {
|
||||
if (completion != null || queued.isEmpty()) return;
|
||||
var next = queued.pollFirstEntry();
|
||||
begin(next.getKey()).whenComplete((result, failure) -> {
|
||||
if (failure != null) next.getValue().completeExceptionally(failure);
|
||||
else next.getValue().complete(result);
|
||||
});
|
||||
if (completion == null && !queued.isEmpty()) Platform.runLater(this::nextQueued);
|
||||
}
|
||||
|
||||
private CompletionStage<CloseResult> begin(DockableId id) {
|
||||
Dockable dockable = host.find(id).orElse(null);
|
||||
if (dockable == null) return CompletableFuture.completedFuture(new CloseResult.NoChange());
|
||||
CompletableFuture<CloseResult> request = new CompletableFuture<>();
|
||||
CompletionStage<CloseResult> result = request.minimalCompletionStage();
|
||||
completion = request;
|
||||
outcome = result;
|
||||
scope.set(new CloseScope(CloseScope.Kind.TAB, List.of(id)));
|
||||
try {
|
||||
CompletionStage<CloseConsent> answer = dockable.getCloseCheck() == null
|
||||
? CompletableFuture.completedFuture(CloseConsent.ALLOW)
|
||||
: Objects.requireNonNull(dockable.getCloseCheck().check(new CloseRequest(scope.get(), host.owner(id))),
|
||||
"Close check returned no stage");
|
||||
answer.whenComplete((consent, failure) -> onFx(() -> answered(request, dockable, consent, failure)));
|
||||
} catch (RuntimeException failure) {
|
||||
answered(request, dockable, null, failure);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void answered(CompletableFuture<CloseResult> request, Dockable dockable,
|
||||
CloseConsent consent, Throwable failure) {
|
||||
if (completion != request) return;
|
||||
if (failure != null || consent == null) {
|
||||
Throwable cause = failure == null ? new IllegalStateException("Close check returned no consent") : failure;
|
||||
DockFailure checkFailure = new DockFailure(DockFailure.Code.CLOSE_CHECK_FAILED,
|
||||
dockable.getId(), "Close consent check failed", cause);
|
||||
finish(request, new CloseResult.Vetoed(List.of(dockable.getId()), List.of(checkFailure)));
|
||||
} else if (consent == CloseConsent.VETO) {
|
||||
finish(request, new CloseResult.Vetoed(List.of(dockable.getId()), List.of()));
|
||||
} else {
|
||||
host.remove(dockable.getId());
|
||||
List<DockFailure> cleanupFailures = List.of();
|
||||
if (dockable.getOnClosed() != null) {
|
||||
try {
|
||||
dockable.getOnClosed().run();
|
||||
} catch (RuntimeException cleanup) {
|
||||
cleanupFailures = List.of(new DockFailure(DockFailure.Code.CLEANUP_FAILED,
|
||||
dockable.getId(), "Closed Dockable resource release failed", cleanup));
|
||||
}
|
||||
}
|
||||
finish(request, new CloseResult.Closed(cleanupFailures));
|
||||
}
|
||||
}
|
||||
|
||||
private void finish(CompletableFuture<CloseResult> request, CloseResult result) {
|
||||
completion = null;
|
||||
outcome = null;
|
||||
scope.set(null);
|
||||
request.complete(result);
|
||||
if (!queued.isEmpty()) Platform.runLater(this::nextQueued);
|
||||
}
|
||||
|
||||
private static void onFx(Runnable action) {
|
||||
if (Platform.isFxApplicationThread()) action.run();
|
||||
else Platform.runLater(action);
|
||||
}
|
||||
}
|
||||
@@ -17,13 +17,16 @@ import javafx.scene.input.MouseButton;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** Translates pointer gestures on tab headers into ordinary checked manager moves. */
|
||||
final class DockDragController {
|
||||
record GroupSurface(DockGroupId id, TabPane tabs) { }
|
||||
record GroupSurface(DockGroupId id, TabPane tabs, Side edge) {
|
||||
GroupSurface(DockGroupId id, TabPane tabs) { this(id, tabs, null); }
|
||||
}
|
||||
private record Proposal(DockTarget target, Bounds marker) { }
|
||||
private record Header(DockableId id, Bounds bounds) { }
|
||||
|
||||
@@ -54,35 +57,72 @@ final class DockDragController {
|
||||
tab.setId(headerId);
|
||||
headers.put(headerId, id);
|
||||
Menu destinations = new Menu("Move to");
|
||||
ContextMenu menu = new ContextMenu(destinations);
|
||||
menu.setOnShowing(event -> populateDestinations(destinations, id));
|
||||
Menu splits = new Menu("Split");
|
||||
MenuItem close = new MenuItem("Close");
|
||||
close.setOnAction(event -> manager.requestTabClose(id));
|
||||
ContextMenu menu = new ContextMenu(destinations, splits, close);
|
||||
menu.setOnShowing(event -> {
|
||||
populateDestinations(destinations, id);
|
||||
populateSplits(splits, id);
|
||||
});
|
||||
tab.setContextMenu(menu);
|
||||
}
|
||||
|
||||
void uninstallTab(Tab tab) {
|
||||
DockableId removed = headers.remove(tab.getId());
|
||||
if (removed != null && removed.equals(pressed)) cancel();
|
||||
if (tab.getContextMenu() != null) {
|
||||
tab.getContextMenu().hide();
|
||||
tab.getContextMenu().setOnShowing(null);
|
||||
tab.setContextMenu(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void populateDestinations(Menu menu, DockableId id) {
|
||||
menu.getItems().clear();
|
||||
var visited = new HashSet<DockGroupId>();
|
||||
for (DockLayoutView.Window window : manager.layout().windows()) {
|
||||
for (DockLayoutView.Area area : window.areas()) {
|
||||
for (DockLayoutView.Group group : area.groups()) {
|
||||
if (!visited.add(group.id())) continue;
|
||||
DockTarget target = DockTarget.group(group.id());
|
||||
MoveCheck check = manager.checkMove(id, target);
|
||||
String selectedTitle = group.selectedDockableId() == null ? "" : manager
|
||||
.findDockable(group.selectedDockableId()).map(Dockable::getTitle).orElse("");
|
||||
String label = group.id().value() + (selectedTitle.isBlank() ? "" : " — " + selectedTitle);
|
||||
if (check instanceof MoveCheck.Rejected rejected) {
|
||||
label += " (" + rejected.failure().message() + ")";
|
||||
} else if (check instanceof MoveCheck.NoChange) {
|
||||
label += " (Already here)";
|
||||
}
|
||||
MenuItem item = new MenuItem(label);
|
||||
item.setDisable(!(check instanceof MoveCheck.Allowed));
|
||||
item.setOnAction(event -> manager.move(id, target));
|
||||
menu.getItems().add(item);
|
||||
menu.getItems().add(moveItem(label, id, target));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void populateSplits(Menu menu, DockableId id) {
|
||||
menu.getItems().clear();
|
||||
manager.layout().windows().stream().flatMap(window -> window.areas().stream())
|
||||
.flatMap(area -> area.groups().stream()).filter(group -> group.dockableIds().contains(id))
|
||||
.findFirst().ifPresent(group -> {
|
||||
for (Side side : Side.values()) {
|
||||
String label = switch (side) {
|
||||
case TOP -> "Above";
|
||||
case RIGHT -> "Right";
|
||||
case BOTTOM -> "Below";
|
||||
case LEFT -> "Left";
|
||||
};
|
||||
menu.getItems().add(moveItem(label, id, DockTarget.split(group.id(), side)));
|
||||
}
|
||||
});
|
||||
menu.setDisable(menu.getItems().isEmpty());
|
||||
}
|
||||
|
||||
private MenuItem moveItem(String label, DockableId id, DockTarget target) {
|
||||
MoveCheck check = manager.checkMove(id, target);
|
||||
if (check instanceof MoveCheck.Rejected rejected) label += " (" + rejected.failure().message() + ")";
|
||||
else if (check instanceof MoveCheck.NoChange) label += " (Already here)";
|
||||
MenuItem item = new MenuItem(label);
|
||||
item.setDisable(!(check instanceof MoveCheck.Allowed));
|
||||
item.setOnAction(event -> manager.move(id, target));
|
||||
return item;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
pane.removeEventFilter(MouseEvent.ANY, mouseHandler);
|
||||
scene.removeEventFilter(KeyEvent.KEY_PRESSED, keyHandler);
|
||||
@@ -113,7 +153,7 @@ final class DockDragController {
|
||||
boolean apply = dragging;
|
||||
cancel();
|
||||
if (apply) {
|
||||
Proposal proposal = proposal(event.getScreenX(), event.getScreenY());
|
||||
Proposal proposal = proposal(id, event.getScreenX(), event.getScreenY());
|
||||
if (proposal != null) manager.move(id, proposal.target());
|
||||
event.consume();
|
||||
}
|
||||
@@ -121,7 +161,7 @@ final class DockDragController {
|
||||
}
|
||||
|
||||
private void preview(double screenX, double screenY) {
|
||||
Proposal proposal = proposal(screenX, screenY);
|
||||
Proposal proposal = proposal(pressed, screenX, screenY);
|
||||
if (proposal != null) {
|
||||
MoveCheck check = manager.checkMove(pressed, proposal.target());
|
||||
if (!(check instanceof MoveCheck.Rejected)) {
|
||||
@@ -132,8 +172,16 @@ final class DockDragController {
|
||||
pane.hideDropIndicator();
|
||||
}
|
||||
|
||||
private Proposal proposal(double screenX, double screenY) {
|
||||
for (GroupSurface group : surfaces.get()) {
|
||||
private Proposal proposal(DockableId id, double screenX, double screenY) {
|
||||
List<GroupSurface> groups = surfaces.get();
|
||||
for (GroupSurface group : groups) {
|
||||
if (hidden(group) && (group.edge() != null || group.tabs().isManaged())) {
|
||||
Proposal empty = emptyProposal(group, screenX, screenY);
|
||||
if (empty != null) return empty;
|
||||
}
|
||||
}
|
||||
for (GroupSurface group : groups) {
|
||||
if (hidden(group)) continue;
|
||||
Bounds bounds = group.tabs().localToScreen(group.tabs().getBoundsInLocal());
|
||||
if (bounds == null || !bounds.contains(screenX, screenY)) continue;
|
||||
Node strip = group.tabs().lookup(".tab-header-area");
|
||||
@@ -142,11 +190,86 @@ final class DockDragController {
|
||||
Proposal insertion = insertion(group, screenX, screenY);
|
||||
if (insertion != null) return insertion;
|
||||
}
|
||||
Bounds content = contentBounds(bounds, stripBounds, group.tabs().getSide());
|
||||
Side edge = edgeAt(content, screenX, screenY);
|
||||
if (edge != null) {
|
||||
DockTarget target = DockTarget.split(group.id(), edge);
|
||||
MoveCheck split = manager.checkMove(id, target);
|
||||
if (split instanceof MoveCheck.Allowed) return new Proposal(target, half(content, edge));
|
||||
if (split instanceof MoveCheck.NoChange) return new Proposal(target, bounds);
|
||||
}
|
||||
return new Proposal(DockTarget.group(group.id()), bounds);
|
||||
}
|
||||
for (GroupSurface group : groups) {
|
||||
if (hidden(group) && group.edge() == null && !group.tabs().isManaged()) {
|
||||
Proposal empty = emptyProposal(group, screenX, screenY);
|
||||
if (empty != null) return empty;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean hidden(GroupSurface group) {
|
||||
return !group.tabs().isVisible() || !group.tabs().isManaged();
|
||||
}
|
||||
|
||||
private Proposal emptyProposal(GroupSurface group, double x, double y) {
|
||||
Bounds bounds = group.tabs().isManaged()
|
||||
? group.tabs().localToScreen(group.tabs().getBoundsInLocal())
|
||||
: pane.localToScreen(pane.getBoundsInLocal());
|
||||
if (bounds == null) return null;
|
||||
if (!group.tabs().isManaged() && group.edge() != null) {
|
||||
double width = Math.min(20, bounds.getWidth());
|
||||
double height = Math.min(20, bounds.getHeight());
|
||||
bounds = switch (group.edge()) {
|
||||
case LEFT -> new BoundingBox(bounds.getMinX(), bounds.getMinY(), width, bounds.getHeight());
|
||||
case RIGHT -> new BoundingBox(bounds.getMaxX() - width, bounds.getMinY(), width, bounds.getHeight());
|
||||
case TOP -> new BoundingBox(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), height);
|
||||
case BOTTOM -> new BoundingBox(bounds.getMinX(), bounds.getMaxY() - height, bounds.getWidth(), height);
|
||||
};
|
||||
}
|
||||
return bounds.contains(x, y) ? new Proposal(DockTarget.group(group.id()), bounds) : null;
|
||||
}
|
||||
|
||||
private static Bounds contentBounds(Bounds group, Bounds strip, Side side) {
|
||||
if (strip == null) return group;
|
||||
double left = side == Side.LEFT ? Math.max(group.getMinX(), strip.getMaxX()) : group.getMinX();
|
||||
double top = side == Side.TOP ? Math.max(group.getMinY(), strip.getMaxY()) : group.getMinY();
|
||||
double right = side == Side.RIGHT ? Math.min(group.getMaxX(), strip.getMinX()) : group.getMaxX();
|
||||
double bottom = side == Side.BOTTOM ? Math.min(group.getMaxY(), strip.getMinY()) : group.getMaxY();
|
||||
return new BoundingBox(left, top, Math.max(0, right - left), Math.max(0, bottom - top));
|
||||
}
|
||||
|
||||
private static Side edgeAt(Bounds content, double x, double y) {
|
||||
if (!content.contains(x, y) || content.getWidth() <= 0 || content.getHeight() <= 0) return null;
|
||||
double horizontal = Math.min(40, content.getWidth() / 5);
|
||||
double vertical = Math.min(40, content.getHeight() / 5);
|
||||
double best = 1;
|
||||
Side selected = null;
|
||||
for (Side side : Side.values()) {
|
||||
double distance = switch (side) {
|
||||
case LEFT -> (x - content.getMinX()) / horizontal;
|
||||
case RIGHT -> (content.getMaxX() - x) / horizontal;
|
||||
case TOP -> (y - content.getMinY()) / vertical;
|
||||
case BOTTOM -> (content.getMaxY() - y) / vertical;
|
||||
};
|
||||
if (distance < best) {
|
||||
best = distance;
|
||||
selected = side;
|
||||
}
|
||||
}
|
||||
return selected;
|
||||
}
|
||||
|
||||
private static Bounds half(Bounds bounds, Side side) {
|
||||
return switch (side) {
|
||||
case LEFT -> new BoundingBox(bounds.getMinX(), bounds.getMinY(), bounds.getWidth() / 2, bounds.getHeight());
|
||||
case RIGHT -> new BoundingBox(bounds.getCenterX(), bounds.getMinY(), bounds.getWidth() / 2, bounds.getHeight());
|
||||
case TOP -> new BoundingBox(bounds.getMinX(), bounds.getMinY(), bounds.getWidth(), bounds.getHeight() / 2);
|
||||
case BOTTOM -> new BoundingBox(bounds.getMinX(), bounds.getCenterY(), bounds.getWidth(), bounds.getHeight() / 2);
|
||||
};
|
||||
}
|
||||
|
||||
private Proposal insertion(GroupSurface group, double x, double y) {
|
||||
List<Header> visible = group.tabs().getTabs().stream().map(tab -> {
|
||||
Node node = group.tabs().lookup("#" + tab.getId());
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.geometry.Side;
|
||||
import javafx.scene.Cursor;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.input.MouseButton;
|
||||
import javafx.scene.layout.Region;
|
||||
|
||||
import java.util.function.DoubleSupplier;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
/** Retains a side area's extent while its split branch is replaced or hidden. */
|
||||
final class DockEdgeRegion extends Region {
|
||||
private static final double DIVIDER_SIZE = 5;
|
||||
private final Side side;
|
||||
private final Region divider = new Region();
|
||||
private final DoubleSupplier maximum;
|
||||
private final Runnable completed;
|
||||
private final BooleanSupplier canResize;
|
||||
private Node content;
|
||||
private double extent = -1;
|
||||
private double committedExtent = -1;
|
||||
private double pressedAt;
|
||||
private double pressedExtent;
|
||||
private boolean dragging;
|
||||
|
||||
DockEdgeRegion(Side side, DoubleSupplier maximum, BooleanSupplier canResize, Runnable completed) {
|
||||
this.side = side;
|
||||
this.maximum = maximum;
|
||||
this.completed = completed;
|
||||
this.canResize = canResize;
|
||||
getStyleClass().add("dock-edge-area");
|
||||
divider.getStyleClass().add("dock-area-divider");
|
||||
divider.setCursor(side.isVertical() ? Cursor.H_RESIZE : Cursor.V_RESIZE);
|
||||
divider.setOnMousePressed(event -> {
|
||||
if (event.getButton() != MouseButton.PRIMARY) return;
|
||||
if (!canResize.getAsBoolean()) {
|
||||
event.consume();
|
||||
return;
|
||||
}
|
||||
dragging = true;
|
||||
pressedAt = side.isVertical() ? event.getScreenX() : event.getScreenY();
|
||||
pressedExtent = Math.max(0, (side.isVertical() ? getWidth() : getHeight()) - DIVIDER_SIZE);
|
||||
event.consume();
|
||||
});
|
||||
divider.setOnMouseDragged(event -> {
|
||||
if (!dragging || content == null) return;
|
||||
if (!canResize.getAsBoolean()) {
|
||||
cancelResize();
|
||||
event.consume();
|
||||
return;
|
||||
}
|
||||
double current = side.isVertical() ? event.getScreenX() : event.getScreenY();
|
||||
double direction = side == Side.LEFT || side == Side.TOP ? 1 : -1;
|
||||
double minimum = side.isVertical() ? content.minWidth(-1) : content.minHeight(-1);
|
||||
extent = Math.max(minimum, Math.min(maximum.getAsDouble() - DIVIDER_SIZE,
|
||||
pressedExtent + direction * (current - pressedAt)));
|
||||
requestLayout();
|
||||
event.consume();
|
||||
});
|
||||
divider.setOnMouseReleased(event -> {
|
||||
if (!dragging) return;
|
||||
if (!canResize.getAsBoolean()) {
|
||||
cancelResize();
|
||||
event.consume();
|
||||
return;
|
||||
}
|
||||
dragging = false;
|
||||
if (Double.compare(committedExtent, extent) != 0) {
|
||||
committedExtent = extent;
|
||||
completed.run();
|
||||
}
|
||||
event.consume();
|
||||
});
|
||||
}
|
||||
|
||||
private void cancelResize() {
|
||||
dragging = false;
|
||||
extent = committedExtent;
|
||||
requestLayout();
|
||||
}
|
||||
|
||||
void setContent(Node content) {
|
||||
managedProperty().unbind();
|
||||
visibleProperty().unbind();
|
||||
this.content = content;
|
||||
getChildren().clear();
|
||||
if (content == null) {
|
||||
setManaged(false);
|
||||
setVisible(false);
|
||||
return;
|
||||
}
|
||||
if (extent < 0) {
|
||||
extent = side.isVertical() ? content.prefWidth(-1) : content.prefHeight(-1);
|
||||
committedExtent = extent;
|
||||
}
|
||||
getChildren().addAll(content, divider);
|
||||
managedProperty().bind(content.managedProperty());
|
||||
visibleProperty().bind(content.visibleProperty());
|
||||
requestLayout();
|
||||
}
|
||||
|
||||
double committedExtent() { return committedExtent; }
|
||||
Side side() { return side; }
|
||||
|
||||
@Override protected double computePrefWidth(double height) {
|
||||
return side.isVertical() ? extent + DIVIDER_SIZE : content == null ? 0 : content.prefWidth(height);
|
||||
}
|
||||
|
||||
@Override protected double computePrefHeight(double width) {
|
||||
return side.isHorizontal() ? extent + DIVIDER_SIZE : content == null ? 0 : content.prefHeight(width);
|
||||
}
|
||||
|
||||
@Override protected double computeMinWidth(double height) {
|
||||
return content == null ? 0 : content.minWidth(height) + (side.isVertical() ? DIVIDER_SIZE : 0);
|
||||
}
|
||||
|
||||
@Override protected double computeMinHeight(double width) {
|
||||
return content == null ? 0 : content.minHeight(width) + (side.isHorizontal() ? DIVIDER_SIZE : 0);
|
||||
}
|
||||
|
||||
@Override protected void layoutChildren() {
|
||||
if (content == null) return;
|
||||
double width = getWidth();
|
||||
double height = getHeight();
|
||||
if (side.isVertical()) {
|
||||
content.resizeRelocate(side == Side.RIGHT ? DIVIDER_SIZE : 0, 0,
|
||||
Math.max(0, width - DIVIDER_SIZE), height);
|
||||
divider.resizeRelocate(side == Side.RIGHT ? 0 : width - DIVIDER_SIZE, 0, DIVIDER_SIZE, height);
|
||||
} else {
|
||||
content.resizeRelocate(0, side == Side.BOTTOM ? DIVIDER_SIZE : 0,
|
||||
width, Math.max(0, height - DIVIDER_SIZE));
|
||||
divider.resizeRelocate(0, side == Side.BOTTOM ? 0 : height - DIVIDER_SIZE, width, DIVIDER_SIZE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package de.neitzel.fx.docking;
|
||||
|
||||
/** Machine-readable rejection with optional affected identity and technical cause. */
|
||||
public record DockFailure(Code code, DockableId dockableId, String message, Throwable cause) {
|
||||
public enum Code { TARGET_NOT_FOUND, REFERENCE_TAB_NOT_FOUND, DOCKABLE_NOT_FOUND, CONTENT_ALREADY_ATTACHED,
|
||||
PLACEMENT_REJECTED, PLACEMENT_CHECK_FAILED, ID_MISMATCH, FACTORY_FAILED }
|
||||
public enum Code { SPLIT_DISABLED, SPLIT_TOO_SMALL, TARGET_NOT_FOUND, REFERENCE_TAB_NOT_FOUND, DOCKABLE_NOT_FOUND, CONTENT_ALREADY_ATTACHED,
|
||||
PLACEMENT_REJECTED, PLACEMENT_CHECK_FAILED, ID_MISMATCH, FACTORY_FAILED,
|
||||
CLOSE_IN_PROGRESS, CLOSE_CHECK_FAILED, CLEANUP_FAILED }
|
||||
}
|
||||
|
||||
@@ -6,11 +6,17 @@ import lombok.Setter;
|
||||
|
||||
/** JavaBean definition of a named initial tab group, usable in ordinary FXML. */
|
||||
@Getter @Setter
|
||||
public final class DockGroupDefinition {
|
||||
public final class DockGroupDefinition implements DockLayoutNode {
|
||||
private String destinationId;
|
||||
private Side tabSide = Side.TOP;
|
||||
private double preferredWidth = 200;
|
||||
private double preferredHeight = 150;
|
||||
private double minimumWidth = 80;
|
||||
private double minimumHeight = 80;
|
||||
private boolean persistent = true;
|
||||
private boolean visibleWhenEmpty = true;
|
||||
private boolean releaseSpaceWhenEmpty;
|
||||
private SplitDirections splitDirections = SplitDirections.ALL;
|
||||
|
||||
/** Creates a definition for FXML configuration. */
|
||||
public DockGroupDefinition() { }
|
||||
|
||||
@@ -6,9 +6,9 @@ import lombok.Setter;
|
||||
/** Initial named destinations surrounding a central group. */
|
||||
@Getter @Setter
|
||||
public final class DockLayout {
|
||||
private DockGroupDefinition center;
|
||||
private DockGroupDefinition left;
|
||||
private DockGroupDefinition right;
|
||||
private DockGroupDefinition top;
|
||||
private DockGroupDefinition bottom;
|
||||
private DockLayoutNode center;
|
||||
private DockLayoutNode left;
|
||||
private DockLayoutNode right;
|
||||
private DockLayoutNode top;
|
||||
private DockLayoutNode bottom;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** A named initial destination, containing either a group or a binary split. */
|
||||
public sealed interface DockLayoutNode permits DockGroupDefinition, DockSplitDefinition {
|
||||
String getDestinationId();
|
||||
void setDestinationId(String id);
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.geometry.Orientation;
|
||||
import javafx.geometry.Side;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.control.SplitPane;
|
||||
import javafx.scene.control.Tab;
|
||||
import javafx.scene.Cursor;
|
||||
import javafx.scene.input.MouseButton;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/** Owns the visual split hierarchy and the stable predefined destinations into it. */
|
||||
final class DockLayoutTree {
|
||||
private sealed interface Branch permits Leaf, Split {
|
||||
Node node();
|
||||
List<DockingManager.Group> groups();
|
||||
DockLayoutView.LayoutNode view();
|
||||
}
|
||||
|
||||
private record Leaf(DockingManager.Group group) implements Branch {
|
||||
@Override public Node node() { return group.tabs(); }
|
||||
@Override public List<DockingManager.Group> groups() { return List.of(group); }
|
||||
@Override public DockLayoutView.LayoutNode view() {
|
||||
Tab selected = group.tabs().getSelectionModel().getSelectedItem();
|
||||
return new DockLayoutView.Group(group.id(), group.tabs().getSide(),
|
||||
group.tabs().getTabs().stream().map(tab -> (DockableId) tab.getUserData()).toList(),
|
||||
selected == null ? null : (DockableId) selected.getUserData());
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Split implements Branch {
|
||||
private Branch first;
|
||||
private Branch second;
|
||||
private final SplitPane pane = new SplitPane();
|
||||
private double position = .5;
|
||||
private boolean dragging;
|
||||
Split(Branch first, Branch second, Orientation orientation) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
pane.setOrientation(orientation);
|
||||
}
|
||||
@Override public Node node() { return pane; }
|
||||
@Override public List<DockingManager.Group> groups() {
|
||||
var groups = new ArrayList<>(first.groups());
|
||||
groups.addAll(second.groups());
|
||||
return List.copyOf(groups);
|
||||
}
|
||||
@Override public DockLayoutView.LayoutNode view() {
|
||||
return new DockLayoutView.Split(pane.getOrientation(), position, first.view(), second.view());
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Root {
|
||||
private Branch branch;
|
||||
private final Consumer<Node> place;
|
||||
Root(Branch branch, Consumer<Node> place) { this.branch = branch; this.place = place; }
|
||||
}
|
||||
|
||||
private final Runnable geometryChanged;
|
||||
private final java.util.function.Predicate<DockGroupId> canResize;
|
||||
|
||||
DockLayoutTree(Runnable geometryChanged, java.util.function.Predicate<DockGroupId> canResize) {
|
||||
this.geometryChanged = geometryChanged;
|
||||
this.canResize = canResize;
|
||||
}
|
||||
|
||||
private final Map<DockAreaId, Branch> areas = new LinkedHashMap<>();
|
||||
private final List<Root> roots = new ArrayList<>();
|
||||
|
||||
void addArea(DockLayoutNode definition, Consumer<Node> place,
|
||||
java.util.function.Function<DockGroupDefinition, DockingManager.Group> groupFactory) {
|
||||
Branch branch = build(definition, groupFactory);
|
||||
roots.add(new Root(branch, place));
|
||||
render(branch);
|
||||
place.accept(branch.node());
|
||||
}
|
||||
|
||||
private Branch build(DockLayoutNode definition,
|
||||
java.util.function.Function<DockGroupDefinition, DockingManager.Group> groupFactory) {
|
||||
DockAreaId id = new DockAreaId(definition.getDestinationId());
|
||||
areas.put(id, null);
|
||||
Branch branch;
|
||||
if (definition instanceof DockGroupDefinition group) {
|
||||
branch = new Leaf(groupFactory.apply(group));
|
||||
} else {
|
||||
DockSplitDefinition splitDefinition = (DockSplitDefinition) definition;
|
||||
Split split = split(build(splitDefinition.getFirst(), groupFactory),
|
||||
build(splitDefinition.getSecond(), groupFactory), splitDefinition.getOrientation());
|
||||
split.position = splitDefinition.getPosition();
|
||||
branch = split;
|
||||
}
|
||||
areas.put(id, branch);
|
||||
return branch;
|
||||
}
|
||||
|
||||
List<DockingManager.Group> groups(DockAreaId id) {
|
||||
Branch branch = areas.get(id);
|
||||
return branch == null ? List.of() : branch.groups();
|
||||
}
|
||||
|
||||
void split(DockingManager.Group target, DockingManager.Group added, Side side) {
|
||||
Branch old = find(target.id());
|
||||
Branch leaf = new Leaf(added);
|
||||
Split split = side == Side.LEFT || side == Side.TOP
|
||||
? split(leaf, old, side.isVertical() ? Orientation.HORIZONTAL : Orientation.VERTICAL)
|
||||
: split(old, leaf, side.isVertical() ? Orientation.HORIZONTAL : Orientation.VERTICAL);
|
||||
detach();
|
||||
replace(old, split);
|
||||
render();
|
||||
}
|
||||
|
||||
private Split split(Branch first, Branch second, Orientation orientation) {
|
||||
Split split = new Split(first, second, orientation);
|
||||
split.pane.getStyleClass().add("dock-split");
|
||||
split.pane.addEventFilter(MouseEvent.ANY, event -> {
|
||||
Node divider = divider(event.getPickResult().getIntersectedNode(), split.pane);
|
||||
if (divider == null && !split.dragging) return;
|
||||
if (divider != null) {
|
||||
divider.setCursor(orientation == Orientation.HORIZONTAL ? Cursor.H_RESIZE : Cursor.V_RESIZE);
|
||||
if (!divider.getStyleClass().contains("dock-divider")) divider.getStyleClass().add("dock-divider");
|
||||
}
|
||||
boolean geometryEvent = event.getEventType() == MouseEvent.MOUSE_PRESSED
|
||||
|| event.getEventType() == MouseEvent.MOUSE_DRAGGED || event.getEventType() == MouseEvent.MOUSE_RELEASED;
|
||||
if (geometryEvent && split.groups().stream().anyMatch(group -> !canResize.test(group.id()))) {
|
||||
if (split.dragging) split.pane.setDividerPositions(split.position);
|
||||
split.dragging = false;
|
||||
event.consume();
|
||||
return;
|
||||
}
|
||||
if (event.getEventType() == MouseEvent.MOUSE_PRESSED && event.getButton() == MouseButton.PRIMARY) {
|
||||
split.dragging = true;
|
||||
} else if (event.getEventType() == MouseEvent.MOUSE_RELEASED && split.dragging) {
|
||||
split.dragging = false;
|
||||
double position = split.pane.getDividers().getFirst().getPosition();
|
||||
if (Double.compare(split.position, position) != 0) {
|
||||
split.position = position;
|
||||
geometryChanged.run();
|
||||
}
|
||||
}
|
||||
});
|
||||
return split;
|
||||
}
|
||||
|
||||
private static Node divider(Node node, SplitPane pane) {
|
||||
Node divider = null;
|
||||
for (Node current = node; current != null; current = current.getParent()) {
|
||||
if (current instanceof SplitPane) return current == pane ? divider : null;
|
||||
if (current.getStyleClass().contains("split-pane-divider")) divider = current;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
boolean normalizationAffects(DockGroupId source, DockGroupId destination,
|
||||
java.util.function.Predicate<DockingManager.Group> protectedGroup) {
|
||||
if (find(source).groups().getFirst().tabs().getTabs().size() != 1) return false;
|
||||
return roots.stream().anyMatch(root -> normalizationAffects(root.branch, source, destination, protectedGroup));
|
||||
}
|
||||
|
||||
private static boolean normalizationAffects(Branch branch, DockGroupId source, DockGroupId destination,
|
||||
java.util.function.Predicate<DockingManager.Group> protectedGroup) {
|
||||
if (!(branch instanceof Split split) || find(branch, source) == null) return false;
|
||||
if ((emptyAfterMove(split.first, source, destination) || emptyAfterMove(split.second, source, destination))
|
||||
&& split.groups().stream().anyMatch(protectedGroup)) return true;
|
||||
return normalizationAffects(split.first, source, destination, protectedGroup)
|
||||
|| normalizationAffects(split.second, source, destination, protectedGroup);
|
||||
}
|
||||
|
||||
private static boolean emptyAfterMove(Branch branch, DockGroupId source, DockGroupId destination) {
|
||||
return branch.groups().stream().allMatch(group -> group.tabs().getTabs().size()
|
||||
- (group.id().equals(source) ? 1 : 0) + (group.id().equals(destination) ? 1 : 0) == 0);
|
||||
}
|
||||
|
||||
List<DockGroupId> normalize(DockGroupId source) {
|
||||
Branch emptied = find(source);
|
||||
if (!empty(emptied)) return List.of();
|
||||
detach();
|
||||
List<DockGroupId> removed = new ArrayList<>();
|
||||
for (var iterator = roots.iterator(); iterator.hasNext();) {
|
||||
Root root = iterator.next();
|
||||
boolean affected = includes(root.branch, emptied);
|
||||
root.branch = normalize(root.branch, emptied, removed);
|
||||
if (affected && root.branch instanceof Leaf leaf && empty(leaf)
|
||||
&& !leaf.group.definition().isPersistent()) {
|
||||
areas.entrySet().removeIf(entry -> entry.getValue() == leaf);
|
||||
removed.add(leaf.group.id());
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
render();
|
||||
return List.copyOf(removed);
|
||||
}
|
||||
|
||||
private Branch normalize(Branch branch, Branch emptied, List<DockGroupId> removed) {
|
||||
if (!(branch instanceof Split split) || !includes(branch, emptied)) return branch;
|
||||
split.first = normalize(split.first, emptied, removed);
|
||||
split.second = normalize(split.second, emptied, removed);
|
||||
boolean firstEmpty = empty(split.first);
|
||||
boolean secondEmpty = empty(split.second);
|
||||
if (!firstEmpty && !secondEmpty) return split;
|
||||
Branch survivor = firstEmpty && !secondEmpty ? split.second : split.first;
|
||||
Branch discarded = survivor == split.first ? split.second : split.first;
|
||||
discarded.groups().forEach(group -> removed.add(group.id()));
|
||||
areas.replaceAll((id, destination) -> destination == split || includes(discarded, destination)
|
||||
? survivor : destination);
|
||||
return survivor;
|
||||
}
|
||||
|
||||
private static boolean includes(Branch branch, Branch target) {
|
||||
if (branch == target) return true;
|
||||
return branch instanceof Split split && (includes(split.first, target) || includes(split.second, target));
|
||||
}
|
||||
|
||||
private static boolean empty(Branch branch) {
|
||||
return branch.groups().stream().allMatch(group -> group.tabs().getTabs().isEmpty());
|
||||
}
|
||||
|
||||
void captureGeometry() {
|
||||
for (Root root : roots) captureGeometry(root.branch);
|
||||
}
|
||||
|
||||
private static void captureGeometry(Branch branch) {
|
||||
if (branch instanceof Split split) {
|
||||
if (!split.dragging && split.pane.getWidth() > 0 && split.pane.getHeight() > 0
|
||||
&& !split.pane.getDividers().isEmpty()) {
|
||||
split.position = split.pane.getDividers().getFirst().getPosition();
|
||||
}
|
||||
captureGeometry(split.first);
|
||||
captureGeometry(split.second);
|
||||
}
|
||||
}
|
||||
|
||||
List<DockLayoutView.Area> view() {
|
||||
return areas.entrySet().stream().map(entry -> new DockLayoutView.Area(entry.getKey(),
|
||||
entry.getValue().groups().stream().map(group -> (DockLayoutView.Group) new Leaf(group).view()).toList(),
|
||||
entry.getValue().view())).toList();
|
||||
}
|
||||
|
||||
private Branch find(DockGroupId id) {
|
||||
for (Root root : roots) {
|
||||
Branch found = find(root.branch, id);
|
||||
if (found != null) return found;
|
||||
}
|
||||
throw new IllegalArgumentException("Group is not in the layout: " + id);
|
||||
}
|
||||
|
||||
private static Branch find(Branch branch, DockGroupId id) {
|
||||
if (branch instanceof Leaf leaf) return leaf.group.id().equals(id) ? leaf : null;
|
||||
Split split = (Split) branch;
|
||||
Branch found = find(split.first, id);
|
||||
return found == null ? find(split.second, id) : found;
|
||||
}
|
||||
|
||||
private void replace(Branch old, Branch replacement) {
|
||||
for (Root root : roots) root.branch = replace(root.branch, old, replacement);
|
||||
areas.replaceAll((id, branch) -> branch == old ? replacement : branch);
|
||||
}
|
||||
|
||||
private static Branch replace(Branch branch, Branch old, Branch replacement) {
|
||||
if (branch == old) return replacement;
|
||||
if (branch instanceof Split split) {
|
||||
split.first = replace(split.first, old, replacement);
|
||||
split.second = replace(split.second, old, replacement);
|
||||
}
|
||||
return branch;
|
||||
}
|
||||
|
||||
private void detach() {
|
||||
for (Root root : roots) {
|
||||
root.place.accept(null);
|
||||
detach(root.branch);
|
||||
}
|
||||
}
|
||||
|
||||
private static void detach(Branch branch) {
|
||||
if (branch instanceof Split split) {
|
||||
split.pane.getItems().clear();
|
||||
detach(split.first);
|
||||
detach(split.second);
|
||||
}
|
||||
}
|
||||
|
||||
private void render() {
|
||||
for (Root root : roots) {
|
||||
render(root.branch);
|
||||
root.place.accept(root.branch.node());
|
||||
}
|
||||
}
|
||||
|
||||
private static void render(Branch branch) {
|
||||
if (branch instanceof Split split) {
|
||||
render(split.first);
|
||||
render(split.second);
|
||||
split.pane.getItems().setAll(split.first.node(), split.second.node());
|
||||
split.pane.setDividerPositions(split.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,22 @@ public record DockLayoutView(List<Window> windows) {
|
||||
public record Window(DockWindowId id, boolean main, List<Area> areas) {
|
||||
public Window { areas = List.copyOf(areas); }
|
||||
}
|
||||
public record Area(DockAreaId id, List<Group> groups) {
|
||||
/** Preferred side content extent; -1 means that dimension follows the available space. */
|
||||
public record Area(DockAreaId id, List<Group> groups, LayoutNode root,
|
||||
double preferredWidth, double preferredHeight) {
|
||||
public Area { groups = List.copyOf(groups); }
|
||||
public Area(DockAreaId id, List<Group> groups, LayoutNode root) {
|
||||
this(id, groups, root, -1, -1);
|
||||
}
|
||||
public Area(DockAreaId id, List<Group> groups) {
|
||||
this(id, groups, groups.isEmpty() ? null : groups.getFirst());
|
||||
}
|
||||
}
|
||||
public sealed interface LayoutNode { }
|
||||
public record Split(javafx.geometry.Orientation orientation, double position,
|
||||
LayoutNode first, LayoutNode second) implements LayoutNode { }
|
||||
public record Group(DockGroupId id, Side tabSide, List<DockableId> dockableIds,
|
||||
DockableId selectedDockableId) {
|
||||
DockableId selectedDockableId) implements LayoutNode {
|
||||
public Group { dockableIds = List.copyOf(dockableIds); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package de.neitzel.fx.docking;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.Region;
|
||||
import javafx.geometry.Bounds;
|
||||
import javafx.geometry.Side;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import lombok.Getter;
|
||||
@@ -13,6 +15,7 @@ public final class DockPane extends Region {
|
||||
@Getter @Setter private DockLayout initialLayout = new DockLayout();
|
||||
final BorderPane surface = new BorderPane();
|
||||
private final Rectangle dropIndicator = new Rectangle();
|
||||
private final java.util.Map<Side, DockEdgeRegion> edgeAreas = new java.util.EnumMap<>(Side.class);
|
||||
DockingManager manager;
|
||||
|
||||
public DockPane() {
|
||||
@@ -42,6 +45,50 @@ public final class DockPane extends Region {
|
||||
dropIndicator.setVisible(false);
|
||||
}
|
||||
|
||||
void setArea(Side side, Node content, java.util.function.BooleanSupplier canResize, Runnable completed) {
|
||||
DockEdgeRegion area = edgeAreas.computeIfAbsent(side,
|
||||
edge -> new DockEdgeRegion(edge, () -> availableExtent(edge), canResize, completed));
|
||||
area.setContent(content);
|
||||
switch (side) {
|
||||
case LEFT -> surface.setLeft(area);
|
||||
case RIGHT -> surface.setRight(area);
|
||||
case TOP -> surface.setTop(area);
|
||||
case BOTTOM -> surface.setBottom(area);
|
||||
}
|
||||
}
|
||||
|
||||
double committedExtent(Side side) {
|
||||
DockEdgeRegion area = edgeAreas.get(side);
|
||||
return area == null ? -1 : area.committedExtent();
|
||||
}
|
||||
|
||||
Side edgeOf(Node node) {
|
||||
for (Node current = node; current != null; current = current.getParent()) {
|
||||
if (current instanceof DockEdgeRegion edge) return edge.side();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private double availableExtent(Side side) {
|
||||
boolean horizontal = side.isVertical();
|
||||
double available = horizontal ? getWidth() : getHeight();
|
||||
for (var entry : edgeAreas.entrySet()) {
|
||||
if (entry.getKey() != side && entry.getKey().isVertical() == horizontal && entry.getValue().isManaged()) {
|
||||
available -= horizontal ? entry.getValue().getWidth() : entry.getValue().getHeight();
|
||||
}
|
||||
}
|
||||
Node center = surface.getCenter();
|
||||
double remainingMinimum = center == null || !center.isManaged() ? 0
|
||||
: horizontal ? center.minWidth(-1) : center.minHeight(-1);
|
||||
if (!horizontal) {
|
||||
for (Side edge : new Side[]{Side.LEFT, Side.RIGHT}) {
|
||||
DockEdgeRegion area = edgeAreas.get(edge);
|
||||
if (area != null && area.isManaged()) remainingMinimum = Math.max(remainingMinimum, area.minHeight(-1));
|
||||
}
|
||||
}
|
||||
return available - remainingMinimum;
|
||||
}
|
||||
|
||||
@Override protected void layoutChildren() { surface.resizeRelocate(0, 0, getWidth(), getHeight()); }
|
||||
@Override protected double computePrefWidth(double height) { return surface.prefWidth(height); }
|
||||
@Override protected double computePrefHeight(double width) { return surface.prefHeight(width); }
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.geometry.Orientation;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/** Java/FXML definition of two adjacent groups or nested branches. */
|
||||
@Getter @Setter
|
||||
public final class DockSplitDefinition implements DockLayoutNode {
|
||||
private String destinationId;
|
||||
private Orientation orientation = Orientation.HORIZONTAL;
|
||||
private double position = .5;
|
||||
private DockLayoutNode first;
|
||||
private DockLayoutNode second;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import java.util.Objects;
|
||||
import javafx.geometry.Side;
|
||||
|
||||
/** A destination for a Dockable. */
|
||||
public sealed interface DockTarget {
|
||||
@@ -8,6 +9,10 @@ public sealed interface DockTarget {
|
||||
static DockTarget area(DockAreaId id) { return new Area(id); }
|
||||
static DockTarget group(DockGroupId id, TabPosition position) { return new Group(id, position); }
|
||||
static DockTarget group(DockGroupId id) { return group(id, TabPosition.append()); }
|
||||
static DockTarget split(DockGroupId id, Side side) { return new Split(id, side); }
|
||||
record Split(DockGroupId id, Side side) implements DockTarget {
|
||||
public Split { Objects.requireNonNull(id, "id"); Objects.requireNonNull(side, "side"); }
|
||||
}
|
||||
record Area(DockAreaId id) implements DockTarget {
|
||||
public Area { Objects.requireNonNull(id, "id"); }
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ public final class Dockable {
|
||||
@Getter private final Node content;
|
||||
@Getter @Setter private Supplier<? extends Node> focusTarget;
|
||||
@Getter @Setter private Runnable onDiscarded;
|
||||
@Getter @Setter private CloseCheck closeCheck;
|
||||
@Getter @Setter private Runnable onClosed;
|
||||
private final StringProperty title = new SimpleStringProperty(this, "title", "");
|
||||
private final ObjectProperty<Node> icon = new SimpleObjectProperty<>(this, "icon");
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import javafx.scene.Node;
|
||||
import javafx.scene.control.Tab;
|
||||
import javafx.scene.control.TabPane;
|
||||
import javafx.stage.Stage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -15,16 +16,24 @@ import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Owns the live tab groups of one embedded docking area. */
|
||||
@Slf4j
|
||||
public final class DockingManager {
|
||||
private static final Object ATTACHMENT_KEY = new Object();
|
||||
private final Stage stage;
|
||||
private final DockPane pane;
|
||||
private final PlacementRule placementRule;
|
||||
private final DockDragController drag;
|
||||
private final DockCloseCoordinator closing;
|
||||
private boolean updating;
|
||||
private final Map<DockAreaId, Group> groups = new LinkedHashMap<>();
|
||||
private final Map<DockGroupId, Group> groups = new LinkedHashMap<>();
|
||||
private final Map<DockGroupId, Long> groupActivationOrder = new LinkedHashMap<>();
|
||||
private final DockLayoutTree tree = new DockLayoutTree(this::publish, id -> !groupClosing(group(id)));
|
||||
private final Map<DockAreaId, javafx.geometry.Side> edgeAreas = new LinkedHashMap<>();
|
||||
private long nextGroupId;
|
||||
private final Map<DockableId, Dockable> dockables = new LinkedHashMap<>();
|
||||
private final Map<DockableId, Node> previousFocus = new LinkedHashMap<>();
|
||||
private final Map<DockableId, Long> activationOrder = new LinkedHashMap<>();
|
||||
private long nextActivation;
|
||||
private final javafx.beans.value.ChangeListener<Node> focusListener = (observable, old, current) -> rememberFocus(current);
|
||||
private final ReadOnlyObjectWrapper<Dockable> active = new ReadOnlyObjectWrapper<>(this, "activeDockable");
|
||||
private final ReadOnlyObjectWrapper<DockLayoutView> layout = new ReadOnlyObjectWrapper<>(this, "layout");
|
||||
@@ -32,16 +41,22 @@ public final class DockingManager {
|
||||
private DockingManager(Stage stage, DockPane pane, DockingOptions options) {
|
||||
this.stage = stage;
|
||||
this.pane = pane;
|
||||
closing = new DockCloseCoordinator(new DockCloseCoordinator.Host() {
|
||||
@Override public Optional<Dockable> find(DockableId id) { return findDockable(id); }
|
||||
@Override public javafx.stage.Window owner(DockableId id) { return stage; }
|
||||
@Override public void remove(DockableId id) { removeDockable(id); }
|
||||
});
|
||||
this.placementRule = Objects.requireNonNull(options.getPlacementRule(), "placementRule");
|
||||
DockLayout initial = Objects.requireNonNull(pane.getInitialLayout(), "initialLayout");
|
||||
validate(initial);
|
||||
addGroup(initial.getCenter(), pane.surface::setCenter);
|
||||
addGroup(initial.getLeft(), pane.surface::setLeft);
|
||||
addGroup(initial.getRight(), pane.surface::setRight);
|
||||
addGroup(initial.getTop(), pane.surface::setTop);
|
||||
addGroup(initial.getBottom(), pane.surface::setBottom);
|
||||
addEdgeGroup(initial.getLeft(), javafx.geometry.Side.LEFT);
|
||||
addEdgeGroup(initial.getRight(), javafx.geometry.Side.RIGHT);
|
||||
addEdgeGroup(initial.getTop(), javafx.geometry.Side.TOP);
|
||||
addEdgeGroup(initial.getBottom(), javafx.geometry.Side.BOTTOM);
|
||||
drag = new DockDragController(this, pane, () -> groups.values().stream()
|
||||
.map(group -> new DockDragController.GroupSurface(group.id, group.tabs)).toList());
|
||||
.map(group -> new DockDragController.GroupSurface(group.id, group.tabs, pane.edgeOf(group.tabs))).toList());
|
||||
refreshEmptyGroups();
|
||||
publish();
|
||||
stage.getScene().focusOwnerProperty().addListener(focusListener);
|
||||
}
|
||||
@@ -69,13 +84,18 @@ public final class DockingManager {
|
||||
if (dockables.containsKey(dockable.getId())) return activate(dockable.getId());
|
||||
MoveCheck check = resolve(dockable, target);
|
||||
if (check instanceof MoveCheck.Rejected rejected) return new ActionResult.Rejected(rejected.failure());
|
||||
var destination = (ResolvedDockTarget.Insertion) ((MoveCheck.Allowed) check).destination();
|
||||
Group group = group(destination.groupId());
|
||||
var destination = ((MoveCheck.Allowed) check).destination();
|
||||
if (dockable.getContent().getParent() != null || dockable.getContent().getScene() != null) {
|
||||
return rejected(DockFailure.Code.CONTENT_ALREADY_ATTACHED, dockable.getId(), "Content already belongs to a scene graph");
|
||||
}
|
||||
Group group = receiver(destination);
|
||||
int insertionIndex = destination instanceof ResolvedDockTarget.Insertion insertion ? insertion.index() : 0;
|
||||
Tab tab = new Tab();
|
||||
tab.setClosable(false);
|
||||
tab.setClosable(true);
|
||||
tab.setOnCloseRequest(event -> {
|
||||
event.consume();
|
||||
requestTabClose(dockable.getId());
|
||||
});
|
||||
tab.textProperty().bind(dockable.titleProperty());
|
||||
tab.graphicProperty().bind(dockable.iconProperty());
|
||||
tab.setContent(dockable.getContent());
|
||||
@@ -84,12 +104,13 @@ public final class DockingManager {
|
||||
dockables.put(dockable.getId(), dockable);
|
||||
updating = true;
|
||||
try {
|
||||
group.tabs.getTabs().add(destination.index(), tab);
|
||||
group.tabs.getTabs().add(insertionIndex, tab);
|
||||
group.tabs.getSelectionModel().select(tab);
|
||||
refreshEmptyGroups();
|
||||
} finally {
|
||||
updating = false;
|
||||
}
|
||||
active.set(dockable);
|
||||
setActive(dockable);
|
||||
focus(dockable);
|
||||
publish();
|
||||
return new ActionResult.Applied();
|
||||
@@ -144,7 +165,7 @@ public final class DockingManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
active.set(dockable);
|
||||
setActive(dockable);
|
||||
if (stage.isShowing()) stage.toFront();
|
||||
focus(dockable);
|
||||
if (changed) publish();
|
||||
@@ -153,11 +174,77 @@ public final class DockingManager {
|
||||
|
||||
public Optional<Dockable> findDockable(DockableId id) { return Optional.ofNullable(dockables.get(id)); }
|
||||
|
||||
/** Requests consent without closing or releasing content while the answer is pending. */
|
||||
public java.util.concurrent.CompletionStage<CloseResult> requestTabClose(DockableId id) {
|
||||
return closing.requestTabClose(id);
|
||||
}
|
||||
|
||||
/** Scope remains available until checks and resource release have finished. */
|
||||
public ReadOnlyObjectProperty<CloseScope> closingScopeProperty() {
|
||||
return closing.closingScopeProperty();
|
||||
}
|
||||
|
||||
private void removeDockable(DockableId id) {
|
||||
Group group = containing(id);
|
||||
if (group == null) return;
|
||||
Tab tab = group.tabs.getTabs().get(tabIndex(group, id));
|
||||
boolean wasActive = active.get() == dockables.get(id);
|
||||
updating = true;
|
||||
try {
|
||||
drag.uninstallTab(tab);
|
||||
tab.textProperty().unbind();
|
||||
tab.graphicProperty().unbind();
|
||||
tab.setGraphic(null);
|
||||
tab.setContent(null);
|
||||
group.tabs.getTabs().remove(tab);
|
||||
dockables.remove(id);
|
||||
previousFocus.remove(id);
|
||||
activationOrder.remove(id);
|
||||
normalizeLayout(group.id);
|
||||
} finally {
|
||||
updating = false;
|
||||
}
|
||||
if (wasActive) {
|
||||
Tab selected = group.tabs.getSelectionModel().getSelectedItem();
|
||||
if (selected == null) {
|
||||
selected = groups.values().stream().map(candidate -> candidate.tabs.getSelectionModel().getSelectedItem())
|
||||
.filter(Objects::nonNull)
|
||||
.max(java.util.Comparator.comparingLong(candidate ->
|
||||
activationOrder.getOrDefault((DockableId) candidate.getUserData(), 0L)))
|
||||
.orElse(null);
|
||||
}
|
||||
setActive(selected == null ? null : dockables.get((DockableId) selected.getUserData()));
|
||||
focus(active.get());
|
||||
}
|
||||
publish();
|
||||
}
|
||||
|
||||
private boolean groupClosing(Group group) {
|
||||
CloseScope scope = closing.scope();
|
||||
if (scope == null) return false;
|
||||
for (DockableId id : scope.dockableIds()) {
|
||||
if (!dockables.containsKey(id) || (group != null && tabIndex(group, id) >= 0)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Resolves a move without mutating content, layout or identity allocation. */
|
||||
public MoveCheck checkMove(DockableId id, DockTarget target) {
|
||||
requireFxThread();
|
||||
if (!dockables.containsKey(id)) return moveRejected(DockFailure.Code.DOCKABLE_NOT_FOUND, id, "Dockable is not open");
|
||||
return resolve(dockables.get(id), target);
|
||||
if (groupClosing(containing(id))) return moveRejected(DockFailure.Code.CLOSE_IN_PROGRESS, id, "A close request covers this group");
|
||||
MoveCheck check = resolve(dockables.get(id), target);
|
||||
if (check instanceof MoveCheck.Allowed allowed) {
|
||||
DockGroupId destination = switch (allowed.destination()) {
|
||||
case ResolvedDockTarget.Insertion insertion -> insertion.groupId();
|
||||
case ResolvedDockTarget.Split split -> split.groupId();
|
||||
};
|
||||
if (tree.normalizationAffects(containing(id).id, destination, this::groupClosing)) {
|
||||
return moveRejected(DockFailure.Code.CLOSE_IN_PROGRESS, id,
|
||||
"Removing this tab would resize a group covered by a close request");
|
||||
}
|
||||
}
|
||||
return check;
|
||||
}
|
||||
|
||||
/** Moves the live content, selecting it only after the complete transfer. */
|
||||
@@ -165,21 +252,23 @@ public final class DockingManager {
|
||||
MoveCheck check = checkMove(id, target);
|
||||
if (check instanceof MoveCheck.Rejected rejected) return new ActionResult.Rejected(rejected.failure());
|
||||
if (check instanceof MoveCheck.NoChange) return new ActionResult.NoChange();
|
||||
var destination = (ResolvedDockTarget.Insertion) ((MoveCheck.Allowed) check).destination();
|
||||
var destination = ((MoveCheck.Allowed) check).destination();
|
||||
Group source = containing(id);
|
||||
Group receiver = group(destination.groupId());
|
||||
Group receiver = receiver(destination);
|
||||
int insertionIndex = destination instanceof ResolvedDockTarget.Insertion insertion ? insertion.index() : 0;
|
||||
Tab tab = source.tabs.getTabs().get(tabIndex(source, id));
|
||||
updating = true;
|
||||
try {
|
||||
tab.setContent(null);
|
||||
source.tabs.getTabs().remove(tab);
|
||||
receiver.tabs.getTabs().add(destination.index(), tab);
|
||||
receiver.tabs.getTabs().add(insertionIndex, tab);
|
||||
tab.setContent(dockables.get(id).getContent());
|
||||
receiver.tabs.getSelectionModel().select(tab);
|
||||
normalizeLayout(source.id);
|
||||
} finally {
|
||||
updating = false;
|
||||
}
|
||||
active.set(dockables.get(id));
|
||||
setActive(dockables.get(id));
|
||||
focus(active.get());
|
||||
publish();
|
||||
return new ActionResult.Applied();
|
||||
@@ -188,17 +277,40 @@ public final class DockingManager {
|
||||
private MoveCheck resolve(Dockable dockable, DockTarget target) {
|
||||
DockableId id = dockable.getId();
|
||||
Objects.requireNonNull(target, "target");
|
||||
Group receiver;
|
||||
TabPosition position;
|
||||
if (target instanceof DockTarget.Area area) {
|
||||
receiver = groups.get(area.id());
|
||||
position = TabPosition.append();
|
||||
} else {
|
||||
DockTarget.Group concrete = (DockTarget.Group) target;
|
||||
receiver = group(concrete.id());
|
||||
position = concrete.position();
|
||||
if (target instanceof DockTarget.Split split) {
|
||||
Group receiver = group(split.id());
|
||||
if (receiver == null) return moveRejected(DockFailure.Code.TARGET_NOT_FOUND, id, "Destination does not exist");
|
||||
if (groupClosing(receiver)) return moveRejected(DockFailure.Code.CLOSE_IN_PROGRESS, id, "A close request covers this group");
|
||||
if (containing(id) == receiver && receiver.tabs.getTabs().size() == 1) return new MoveCheck.NoChange();
|
||||
if (!receiver.definition.getSplitDirections().allows(split.side())) {
|
||||
return moveRejected(DockFailure.Code.SPLIT_DISABLED, id, "Splitting is disabled in this direction");
|
||||
}
|
||||
double available = split.side().isVertical() ? receiver.tabs.getWidth() : receiver.tabs.getHeight();
|
||||
if (available <= 0) available = split.side().isVertical()
|
||||
? receiver.definition.getPreferredWidth() : receiver.definition.getPreferredHeight();
|
||||
double minimum = split.side().isVertical()
|
||||
? receiver.definition.getMinimumWidth() : receiver.definition.getMinimumHeight();
|
||||
if (available - 8 < minimum * 2) {
|
||||
return moveRejected(DockFailure.Code.SPLIT_TOO_SMALL, id, "Both group minima do not fit beside the divider");
|
||||
}
|
||||
return checkPlacement(dockable, new ResolvedDockTarget.Split(receiver.id, split.side()));
|
||||
}
|
||||
if (target instanceof DockTarget.Area area) {
|
||||
List<Group> candidates = tree.groups(area.id()).stream().sorted(java.util.Comparator
|
||||
.comparingLong((Group candidate) -> groupActivationOrder.getOrDefault(candidate.id, 0L)).reversed()).toList();
|
||||
MoveCheck rejected = moveRejected(DockFailure.Code.TARGET_NOT_FOUND, id, "Destination does not exist");
|
||||
for (Group candidate : candidates) {
|
||||
MoveCheck result = resolve(dockable, DockTarget.group(candidate.id));
|
||||
if (!(result instanceof MoveCheck.Rejected)) return result;
|
||||
if (candidate == candidates.getFirst()) rejected = result;
|
||||
}
|
||||
return rejected;
|
||||
}
|
||||
DockTarget.Group concrete = (DockTarget.Group) target;
|
||||
Group receiver = group(concrete.id());
|
||||
TabPosition position = concrete.position();
|
||||
if (receiver == null) return moveRejected(DockFailure.Code.TARGET_NOT_FOUND, id, "Destination does not exist");
|
||||
if (groupClosing(receiver)) return moveRejected(DockFailure.Code.CLOSE_IN_PROGRESS, id, "A close request covers this group");
|
||||
int index = receiver.tabs.getTabs().size();
|
||||
DockableId reference = switch (position) {
|
||||
case TabPosition.Before before -> before.id();
|
||||
@@ -217,6 +329,11 @@ public final class DockingManager {
|
||||
if (index == oldIndex) return new MoveCheck.NoChange();
|
||||
}
|
||||
var resolved = new ResolvedDockTarget.Insertion(receiver.id, index);
|
||||
return checkPlacement(dockable, resolved);
|
||||
}
|
||||
|
||||
private MoveCheck checkPlacement(Dockable dockable, ResolvedDockTarget resolved) {
|
||||
DockableId id = dockable.getId();
|
||||
try {
|
||||
Optional<DockFailure> failure = Objects.requireNonNull(placementRule.check(dockable, resolved), "Placement rule result");
|
||||
if (failure.isPresent()) return new MoveCheck.Rejected(failure.get());
|
||||
@@ -227,8 +344,25 @@ public final class DockingManager {
|
||||
return new MoveCheck.Allowed(resolved);
|
||||
}
|
||||
|
||||
private void normalizeLayout(DockGroupId source) {
|
||||
for (DockGroupId id : tree.normalize(source)) {
|
||||
groups.remove(id);
|
||||
groupActivationOrder.remove(id);
|
||||
}
|
||||
refreshEmptyGroups();
|
||||
}
|
||||
|
||||
private void refreshEmptyGroups() {
|
||||
for (Group group : groups.values()) {
|
||||
boolean occupied = !group.tabs.getTabs().isEmpty();
|
||||
group.tabs.setVisible(occupied || (group.definition.isVisibleWhenEmpty()
|
||||
&& !group.definition.isReleaseSpaceWhenEmpty()));
|
||||
group.tabs.setManaged(occupied || !group.definition.isReleaseSpaceWhenEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
private Group group(DockGroupId id) {
|
||||
return groups.values().stream().filter(group -> group.id.equals(id)).findFirst().orElse(null);
|
||||
return groups.get(id);
|
||||
}
|
||||
|
||||
private Group containing(DockableId id) {
|
||||
@@ -250,23 +384,66 @@ public final class DockingManager {
|
||||
public DockLayoutView layout() { return layout.get(); }
|
||||
public ReadOnlyObjectProperty<DockLayoutView> layoutProperty() { return layout.getReadOnlyProperty(); }
|
||||
|
||||
private void addGroup(DockGroupDefinition definition, java.util.function.Consumer<javafx.scene.Node> place) {
|
||||
private void addEdgeGroup(DockLayoutNode definition, javafx.geometry.Side side) {
|
||||
if (definition == null) return;
|
||||
DockAreaId area = new DockAreaId(definition.getDestinationId());
|
||||
if (groups.containsKey(area)) throw new IllegalArgumentException("Duplicate destination: " + area.value());
|
||||
edgeAreas.put(new DockAreaId(definition.getDestinationId()), side);
|
||||
DockAreaId areaId = new DockAreaId(definition.getDestinationId());
|
||||
addGroup(definition, node -> pane.setArea(side, node,
|
||||
() -> tree.groups(areaId).stream().noneMatch(this::groupClosing), this::publish));
|
||||
}
|
||||
|
||||
private void addGroup(DockLayoutNode definition, java.util.function.Consumer<javafx.scene.Node> place) {
|
||||
if (definition == null) return;
|
||||
tree.addArea(definition, place,
|
||||
group -> createGroup(new DockGroupId(group.getDestinationId()), group));
|
||||
}
|
||||
|
||||
private Group createGroup(DockGroupId id, DockGroupDefinition definition) {
|
||||
DockGroupDefinition captured = new DockGroupDefinition(definition.getDestinationId());
|
||||
captured.setTabSide(definition.getTabSide());
|
||||
captured.setPreferredWidth(definition.getPreferredWidth());
|
||||
captured.setPreferredHeight(definition.getPreferredHeight());
|
||||
captured.setMinimumWidth(definition.getMinimumWidth());
|
||||
captured.setMinimumHeight(definition.getMinimumHeight());
|
||||
captured.setSplitDirections(definition.getSplitDirections());
|
||||
captured.setPersistent(definition.isPersistent());
|
||||
captured.setVisibleWhenEmpty(definition.isVisibleWhenEmpty());
|
||||
captured.setReleaseSpaceWhenEmpty(definition.isReleaseSpaceWhenEmpty());
|
||||
TabPane tabs = new TabPane();
|
||||
tabs.setSide(Objects.requireNonNull(definition.getTabSide(), "tabSide"));
|
||||
tabs.setPrefSize(definition.getPreferredWidth(), definition.getPreferredHeight());
|
||||
Group group = new Group(new DockGroupId(area.value()), tabs);
|
||||
groups.put(area, group);
|
||||
tabs.setSide(captured.getTabSide());
|
||||
tabs.setPrefSize(captured.getPreferredWidth(), captured.getPreferredHeight());
|
||||
tabs.setMinSize(captured.getMinimumWidth(), captured.getMinimumHeight());
|
||||
Group group = new Group(id, tabs, captured);
|
||||
groups.put(id, group);
|
||||
tabs.getSelectionModel().selectedItemProperty().addListener((observable, old, selected) -> {
|
||||
if (!updating && selected != null) {
|
||||
active.set(dockables.get((DockableId) selected.getUserData()));
|
||||
setActive(dockables.get((DockableId) selected.getUserData()));
|
||||
focus(active.get());
|
||||
publish();
|
||||
}
|
||||
});
|
||||
place.accept(tabs);
|
||||
return group;
|
||||
}
|
||||
|
||||
private Group receiver(ResolvedDockTarget destination) {
|
||||
if (destination instanceof ResolvedDockTarget.Insertion insertion) return group(insertion.groupId());
|
||||
ResolvedDockTarget.Split split = (ResolvedDockTarget.Split) destination;
|
||||
Group target = group(split.groupId());
|
||||
DockGroupId id;
|
||||
do { id = new DockGroupId("group-" + ++nextGroupId); } while (groups.containsKey(id));
|
||||
Group added = createGroup(id, target.definition);
|
||||
tree.split(target, added, split.side());
|
||||
return added;
|
||||
}
|
||||
|
||||
private void setActive(Dockable dockable) {
|
||||
if (dockable != null) {
|
||||
long order = ++nextActivation;
|
||||
activationOrder.put(dockable.getId(), order);
|
||||
Group group = containing(dockable.getId());
|
||||
if (group != null) groupActivationOrder.put(group.id, order);
|
||||
}
|
||||
active.set(dockable);
|
||||
}
|
||||
|
||||
private void rememberFocus(Node current) {
|
||||
@@ -274,14 +451,21 @@ public final class DockingManager {
|
||||
for (Dockable dockable : dockables.values()) {
|
||||
if (contains(dockable.getContent(), current)) {
|
||||
previousFocus.put(dockable.getId(), current);
|
||||
active.set(dockable);
|
||||
setActive(dockable);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Node focusTarget(Dockable dockable) {
|
||||
Node target = dockable.getFocusTarget() == null ? previousFocus.get(dockable.getId()) : dockable.getFocusTarget().get();
|
||||
Node target = previousFocus.get(dockable.getId());
|
||||
if (dockable.getFocusTarget() != null) {
|
||||
try {
|
||||
target = dockable.getFocusTarget().get();
|
||||
} catch (RuntimeException failure) {
|
||||
log.warn("Cannot resolve focus target for Dockable {}; using its content focus", dockable.getId(), failure);
|
||||
}
|
||||
}
|
||||
if (target == null || !contains(dockable.getContent(), target)) return dockable.getContent();
|
||||
return target;
|
||||
}
|
||||
@@ -302,26 +486,53 @@ public final class DockingManager {
|
||||
|
||||
private static void validate(DockLayout initial) {
|
||||
var destinations = new java.util.HashSet<DockAreaId>();
|
||||
for (DockGroupDefinition definition : new DockGroupDefinition[]{initial.getCenter(), initial.getLeft(),
|
||||
for (DockLayoutNode definition : new DockLayoutNode[]{initial.getCenter(), initial.getLeft(),
|
||||
initial.getRight(), initial.getTop(), initial.getBottom()}) {
|
||||
if (definition == null) continue;
|
||||
DockAreaId id = new DockAreaId(definition.getDestinationId());
|
||||
if (!destinations.add(id)) throw new IllegalArgumentException("Duplicate destination: " + id.value());
|
||||
if (definition.getTabSide() == null) throw new IllegalArgumentException("Tab side is required");
|
||||
if (!Double.isFinite(definition.getPreferredWidth()) || definition.getPreferredWidth() <= 0
|
||||
|| !Double.isFinite(definition.getPreferredHeight()) || definition.getPreferredHeight() <= 0) {
|
||||
if (definition != null) validate(definition, destinations);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validate(DockLayoutNode definition, java.util.Set<DockAreaId> destinations) {
|
||||
if (definition == null) throw new IllegalArgumentException("A split needs two branches");
|
||||
DockAreaId id = new DockAreaId(definition.getDestinationId());
|
||||
if (!destinations.add(id)) throw new IllegalArgumentException("Duplicate destination: " + id.value());
|
||||
if (definition instanceof DockSplitDefinition split) {
|
||||
if (split.getOrientation() == null || !Double.isFinite(split.getPosition())
|
||||
|| split.getPosition() <= 0 || split.getPosition() >= 1) {
|
||||
throw new IllegalArgumentException("A split needs an orientation and a position between zero and one");
|
||||
}
|
||||
validate(split.getFirst(), destinations);
|
||||
validate(split.getSecond(), destinations);
|
||||
} else {
|
||||
DockGroupDefinition group = (DockGroupDefinition) definition;
|
||||
if (group.getSplitDirections() == null || !Double.isFinite(group.getMinimumWidth())
|
||||
|| group.getMinimumWidth() < 0 || !Double.isFinite(group.getMinimumHeight()) || group.getMinimumHeight() < 0) {
|
||||
throw new IllegalArgumentException("Group minimum dimensions must be nonnegative and finite, with split directions");
|
||||
}
|
||||
if (group.getTabSide() == null) throw new IllegalArgumentException("Tab side is required");
|
||||
if (!Double.isFinite(group.getPreferredWidth()) || group.getPreferredWidth() <= 0
|
||||
|| !Double.isFinite(group.getPreferredHeight()) || group.getPreferredHeight() <= 0) {
|
||||
throw new IllegalArgumentException("Group preferred dimensions must be positive and finite");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void publish() {
|
||||
List<DockLayoutView.Area> areas = groups.entrySet().stream().map(entry -> {
|
||||
Group group = entry.getValue();
|
||||
List<DockableId> ids = group.tabs.getTabs().stream().map(tab -> (DockableId) tab.getUserData()).toList();
|
||||
Tab selected = group.tabs.getSelectionModel().getSelectedItem();
|
||||
return new DockLayoutView.Area(entry.getKey(), List.of(new DockLayoutView.Group(group.id,
|
||||
group.tabs.getSide(), ids, selected == null ? null : (DockableId) selected.getUserData())));
|
||||
boolean wasUpdating = updating;
|
||||
updating = true;
|
||||
try {
|
||||
stage.getScene().getRoot().applyCss();
|
||||
stage.getScene().getRoot().layout();
|
||||
tree.captureGeometry();
|
||||
} finally {
|
||||
updating = wasUpdating;
|
||||
}
|
||||
var areas = tree.view().stream().map(area -> {
|
||||
javafx.geometry.Side side = edgeAreas.get(area.id());
|
||||
if (side == null) return area;
|
||||
double extent = pane.committedExtent(side);
|
||||
return new DockLayoutView.Area(area.id(), area.groups(), area.root(),
|
||||
side.isVertical() ? extent : -1, side.isHorizontal() ? extent : -1);
|
||||
}).toList();
|
||||
layout.set(new DockLayoutView(List.of(new DockLayoutView.Window(new DockWindowId("main"), true, areas))));
|
||||
}
|
||||
@@ -334,5 +545,5 @@ public final class DockingManager {
|
||||
if (!Platform.isFxApplicationThread()) throw new IllegalStateException("Docking operations require the JavaFX Application Thread");
|
||||
}
|
||||
|
||||
private record Group(DockGroupId id, TabPane tabs) { }
|
||||
record Group(DockGroupId id, TabPane tabs, DockGroupDefinition definition) { }
|
||||
}
|
||||
|
||||
@@ -2,5 +2,6 @@ package de.neitzel.fx.docking;
|
||||
|
||||
/** Concrete placement checked against the current layout, without allocating IDs. */
|
||||
public sealed interface ResolvedDockTarget {
|
||||
record Split(DockGroupId groupId, javafx.geometry.Side side) implements ResolvedDockTarget { }
|
||||
record Insertion(DockGroupId groupId, int index) implements ResolvedDockTarget { }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.geometry.Side;
|
||||
|
||||
/** Directions in which a target group permits an adjacent split. */
|
||||
public enum SplitDirections {
|
||||
NONE, HORIZONTAL, VERTICAL, ALL;
|
||||
|
||||
public boolean allows(Side side) {
|
||||
return this == ALL || (this == HORIZONTAL && side.isVertical())
|
||||
|| (this == VERTICAL && side.isHorizontal());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user