1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*******************************************************************************
* Copyright (c) 2011 Frank Becker and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Frank Becker - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.internal.commons.ui;
import org.eclipse.jface.util.Geometry;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
/**
* This class copies the code from org.eclipse.jface.window because we can not access the protected method
* getConstrainedShellBounds. getConstrainedShellBounds has now the window as the first parameter.
*
* @author Frank Becker
*/
public class WindowUtil {
/**
* 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. If this is ever made public, it should be moved into a separate utility
* class.
*
* @param toSearch
* point to find (display coordinates)
* @param toFind
* point to find (display coordinates)
* @return the montor closest to the given point
*/
private static Monitor getClosestMonitor(Display toSearch, Point toFind) {
int closest = Integer.MAX_VALUE;
Monitor[] monitors = toSearch.getMonitors();
Monitor result = monitors[0];
for (Monitor current : monitors) {
Rectangle clientArea = current.getClientArea();
if (clientArea.contains(toFind)) {
return current;
}
int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), toFind);
if (distance < closest) {
closest = distance;
result = current;
}
}
return result;
}
/**
* Given the desired position of the window, 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, and subclasses can use this as a utility method if they want to limit the region in
* which the window may be moved.
*
* @param window
* the window
* @param preferredSize
* the preferred position of the window
* @return a rectangle as close as possible to preferredSize that does not extend outside the monitor
* @see Window#getConstrainedShellBounds
*/
public static Rectangle getConstrainedShellBounds(Window window, Rectangle preferredSize) {
Rectangle result = new Rectangle(preferredSize.x, preferredSize.y, preferredSize.width, preferredSize.height);
Monitor mon = getClosestMonitor(window.getShell().getDisplay(), Geometry.centerPoint(result));
Rectangle bounds = mon.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;
}
}
|