Search in sources :

Example 6 with ViewGroupHandler

use of com.android.tools.idea.uibuilder.api.ViewGroupHandler in project android by JetBrains.

the class ScrollInteraction method createScrollInteraction.

/**
   * Creates a new {@link ScrollInteraction} if any of the components in the component hierarchy can handle scrolling.
   * @return the {@link ScrollInteraction} or null if none of the components handle the scrolling
   */
@Nullable
public static ScrollInteraction createScrollInteraction(@NonNull ScreenView screenView, @NonNull NlComponent component) {
    NlComponent currentComponent = component;
    ScrollHandler scrollHandler = null;
    ViewEditor editor = new ViewEditorImpl(screenView);
    // Find the component that is the lowest in the hierarchy and can take the scrolling events
    while (currentComponent != null) {
        ViewGroupHandler viewGroupHandler = currentComponent.getViewGroupHandler();
        scrollHandler = viewGroupHandler != null ? viewGroupHandler.createScrollHandler(editor, currentComponent) : null;
        if (scrollHandler != null) {
            break;
        }
        currentComponent = currentComponent.getParent();
    }
    if (scrollHandler == null) {
        return null;
    }
    return new ScrollInteraction(screenView, scrollHandler);
}
Also used : ScrollHandler(com.android.tools.idea.uibuilder.api.ScrollHandler) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewEditor(com.android.tools.idea.uibuilder.api.ViewEditor) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) ViewEditorImpl(com.android.tools.idea.uibuilder.handlers.ViewEditorImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with ViewGroupHandler

use of com.android.tools.idea.uibuilder.api.ViewGroupHandler in project android by JetBrains.

the class InteractionManager method updateCursor.

/**
   * Update the cursor to show the type of operation we expect on a mouse press:
   * <ul>
   * <li>Over a selection handle, show a directional cursor depending on the position of
   * the selection handle
   * <li>Over a widget, show a move (hand) cursor
   * <li>Otherwise, show the default arrow cursor
   * </ul>
   */
void updateCursor(@SwingCoordinate int x, @SwingCoordinate int y) {
    // Set cursor for the canvas resizing interaction. If both screen views are present, only set it next to the normal one.
    // Gets the preview screen view if both are present
    ScreenView screenView = mySurface.getCurrentScreenView();
    if (screenView != null) {
        Dimension size = screenView.getSize();
        Rectangle resizeZone = new Rectangle(screenView.getX() + size.width, screenView.getY() + size.height, RESIZING_HOVERING_SIZE, RESIZING_HOVERING_SIZE);
        if (resizeZone.contains(x, y)) {
            mySurface.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
            return;
        }
    }
    // We don't hover on the root since it's not a widget per see and it is always there.
    screenView = mySurface.getScreenView(x, y);
    if (screenView == null) {
        mySurface.setCursor(null);
        return;
    }
    SelectionModel selectionModel = screenView.getSelectionModel();
    if (!selectionModel.isEmpty()) {
        // Gives a chance to the ViewGroupHandlers to update the cursor
        int mx = Coordinates.getAndroidX(screenView, x);
        int my = Coordinates.getAndroidY(screenView, y);
        if (!selectionModel.isEmpty()) {
            NlComponent primary = selectionModel.getPrimary();
            NlComponent parent = primary != null ? primary.getParent() : null;
            if (parent != null) {
                ViewGroupHandler handler = parent.getViewGroupHandler();
                if (handler != null) {
                    if (handler.updateCursor(screenView, mx, my)) {
                        return;
                    }
                }
            }
        }
        // in other cases than just for the currently selected widgets.
        for (NlComponent component : selectionModel.getSelection()) {
            ViewGroupHandler viewGroupHandler = component.getViewGroupHandler();
            if (viewGroupHandler != null) {
                if (viewGroupHandler.updateCursor(screenView, mx, my)) {
                    return;
                }
            }
        }
        int max = Coordinates.getAndroidDimension(screenView, PIXEL_RADIUS + PIXEL_MARGIN);
        SelectionHandle handle = selectionModel.findHandle(mx, my, max);
        if (handle != null) {
            Cursor cursor = handle.getCursor();
            if (cursor != mySurface.getCursor()) {
                mySurface.setCursor(cursor);
            }
            return;
        }
        // See if it's over a selected view
        NlComponent component = selectionModel.findComponent(mx, my);
        if (component == null || component.isRoot()) {
            // Finally pick any unselected component in the model under the cursor
            component = screenView.getModel().findLeafAt(mx, my, false);
        }
        if (component != null && !component.isRoot()) {
            Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
            if (cursor != mySurface.getCursor()) {
                mySurface.setCursor(cursor);
            }
            return;
        }
    } else {
        // Allow a view group handler to update the cursor
        NlComponent component = Coordinates.findComponent(screenView, x, y);
        if (component != null) {
            ViewGroupHandler viewGroupHandler = component.getViewGroupHandler();
            if (viewGroupHandler != null) {
                int mx = Coordinates.getAndroidX(screenView, x);
                int my = Coordinates.getAndroidY(screenView, y);
                if (viewGroupHandler.updateCursor(screenView, mx, my)) {
                    mySurface.repaint();
                }
            }
        }
    }
    if (!ConstraintLayoutHandler.USE_SCENE_INTERACTION) {
        mySurface.setCursor(null);
    }
}
Also used : ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler)

Example 8 with ViewGroupHandler

use of com.android.tools.idea.uibuilder.api.ViewGroupHandler in project android by JetBrains.

the class ConstraintsLayer method drawComponent.

/**
   * Draw the given component and its children
   *
   * @param gc the graphics context
   * @param component the component we want to draw
   * @param parentHandlesPainting the parent of the component already handled the painting
   *
   * @return true if the component needs a repaint (for example when running an application)
   */
private boolean drawComponent(@NotNull Graphics2D gc, @NotNull NlComponent component, boolean parentHandlesPainting) {
    if (component.viewInfo != null) {
        ViewHandler handler = component.getViewHandler();
        boolean handlesPainting = false;
        // Check if the view handler handles the painting
        if (handler != null && handler instanceof ViewGroupHandler) {
            ViewGroupHandler viewGroupHandler = (ViewGroupHandler) handler;
            if (viewGroupHandler.handlesPainting()) {
                viewGroupHandler.drawGroup(gc, myScreenView, component);
                handlesPainting = true;
            }
        }
        if (handler != null) {
            handler.paintConstraints(myScreenView, gc, component);
        }
    }
    boolean needsRepaint = false;
    // Draw the children of the component...
    for (NlComponent child : component.getChildren()) {
        needsRepaint |= drawComponent(gc, child, parentHandlesPainting);
    }
    return needsRepaint;
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler)

Example 9 with ViewGroupHandler

use of com.android.tools.idea.uibuilder.api.ViewGroupHandler in project android by JetBrains.

the class DesignSurfaceActionHandler method pasteOperation.

private boolean pasteOperation(boolean checkOnly) {
    ScreenView screenView = mySurface.getCurrentScreenView();
    if (screenView == null) {
        return false;
    }
    List<NlComponent> selection = screenView.getSelectionModel().getSelection();
    NlComponent receiver = !selection.isEmpty() ? selection.get(0) : null;
    if (receiver == null) {
        // In the case where there is no selection but we only have a root component, use that one
        List<NlComponent> components = screenView.getModel().getComponents();
        if (components.size() == 1) {
            receiver = components.get(0);
        }
    }
    if (receiver == null) {
        return false;
    }
    NlComponent before;
    NlModel model = screenView.getModel();
    ViewHandlerManager handlerManager = ViewHandlerManager.get(model.getProject());
    ViewHandler handler = handlerManager.getHandler(receiver);
    if (handler instanceof ViewGroupHandler) {
        before = receiver.getChild(0);
    } else {
        before = receiver.getNextSibling();
        receiver = receiver.getParent();
        if (receiver == null) {
            return false;
        }
    }
    DnDTransferItem item = getClipboardData();
    if (item == null) {
        return false;
    }
    InsertType insertType = model.determineInsertType(DragType.PASTE, item, checkOnly);
    List<NlComponent> pasted = model.createComponents(screenView, item, insertType);
    if (!model.canAddComponents(pasted, receiver, before)) {
        return false;
    }
    if (checkOnly) {
        return true;
    }
    model.addComponents(pasted, receiver, before, insertType);
    return true;
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) InsertType(com.android.tools.idea.uibuilder.api.InsertType)

Example 10 with ViewGroupHandler

use of com.android.tools.idea.uibuilder.api.ViewGroupHandler in project android by JetBrains.

the class SelectionLayer method parentHandlingSelection.

/**
   * Utility function that checks if the component is a child of a view group that
   * handles painting
   *
   * @param component          the component we are looking at
   * @return true if the parent container handles painting
   */
private static boolean parentHandlingSelection(@NotNull NlComponent component) {
    NlComponent parent = component.getParent();
    if (parent == null) {
        return false;
    }
    ViewInfo view = parent.viewInfo;
    if (view == null) {
        return false;
    }
    ViewHandler handler = parent.getViewHandler();
    if (handler != null && handler instanceof ViewGroupHandler) {
        ViewGroupHandler viewGroupHandler = (ViewGroupHandler) handler;
        if (viewGroupHandler.handlesPainting()) {
            return true;
        }
    }
    return false;
}
Also used : ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) ViewInfo(com.android.ide.common.rendering.api.ViewInfo)

Aggregations

ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)14 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)9 ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)6 ViewEditor (com.android.tools.idea.uibuilder.api.ViewEditor)5 ViewEditorImpl (com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)1 InsertType (com.android.tools.idea.uibuilder.api.InsertType)1 ScrollHandler (com.android.tools.idea.uibuilder.api.ScrollHandler)1 ComponentDescriptor (com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)1 ViewHandlerManager (com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)1 AdViewHandler (com.android.tools.idea.uibuilder.handlers.google.AdViewHandler)1 MapViewHandler (com.android.tools.idea.uibuilder.handlers.google.MapViewHandler)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1 FontRenderContext (java.awt.font.FontRenderContext)1 Rectangle2D (java.awt.geom.Rectangle2D)1