Skip to content

Commit

Permalink
fix: the gen-ai reframe panel shows out of the viewport if generated …
Browse files Browse the repository at this point in the history
…topics make the selected topic out of the viewport.
  • Loading branch information
mindolph committed May 24, 2024
1 parent ba19817 commit b434d92
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static Point2D bestLocation(Bounds parentBounds, Bounds hoverBounds,
double padHeight = extraPadding == null ? 1f : extraPadding.getHeight(); // use 1 to avoid calculation bias
double offsetX = (parentBounds.getMaxX() - padWidth) - hoverBounds.getMaxX();
double offsetY = (parentBounds.getMaxY() - padHeight) - hoverBounds.getMaxY();
// different strategy from x and y
// different strategy from x to y
double newX = offsetX > 0 ? hoverBounds.getMinX() : hoverBounds.getMinX() + offsetX;
double newY = offsetY > 0 ? hoverBounds.getMinY() : hoverBounds.getMinY() - hoverBounds.getHeight() - targetDim.getHeight() - padHeight;
newX = Math.max(0, newX);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mindolph.base.util;

import com.mindolph.mfx.util.PointUtils;
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
import javafx.geometry.Dimension2D;
import javafx.geometry.Point2D;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

/**
* @since 1.7.6
*/
public class LayoutUtilsTest {

static Bounds baseParentBounds;
static Bounds baseHoverBounds;

@BeforeAll
public static void setup() {
baseParentBounds = new BoundingBox(0, 0, 1000, 1000);
baseHoverBounds = new BoundingBox(0, 0, 1000, 1000);
}

@Test
public void bestLocation() {

Dimension2D dim = new Dimension2D(100, 100);

Bounds hb = new BoundingBox(0, 0, 100, 100);
Point2D point2D = LayoutUtils.bestLocation(baseParentBounds, hb, dim, null);
System.out.println(PointUtils.pointInStr(point2D));

hb = new BoundingBox(950, 950, 100, 100);
point2D = LayoutUtils.bestLocation(baseParentBounds, hb, dim, null);
System.out.println(PointUtils.pointInStr(point2D));


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,43 +254,50 @@ public ContextMenu createContextMenu(MindMap<TopicNode> model, boolean isFullScr
if (opt.isPresent()) {
Generator generator = opt.get();
// generator.setParentSkin(parentSkin);
// the parent panel is the editor itself for mind map.
generator.setParentPane(super.getParentPane());
MenuItem menuItem = generator.contextMenuItem(null);
ctxMenu.getItems().add(menuItem);
menuItem.setOnAction(e -> {
generator.showInputPanel(topicUnderMouse != null ? topicUnderMouse.getText() : "");
});
generator.setOnPanelShowing(pane -> {
// do calculation on layout bounds changes to mark sure that the bounds is calculated.
// do calculation on layout bounds changes to make sure that the bounds is calculated.
pane.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
if (oldValue.equals(newValue) || newValue.getWidth() == 0 || newValue.getHeight() == 0) {
return;
}
pane.setVisible(false); // avoid flashing
super.setDisable(true);
Bounds panelBounds = pane.getBoundsInParent();
// retrieve the element from topic because the original one might have been changed.
BaseElement element = (BaseElement) super.getFirstSelectedTopic().getPayload();
Bounds boundsInViewPort = getMindMapViewSkin().getBoundsInViewport(element);
log.debug("bounds of topic in viewport: %s".formatted(BoundsUtils.boundsInString(boundsInViewPort)));
log.debug("bounds of pane: %s".formatted(BoundsUtils.boundsInString(panelBounds)));
log.debug("dimension of pane: %sx%s".formatted(pane.getWidth(), pane.getHeight()));

Bounds hoverBounds = BoundsUtils.fromPoint(new Point2D(boundsInViewPort.getMinX(), boundsInViewPort.getMaxY()),
pane.getWidth(), pane.getHeight());

Bounds parentBounds = super.getParentPane().getBoundsInParent();
log.debug("target bounds: " + BoundsUtils.boundsInString(parentBounds));
log.debug("hover bounds: " + BoundsUtils.boundsInString(hoverBounds));
Point2D newPoint = LayoutUtils.bestLocation(parentBounds, hoverBounds, GeometryConvertUtils.boundsToDimension2D(boundsInViewPort),
new Dimension2D(config.getTheme().getSelectLineWidth(), config.getTheme().getSelectLineWidth()));
log.debug("relocated to new point: %s".formatted(PointUtils.pointInStr(newPoint)));

Platform.runLater(() -> {
pane.relocate(newPoint.getX(), newPoint.getY() + config.getTheme().getSelectLineWidth());
pane.setVisible(true);
pane.requestFocus();
// the selected topic might be out of viewport once generated topics are appended to it,
// so before locating panel, the scrolling should be done first.
scrollDoneEvents.subscribeFor(1, unused -> {
pane.setVisible(false); // avoid flashing
Bounds boundsInViewPort = getMindMapViewSkin().getBoundsInViewport(element);
log.debug("bounds of topic in viewport: %s".formatted(BoundsUtils.boundsInString(boundsInViewPort)));
log.debug("bounds of pane: %s".formatted(BoundsUtils.boundsInString(panelBounds)));
log.debug("dimension of pane: %sx%s".formatted(pane.getWidth(), pane.getHeight()));

Bounds parentBounds = getParentPane().getBoundsInParent();
Bounds hoverBounds = BoundsUtils.fromPoint(new Point2D(boundsInViewPort.getMinX(), boundsInViewPort.getMaxY()),
pane.getWidth(), pane.getHeight());
log.debug("parent bounds: %s".formatted(BoundsUtils.boundsInString(parentBounds)));
log.debug("hover bounds: %s".formatted(BoundsUtils.boundsInString(hoverBounds)));
Point2D newPoint = LayoutUtils.bestLocation(parentBounds, hoverBounds, GeometryConvertUtils.boundsToDimension2D(boundsInViewPort),
new Dimension2D(config.getTheme().getSelectLineWidth(), config.getTheme().getSelectLineWidth()));
log.debug("relocated to new point: %s".formatted(PointUtils.pointInStr(newPoint)));

Platform.runLater(() -> {
pane.relocate(newPoint.getX(), newPoint.getY() + config.getTheme().getSelectLineWidth());
pane.setVisible(true);
pane.requestFocus();
});
});
ensureVisibilityOfTopic(getFirstSelectedTopic());

});
});
generator.setOnGenerated(output -> {
Expand Down

0 comments on commit b434d92

Please sign in to comment.