FloatingX icon indicating copy to clipboard operation
FloatingX copied to clipboard

Fix floating window positioning when view size changes with gravity settings

Open Copilot opened this issue 6 months ago • 0 comments

When a floating window's content is hidden or resized (causing view size changes), the remaining content would incorrectly position at the top-left corner instead of respecting the configured FxGravity setting.

Problem

The issue occurred in the onSizeChanged method of FxViewLocationHelper.kt. When needUpdateLocation was false, the code would only apply safe coordinate bounds to the current position without recalculating based on gravity:

// Previous behavior - only ensured coordinates were within safe bounds
basicView?.internalMoveToXY(safeX(x), safeY(y))

This meant that when a floating window with FxGravity.RIGHT_OR_BOTTOM had its content hidden (reducing view size), the button would appear at the top-left of the original floating window area instead of repositioning to the right-bottom corner.

Solution

The fix detects when the view size actually changes and recalculates position based on the configured gravity:

override fun onSizeChanged(w: Int, h: Int, oldW: Int, oldH: Int) {
    val sizeChanged = updateViewSize()
    if (isInitLocation) return
    if (needUpdateLocation) {
        checkOrRestoreLocation()
    } else if (sizeChanged && !config.gravity.isDefault()) {
        // Recalculate position based on gravity when view size changes
        val (newX, newY) = recalculatePositionByGravity()
        basicView?.internalMoveToXY(newX, newY)
    } else {
        basicView?.internalMoveToXY(safeX(x), safeY(y))
    }
}

Key Changes

  1. Modified updateViewSize() to return a boolean indicating if the view size actually changed
  2. Added recalculatePositionByGravity() method that reuses existing gravity calculation logic
  3. Enhanced onSizeChanged() to detect size changes and apply gravity-based repositioning when appropriate

Behavior

  • Before: Floating window with FxGravity.RIGHT_OR_BOTTOM would move to top-left when content was hidden
  • After: Floating window correctly repositions to right-bottom corner based on new view size
  • Preserved: All existing behavior for initialization, full updates, and default gravity cases

The fix only applies when:

  • View size actually changes (not just method calls)
  • Gravity is set to non-default values (excludes DEFAULT and LEFT_OR_TOP)
  • No full location update is in progress

Fixes #206.

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dl.google.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.14.3-all/8fdzzxam9lo77uruaibtsldo0/gradle-8.14.3/lib/gradle-daemon-main-8.14.3.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.14.3-all/8fdzzxam9lo77uruaibtsldo0/gradle-8.14.3/lib/agents/gradle-instrumentation-agent-8.14.3.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.14.3 (dns block)
  • jitpack.io
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.14.3-all/8fdzzxam9lo77uruaibtsldo0/gradle-8.14.3/lib/gradle-daemon-main-8.14.3.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.14.3-all/8fdzzxam9lo77uruaibtsldo0/gradle-8.14.3/lib/agents/gradle-instrumentation-agent-8.14.3.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.14.3 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Aug 25 '25 03:08 Copilot