Search in sources :

Example 1 with Window

use of elemental.html.Window in project che by eclipse.

the class PopupWidget method show.

/**
     * Show the widget at the given document position.
     * @param left the horizontal pixel position in the document
     * @param top the vertical pixel position in the document
     */
public void show(final float left, final float top) {
    if (!listElement.hasChildNodes()) {
        Element emptyElement = Elements.createLiElement(popupResources.popupStyle().item());
        emptyElement.setTextContent(getEmptyMessage());
        listElement.appendChild(emptyElement);
        return;
    }
    /* Reset popup dimensions and show. */
    popupElement.getStyle().setLeft(left, PX);
    popupElement.getStyle().setTop(top, PX);
    popupElement.getStyle().setWidth("" + MIN_WIDTH + "px");
    popupElement.getStyle().setHeight("" + MIN_HEIGHT + "px");
    popupElement.getStyle().setOpacity(0);
    Elements.getDocument().getBody().appendChild(popupElement);
    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {

        @Override
        public void execute() {
            popupElement.getStyle().setOpacity(1);
        }
    });
    Elements.getDocument().addEventListener(Event.MOUSEDOWN, popupListener, false);
    // does it fit inside the doc body?
    // This does exactly the same thing for height/top and width/left
    final Window window = Elements.getWindow();
    final int winX = window.getInnerWidth();
    final int winY = window.getInnerHeight();
    ClientRect widgetRect = this.popupElement.getBoundingClientRect();
    if (widgetRect.getBottom() > winY) {
        // it doesn't fit
        final float overflow = widgetRect.getBottom() - winY;
        if (widgetRect.getHeight() - overflow > MIN_HEIGHT) {
            // the widget can be shrunk to fit
            this.popupElement.getStyle().setHeight(widgetRect.getHeight() - overflow, PX);
        } else {
            // we need to shrink AND move the widget up
            this.popupElement.getStyle().setHeight(MIN_HEIGHT, PX);
            final int newTop = Math.max(winY - MIN_HEIGHT, MIN_HEIGHT);
            this.popupElement.getStyle().setTop(newTop, PX);
        }
    }
    // bounding rect has changed
    widgetRect = this.popupElement.getBoundingClientRect();
    if (widgetRect.getRight() > winX) {
        // it doesn't fit
        final float overflow = widgetRect.getRight() - winX;
        if (widgetRect.getWidth() - overflow > MIN_WIDTH) {
            // the widget can be shrunk to fit
            this.popupElement.getStyle().setWidth(widgetRect.getWidth() - overflow, PX);
        } else {
            // we need to shrink AND move the widget up
            this.popupElement.getStyle().setWidth(MIN_WIDTH, PX);
            final int newLeft = Math.max(winX - MIN_WIDTH, MIN_WIDTH);
            this.popupElement.getStyle().setLeft(newLeft - MIN_WIDTH, PX);
        }
    }
    if (needsFocus()) {
        // save previous focus and set focus in popup
        previousFocus = Elements.getDocument().getActiveElement();
        listElement.getFirstElementChild().focus();
    }
    // add key event listener on popup
    listElement.addEventListener(Event.KEYDOWN, keyboardListener, false);
}
Also used : Window(elemental.html.Window) Scheduler(com.google.gwt.core.client.Scheduler) Element(elemental.dom.Element) ClientRect(elemental.html.ClientRect)

Example 2 with Window

use of elemental.html.Window in project che by eclipse.

the class ContentAssistWidget method show.

/**
     * Displays assist popup relative to the current cursor position.
     *
     * @param proposals
     *         proposals to display
     */
public void show(final List<CompletionProposal> proposals) {
    this.proposals = proposals;
    OrionTextViewOverlay textView = textEditor.getTextView();
    OrionPixelPositionOverlay caretLocation = textView.getLocationAtOffset(textView.getCaretOffset());
    caretLocation.setY(caretLocation.getY() + textView.getLineHeight());
    caretLocation = textView.convert(caretLocation, "document", "page");
    /** The fastest way to remove element children. Clear and add items. */
    listElement.setInnerHTML("");
    /* Display an empty popup when it is nothing to show. */
    if (getTotalItems() == 0) {
        final Element emptyElement = Elements.createLiElement(popupResources.popupStyle().item());
        emptyElement.setTextContent("No proposals");
        listElement.appendChild(emptyElement);
        return;
    }
    /* Automatically apply the completion proposal if it only one. */
    if (getTotalItems() == 1) {
        applyProposal(proposals.get(0));
        return;
    }
    /* Reset popup dimensions and show. */
    popupElement.getStyle().setLeft(caretLocation.getX(), PX);
    popupElement.getStyle().setTop(caretLocation.getY(), PX);
    popupElement.getStyle().setWidth("400px");
    popupElement.getStyle().setHeight("200px");
    popupElement.getStyle().setOpacity(1);
    Elements.getDocument().getBody().appendChild(this.popupElement);
    /* Add the top extra row. */
    setExtraRowHeight(appendExtraRow(), 0);
    /* Add the popup items. */
    for (int i = 0; i < Math.min(DOM_ITEMS_SIZE, getTotalItems()); i++) {
        listElement.appendChild(createProposalPopupItem(i));
    }
    /* Add the bottom extra row. */
    setExtraRowHeight(appendExtraRow(), Math.max(0, getTotalItems() - DOM_ITEMS_SIZE));
    /* Correct popup position (wants to be refactored) */
    final Window window = Elements.getWindow();
    final int viewportWidth = window.getInnerWidth();
    final int viewportHeight = window.getInnerHeight();
    int spaceBelow = viewportHeight - caretLocation.getY();
    if (this.popupElement.getOffsetHeight() > spaceBelow) {
        // Check if div is too large to fit above
        int spaceAbove = caretLocation.getY() - textView.getLineHeight();
        if (this.popupElement.getOffsetHeight() > spaceAbove) {
            // Squeeze the div into the larger area
            if (spaceBelow > spaceAbove) {
                this.popupElement.getStyle().setProperty("maxHeight", spaceBelow + "px");
            } else {
                this.popupElement.getStyle().setProperty("maxHeight", spaceAbove + "px");
                this.popupElement.getStyle().setTop("0");
            }
        } else {
            // Put the div above the line
            this.popupElement.getStyle().setTop((caretLocation.getY() - this.popupElement.getOffsetHeight() - textView.getLineHeight()) + "px");
            this.popupElement.getStyle().setProperty("maxHeight", spaceAbove + "px");
        }
    } else {
        this.popupElement.getStyle().setProperty("maxHeight", spaceBelow + "px");
    }
    if (caretLocation.getX() + this.popupElement.getOffsetWidth() > viewportWidth) {
        int leftSide = viewportWidth - this.popupElement.getOffsetWidth();
        if (leftSide < 0) {
            leftSide = 0;
        }
        this.popupElement.getStyle().setLeft(leftSide + "px");
        this.popupElement.getStyle().setProperty("maxWidth", (viewportWidth - leftSide) + "px");
    } else {
        this.popupElement.getStyle().setProperty("maxWidth", viewportWidth + caretLocation.getX() + "px");
    }
    /* Don't attach handlers twice. Visible popup must already have their attached. */
    if (!visible) {
        addPopupEventListeners();
    }
    /* Indicates the codeassist is visible. */
    visible = true;
    focused = false;
    /* Update documentation popup position */
    docPopup.getElement().getStyle().setLeft(popupElement.getOffsetLeft() + popupElement.getOffsetWidth() + 3, Style.Unit.PX);
    docPopup.getElement().getStyle().setTop(popupElement.getOffsetTop(), Style.Unit.PX);
    /* Select first row. */
    selectFirst();
}
Also used : Window(elemental.html.Window) Element(elemental.dom.Element) SpanElement(elemental.html.SpanElement) OrionTextViewOverlay(org.eclipse.che.ide.editor.orion.client.jso.OrionTextViewOverlay) OrionPixelPositionOverlay(org.eclipse.che.ide.editor.orion.client.jso.OrionPixelPositionOverlay)

Aggregations

Element (elemental.dom.Element)2 Window (elemental.html.Window)2 Scheduler (com.google.gwt.core.client.Scheduler)1 ClientRect (elemental.html.ClientRect)1 SpanElement (elemental.html.SpanElement)1 OrionPixelPositionOverlay (org.eclipse.che.ide.editor.orion.client.jso.OrionPixelPositionOverlay)1 OrionTextViewOverlay (org.eclipse.che.ide.editor.orion.client.jso.OrionTextViewOverlay)1