From 3bd5ff3e5f5378c6eb9f521ec9ae713828aba5f8 Mon Sep 17 00:00:00 2001 From: Steve Hannah Date: Mon, 13 Nov 2023 07:03:41 -0800 Subject: [PATCH] fixed bug in Style that was causing unexpected results in statusbar Previously the padding units or margin units to top/left/bottom/right on a proxy style, were not being applied to the underlying styles. Now they are. --- CodenameOne/src/com/codename1/ui/Toolbar.java | 20 +++-- .../src/com/codename1/ui/plaf/Style.java | 48 ++++++++++ .../com/codename1/ui/plaf/StyleProxyTest.java | 88 +++++++++++++++++++ .../java/com/codename1/ui/plaf/StyleTest.java | 81 +++++++++++++++++ 4 files changed, 232 insertions(+), 5 deletions(-) create mode 100644 maven/core-unittests/src/test/java/com/codename1/ui/plaf/StyleProxyTest.java create mode 100644 maven/core-unittests/src/test/java/com/codename1/ui/plaf/StyleTest.java diff --git a/CodenameOne/src/com/codename1/ui/Toolbar.java b/CodenameOne/src/com/codename1/ui/Toolbar.java index bf485edfd8..ac531c9e92 100644 --- a/CodenameOne/src/com/codename1/ui/Toolbar.java +++ b/CodenameOne/src/com/codename1/ui/Toolbar.java @@ -2250,18 +2250,24 @@ protected void initTitleBarStatus() { } else { bar.setUIID("StatusBar"); } - bar.setSafeArea(true); - // Safe areas will be applied to the top padding, so we would prefer if all of the status - // bar margins and padding were on the the top, so that they will be adjusted correctly - // by safe areas. - // And we don't want any additional spacing at the bottom of the status bar. reallocateVerticalPaddingAndMarginsToTop(bar); + bar.setSafeArea(true); addComponent(BorderLayout.NORTH, bar); } } else { setSafeArea(true); } } + + /** + * Reallocates vertical padding and margins to be all on top, so that bottom padding/margin + * is zero, but the total padding/margin is the same. + * + * This is helpful for the status bar so that, when the padding is adjusted by the safeArea + * we don't end up with extra padding at the bottom of the component, which would increase + * the height of the status bar. + * @param cmp + */ private void reallocateVerticalPaddingAndMarginsToTop(Component cmp) { Style allStyles = cmp.getAllStyles(); Style style = cmp.getStyle(); @@ -2269,6 +2275,10 @@ private void reallocateVerticalPaddingAndMarginsToTop(Component cmp) { int topMargin = style.getMarginTop(); int bottomPadding = style.getPaddingBottom(); int bottomMargin = style.getMarginBottom(); + allStyles.setPaddingUnitTop(Style.UNIT_TYPE_PIXELS); + allStyles.setMarginUnitTop(Style.UNIT_TYPE_PIXELS); + allStyles.setPaddingUnitBottom(Style.UNIT_TYPE_PIXELS); + allStyles.setMarginUnitBottom(Style.UNIT_TYPE_PIXELS); allStyles.setPaddingTop(topPadding + bottomPadding); allStyles.setMarginTop(topMargin + bottomMargin); allStyles.setPaddingBottom(0); diff --git a/CodenameOne/src/com/codename1/ui/plaf/Style.java b/CodenameOne/src/com/codename1/ui/plaf/Style.java index f01be2127a..8bbe946f09 100644 --- a/CodenameOne/src/com/codename1/ui/plaf/Style.java +++ b/CodenameOne/src/com/codename1/ui/plaf/Style.java @@ -1809,6 +1809,12 @@ private void initPaddingUnits() { * @since 7.0 */ public void setPaddingUnitLeft(byte unit) { + if(proxyTo != null) { + for(Style s : proxyTo) { + s.setPaddingUnitLeft(unit); + } + return; + } initPaddingUnits(); paddingUnit[Component.LEFT] = unit; } @@ -1820,6 +1826,12 @@ public void setPaddingUnitLeft(byte unit) { * @since 7.0 */ public void setPaddingUnitRight(byte unit) { + if(proxyTo != null) { + for(Style s : proxyTo) { + s.setPaddingUnitRight(unit); + } + return; + } initPaddingUnits(); paddingUnit[Component.RIGHT] = unit; } @@ -1831,6 +1843,12 @@ public void setPaddingUnitRight(byte unit) { * @since 7.0 */ public void setPaddingUnitTop(byte unit) { + if(proxyTo != null) { + for(Style s : proxyTo) { + s.setPaddingUnitTop(unit); + } + return; + } initPaddingUnits(); paddingUnit[Component.TOP] = unit; } @@ -1842,6 +1860,12 @@ public void setPaddingUnitTop(byte unit) { * @since 7.0 */ public void setPaddingUnitBottom(byte unit) { + if(proxyTo != null) { + for(Style s : proxyTo) { + s.setPaddingUnitBottom(unit); + } + return; + } initPaddingUnits(); paddingUnit[Component.BOTTOM] = unit; } @@ -3027,6 +3051,12 @@ private void initMarginUnits() { * @since 7.0 */ public void setMarginUnitLeft(byte unit) { + if(proxyTo != null) { + for(Style s : proxyTo) { + s.setMarginUnitLeft(unit); + } + return; + } initMarginUnits(); marginUnit[Component.LEFT] = unit; } @@ -3038,6 +3068,12 @@ public void setMarginUnitLeft(byte unit) { * @since 7.0 */ public void setMarginUnitRight(byte unit) { + if(proxyTo != null) { + for(Style s : proxyTo) { + s.setMarginUnitRight(unit); + } + return; + } initMarginUnits(); marginUnit[Component.RIGHT] = unit; } @@ -3049,6 +3085,12 @@ public void setMarginUnitRight(byte unit) { * @since 7.0 */ public void setMarginUnitTop(byte unit) { + if(proxyTo != null) { + for(Style s : proxyTo) { + s.setMarginUnitTop(unit); + } + return; + } initMarginUnits(); marginUnit[Component.TOP] = unit; } @@ -3060,6 +3102,12 @@ public void setMarginUnitTop(byte unit) { * @since 7.0 */ public void setMarginUnitBottom(byte unit) { + if(proxyTo != null) { + for(Style s : proxyTo) { + s.setMarginUnitBottom(unit); + } + return; + } initMarginUnits(); marginUnit[Component.BOTTOM] = unit; } diff --git a/maven/core-unittests/src/test/java/com/codename1/ui/plaf/StyleProxyTest.java b/maven/core-unittests/src/test/java/com/codename1/ui/plaf/StyleProxyTest.java new file mode 100644 index 0000000000..8aa82cbfb3 --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/ui/plaf/StyleProxyTest.java @@ -0,0 +1,88 @@ +package com.codename1.ui.plaf; + +import com.codename1.ui.Component; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class StyleProxyTest { + @Test + public void testSetMarginUnitLeft() { + Style s = new Style(); + Style proxy = Style.createProxyStyle(s); + proxy.setMarginUnitLeft(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getMarginUnit()[Component.LEFT]); + proxy.setMarginUnitLeft(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getMarginUnit()[Component.LEFT]); + } + + @Test + public void testSetMarginUnitRight() { + Style s = new Style(); + Style proxy = Style.createProxyStyle(s); + proxy.setMarginUnitRight(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getMarginUnit()[Component.RIGHT]); + proxy.setMarginUnitRight(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getMarginUnit()[Component.RIGHT]); + } + + @Test + public void testSetMarginUnitBottom() { + Style s = new Style(); + Style proxy = Style.createProxyStyle(s); + proxy.setMarginUnitBottom(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getMarginUnit()[Component.BOTTOM]); + proxy.setMarginUnitBottom(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getMarginUnit()[Component.BOTTOM]); + } + + @Test + public void testSetMarginUnitTop() { + Style s = new Style(); + Style proxy = Style.createProxyStyle(s); + proxy.setMarginUnitTop(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getMarginUnit()[Component.TOP]); + proxy.setMarginUnitTop(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getMarginUnit()[Component.TOP]); + } + + @Test + public void testSetPaddingUnitTop() { + Style s = new Style(); + Style proxy = Style.createProxyStyle(s); + proxy.setPaddingUnitTop(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getPaddingUnit()[Component.TOP]); + proxy.setPaddingUnitTop(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getPaddingUnit()[Component.TOP]); + } + + @Test + public void testSetPaddingUnitBottom() { + Style s = new Style(); + Style proxy = Style.createProxyStyle(s); + proxy.setPaddingUnitBottom(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getPaddingUnit()[Component.BOTTOM]); + proxy.setPaddingUnitBottom(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getPaddingUnit()[Component.BOTTOM]); + } + + @Test + public void testSetPaddingUnitLeft() { + Style s = new Style(); + Style proxy = Style.createProxyStyle(s); + proxy.setPaddingUnitLeft(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getPaddingUnit()[Component.LEFT]); + proxy.setPaddingUnitLeft(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getPaddingUnit()[Component.LEFT]); + } + + @Test + public void testSetPaddingUnitRight() { + Style s = new Style(); + Style proxy = Style.createProxyStyle(s); + proxy.setPaddingUnitRight(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getPaddingUnit()[Component.RIGHT]); + proxy.setPaddingUnitRight(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getPaddingUnit()[Component.RIGHT]); + } +} diff --git a/maven/core-unittests/src/test/java/com/codename1/ui/plaf/StyleTest.java b/maven/core-unittests/src/test/java/com/codename1/ui/plaf/StyleTest.java new file mode 100644 index 0000000000..2b044651bd --- /dev/null +++ b/maven/core-unittests/src/test/java/com/codename1/ui/plaf/StyleTest.java @@ -0,0 +1,81 @@ +package com.codename1.ui.plaf; + +import com.codename1.ui.Component; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class StyleTest { + @Test + public void testSetMarginUnitLeft() { + Style s = new Style(); + s.setMarginUnitLeft(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getMarginUnit()[Component.LEFT]); + s.setMarginUnitLeft(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getMarginUnit()[Component.LEFT]); + } + + @Test + public void testSetMarginUnitRight() { + Style s = new Style(); + s.setMarginUnitRight(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getMarginUnit()[Component.RIGHT]); + s.setMarginUnitRight(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getMarginUnit()[Component.RIGHT]); + } + + @Test + public void testSetMarginUnitBottom() { + Style s = new Style(); + s.setMarginUnitBottom(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getMarginUnit()[Component.BOTTOM]); + s.setMarginUnitBottom(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getMarginUnit()[Component.BOTTOM]); + } + + @Test + public void testSetMarginUnitTop() { + Style s = new Style(); + s.setMarginUnitTop(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getMarginUnit()[Component.TOP]); + s.setMarginUnitTop(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getMarginUnit()[Component.TOP]); + } + + @Test + public void testSetPaddingUnitTop() { + Style s = new Style(); + s.setPaddingUnitTop(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getPaddingUnit()[Component.TOP]); + s.setPaddingUnitTop(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getPaddingUnit()[Component.TOP]); + } + + @Test + public void testSetPaddingUnitBottom() { + Style s = new Style(); + s.setPaddingUnitBottom(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getPaddingUnit()[Component.BOTTOM]); + s.setPaddingUnitBottom(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getPaddingUnit()[Component.BOTTOM]); + } + + @Test + public void testSetPaddingUnitLeft() { + Style s = new Style(); + s.setPaddingUnitLeft(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getPaddingUnit()[Component.LEFT]); + s.setPaddingUnitLeft(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getPaddingUnit()[Component.LEFT]); + } + + @Test + public void testSetPaddingUnitRight() { + Style s = new Style(); + s.setPaddingUnitRight(Style.UNIT_TYPE_PIXELS); + assertEquals(Style.UNIT_TYPE_PIXELS, s.getPaddingUnit()[Component.RIGHT]); + s.setPaddingUnitRight(Style.UNIT_TYPE_DIPS); + assertEquals(Style.UNIT_TYPE_DIPS, s.getPaddingUnit()[Component.RIGHT]); + } + +}