From 8ef313b2ae1604e041e2b8a5e3f261c85391ef9a Mon Sep 17 00:00:00 2001 From: Danil Sokolov Date: Sun, 22 Nov 2026 13:10:56 +0000 Subject: [PATCH] Apply split percentage once size is known Without constant percent, JSplitPane converted the percentage to pixels from the zero size of a split that was not laid out yet, so a stored proportion was lost on importLayout (0.5 below 1/3, preferred sizes from 1/3 on). Keep the percentage and apply it in setBounds() once the size is known, as constant-percent mode does. Fixes #33 --- .../docking/defaults/DockingSplitPane.java | 9 +++++++++ .../test/docking/SplitPersistenceTest.java | 20 ++++++++----------- release-notes.txt | 2 ++ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/flexdock-core/src/main/java/org/flexdock/docking/defaults/DockingSplitPane.java b/flexdock-core/src/main/java/org/flexdock/docking/defaults/DockingSplitPane.java index 1aaad73e..e3c5bc96 100644 --- a/flexdock-core/src/main/java/org/flexdock/docking/defaults/DockingSplitPane.java +++ b/flexdock-core/src/main/java/org/flexdock/docking/defaults/DockingSplitPane.java @@ -54,6 +54,8 @@ public class DockingSplitPane extends JSplitPane implements DockingConstants { private boolean constantPercent; + private boolean percentPending; + /** * Creates a new {@code DockingSplitPane} for the specified * {@code DockingPort} with the understanding that the resulting @@ -132,6 +134,9 @@ public void setBounds(int x, int y, int w, int h) { if (constantPercent) { setResizeWeight(percent); super.setDividerLocation((int) (percent * getSplitSize())); + } else if (percentPending && getSplitSize() > 0) { + percentPending = false; + super.setDividerLocation(percent); } } @@ -193,6 +198,10 @@ public void setDividerLocation(double percent) { if (splitSize > 0) { super.setDividerLocation((int) (percent * splitSize)); } + } else if (valid && splitSize <= 0) { + // JSplitPane would compute a location from the zero size and lose the percentage, + // so setBounds() applies it once the size is known + percentPending = true; } else { super.setDividerLocation(percent); } diff --git a/flexdock-view/src/test/java/org/flexdock/test/docking/SplitPersistenceTest.java b/flexdock-view/src/test/java/org/flexdock/test/docking/SplitPersistenceTest.java index e6320063..c9793529 100644 --- a/flexdock-view/src/test/java/org/flexdock/test/docking/SplitPersistenceTest.java +++ b/flexdock-view/src/test/java/org/flexdock/test/docking/SplitPersistenceTest.java @@ -68,12 +68,12 @@ private static void layoutTree(Component component) { } } - // Proportion of the split after importing a stored 0.3 layout into a sized port. There is no + // Proportion of the split after importing a stored layout into a sized port. There is no // peer, so validate() does nothing: size the port and run doLayout() on the whole tree instead. - private static float importStoredProportion(boolean constantPercent) throws Exception { + private static float importStoredProportion(boolean constantPercent, float percent) throws Exception { DefaultDockingPort source = createSplitPort(DockingConstants.WEST_REGION); LayoutNode layout = source.exportLayout(); - getSplitNode(layout).setPercentage(.3f); + getSplitNode(layout).setPercentage(percent); LayoutNode restored = roundTripThroughXml(layout); // The flag is read when the split panes are created, so set it before the import @@ -132,18 +132,14 @@ void testOrientationSurvivesXmlAndImport() throws Exception { @Test void testProportionIsRestoredWithConstantPercent() throws Exception { - assertEquals(.3f, importStoredProportion(true), DELTA); + assertEquals(.3f, importStoredProportion(true, .3f), DELTA); } - // Known bug (https://github.com/opencollab/flexdock/issues/33): - // without constant percent the stored proportion is lost on import (the split comes - // back at 0.5). Expected to fail until it is fixed; then the assertion below holds, - // assertThrows fails and the wrapper is to be removed. + // Below 1/3 the unsized split used to come back at 0.5, from 1/3 at Swing's preferred sizes @Test - void testProportionIsRestoredWithoutConstantPercentKnownBug() throws Exception { - float restored = importStoredProportion(false); - - assertThrows(AssertionError.class, () -> assertEquals(.3f, restored, DELTA)); + void testProportionIsRestoredWithoutConstantPercent() throws Exception { + assertEquals(.3f, importStoredProportion(false, .3f), DELTA); + assertEquals(.7f, importStoredProportion(false, .7f), DELTA); } } diff --git a/release-notes.txt b/release-notes.txt index b0bb8f5f..464d9da0 100644 --- a/release-notes.txt +++ b/release-notes.txt @@ -20,6 +20,8 @@ Fixes: * DockingSplitPane: track the divider percentage from the first drag * DockingSplitPane: apply the percentage consistently in setDividerLocation() * DockingSplitPane: keep the percentage set before the size is known +* DockingSplitPane: apply the percentage set before the size is known also + without constant percent, so imported layouts keep their split proportions * DockingManager: use stub managers when flexdock-view or flexdock-perspective is missing, instead of overflowing the stack * UIFactory: create a custom theme under a look and feel without a theme mapping