Skip to content

Commit

Permalink
fixed bug in Style that was causing unexpected results in statusbar
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shannah committed Nov 13, 2023
1 parent ea026fe commit 3bd5ff3
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 5 deletions.
20 changes: 15 additions & 5 deletions CodenameOne/src/com/codename1/ui/Toolbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -2250,25 +2250,35 @@ 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();
int topPadding = style.getPaddingTop();
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);
Expand Down
48 changes: 48 additions & 0 deletions CodenameOne/src/com/codename1/ui/plaf/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
}
}
Original file line number Diff line number Diff line change
@@ -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]);
}

}

0 comments on commit 3bd5ff3

Please sign in to comment.