From 19f2c9f06e9e9018f056858dcd98cdc328b43571 Mon Sep 17 00:00:00 2001 From: Danil Sokolov Date: Mon, 23 Nov 2026 19:25:06 +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. An explicit pixel location set in the meantime replaces the waiting percentage. Fixes #33 --- .../docking/defaults/DockingSplitPane.java | 16 ++++++++ .../test/docking/DockingSplitPaneTest.java | 38 +++++++++++++++++++ .../test/docking/SplitPersistenceTest.java | 20 ++++------ release-notes.txt | 7 ++-- 4 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 flexdock-view/src/test/java/org/flexdock/test/docking/DockingSplitPaneTest.java 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 2fff5431..14108ab1 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,12 +198,23 @@ 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); } setResizeWeight(percent); } + @Override + public void setDividerLocation(int location) { + // an explicit location replaces a percentage that still waits for the size + percentPending = false; + super.setDividerLocation(location); + } + public boolean isConstantPercent() { return constantPercent; } diff --git a/flexdock-view/src/test/java/org/flexdock/test/docking/DockingSplitPaneTest.java b/flexdock-view/src/test/java/org/flexdock/test/docking/DockingSplitPaneTest.java new file mode 100644 index 00000000..77274ca8 --- /dev/null +++ b/flexdock-view/src/test/java/org/flexdock/test/docking/DockingSplitPaneTest.java @@ -0,0 +1,38 @@ +package org.flexdock.test.docking; + +import org.flexdock.docking.DockingConstants; +import org.flexdock.docking.defaults.DefaultDockingPort; +import org.flexdock.docking.defaults.DockingSplitPane; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DockingSplitPaneTest { + + private static DockingSplitPane createUnsizedSplit() { + return new DockingSplitPane(new DefaultDockingPort(), DockingConstants.WEST_REGION); + } + + @Test + void testPercentSetBeforeSizeIsAppliedOnceSized() { + DockingSplitPane split = createUnsizedSplit(); + + split.setDividerLocation(.3); + split.setBounds(0, 0, 1000, 600); + + int expected = (int) ((1000 - split.getDividerSize()) * .3); + assertEquals(expected, split.getDividerLocation()); + } + + @Test + void testIntLocationReplacesPendingPercent() { + DockingSplitPane split = createUnsizedSplit(); + + split.setDividerLocation(.3); + split.setDividerLocation(100); + split.setBounds(0, 0, 1000, 600); + + assertEquals(100, split.getDividerLocation()); + } + +} 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 11b23da1..cfeef65b 100644 --- a/release-notes.txt +++ b/release-notes.txt @@ -20,9 +20,10 @@ Fixes: * DefaultDockingPort: clear() also clears the nested docking ports * DockingStateListener: validate pending layout before capturing docking state * PlafManager: keep the preferred theme after a look and feel change -* 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: track the divider percentage from the first drag and + apply it consistently in setDividerLocation() +* DockingSplitPane: apply a percentage set before the size is known once it is, + also without constant percent, so imported layouts keep their 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