diff --git a/CodenameOne/src/com/codename1/ui/Sheet.java b/CodenameOne/src/com/codename1/ui/Sheet.java index 69cc9b045f..01b96fd6e9 100644 --- a/CodenameOne/src/com/codename1/ui/Sheet.java +++ b/CodenameOne/src/com/codename1/ui/Sheet.java @@ -25,6 +25,7 @@ import com.codename1.ui.ComponentSelector.ComponentClosure; import com.codename1.ui.events.ActionEvent; import com.codename1.ui.events.ActionListener; +import com.codename1.ui.geom.Dimension; import com.codename1.ui.geom.Rectangle; import com.codename1.ui.layouts.BorderLayout; import com.codename1.ui.layouts.BoxLayout; @@ -119,6 +120,8 @@ public class Sheet extends Container { private static final int W = 3; private static final int C = 4; private static final int DEFAULT_TRANSITION_DURATION = 300; + private static final String SHEET_BLOCKER_PROPERTY = "cn1$sheetBlocker"; + private static final String SHEET_LAYER_PROPERTY = "cn1$sheetLayer"; private final Sheet parentSheet; private final Label title = new Label(); private final EventDispatcher closeListeners = new EventDispatcher(); @@ -458,15 +461,16 @@ public void run() { remove(); } Container cnt = CN.getCurrentForm().getFormLayeredPane(Sheet.class, true); - if (!(cnt.getLayout() instanceof BorderLayout)) { - cnt.setLayout(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE)); + if (!(cnt.getLayout() instanceof LayeredLayout)) { + cnt.setLayout(new LayeredLayout()); cnt.getStyle().setBgPainter(new ShowPainter()); cnt.revalidate(); } - if (cnt.getComponentCount() > 0) { + Container sheetLayer = getSheetLayer(cnt); + if (sheetLayer.getComponentCount() > 0) { $(".Sheet", cnt).each(new ComponentClosure() { @Override public void call(Component c) { @@ -502,17 +506,17 @@ public void call(Component c) { } }); - Component existing = cnt.getComponentAt(0); - cnt.replace(existing, this, null); - cnt.animateLayout(duration); + Component existing = sheetLayer.getComponentAt(0); + sheetLayer.replace(existing, this, null); + sheetLayer.animateLayout(duration); } else { - cnt.add(getPosition(), this); + sheetLayer.add(getPosition(), this); - this.setWidth(getPreferredW(cnt)); - this.setHeight(getPreferredH(cnt)); - this.setX(getHiddenX(cnt)); - this.setY(getHiddenY(cnt)); - cnt.animateLayout(duration); + this.setWidth(getPreferredW(sheetLayer)); + this.setHeight(getPreferredH(sheetLayer)); + this.setX(getHiddenX(sheetLayer)); + this.setY(getHiddenY(sheetLayer)); + sheetLayer.animateLayout(duration); } } @@ -788,9 +792,10 @@ public void back(int duration) { private void hide(int duration) { final Container cnt = CN.getCurrentForm().getFormLayeredPane(Sheet.class, true); - setX(getHiddenX(cnt)); - setY(getHiddenY(cnt)); - cnt.animateUnlayout(duration, 255, new Runnable() { + final Container sheetLayer = getSheetLayer(cnt); + setX(getHiddenX(sheetLayer)); + setY(getHiddenY(sheetLayer)); + sheetLayer.animateUnlayout(duration, 255, new Runnable() { @Override public void run() { Container parent = cnt.getParent(); @@ -920,6 +925,42 @@ private void fireBackEvent() { } + private Container getSheetLayer(Container cnt) { + Container sheetLayer = (Container) cnt.getClientProperty(SHEET_LAYER_PROPERTY); + if (sheetLayer == null) { + sheetLayer = new Container(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE)); + sheetLayer.setName("SheetLayer"); + cnt.putClientProperty(SHEET_LAYER_PROPERTY, sheetLayer); + ensureSheetBlocker(cnt, sheetLayer); + cnt.add(sheetLayer); + } else { + ensureSheetBlocker(cnt, sheetLayer); + } + return sheetLayer; + } + + private void ensureSheetBlocker(Container cnt, Container sheetLayer) { + Component blocker = (Component) cnt.getClientProperty(SHEET_BLOCKER_PROPERTY); + if (Form.activePeerCount <= 0) { + if (blocker != null && blocker.getParent() == cnt) { //NOPMD CompareObjectsWithEquals + cnt.removeComponent(blocker); + } + return; + } + if (blocker == null) { + blocker = new SheetBlocker(); + cnt.putClientProperty(SHEET_BLOCKER_PROPERTY, blocker); + } + if (blocker.getParent() != cnt) { //NOPMD CompareObjectsWithEquals + int sheetLayerIndex = cnt.getComponentIndex(sheetLayer); + if (sheetLayerIndex > -1) { + cnt.addComponent(sheetLayerIndex, blocker); + } else { + cnt.addComponent(0, blocker); + } + } + } + private static class ShowPainter implements Painter { @Override @@ -932,4 +973,16 @@ public void paint(Graphics g, Rectangle rect) { } } + + private static class SheetBlocker extends Container { + SheetBlocker() { + setGrabsPointerEvents(true); + setUIID("SheetBlocker"); + } + + @Override + protected Dimension calcPreferredSize() { + return new Dimension(CN.getDisplayWidth(), CN.getDisplayHeight()); + } + } } diff --git a/scripts/android/screenshots/BrowserComponent.png b/scripts/android/screenshots/BrowserComponent.png index ac97ac980e..469d7cfca9 100644 Binary files a/scripts/android/screenshots/BrowserComponent.png and b/scripts/android/screenshots/BrowserComponent.png differ diff --git a/scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/BrowserComponentScreenshotTest.java b/scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/BrowserComponentScreenshotTest.java index ff7c9e39de..4caa08eee4 100644 --- a/scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/BrowserComponentScreenshotTest.java +++ b/scripts/hellocodenameone/common/src/main/java/com/codenameone/examples/hellocodenameone/tests/BrowserComponentScreenshotTest.java @@ -2,6 +2,7 @@ import com.codename1.ui.BrowserComponent; import com.codename1.ui.Form; +import com.codename1.ui.Sheet; import com.codename1.ui.layouts.BorderLayout; import com.codename1.ui.util.UITimer; import com.codename1.util.SuccessCallback; @@ -13,6 +14,7 @@ public class BrowserComponentScreenshotTest extends BaseTest { private Form form; private boolean jsReady; private boolean jsCheckPending; + private boolean sheetShown; @Override public boolean runTest() throws Exception { @@ -56,6 +58,11 @@ public void onSucess(BrowserComponent.JSRef result) { return; } + if (!sheetShown) { + sheetShown = true; + Sheet sheet = new Sheet(null, "Browser Sheet"); + sheet.show(); + } UITimer.timer(2000, false, form, readyRunnable); readyRunnable = null; }