1 /*******************************************************************************
2 * Copyright (c) 2021 Thomas Wolf <thomas.wolf@paranor.ch> and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * Thomas Wolf - factored out of Activator
13 *******************************************************************************/
14 package org
.eclipse
.egit
.ui
.internal
;
16 import org
.eclipse
.e4
.core
.services
.events
.IEventBroker
;
17 import org
.eclipse
.e4
.ui
.workbench
.UIEvents
;
18 import org
.eclipse
.swt
.SWTException
;
19 import org
.eclipse
.swt
.widgets
.Display
;
20 import org
.eclipse
.ui
.IWindowListener
;
21 import org
.eclipse
.ui
.IWorkbenchWindow
;
22 import org
.eclipse
.ui
.PlatformUI
;
23 import org
.osgi
.service
.component
.annotations
.Component
;
24 import org
.osgi
.service
.component
.annotations
.Deactivate
;
25 import org
.osgi
.service
.event
.Event
;
26 import org
.osgi
.service
.event
.EventConstants
;
27 import org
.osgi
.service
.event
.EventHandler
;
30 * Determines whether any shell of the application is active and fires an event
31 * when the value changes.
33 @Component(property
= {
34 EventConstants
.EVENT_TOPIC
+ '='
35 + UIEvents
.UILifeCycle
.APP_STARTUP_COMPLETE
,
36 EventConstants
.EVENT_TOPIC
+ '='
37 + UIEvents
.UILifeCycle
.APP_SHUTDOWN_STARTED
})
38 public class ApplicationActiveListener
implements EventHandler
{
41 * Event topic for the events posted by this component.
43 public static final String TOPIC_APPLICATION_ACTIVE
= "org/eclipse/egit/ui/APPLICATION_ACTIVE"; //$NON-NLS-1$
45 private volatile WindowTracker listener
;
48 public void handleEvent(Event event
) {
49 String topic
= event
.getTopic();
54 case UIEvents
.UILifeCycle
.APP_STARTUP_COMPLETE
:
55 if (listener
== null) {
56 listener
= new WindowTracker();
58 PlatformUI
.getWorkbench().addWindowListener(listener
);
61 case UIEvents
.UILifeCycle
.APP_SHUTDOWN_STARTED
:
71 if (listener
!= null) {
72 PlatformUI
.getWorkbench().removeWindowListener(listener
);
77 private static class WindowTracker
implements IWindowListener
{
79 private boolean isActive
;
82 if (PlatformUI
.isWorkbenchRunning()) {
83 Display display
= PlatformUI
.getWorkbench().getDisplay();
84 if (display
!= null && !display
.isDisposed()) {
86 display
.asyncExec(() -> {
87 boolean wasActive
= isActive
;
88 isActive
= !display
.isDisposed()
89 && display
.getActiveShell() != null;
90 if (wasActive
!= isActive
) {
94 } catch (SWTException e
) {
95 // Silently ignore -- display was disposed already
101 private void notify(boolean active
) {
102 if (PlatformUI
.isWorkbenchRunning()) {
103 IEventBroker broker
= PlatformUI
.getWorkbench()
104 .getService(IEventBroker
.class);
105 if (broker
!= null) {
106 broker
.post(TOPIC_APPLICATION_ACTIVE
,
107 Boolean
.valueOf(active
));
113 public void windowActivated(IWorkbenchWindow window
) {
118 public void windowDeactivated(IWorkbenchWindow window
) {
123 public void windowClosed(IWorkbenchWindow window
) {
128 public void windowOpened(IWorkbenchWindow window
) {