Time for action – modal and other effects
There are a number of style bits that are applicable to windows, and some useful methods to affect how the window appears. For example, it might be desirable to make the clock appear semi-transparent, which allows the clock to float above other windows. SWT's Shell
has a number of these options that can be set.
- Modify the instantiation of the
Shell
inside thewidgetSelected
method in theActivator
inner class to addSWT.NO_TRIM
(noclose/minimise/maximise widgets
) andSWT.ON_TOP
(floating on top of other windows):shell = new Shell(trayItem.getDisplay(), SWT.NO_TRIM | SWT.ON_TOP);
- Set the
alpha
value as128
, which is semi-transparent:shell.setAlpha(128);
- Run the target Eclipse instance, and click on the
tray
item to see what kind of window is created. - To create a modal window (and thus, prevent interaction on the main window), change the flag to use
SWT.APPLICATION_MODAL
:shell = new Shell(trayItem.getDisplay(), SWT.APPLICATION_MODAL);
- To make the application full-screen, call either
setFullScreen
or thesetMaximized
depending on the platform:shell.setFullScreen(true); shell.setMaximized(true);
Note
Note that without trims, it may be necessary to add controls such as detecting selection events to close the window.
- Run the target Eclipse application and see the effect these flags have on the window.
- Change the
Shell
back to useSWT.NO_TRIM
andSWT.ON_TOP
. - To calculate a circular shape for the floating clock window, add the circle method to the
Activator
class, which has been taken from SWT'sSnippet134.java
, from http://www.eclipse.org/swt/snippets/:private static int[] circle(int r, int offsetX, int offsetY) { int[] polygon = new int[8 * r + 4]; // x^2 + y^2 = r^2 for (int i = 0; i < 2 * r + 1; i++) { int x = i – r; int y = (int)Math.sqrt(r*r – x*x); polygon[2*i] = offsetX + x; polygon[2*i+1] = offsetY + y; polygon[8*r - 2*i - 2] = offsetX + x; polygon[8*r - 2*i - 1] = offsetY – y; } return polygon; }
- Finally, change the shape of the window to be circular by setting the
Region
of theShell
. This will have the effect of making it look as if the clock itself is floating. Add the following code after theShell
is created in thewidgetSelected
method:final Region region = new Region(); region.add(circle(25, 25, 25)); shell.setRegion(region);
- When run, the clock will look something like this:
- For completeness, register a
dispose
listener on the shell to ensure that theRegion
is cleaned up:shell.addDisposeListener(event -> region.dispose());
What just happened?
Changing the flags affects how that window is displayed and interacts with the user. Other calls on the shell can programmatically drive transitions to full-screen and maximized or minimized status, which can be useful in specific circumstances. Some windowing systems do not differentiate maximized and full-screen; others have distinct properties.
The SWT.NO_TRIM
flag is used to display a window without the normal window furniture. This can be combined with setting a region via setRegion
, which allows creation of a non-rectangular shape.
Often, windows without trim are floating—that is, they stay on top of the application window even when it hasn't got the focus. To achieve this, set the SWT.ON_TOP
flag as well, and adjust the alpha (transparency) value with setAlpha
. The alpha
value is between 0
(fully transparent) and 255
(fully opaque).
A Region
can be defined from a set of connected points, or set on a pixel-by-pixel basis. It's important to note that a Region
is also a Resource
, and thus it must be disposed
after use (which is typically when the Shell
is closed). The cleanup operation is similar to that of the others previously mentioned, via the addDisposeListener
on the Shell
.