Skip to main content

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

Thanks Stefan.

There is a lot of work done in a background thread before we need an UI. Thus, I’d like to skip processing at the earliest possible stage. With Display.asyncExec I can 
(i) check at the end (short before we need the UI) - but then the whole work is already done or 
(ii) check in the beginning (somewhere in between) but then the whole processing has to wait until we have an UI thread available. Since we may discard the log event due to some other filters, this may be unnecessary as well.

Just „knowing“ whether there is an active window from a background thread would make this processing pipe more clean.

Having said this: you approach would work but it’s not clear to me whether it is better. I was hoping for a way that allows me to omit any call to Display.(a)syncexec. If there is none, I’d currently prefer the listener approach - except someone points out that this is really a bad idea.

Thanks,
Marcel


Am 14.09.2015 um 05:39 schrieb Stefan Xenos <sxenos@xxxxxxxxx>:

Wouldn't Display.getActiveShell() != null tell you whether or not Eclipse has focus?

If you want to check this from a background thread, do this:

void backgroundJobWantsToCheckIfEclipseActive() {
  display.asyncExec(() -> {
    boolean isActive = display.getActiveShell() != null;
    
    Job.create(() -> {
      continueBackgroundJob(isActive);
    }).schedule();
  })
}

void continueBackgroundJob(boolean isEclipseActive) {
   ...
}

...but please, please, please don't use syncExec. The world doesn't need more deadlocks. :-)


On Sat, Sep 12, 2015 at 12:16 AM, Marcel Bruch <marcel.bruch@xxxxxxxxxxxxxx> wrote:
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

_______________________________________________
platform-ui-dev mailing list
platform-ui-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/platform-ui-dev

_______________________________________________
platform-ui-dev mailing list
platform-ui-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/platform-ui-dev

-- 
Codetrails GmbH
The knowledge transfer company

Robert-Bosch-Str. 7, 64293 Darmstadt
Phone: +49-6151-276-7092
Mobile: +49-179-131-7721
http://www.codetrails.com/

Managing Director: Dr. Marcel Bruch
Handelsregister: Darmstadt HRB 91940


Back to the top