Skip to content

Instantly share code, notes, and snippets.

@guw
Created March 15, 2012 13:35
Show Gist options
  • Save guw/2044231 to your computer and use it in GitHub Desktop.
Save guw/2044231 to your computer and use it in GitHub Desktop.
SWT Mouse Cursor Auto Hide
/**
* Copyright (c) 2012 Gunnar Wagenknecht 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:
* Gunnar Wagenknecht - initial API and implementation
*/
package net.ageto.labs.dashboard.ui;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.progress.UIJob;
/**
* A job which runs periodically and hides the mouse cursor if it hasn't been
* moved for a while.
* <p>
* Can be used as follows:
*
* <pre>
* final Display display = PlatformUI.createDisplay();
* try {
* final MouseCursorAutoHider cursorHider = new MouseCursorAutoHider(display, 5000L);
* try {
* final int returnCode = PlatformUI.createAndRunWorkbench(display, new DashboardWorkbenchAdvisor());
* if (returnCode == PlatformUI.RETURN_RESTART) {
* return IApplication.EXIT_RESTART;
* }
* return IApplication.EXIT_OK;
* } finally {
* cursorHider.dispose();
* }
* } finally {
* display.dispose();
* }
* </pre>
*
* </p>
*/
public final class MouseCursorAutoHider extends UIJob {
private final long timeout;
private long lastMouseActivity;
private Cursor transparentMouseCursor;
private boolean hidden;
private final Listener listener = new Listener() {
@Override
public void handleEvent(final Event event) {
mouseMovement();
}
};
public MouseCursorAutoHider(final Display display, final long timeout) {
super(display, "Mouse Cursor Auto Hide");
this.timeout = Math.max(timeout, 5000L);
setSystem(true);
setPriority(Job.INTERACTIVE);
activate();
}
private void activate() {
// get display
final Display display = getDisplay();
// create transparent cursor
final PaletteData palette = new PaletteData(new RGB[] { display.getSystemColor(SWT.COLOR_WHITE).getRGB(), display.getSystemColor(SWT.COLOR_BLACK).getRGB() });
final ImageData sourceData = new ImageData(16, 16, 1, palette);
sourceData.transparentPixel = 0;
transparentMouseCursor = new Cursor(display, sourceData, 0, 0);
display.addFilter(SWT.MouseMove, listener);
schedule(timeout);
}
public void dispose() {
try {
if (null != transparentMouseCursor) {
transparentMouseCursor.dispose();
}
} finally {
transparentMouseCursor = null;
getDisplay().removeFilter(SWT.MouseMove, listener);
}
}
void hideCursor() {
if (hidden) {
return;
}
final Shell shell = getDisplay().getActiveShell();
if (shell == null) {
return;
}
final Cursor cursor = transparentMouseCursor;
if ((null != cursor) && !cursor.isDisposed()) {
shell.setCursor(cursor);
hidden = true;
}
}
void mouseMovement() {
lastMouseActivity = System.currentTimeMillis();
// show cursor if hidden
if (!hidden) {
return;
}
final Shell shell = getDisplay().getActiveShell();
if (null != shell.getCursor()) {
shell.setCursor(null);
hidden = false;
}
}
@Override
public IStatus runInUIThread(final IProgressMonitor monitor) {
if (null == transparentMouseCursor) {
return Status.CANCEL_STATUS;
}
final long timeWithoutMovement = System.currentTimeMillis() - lastMouseActivity;
if (timeWithoutMovement > timeout) {
hideCursor();
}
schedule(Math.max(1000L, timeout - timeWithoutMovement));
return Status.OK_STATUS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment