Search in sources :

Example 6 with Monitor

use of org.eclipse.swt.widgets.Monitor in project eclipse.platform.text by eclipse.

the class AbstractInformationControlManager method computeInformationControlLocation.

/**
 * Computes the location of the information control depending on the
 * subject area and the size of the information control. This method attempts
 * to find a location at which the information control lies completely in the display's
 * client area while honoring the manager's default anchor. If this isn't possible using the
 * default anchor, the fallback anchors are tried out.
 *
 * @param subjectArea the information area
 * @param controlSize the size of the information control
 * @return the computed location of the information control
 */
protected Point computeInformationControlLocation(Rectangle subjectArea, Point controlSize) {
    Rectangle subjectAreaDisplayRelative = Geometry.toDisplay(fSubjectControl, subjectArea);
    Point upperLeft;
    Anchor testAnchor = fAnchor;
    Rectangle bestBounds = null;
    int bestArea = Integer.MIN_VALUE;
    Anchor bestAnchor = null;
    do {
        upperLeft = computeLocation(subjectArea, controlSize, testAnchor);
        Monitor monitor = getClosestMonitor(subjectAreaDisplayRelative, testAnchor);
        if (updateLocation(upperLeft, controlSize, monitor.getClientArea(), testAnchor))
            return upperLeft;
        // compute available area for this anchor and update if better than best
        Rectangle available = computeAvailableArea(subjectAreaDisplayRelative, monitor.getClientArea(), testAnchor);
        Rectangle proposed = new Rectangle(upperLeft.x, upperLeft.y, controlSize.x, controlSize.y);
        available.intersect(proposed);
        int area = available.width * available.height;
        if (area > bestArea) {
            bestArea = area;
            bestBounds = available;
            bestAnchor = testAnchor;
        }
        testAnchor = getNextFallbackAnchor(testAnchor);
    } while (testAnchor != fAnchor && testAnchor != null);
    // no anchor is perfect - select the one with larges area and set the size to not overlap with the subjectArea
    if (bestAnchor != ANCHOR_GLOBAL)
        Geometry.set(controlSize, Geometry.getSize(bestBounds));
    return Geometry.getLocation(bestBounds);
}
Also used : Monitor(org.eclipse.swt.widgets.Monitor) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Example 7 with Monitor

use of org.eclipse.swt.widgets.Monitor in project n4js by eclipse.

the class UIUtils method getClosestMonitor.

/**
 * Returns the monitor whose client area contains the given point. If no monitor contains the point, returns the
 * monitor that is closest to the point.
 *
 * @param toSearch
 *            point to find (display coordinates).
 * @param toFind
 *            point to find (display coordinates).
 * @return the monitor closest to the given point.
 */
private static Monitor getClosestMonitor(final Display toSearch, final Point toFind) {
    int closest = Integer.MAX_VALUE;
    final Monitor[] monitors = toSearch.getMonitors();
    Monitor result = monitors[0];
    for (int index = 0; index < monitors.length; index++) {
        final Monitor current = monitors[index];
        final Rectangle clientArea = current.getClientArea();
        if (clientArea.contains(toFind)) {
            return current;
        }
        final int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), toFind);
        if (distance < closest) {
            closest = distance;
            result = current;
        }
    }
    return result;
}
Also used : Monitor(org.eclipse.swt.widgets.Monitor) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point)

Example 8 with Monitor

use of org.eclipse.swt.widgets.Monitor in project n4js by eclipse.

the class UIUtils method getConstrainedShellBounds.

/**
 * Given the desired position of the shell, this method returns an adjusted position such that the window is no
 * larger than its monitor, and does not extend beyond the edge of the monitor. This is used for computing the
 * initial window position via the parent shell, clients can use this as a utility method if they want to limit the
 * region in which the window may be moved.
 *
 * @param shell
 *            the shell which shell bounds is being calculated.
 * @param preferredSize
 *            the preferred position of the window.
 * @return a rectangle as close as possible to preferredSize that does not extend outside the monitor.
 */
public static Rectangle getConstrainedShellBounds(final Shell shell, final Point preferredSize) {
    final Point location = getInitialLocation(shell, preferredSize);
    final Rectangle result = new Rectangle(location.x, location.y, preferredSize.x, preferredSize.y);
    final Monitor monitor = getClosestMonitor(shell.getDisplay(), Geometry.centerPoint(result));
    final Rectangle bounds = monitor.getClientArea();
    if (result.height > bounds.height) {
        result.height = bounds.height;
    }
    if (result.width > bounds.width) {
        result.width = bounds.width;
    }
    result.x = Math.max(bounds.x, Math.min(result.x, bounds.x + bounds.width - result.width));
    result.y = Math.max(bounds.y, Math.min(result.y, bounds.y + bounds.height - result.height));
    return result;
}
Also used : Monitor(org.eclipse.swt.widgets.Monitor) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point)

Example 9 with Monitor

use of org.eclipse.swt.widgets.Monitor in project ServiceStack.Java by ServiceStack.

the class AddReferenceAction method run.

/**
 * @see IActionDelegate#run(IAction)
 */
public void run(IAction action) {
    if (_selection != null) {
        IStructuredSelection packageSelection = (IStructuredSelection) selectionService.getSelection("org.eclipse.jdt.ui.PackageExplorer");
        if (packageSelection == null) {
            ISelectionService selectionService = Workbench.getInstance().getActiveWorkbenchWindow().getSelectionService();
            packageSelection = (IStructuredSelection) selectionService.getSelection();
        }
        AddReferenceWizard generationWizard = new AddReferenceWizard(_selection, packageSelection);
        WizardDialog dialog = new WizardDialog(shell, generationWizard);
        dialog.setBlockOnOpen(false);
        Display display = shell.getDisplay();
        Monitor primaryMonitor = display.getPrimaryMonitor();
        Rectangle bounds = primaryMonitor.getBounds();
        dialog.open();
        Rectangle rect = dialog.getShell().getBounds();
        int x = bounds.x + (bounds.width - rect.width) / 2;
        int y = bounds.y + (bounds.height - rect.height) / 2;
        dialog.getShell().setLocation(x, y);
    }
}
Also used : Monitor(org.eclipse.swt.widgets.Monitor) Rectangle(org.eclipse.swt.graphics.Rectangle) ISelectionService(org.eclipse.ui.ISelectionService) AddReferenceWizard(net.servicestack.eclipse.wizard.AddReferenceWizard) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) WizardDialog(org.eclipse.jface.wizard.WizardDialog) Display(org.eclipse.swt.widgets.Display)

Example 10 with Monitor

use of org.eclipse.swt.widgets.Monitor in project uiautomatorviewer by yangzaiCN.

the class UiAutomatorViewer method adjustShellLocation.

private void adjustShellLocation() {
    Monitor m = findCurrentMonitor();
    if (m == null) {
        System.err.println("Cannot find current monitor!");
        return;
    }
    Rectangle r = m.getBounds();
    Rectangle b = getShell().getBounds();
    int x = b.x, y = b.y;
    boolean shouldChangePosition = false;
    if (!(r.x <= b.x && b.x + b.width < r.x + r.width)) {
        // out of bounds horizontally, need adjustment
        shouldChangePosition = true;
        // since we are scaling down, the window really shouldn't be larger than monitor
        // i.e. should not have negative here, just a safety measure
        x = Math.max(0, (r.width - b.width) / 2) + r.x;
    }
    if (!(r.y <= b.y && b.y + b.height < r.y + r.height)) {
        // out of bounds vertically, need adjustment
        shouldChangePosition = true;
        y = Math.max(0, (r.height - b.height) / 2) + r.y;
    }
    if (shouldChangePosition) {
        getShell().setLocation(x, y);
    }
}
Also used : Monitor(org.eclipse.swt.widgets.Monitor) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point)

Aggregations

Monitor (org.eclipse.swt.widgets.Monitor)20 Rectangle (org.eclipse.swt.graphics.Rectangle)15 Point (org.eclipse.swt.graphics.Point)12 Display (org.eclipse.swt.widgets.Display)6 Test (org.junit.Test)3 PropsUI (org.pentaho.di.ui.core.PropsUI)3 WindowProperty (org.pentaho.di.ui.core.gui.WindowProperty)3 AddReferenceWizard (net.servicestack.eclipse.wizard.AddReferenceWizard)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 Composite (org.eclipse.swt.widgets.Composite)1 Shell (org.eclipse.swt.widgets.Shell)1 ISelectionService (org.eclipse.ui.ISelectionService)1