Search in sources :

Example 6 with Sheet

use of com.codename1.ui.Sheet in project CodenameOne by codenameone.

the class Sheet method show.

/**
 * Shows the sheet over the current form using a slide-up transition with given duration in milliseconds.
 *
 * <p>If another sheet is currently being shown, then this will replace that sheet, and use an appropriate slide
 * animation to adjust the size.</p>
 * @param duration The duration of the slide transition in milliseconds.
 * @see #show()
 */
public void show(final int duration) {
    // We need to add some margin to the title  to prevent overlap with the
    // back button and the commaneds.
    int titleMargin = Math.max(commandsContainer.getPreferredW() + commandsContainer.getStyle().getHorizontalMargins(), backButton.getPreferredW() + backButton.getStyle().getHorizontalMargins());
    // Set the padding in the content pane to match the corner radius
    Style s = getStyle();
    Style titleParentStyle = title.getParent().getStyle();
    titleParentStyle.setMarginLeft(titleMargin);
    titleParentStyle.setMarginRight(titleMargin);
    Border border = s.getBorder();
    if (border instanceof RoundRectBorder) {
        RoundRectBorder b = (RoundRectBorder) border;
        $(contentPane).setPaddingMillimeters(b.getCornerRadius());
    }
    // Deal with iPhoneX notch.
    UIManager uim = UIManager.getInstance();
    Style statusBarStyle = uim.getComponentStyle("StatusBar");
    Style titleAreaStyle = uim.getComponentStyle("TitleArea");
    int topPadding = statusBarStyle.getPaddingTop() + statusBarStyle.getPaddingBottom() + titleAreaStyle.getPaddingTop();
    int positionInt = getPositionInt();
    Rectangle displaySafeArea = new Rectangle();
    Display.getInstance().getDisplaySafeArea(displaySafeArea);
    int bottomPadding = s.getPaddingBottom();
    int safeAreaBottomPadding = CN.getDisplayHeight() - (displaySafeArea.getY() + displaySafeArea.getHeight());
    bottomPadding = bottomPadding + safeAreaBottomPadding;
    if (positionInt == S || positionInt == C) {
        // For Center and South position we use margin to
        // prevent overlap with top notch.  This looks better as overlap is only
        // an edge case that occurs when the sheet is the full screen height.
        $(this).setMargin(topPadding, 0, 0, 0);
        $(this).setPadding(s.getPaddingTop(), s.getPaddingRightNoRTL(), bottomPadding, s.getPaddingLeftNoRTL());
    } else {
        // For other cases we use padding to prevent overlap with top notch.  This looks
        // better as it appears that the sheet bleeds all the way to the top edge of the screen,
        // but the content is not obscured by the notch.
        $(this).setPadding(topPadding, s.getPaddingRightNoRTL(), bottomPadding, s.getPaddingLeftNoRTL());
    }
    // END Deal with iPhoneX notch
    Form f = CN.getCurrentForm();
    if (f.getAnimationManager().isAnimating()) {
        f.getAnimationManager().flushAnimation(new Runnable() {

            public void run() {
                show(duration);
            }
        });
        return;
    }
    if (getParent() != null) {
        remove();
    }
    Container cnt = CN.getCurrentForm().getFormLayeredPane(Sheet.class, true);
    if (!(cnt.getLayout() instanceof BorderLayout)) {
        cnt.setLayout(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
        cnt.getStyle().setBgPainter(new Painter() {

            @Override
            public void paint(Graphics g, Rectangle rect) {
                int alph = g.getAlpha();
                g.setAlpha((int) (alph * 30 / 100.0));
                g.setColor(0x0);
                g.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
                g.setAlpha(alph);
            }
        });
        cnt.revalidate();
    }
    if (cnt.getComponentCount() > 0) {
        $(".Sheet", cnt).each(new ComponentClosure() {

            @Override
            public void call(Component c) {
                if (c instanceof Sheet) {
                    Sheet s = (Sheet) c;
                    if (s.isAncestorSheetOf(Sheet.this) || s == Sheet.this) {
                        // via a back chain
                        return;
                    }
                    s.fireCloseEvent(false);
                    // Hiding this sheet may eliminate the possibility of
                    // its parent sheets from being shown again,
                    // so their close events should also be fired in this case.
                    Sheet sp = s.getParentSheet();
                    while (sp != null) {
                        if (sp == Sheet.this) {
                            break;
                        }
                        if (!sp.isAncestorSheetOf(Sheet.this)) {
                            sp.fireCloseEvent(false);
                        }
                        sp = sp.getParentSheet();
                    }
                }
            }
        });
        Component existing = cnt.getComponentAt(0);
        cnt.replace(existing, this, null);
        cnt.animateLayout(duration);
    } else {
        cnt.add(getPosition(), this);
        this.setWidth(getPreferredW(cnt));
        this.setHeight(getPreferredH(cnt));
        this.setX(getHiddenX(cnt));
        this.setY(getHiddenY(cnt));
        cnt.animateLayout(duration);
    }
}
Also used : RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) Rectangle(com.codename1.ui.geom.Rectangle) UIManager(com.codename1.ui.plaf.UIManager) BorderLayout(com.codename1.ui.layouts.BorderLayout) ComponentClosure(com.codename1.ui.ComponentSelector.ComponentClosure) Style(com.codename1.ui.plaf.Style) RoundRectBorder(com.codename1.ui.plaf.RoundRectBorder) Border(com.codename1.ui.plaf.Border)

Example 7 with Sheet

use of com.codename1.ui.Sheet in project CodenameOne by codenameone.

the class JSDialogHandler method onJSDialog.

@Override
public boolean onJSDialog(final CefBrowser browser, final String origin_url, final JSDialogType dialog_type, final String message_text, final String default_prompt_text, final CefJSDialogCallback callback, BoolRef suppress_message) {
    switch(dialog_type) {
        case JSDIALOGTYPE_ALERT:
            CN.callSerially(new Runnable() {

                public void run() {
                    final Sheet sheet = new Sheet(null, origin_url + " says");
                    sheet.setPosition(com.codename1.ui.layouts.BorderLayout.CENTER);
                    SpanLabel lbl = new SpanLabel(message_text);
                    sheet.getContentPane().setLayout(new BorderLayout());
                    sheet.getContentPane().add(BorderLayout.CENTER, lbl);
                    sheet.addCloseListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            EventQueue.invokeLater(new Runnable() {

                                public void run() {
                                    callback.Continue(true, "");
                                }
                            });
                        }
                    });
                    Button btn = new Button("OK");
                    btn.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            sheet.back();
                        }
                    });
                    sheet.getContentPane().add(BorderLayout.SOUTH, btn);
                    sheet.show();
                // Dialog.show(origin_url+" says", message_text, "OK", null);
                // callback.Continue(true, "");
                }
            });
            return true;
        case JSDIALOGTYPE_CONFIRM:
            CN.callSerially(new Runnable() {

                public void run() {
                    final Sheet sheet = new Sheet(null, origin_url + " says");
                    sheet.setPosition(com.codename1.ui.layouts.BorderLayout.CENTER);
                    SpanLabel lbl = new SpanLabel(message_text);
                    sheet.getContentPane().setLayout(new BorderLayout());
                    sheet.getContentPane().add(BorderLayout.CENTER, lbl);
                    final boolean[] callbackComplete = new boolean[1];
                    sheet.addCloseListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            EventQueue.invokeLater(new Runnable() {

                                public void run() {
                                    if (callbackComplete[0]) {
                                        return;
                                    }
                                    callbackComplete[0] = true;
                                    callback.Continue(false, "");
                                }
                            });
                        }
                    });
                    Button okBtn = new Button("OK");
                    okBtn.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            EventQueue.invokeLater(new Runnable() {

                                public void run() {
                                    if (callbackComplete[0]) {
                                        return;
                                    }
                                    callbackComplete[0] = true;
                                    callback.Continue(true, "");
                                }
                            });
                            sheet.back();
                        }
                    });
                    Button cancelBtn = new Button("Cancel");
                    cancelBtn.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            sheet.back();
                        }
                    });
                    sheet.getContentPane().add(BorderLayout.SOUTH, GridLayout.encloseIn(2, okBtn, cancelBtn));
                    sheet.show();
                // Dialog.show(origin_url+" says", message_text, "OK", null);
                // callback.Continue(true, "");
                }
            });
            return true;
        case JSDIALOGTYPE_PROMPT:
            CN.callSerially(new Runnable() {

                public void run() {
                    final Sheet sheet = new Sheet(null, origin_url + " says");
                    sheet.setPosition(com.codename1.ui.layouts.BorderLayout.CENTER);
                    SpanLabel lbl = new SpanLabel(message_text);
                    sheet.getContentPane().setLayout(new BorderLayout());
                    final TextField input = new TextField();
                    if (default_prompt_text != null) {
                        input.setText(default_prompt_text);
                    }
                    sheet.getContentPane().add(BorderLayout.CENTER, BoxLayout.encloseY(lbl, input));
                    final boolean[] callbackComplete = new boolean[1];
                    sheet.addCloseListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            EventQueue.invokeLater(new Runnable() {

                                public void run() {
                                    if (callbackComplete[0]) {
                                        return;
                                    }
                                    callbackComplete[0] = true;
                                    callback.Continue(false, null);
                                }
                            });
                        }
                    });
                    Button okBtn = new Button("OK");
                    okBtn.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            EventQueue.invokeLater(new Runnable() {

                                public void run() {
                                    if (callbackComplete[0]) {
                                        return;
                                    }
                                    callbackComplete[0] = true;
                                    callback.Continue(true, input.getText());
                                }
                            });
                            sheet.back();
                        }
                    });
                    Button cancelBtn = new Button("Cancel");
                    cancelBtn.addActionListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            sheet.back();
                        }
                    });
                    sheet.getContentPane().add(BorderLayout.SOUTH, GridLayout.encloseIn(2, okBtn, cancelBtn));
                    sheet.show();
                    input.requestFocus();
                // Dialog.show(origin_url+" says", message_text, "OK", null);
                // callback.Continue(true, "");
                }
            });
            return true;
    }
    if (message_text.equalsIgnoreCase("Never displayed")) {
        suppress_message.set(true);
        System.out.println("The " + dialog_type + " from origin \"" + origin_url + "\" was suppressed.");
        System.out.println("   The content of the suppressed dialog was: \"" + message_text + "\"");
    }
    return false;
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Button(com.codename1.ui.Button) ActionEvent(com.codename1.ui.events.ActionEvent) TextField(com.codename1.ui.TextField) SpanLabel(com.codename1.components.SpanLabel) Sheet(com.codename1.ui.Sheet)

Aggregations

BorderLayout (com.codename1.ui.layouts.BorderLayout)5 Sheet (com.codename1.ui.Sheet)4 Button (com.codename1.ui.Button)3 Form (com.codename1.ui.Form)3 ActionEvent (com.codename1.ui.events.ActionEvent)3 ActionListener (com.codename1.ui.events.ActionListener)3 AudioRecorderComponent (com.codename1.components.AudioRecorderComponent)2 SpanLabel (com.codename1.components.SpanLabel)2 FileSystemStorage (com.codename1.io.FileSystemStorage)2 MediaRecorderBuilder (com.codename1.media.MediaRecorderBuilder)2 RadioButtonList (com.codename1.components.RadioButtonList)1 BufferedOutputStream (com.codename1.io.BufferedOutputStream)1 File (com.codename1.io.File)1 com.codename1.ui (com.codename1.ui)1 BrowserComponent (com.codename1.ui.BrowserComponent)1 Component (com.codename1.ui.Component)1 ComponentClosure (com.codename1.ui.ComponentSelector.ComponentClosure)1 Graphics (com.codename1.ui.Graphics)1 Image (com.codename1.ui.Image)1 Label (com.codename1.ui.Label)1