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.

  1. Modify the instantiation of the Shell inside the widgetSelected method in the Activator inner class to add SWT.NO_TRIM (no close/minimise/maximise widgets) and SWT.ON_TOP (floating on top of other windows):
    shell = new Shell(trayItem.getDisplay(), SWT.NO_TRIM | SWT.ON_TOP);
  2. Set the alpha value as 128, which is semi-transparent:
    shell.setAlpha(128);
  3. Run the target Eclipse instance, and click on the tray item to see what kind of window is created.
  4. 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);
  5. To make the application full-screen, call either setFullScreen or the setMaximized 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.

  6. Run the target Eclipse application and see the effect these flags have on the window.
  7. Change the Shell back to use SWT.NO_TRIM and SWT.ON_TOP.
  8. To calculate a circular shape for the floating clock window, add the circle method to the Activator class, which has been taken from SWT's Snippet134.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;
    }
  9. Finally, change the shape of the window to be circular by setting the Region of the Shell. This will have the effect of making it look as if the clock itself is floating. Add the following code after the Shell is created in the widgetSelected method:
    final Region region = new Region();
    region.add(circle(25, 25, 25));
    shell.setRegion(region);
  10. When run, the clock will look something like this:
    Time for action – modal and other effects
  11. For completeness, register a dispose listener on the shell to ensure that the Region 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.144.69