2121import org .quiltmc .enigma .gui .docker .ObfuscatedClassesDocker ;
2222import org .quiltmc .enigma .gui .util .GridBagConstraintsBuilder ;
2323import org .quiltmc .enigma .gui .util .ScaleUtil ;
24+ import org .quiltmc .enigma .util .Utils ;
2425
2526import javax .annotation .Nullable ;
2627import javax .swing .Box ;
@@ -477,7 +478,16 @@ private void moveNearCursor() {
477478 }
478479
479480 /**
480- * After resizing, moves this so that the old distance between the cursor and the closest corner remains the same.
481+ * After resizing, moves this so that the cursor is at an 'equivalent' relative location.
482+ *
483+ * <p> The way the equivalent location is determined depends on whether this grew or shrunk, independently for the
484+ * {@code x} and {@code y} axes:
485+ * <ul>
486+ * <li> if this grew in an axis, then the distance between the cursor and the closest edge perpendicular to
487+ * that axis is maintained
488+ * <li> if this shrunk in an axis, then the ratio of the distances between the cursor and the two edges
489+ * perpendicular to that axis is maintained
490+ * </ul>
481491 *
482492 * <p> Also ensures this is entirely on-screen.
483493 */
@@ -488,24 +498,38 @@ private void moveMaintainingAnchor(Point oldMousePos, Dimension oldSize) {
488498
489499 final Point pos = this .getLocationOnScreen ();
490500
491- final int oldLeft = oldMousePos .x - pos .x ;
492- final int oldRight = pos .x + oldSize .width - oldMousePos .x ;
493- final boolean anchorRight = oldLeft > oldRight ;
494-
495- final int oldTop = oldMousePos .y - pos .y ;
496- final int oldBottom = pos .y + oldSize .height - oldMousePos .y ;
497- final boolean anchorBottom = oldTop > oldBottom ;
501+ final int left = oldMousePos .x - pos .x ;
502+ final int top = oldMousePos .y - pos .y ;
498503
499- if (anchorRight || anchorBottom ) {
500- final Dimension newSize = this .getSize ();
504+ final Dimension newSize = this .getSize ();
501505
502- final int xDiff = anchorRight ? oldSize .width - newSize .width : 0 ;
503- final int yDiff = anchorBottom ? oldSize .height - newSize .height : 0 ;
506+ final int anchoredX ;
507+ if (oldSize .width > newSize .width ) {
508+ final int targetLeft = (int ) ((double ) (left * newSize .width ) / oldSize .width );
509+ anchoredX = pos .x + left - targetLeft ;
510+ } else {
511+ final int oldRight = pos .x + oldSize .width - oldMousePos .x ;
512+ final int xDiff = left > oldRight ? oldSize .width - newSize .width : 0 ;
513+ anchoredX = pos .x + xDiff ;
514+ }
504515
505- this .setLocation (pos .x + xDiff , pos .y + yDiff );
516+ final int anchoredY ;
517+ if (oldSize .height > newSize .height ) {
518+ final int targetTop = (int ) ((double ) (top * newSize .height ) / oldSize .height );
519+ anchoredY = pos .y + top - targetTop ;
520+ } else {
521+ final int oldBottom = pos .y + oldSize .height - oldMousePos .y ;
522+ final int yDiff = top > oldBottom ? oldSize .height - newSize .height : 0 ;
523+ anchoredY = pos .y + yDiff ;
506524 }
507525
508- this .moveOnScreen ();
526+ final Dimension screenSize = Toolkit .getDefaultToolkit ().getScreenSize ();
527+ final int targetX = Utils .clamp (anchoredX , 0 , screenSize .width - newSize .width );
528+ final int targetY = Utils .clamp (anchoredY , 0 , screenSize .height - newSize .height );
529+
530+ if (targetX != pos .x || targetY != pos .y ) {
531+ this .setLocation (targetX , targetY );
532+ }
509533 }
510534
511535 /**
0 commit comments