-
Notifications
You must be signed in to change notification settings - Fork 554
8372530: Easier placement of windows with positioning anchor #1986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mstr2
wants to merge
12
commits into
openjdk:master
Choose a base branch
from
mstr2:feature/stage-anchor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,600
−89
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a386784
Implementation of positioning anchor for Stage
mstr2 3789271
refactor Anchor -> AnchorPoint
mstr2 d445623
update tests
mstr2 1e4149e
add clampPolicy and screenPadding
mstr2 6cf52e9
ensure screenPadding is not negative
mstr2 141c188
javadocs
mstr2 d0a56ec
refactor everything
mstr2 5ca15d3
tweaks
mstr2 007d485
doc fixes
mstr2 8d9138b
small changes
mstr2 a201287
refactor
mstr2 e5dfb0e
add relocate() overload that accepts Screen
mstr2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
modules/javafx.graphics/src/main/java/com/sun/javafx/stage/WindowLocationAlgorithm.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. Oracle designates this | ||
| * particular file as subject to the "Classpath" exception as provided | ||
| * by Oracle in the LICENSE file that accompanied this code. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| package com.sun.javafx.stage; | ||
|
|
||
| import javafx.stage.Screen; | ||
|
|
||
| public interface WindowLocationAlgorithm { | ||
|
|
||
| record ComputedLocation( | ||
| double x, double y, | ||
| double xGravity, double yGravity) {} | ||
|
|
||
| ComputedLocation compute(Screen screen, double windowWidth, double windowHeight); | ||
| } |
327 changes: 327 additions & 0 deletions
327
modules/javafx.graphics/src/main/java/com/sun/javafx/stage/WindowRelocator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,327 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. Oracle designates this | ||
| * particular file as subject to the "Classpath" exception as provided | ||
| * by Oracle in the LICENSE file that accompanied this code. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| package com.sun.javafx.stage; | ||
|
|
||
| import com.sun.javafx.util.Utils; | ||
| import javafx.geometry.AnchorPoint; | ||
| import javafx.geometry.Insets; | ||
| import javafx.geometry.Point2D; | ||
| import javafx.geometry.Rectangle2D; | ||
| import javafx.stage.AnchorPolicy; | ||
| import javafx.stage.Screen; | ||
| import javafx.stage.Window; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public final class WindowRelocator { | ||
|
|
||
| private WindowRelocator() {} | ||
|
|
||
| /** | ||
| * Creates a location algorithm that computes the position of the {@link Window} at the requested screen | ||
| * coordinates using an {@link AnchorPoint}, {@link AnchorPolicy}, and per-edge screen constraints. | ||
| * {@code screenAnchor} is specified relative to {@code userScreen}. If {@code userScreen} is {@code null}, | ||
| * the screen anchor is specified relative to the current window screen; if the window has not been shown | ||
| * yet, it is specified relative to the primary screen. | ||
| * <p> | ||
| * Screen edge constraints are specified by {@code screenPadding}: | ||
| * <ul> | ||
| * <li>values {@code >= 0} enable a constraint for the corresponding edge (minimum distance to keep) | ||
| * <li>values {@code < 0} disable the constraint for that edge | ||
| * </ul> | ||
| * Enabled constraints reduce the usable area for placement by the given insets. | ||
| */ | ||
| public static WindowLocationAlgorithm newRelocationAlgorithm(Screen userScreen, | ||
| AnchorPoint screenAnchor, | ||
| Insets screenPadding, | ||
| AnchorPoint stageAnchor, | ||
| AnchorPolicy anchorPolicy) { | ||
| Objects.requireNonNull(screenAnchor, "screenAnchor cannot be null"); | ||
| Objects.requireNonNull(screenPadding, "screenPadding cannot be null"); | ||
| Objects.requireNonNull(stageAnchor, "stageAnchor cannot be null"); | ||
| Objects.requireNonNull(anchorPolicy, "anchorPolicy cannot be null"); | ||
|
|
||
| return (windowScreen, windowWidth, windowHeight) -> { | ||
| double screenX, screenY; | ||
| double gravityX, gravityY; | ||
| Screen currentScreen = Objects.requireNonNullElse(userScreen, windowScreen); | ||
| Rectangle2D currentBounds = Utils.hasFullScreenStage(currentScreen) | ||
| ? currentScreen.getBounds() | ||
| : currentScreen.getVisualBounds(); | ||
|
|
||
| // Compute the absolute coordinates of the screen anchor. | ||
| // If the screen anchor is specified in proportional coordinates, it is proportional to the complete | ||
| // screen bounds when a full-screen stage is showing on the screen, and the visual bounds otherwise. | ||
| if (screenAnchor.isProportional()) { | ||
| screenX = currentBounds.getMinX() + screenAnchor.getX() * currentBounds.getWidth(); | ||
| screenY = currentBounds.getMinY() + screenAnchor.getY() * currentBounds.getHeight(); | ||
| } else { | ||
| screenX = currentBounds.getMinX() + screenAnchor.getX(); | ||
| screenY = currentBounds.getMinY() + screenAnchor.getY(); | ||
| } | ||
|
|
||
| // The absolute screen anchor might be on a different screen than the current window, so we | ||
| // need to recompute the actual screen and its bounds (complete when full-screen stage showing, | ||
| // visual otherwise). | ||
| Screen targetScreen = Utils.getScreenForPoint(screenX, screenY); | ||
| Rectangle2D screenBounds = Utils.hasFullScreenStage(targetScreen) | ||
| ? targetScreen.getBounds() | ||
| : targetScreen.getVisualBounds(); | ||
|
|
||
| Point2D location = computeAdjustedLocation( | ||
| screenX, screenY, | ||
| windowWidth, windowHeight, | ||
| stageAnchor, anchorPolicy, | ||
| screenBounds, screenPadding); | ||
|
|
||
| if (stageAnchor.isProportional()) { | ||
| gravityX = stageAnchor.getX(); | ||
| gravityY = stageAnchor.getY(); | ||
| } else { | ||
| gravityX = stageAnchor.getX() / windowWidth; | ||
| gravityY = stageAnchor.getY() / windowHeight; | ||
| } | ||
|
|
||
| return new WindowLocationAlgorithm.ComputedLocation(location.getX(), location.getY(), gravityX, gravityY); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Computes the adjusted top-left location of a window for a requested anchor position on screen. | ||
| * <p> | ||
| * The requested screen coordinates {@code (screenX, screenY)} are interpreted as the desired location | ||
| * of {@code anchor} on the window. The raw (unadjusted) window position is derived from the anchor and | ||
| * the given {@code width}/{@code height}. If that raw position violates any enabled constraints, the | ||
| * method considers alternative anchors depending on {@code policy} (for example, horizontally and/or | ||
| * vertically flipped anchors) and chooses the alternative that yields the smallest adjustment after | ||
| * constraints are applied. | ||
| * <p> | ||
| * Screen edge constraints are specified by {@code screenPadding}: | ||
| * values {@code >= 0} enable a constraint for the corresponding edge (minimum distance to keep), | ||
| * values {@code < 0} disable the constraint for that edge. Enabled constraints reduce the usable area | ||
| * for placement by the given insets. | ||
| */ | ||
| public static Point2D computeAdjustedLocation(double screenX, double screenY, | ||
| double width, double height, | ||
| AnchorPoint anchor, AnchorPolicy policy, | ||
| Rectangle2D screenBounds, Insets screenPadding) { | ||
| Constraints constraints = computeConstraints(screenBounds, width, height, screenPadding); | ||
| Position preferredRaw = getRawForAnchor(screenX, screenY, anchor, width, height); | ||
| boolean validH = isHorizontalValid(preferredRaw, constraints); | ||
| boolean validV = isVerticalValid(preferredRaw, constraints); | ||
| if (validH && validV) { | ||
| return new Point2D(preferredRaw.x, preferredRaw.y); | ||
| } | ||
|
|
||
| List<AnchorPoint> alternatives = computeAlternatives(anchor, policy, validH, validV, width, height); | ||
| Point2D bestAdjusted = applyConstraints(preferredRaw, constraints); | ||
| double bestCost = getAdjustmentCost(preferredRaw, bestAdjusted); | ||
|
|
||
| for (AnchorPoint alternative : alternatives) { | ||
| Position raw = getRawForAnchor(screenX, screenY, alternative, width, height); | ||
| Point2D adjusted = applyConstraints(raw, constraints); | ||
| double cost = getAdjustmentCost(raw, adjusted); | ||
|
|
||
| if (cost < bestCost) { | ||
| bestCost = cost; | ||
| bestAdjusted = adjusted; | ||
| } | ||
| } | ||
|
|
||
| return bestAdjusted; | ||
| } | ||
|
|
||
| /** | ||
| * Computes effective constraints from screen bounds, window size, and edge insets. | ||
| * <p> | ||
| * For each inset value: | ||
| * <ul> | ||
| * <li>{@code >= 0} enables a constraint for that edge | ||
| * <li>{@code < 0} disables the constraint for that edge | ||
| * </ul> | ||
| * Enabled constraints shrink the usable region by the given amounts. The computed {@code maxX} | ||
| * and {@code maxY} incorporate the window size (i.e., they are the maximum allowed top-left | ||
| * coordinates that still keep the window within the constrained region). | ||
| */ | ||
| private static Constraints computeConstraints(Rectangle2D screenBounds, | ||
| double width, double height, | ||
| Insets screenPadding) { | ||
| boolean hasMinX = screenPadding.getLeft() >= 0; | ||
| boolean hasMaxX = screenPadding.getRight() >= 0; | ||
| boolean hasMinY = screenPadding.getTop() >= 0; | ||
| boolean hasMaxY = screenPadding.getBottom() >= 0; | ||
|
|
||
| double minX = screenBounds.getMinX() + (hasMinX ? screenPadding.getLeft() : 0); | ||
| double maxX = screenBounds.getMaxX() - (hasMaxX ? screenPadding.getRight() : 0) - width; | ||
| double minY = screenBounds.getMinY() + (hasMinY ? screenPadding.getTop() : 0); | ||
| double maxY = screenBounds.getMaxY() - (hasMaxY ? screenPadding.getBottom() : 0) - height; | ||
|
|
||
| return new Constraints(hasMinX, hasMaxX, hasMinY, hasMaxY, minX, maxX, minY, maxY); | ||
| } | ||
|
|
||
| /** | ||
| * Computes the raw (unadjusted) top-left position for the given anchor. | ||
| * <p> | ||
| * The result is the position at which the window would be located if no edge constraints were applied. | ||
| */ | ||
| private static Position getRawForAnchor(double screenX, double screenY, AnchorPoint anchor, | ||
| double width, double height) { | ||
| double x, y, relX, relY; | ||
|
|
||
| if (anchor.isProportional()) { | ||
| x = width * anchor.getX(); | ||
| y = height * anchor.getY(); | ||
| relX = anchor.getX(); | ||
| relY = anchor.getY(); | ||
| } else { | ||
| x = anchor.getX(); | ||
| y = anchor.getY(); | ||
| relX = width != 0 ? anchor.getX() / width : 0; | ||
| relY = height != 0 ? anchor.getY() / height : 0; | ||
| } | ||
|
|
||
| return new Position(screenX - x, screenY - y, relX, relY); | ||
| } | ||
|
|
||
| /** | ||
| * Computes the list of alternative candidate anchors to consider, based on the requested policy | ||
| * and which constraint the preferred placement violates. | ||
| * <p> | ||
| * Candidates are ordered from most preferred to least preferred for the given policy. | ||
| */ | ||
| private static List<AnchorPoint> computeAlternatives(AnchorPoint preferred, AnchorPolicy policy, | ||
| boolean validH, boolean validV, | ||
| double width, double height) { | ||
| return switch (policy) { | ||
| case FIXED -> List.of(); | ||
|
|
||
| case FLIP_HORIZONTAL -> validH | ||
| ? List.of() | ||
| : List.of(flipAnchor(preferred, width, height, true, false)); | ||
|
|
||
| case FLIP_VERTICAL -> validV | ||
| ? List.of() | ||
| : List.of(flipAnchor(preferred, width, height, false, true)); | ||
|
|
||
| case AUTO -> { | ||
| if (!validH && !validV) { | ||
| // Try diagonal flip first, then horizontal flip, then vertical flip | ||
| yield List.of( | ||
| flipAnchor(preferred, width, height, true, true), | ||
| flipAnchor(preferred, width, height, true, false), | ||
| flipAnchor(preferred, width, height, false, true)); | ||
| } else if (!validH) { | ||
| yield List.of(flipAnchor(preferred, width, height, true, false)); | ||
| } else if (!validV) { | ||
| yield List.of(flipAnchor(preferred, width, height, false, true)); | ||
| } else{ | ||
| yield List.of(); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Applies enabled edge constraints to a raw position. | ||
| * <p> | ||
| * Constraints may be disabled per edge (via negative inset values). When both edges for an axis | ||
| * are enabled, the position is constrained to the resulting interval. When only one edge is enabled, | ||
| * a one-sided minimum or maximum constraint is applied. If the constrained interval is too small to | ||
| * fit the window, a side is chosen based on the relative anchor location. | ||
| */ | ||
| private static Point2D applyConstraints(Position raw, Constraints c) { | ||
| double x = raw.x; | ||
| double y = raw.y; | ||
|
|
||
| if (c.hasMinX && c.hasMaxX) { | ||
| if (c.maxX >= c.minX) { | ||
| x = Utils.clamp(c.minX, x, c.maxX); | ||
| } else { | ||
| // Constrained space too small: choose a side based on anchor | ||
| x = raw.relX > 0.5 ? c.maxX : c.minX; | ||
| } | ||
| } else if (c.hasMinX) { | ||
| x = Math.max(x, c.minX); | ||
| } else if (c.hasMaxX) { | ||
| x = Math.min(x, c.maxX); | ||
| } | ||
|
|
||
| if (c.hasMinY && c.hasMaxY) { | ||
| if (c.maxY >= c.minY) { | ||
| y = Utils.clamp(c.minY, y, c.maxY); | ||
| } else { | ||
| // Constrained space too small: choose a side based on anchor | ||
| y = raw.relY > 0.5 ? c.maxY : c.minY; | ||
| } | ||
| } else if (c.hasMinY) { | ||
| y = Math.max(y, c.minY); | ||
| } else if (c.hasMaxY) { | ||
| y = Math.min(y, c.maxY); | ||
| } | ||
|
|
||
| return new Point2D(x, y); | ||
| } | ||
|
|
||
| /** | ||
| * Computes a scalar "adjustment cost" used to select between candidate anchors. | ||
| * <p> | ||
| * The current implementation uses Manhattan distance (|dx| + |dy|) between the raw and adjusted positions. | ||
| * Lower values indicate that fewer or smaller constraint adjustments were required. | ||
| */ | ||
| private static double getAdjustmentCost(Position raw, Point2D adjusted) { | ||
| return Math.abs(adjusted.getX() - raw.x) + Math.abs(adjusted.getY() - raw.y); | ||
| } | ||
|
|
||
| private static boolean isHorizontalValid(Position raw, Constraints c) { | ||
| return !(c.hasMinX && raw.x < c.minX) && !(c.hasMaxX && raw.x > c.maxX); | ||
| } | ||
|
|
||
| private static boolean isVerticalValid(Position raw, Constraints c) { | ||
| return !(c.hasMinY && raw.y < c.minY) && !(c.hasMaxY && raw.y > c.maxY); | ||
| } | ||
|
|
||
| private static AnchorPoint flipAnchor(AnchorPoint anchor, | ||
| double width, double height, | ||
| boolean flipH, boolean flipV) { | ||
| double x = anchor.getX(); | ||
| double y = anchor.getY(); | ||
|
|
||
| return anchor.isProportional() | ||
| ? AnchorPoint.proportional( | ||
| flipH ? (1.0 - x) : x, | ||
| flipV ? (1.0 - y) : y) | ||
| : AnchorPoint.absolute( | ||
| flipH ? (width - x) : x, | ||
| flipV ? (height - y) : y); | ||
| } | ||
|
|
||
| private record Constraints(boolean hasMinX, boolean hasMaxX, | ||
| boolean hasMinY, boolean hasMaxY, | ||
| double minX, double maxX, | ||
| double minY, double maxY) {} | ||
|
|
||
| private record Position(double x, double y, double relX, double relY) {} | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a good idea to explain what "gravity" is, maybe in
ComputedLocationorWindowLocationAlgorithm.Here, for example, L101 tells us the gravity is measured in pixels, while L104 indicates the gravity is dimensionless.