Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-ui-dev] Knowing whether Eclipse has focus

Hi platform-ui,

I’m looking for a way to check/know whether Eclipse (any of its windows or dialogs) has currently OS focus. I’d like to do that check in a background thread, which means I’d need a way to track the current focus. My current tracking approach looks as follows: I register a listener on the workbench’s display that responds to (all) activate and deactivate events:


    private static final class ShellTracker implements Runnable, Listener {
        @Override
        public void run() {
            IWorkbench workbench = PlatformUI.getWorkbench();

            Display display = workbench.getDisplay();
            display.addFilter(SWT.Deactivate, this);
            display.addFilter(SWT.Activate, this);

            IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
            if (activeWindow != null) {
                active = activeWindow.getShell();
            }
        }

        @Override
        public void handleEvent(Event event) {
            switch (event.type) {
            case SWT.Activate:
                if (isShell(event)) {
                    active = (Shell) event.widget;
                }
                break;
            case SWT.Deactivate:
                if (isShell(event)) {
                    active = null;
                }
            default:
                break;
            }
        }

        private boolean isShell(Event event) {
            return !(event.widget instanceof Shell);
        }
    }



My concern is that this may cause (performance) regressions. Thus, I’m seeking your advice. 
Is there a better way to testing whether Eclipse has OS focus?
Is this listener potentially causing a (notable) performance regression?

Thanks,
Marcel

Back to the top