feat(fx): move live tabs with checked destinations and drag previews
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.geometry.Bounds;
|
||||
import javafx.geometry.BoundingBox;
|
||||
import javafx.geometry.Side;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Tab;
|
||||
import javafx.scene.control.TabPane;
|
||||
import javafx.scene.control.ContextMenu;
|
||||
import javafx.scene.control.Menu;
|
||||
import javafx.scene.control.MenuItem;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.input.MouseButton;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
|
||||
import java.util.HashMap;
|
||||
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) { }
|
||||
private record Proposal(DockTarget target, Bounds marker) { }
|
||||
private record Header(DockableId id, Bounds bounds) { }
|
||||
|
||||
private final DockingManager manager;
|
||||
private final DockPane pane;
|
||||
private final Scene scene;
|
||||
private final Supplier<List<GroupSurface>> surfaces;
|
||||
private final Map<String, DockableId> headers = new HashMap<>();
|
||||
private final EventHandler<MouseEvent> mouseHandler = this::mouse;
|
||||
private final EventHandler<KeyEvent> keyHandler = this::key;
|
||||
private DockableId pressed;
|
||||
private double startX;
|
||||
private double startY;
|
||||
private boolean dragging;
|
||||
private long nextHeaderId;
|
||||
|
||||
DockDragController(DockingManager manager, DockPane pane, Supplier<List<GroupSurface>> surfaces) {
|
||||
this.manager = manager;
|
||||
this.pane = pane;
|
||||
this.scene = pane.getScene();
|
||||
this.surfaces = surfaces;
|
||||
pane.addEventFilter(MouseEvent.ANY, mouseHandler);
|
||||
scene.addEventFilter(KeyEvent.KEY_PRESSED, keyHandler);
|
||||
}
|
||||
|
||||
void installTab(Tab tab, DockableId id) {
|
||||
String headerId = "dock-tab-" + ++nextHeaderId;
|
||||
tab.setId(headerId);
|
||||
headers.put(headerId, id);
|
||||
Menu destinations = new Menu("Move to");
|
||||
ContextMenu menu = new ContextMenu(destinations);
|
||||
menu.setOnShowing(event -> populateDestinations(destinations, id));
|
||||
tab.setContextMenu(menu);
|
||||
}
|
||||
|
||||
private void populateDestinations(Menu menu, DockableId id) {
|
||||
menu.getItems().clear();
|
||||
for (DockLayoutView.Window window : manager.layout().windows()) {
|
||||
for (DockLayoutView.Area area : window.areas()) {
|
||||
for (DockLayoutView.Group group : area.groups()) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
pane.removeEventFilter(MouseEvent.ANY, mouseHandler);
|
||||
scene.removeEventFilter(KeyEvent.KEY_PRESSED, keyHandler);
|
||||
headers.clear();
|
||||
cancel();
|
||||
}
|
||||
|
||||
private void mouse(MouseEvent event) {
|
||||
if (event.getEventType() == MouseEvent.MOUSE_PRESSED && event.getButton() == MouseButton.PRIMARY) {
|
||||
cancel();
|
||||
for (Node node = event.getPickResult().getIntersectedNode(); node != null; node = node.getParent()) {
|
||||
DockableId id = headers.get(node.getId());
|
||||
if (id != null) {
|
||||
pressed = id;
|
||||
startX = event.getScreenX();
|
||||
startY = event.getScreenY();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED && pressed != null) {
|
||||
dragging |= Math.hypot(event.getScreenX() - startX, event.getScreenY() - startY) >= 5;
|
||||
if (dragging) {
|
||||
preview(event.getScreenX(), event.getScreenY());
|
||||
event.consume();
|
||||
}
|
||||
} else if (event.getEventType() == MouseEvent.MOUSE_RELEASED && pressed != null) {
|
||||
DockableId id = pressed;
|
||||
boolean apply = dragging;
|
||||
cancel();
|
||||
if (apply) {
|
||||
Proposal proposal = proposal(event.getScreenX(), event.getScreenY());
|
||||
if (proposal != null) manager.move(id, proposal.target());
|
||||
event.consume();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void preview(double screenX, double screenY) {
|
||||
Proposal proposal = proposal(screenX, screenY);
|
||||
if (proposal != null) {
|
||||
MoveCheck check = manager.checkMove(pressed, proposal.target());
|
||||
if (!(check instanceof MoveCheck.Rejected)) {
|
||||
pane.showDropIndicator(proposal.marker(), check instanceof MoveCheck.NoChange);
|
||||
return;
|
||||
}
|
||||
}
|
||||
pane.hideDropIndicator();
|
||||
}
|
||||
|
||||
private Proposal proposal(double screenX, double screenY) {
|
||||
for (GroupSurface group : surfaces.get()) {
|
||||
Bounds bounds = group.tabs().localToScreen(group.tabs().getBoundsInLocal());
|
||||
if (bounds == null || !bounds.contains(screenX, screenY)) continue;
|
||||
Node strip = group.tabs().lookup(".tab-header-area");
|
||||
Bounds stripBounds = strip == null ? null : strip.localToScreen(strip.getBoundsInLocal());
|
||||
if (stripBounds != null && stripBounds.contains(screenX, screenY)) {
|
||||
Proposal insertion = insertion(group, screenX, screenY);
|
||||
if (insertion != null) return insertion;
|
||||
}
|
||||
return new Proposal(DockTarget.group(group.id()), bounds);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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());
|
||||
Bounds bounds = node == null ? null : node.localToScreen(node.getBoundsInLocal());
|
||||
return bounds == null ? null : new Header((DockableId) tab.getUserData(), bounds);
|
||||
}).filter(java.util.Objects::nonNull).toList();
|
||||
if (visible.isEmpty()) return null;
|
||||
Side side = group.tabs().getSide();
|
||||
boolean horizontal = side.isHorizontal();
|
||||
double direction = 1;
|
||||
if (visible.size() > 1) {
|
||||
double difference = center(visible.get(1).bounds(), horizontal)
|
||||
- center(visible.getFirst().bounds(), horizontal);
|
||||
if (difference != 0) direction = Math.signum(difference);
|
||||
}
|
||||
double coordinate = horizontal ? x : y;
|
||||
for (Header header : visible) {
|
||||
if (coordinate * direction < center(header.bounds(), horizontal) * direction) {
|
||||
return new Proposal(DockTarget.group(group.id(), TabPosition.before(header.id())),
|
||||
insertionMarker(header.bounds(), horizontal, direction < 0));
|
||||
}
|
||||
}
|
||||
Header last = visible.getLast();
|
||||
return new Proposal(DockTarget.group(group.id(), TabPosition.after(last.id())),
|
||||
insertionMarker(last.bounds(), horizontal, direction > 0));
|
||||
}
|
||||
|
||||
private static double center(Bounds bounds, boolean horizontal) {
|
||||
return horizontal ? bounds.getCenterX() : bounds.getCenterY();
|
||||
}
|
||||
|
||||
private static Bounds insertionMarker(Bounds bounds, boolean horizontal, boolean trailing) {
|
||||
if (horizontal) {
|
||||
double x = trailing ? bounds.getMaxX() : bounds.getMinX();
|
||||
return new BoundingBox(x - 1, bounds.getMinY(), 2, bounds.getHeight());
|
||||
}
|
||||
double y = trailing ? bounds.getMaxY() : bounds.getMinY();
|
||||
return new BoundingBox(bounds.getMinX(), y - 1, bounds.getWidth(), 2);
|
||||
}
|
||||
|
||||
private void key(KeyEvent event) {
|
||||
if (pressed != null && event.getCode() == KeyCode.ESCAPE) {
|
||||
cancel();
|
||||
event.consume();
|
||||
}
|
||||
}
|
||||
|
||||
private void cancel() {
|
||||
pressed = null;
|
||||
dragging = false;
|
||||
pane.hideDropIndicator();
|
||||
}
|
||||
}
|
||||
@@ -2,5 +2,6 @@ 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, DOCKABLE_NOT_FOUND, CONTENT_ALREADY_ATTACHED }
|
||||
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 }
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@ package de.neitzel.fx.docking;
|
||||
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.layout.Region;
|
||||
import javafx.geometry.Bounds;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.shape.Rectangle;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@@ -9,11 +12,34 @@ import lombok.Setter;
|
||||
public final class DockPane extends Region {
|
||||
@Getter @Setter private DockLayout initialLayout = new DockLayout();
|
||||
final BorderPane surface = new BorderPane();
|
||||
private final Rectangle dropIndicator = new Rectangle();
|
||||
DockingManager manager;
|
||||
|
||||
public DockPane() {
|
||||
getStyleClass().add("dock-pane");
|
||||
getChildren().add(surface);
|
||||
dropIndicator.getStyleClass().add("dock-drop-indicator");
|
||||
dropIndicator.setManaged(false);
|
||||
dropIndicator.setMouseTransparent(true);
|
||||
dropIndicator.setVisible(false);
|
||||
dropIndicator.setStrokeWidth(2);
|
||||
getChildren().add(dropIndicator);
|
||||
}
|
||||
|
||||
void showDropIndicator(Bounds screenBounds, boolean noChange) {
|
||||
Bounds local = screenToLocal(screenBounds);
|
||||
dropIndicator.setX(local.getMinX());
|
||||
dropIndicator.setY(local.getMinY());
|
||||
dropIndicator.setWidth(local.getWidth());
|
||||
dropIndicator.setHeight(local.getHeight());
|
||||
dropIndicator.setFill(noChange ? Color.rgb(100, 100, 100, .15) : Color.rgb(30, 144, 255, .18));
|
||||
dropIndicator.setStroke(noChange ? Color.GRAY : Color.DODGERBLUE);
|
||||
dropIndicator.pseudoClassStateChanged(javafx.css.PseudoClass.getPseudoClass("no-change"), noChange);
|
||||
dropIndicator.setVisible(true);
|
||||
}
|
||||
|
||||
void hideDropIndicator() {
|
||||
dropIndicator.setVisible(false);
|
||||
}
|
||||
|
||||
@Override protected void layoutChildren() { surface.resizeRelocate(0, 0, getWidth(), getHeight()); }
|
||||
|
||||
@@ -6,7 +6,15 @@ import java.util.Objects;
|
||||
public sealed interface DockTarget {
|
||||
static DockTarget area(String id) { return new Area(new DockAreaId(id)); }
|
||||
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()); }
|
||||
record Area(DockAreaId id) implements DockTarget {
|
||||
public Area { Objects.requireNonNull(id, "id"); }
|
||||
}
|
||||
record Group(DockGroupId id, TabPosition position) implements DockTarget {
|
||||
public Group {
|
||||
Objects.requireNonNull(id, "id");
|
||||
Objects.requireNonNull(position, "position");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ public final class Dockable {
|
||||
@Getter private final DockableId id;
|
||||
@Getter private final Node content;
|
||||
@Getter @Setter private Supplier<? extends Node> focusTarget;
|
||||
@Getter @Setter private Runnable onDiscarded;
|
||||
private final StringProperty title = new SimpleStringProperty(this, "title", "");
|
||||
private final ObjectProperty<Node> icon = new SimpleObjectProperty<>(this, "icon");
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ 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 boolean updating;
|
||||
private final Map<DockAreaId, Group> groups = new LinkedHashMap<>();
|
||||
private final Map<DockableId, Dockable> dockables = new LinkedHashMap<>();
|
||||
@@ -27,9 +29,10 @@ public final class DockingManager {
|
||||
private final ReadOnlyObjectWrapper<Dockable> active = new ReadOnlyObjectWrapper<>(this, "activeDockable");
|
||||
private final ReadOnlyObjectWrapper<DockLayoutView> layout = new ReadOnlyObjectWrapper<>(this, "layout");
|
||||
|
||||
private DockingManager(Stage stage, DockPane pane) {
|
||||
private DockingManager(Stage stage, DockPane pane, DockingOptions options) {
|
||||
this.stage = stage;
|
||||
this.pane = pane;
|
||||
this.placementRule = Objects.requireNonNull(options.getPlacementRule(), "placementRule");
|
||||
DockLayout initial = Objects.requireNonNull(pane.getInitialLayout(), "initialLayout");
|
||||
validate(initial);
|
||||
addGroup(initial.getCenter(), pane.surface::setCenter);
|
||||
@@ -37,6 +40,8 @@ public final class DockingManager {
|
||||
addGroup(initial.getRight(), pane.surface::setRight);
|
||||
addGroup(initial.getTop(), pane.surface::setTop);
|
||||
addGroup(initial.getBottom(), pane.surface::setBottom);
|
||||
drag = new DockDragController(this, pane, () -> groups.values().stream()
|
||||
.map(group -> new DockDragController.GroupSurface(group.id, group.tabs)).toList());
|
||||
publish();
|
||||
stage.getScene().focusOwnerProperty().addListener(focusListener);
|
||||
}
|
||||
@@ -52,7 +57,7 @@ public final class DockingManager {
|
||||
if (pane.manager != null || stage.getProperties().containsKey(ATTACHMENT_KEY)) {
|
||||
throw new IllegalStateException("DockPane or main Stage already has a docking manager");
|
||||
}
|
||||
DockingManager manager = new DockingManager(stage, pane);
|
||||
DockingManager manager = new DockingManager(stage, pane, options);
|
||||
pane.manager = manager;
|
||||
stage.getProperties().put(ATTACHMENT_KEY, manager);
|
||||
return manager;
|
||||
@@ -62,9 +67,10 @@ public final class DockingManager {
|
||||
requireFxThread();
|
||||
Objects.requireNonNull(dockable, "dockable");
|
||||
if (dockables.containsKey(dockable.getId())) return activate(dockable.getId());
|
||||
Objects.requireNonNull(target, "target");
|
||||
Group group = groups.get(((DockTarget.Area) target).id());
|
||||
if (group == null) return rejected(DockFailure.Code.TARGET_NOT_FOUND, dockable.getId(), "Destination does not exist");
|
||||
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());
|
||||
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");
|
||||
}
|
||||
@@ -74,10 +80,11 @@ public final class DockingManager {
|
||||
tab.graphicProperty().bind(dockable.iconProperty());
|
||||
tab.setContent(dockable.getContent());
|
||||
tab.setUserData(dockable.getId());
|
||||
drag.installTab(tab, dockable.getId());
|
||||
dockables.put(dockable.getId(), dockable);
|
||||
updating = true;
|
||||
try {
|
||||
group.tabs.getTabs().add(tab);
|
||||
group.tabs.getTabs().add(destination.index(), tab);
|
||||
group.tabs.getSelectionModel().select(tab);
|
||||
} finally {
|
||||
updating = false;
|
||||
@@ -88,6 +95,35 @@ public final class DockingManager {
|
||||
return new ActionResult.Applied();
|
||||
}
|
||||
|
||||
/** Creates content only for a new identity; rejected prepared content is discarded. */
|
||||
public ActionResult open(DockableId id, java.util.function.Supplier<? extends Dockable> factory, DockTarget target) {
|
||||
requireFxThread();
|
||||
Objects.requireNonNull(id, "id");
|
||||
Objects.requireNonNull(factory, "factory");
|
||||
if (dockables.containsKey(id)) return activate(id);
|
||||
Dockable prepared;
|
||||
try {
|
||||
prepared = Objects.requireNonNull(factory.get(), "Factory returned no Dockable");
|
||||
} catch (RuntimeException failure) {
|
||||
return new ActionResult.Rejected(new DockFailure(DockFailure.Code.FACTORY_FAILED,
|
||||
id, "Dockable creation failed", failure));
|
||||
}
|
||||
ActionResult result = id.equals(prepared.getId()) ? open(prepared, target)
|
||||
: rejected(DockFailure.Code.ID_MISMATCH, id, "Factory returned a different Dockable ID");
|
||||
if (result instanceof ActionResult.Rejected rejected && !dockables.containsValue(prepared)
|
||||
&& prepared.getOnDiscarded() != null) {
|
||||
try {
|
||||
prepared.getOnDiscarded().run();
|
||||
} catch (RuntimeException cleanup) {
|
||||
DockFailure failure = rejected.failure();
|
||||
var cause = new IllegalStateException("Cleanup failed after rejected open", failure.cause());
|
||||
cause.addSuppressed(cleanup);
|
||||
return new ActionResult.Rejected(new DockFailure(failure.code(), failure.dockableId(), failure.message(), cause));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public ActionResult activate(DockableId id) {
|
||||
requireFxThread();
|
||||
Dockable dockable = dockables.get(id);
|
||||
@@ -116,6 +152,99 @@ public final class DockingManager {
|
||||
}
|
||||
|
||||
public Optional<Dockable> findDockable(DockableId id) { return Optional.ofNullable(dockables.get(id)); }
|
||||
|
||||
/** 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);
|
||||
}
|
||||
|
||||
/** Moves the live content, selecting it only after the complete transfer. */
|
||||
public ActionResult move(DockableId id, DockTarget target) {
|
||||
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();
|
||||
Group source = containing(id);
|
||||
Group receiver = group(destination.groupId());
|
||||
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);
|
||||
tab.setContent(dockables.get(id).getContent());
|
||||
receiver.tabs.getSelectionModel().select(tab);
|
||||
} finally {
|
||||
updating = false;
|
||||
}
|
||||
active.set(dockables.get(id));
|
||||
focus(active.get());
|
||||
publish();
|
||||
return new ActionResult.Applied();
|
||||
}
|
||||
|
||||
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 (receiver == null) return moveRejected(DockFailure.Code.TARGET_NOT_FOUND, id, "Destination does not exist");
|
||||
int index = receiver.tabs.getTabs().size();
|
||||
DockableId reference = switch (position) {
|
||||
case TabPosition.Before before -> before.id();
|
||||
case TabPosition.After after -> after.id();
|
||||
case TabPosition.Append ignored -> null;
|
||||
};
|
||||
if (reference != null) {
|
||||
index = tabIndex(receiver, reference);
|
||||
if (index < 0) return moveRejected(DockFailure.Code.REFERENCE_TAB_NOT_FOUND, id, "Reference tab is not in the destination");
|
||||
if (position instanceof TabPosition.After) index++;
|
||||
}
|
||||
Group source = containing(id);
|
||||
if (source == receiver) {
|
||||
int oldIndex = tabIndex(source, id);
|
||||
if (index > oldIndex) index--;
|
||||
if (index == oldIndex) return new MoveCheck.NoChange();
|
||||
}
|
||||
var resolved = new ResolvedDockTarget.Insertion(receiver.id, index);
|
||||
try {
|
||||
Optional<DockFailure> failure = Objects.requireNonNull(placementRule.check(dockable, resolved), "Placement rule result");
|
||||
if (failure.isPresent()) return new MoveCheck.Rejected(failure.get());
|
||||
} catch (RuntimeException failure) {
|
||||
return new MoveCheck.Rejected(new DockFailure(DockFailure.Code.PLACEMENT_CHECK_FAILED,
|
||||
id, "Placement check failed", failure));
|
||||
}
|
||||
return new MoveCheck.Allowed(resolved);
|
||||
}
|
||||
|
||||
private Group group(DockGroupId id) {
|
||||
return groups.values().stream().filter(group -> group.id.equals(id)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private Group containing(DockableId id) {
|
||||
return groups.values().stream().filter(group -> tabIndex(group, id) >= 0).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
private static int tabIndex(Group group, DockableId id) {
|
||||
for (int i = 0; i < group.tabs.getTabs().size(); i++) {
|
||||
if (id.equals(group.tabs.getTabs().get(i).getUserData())) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static MoveCheck moveRejected(DockFailure.Code code, DockableId id, String message) {
|
||||
return new MoveCheck.Rejected(new DockFailure(code, id, message, null));
|
||||
}
|
||||
public Dockable activeDockable() { return active.get(); }
|
||||
public ReadOnlyObjectProperty<Dockable> activeDockableProperty() { return active.getReadOnlyProperty(); }
|
||||
public DockLayoutView layout() { return layout.get(); }
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Attachment options. Additional policies are introduced with their operations. */
|
||||
@Getter @Setter
|
||||
public final class DockingOptions {
|
||||
private PlacementRule placementRule = (dockable, target) -> Optional.empty();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Side-effect-free result of checking a proposed move. */
|
||||
public sealed interface MoveCheck {
|
||||
record Allowed(ResolvedDockTarget destination) implements MoveCheck { }
|
||||
record NoChange() implements MoveCheck { }
|
||||
record Rejected(DockFailure failure) implements MoveCheck { }
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/** Application placement policy; evaluating it must not change the layout. */
|
||||
@FunctionalInterface
|
||||
public interface PlacementRule {
|
||||
/** Empty permits placement; a failure rejects it with an application explanation. */
|
||||
Optional<DockFailure> check(Dockable dockable, ResolvedDockTarget destination);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
/** Concrete placement checked against the current layout, without allocating IDs. */
|
||||
public sealed interface ResolvedDockTarget {
|
||||
record Insertion(DockGroupId groupId, int index) implements ResolvedDockTarget { }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package de.neitzel.fx.docking;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** Stable relative insertion position in a tab group. */
|
||||
public sealed interface TabPosition {
|
||||
static TabPosition append() { return new Append(); }
|
||||
static TabPosition before(DockableId id) { return new Before(id); }
|
||||
static TabPosition after(DockableId id) { return new After(id); }
|
||||
record Append() implements TabPosition { }
|
||||
record Before(DockableId id) implements TabPosition {
|
||||
public Before { Objects.requireNonNull(id, "id"); }
|
||||
}
|
||||
record After(DockableId id) implements TabPosition {
|
||||
public After { Objects.requireNonNull(id, "id"); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user