Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
Expand Down Expand Up @@ -90,6 +90,10 @@ protected void visibleChangedImpl(Window window, boolean visible) {
* Methods used by Window (base) class only
*/

public static Window getWindowOwner(Window window) {
return windowAccessor.getWindowOwner(window);
}

public static TKStage getPeer(Window window) {
return windowAccessor.getPeer(window);
}
Expand Down Expand Up @@ -145,6 +149,7 @@ public interface WindowAccessor {
void setHelper(Window window, WindowHelper windowHelper);
void doVisibleChanging(Window window, boolean visible);
void doVisibleChanged(Window window, boolean visible);
Window getWindowOwner(Window window);
TKStage getPeer(Window window);
void setPeer(Window window, TKStage peer);
WindowPeerListener getPeerListener(Window window);
Expand Down
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);
}
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;
Copy link
Contributor

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 ComputedLocation or WindowLocationAlgorithm.

Here, for example, L101 tells us the gravity is measured in pixels, while L104 indicates the gravity is dimensionless.

}

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) {}
}
Loading