Skip to content

Commit 380a716

Browse files
committed
Ensure active part receives focus on perspective switch
When switching a perspective, if a view exists in both the old and the new perspective, it doesn't gain focus after the perspective switch. This is contrary to the case in which a view is open only in the new perspective. Since editors are shared between perspectives, the same bug always occurs for editors - the editor will lose focus on a perspective switch. This change adds part focusing code on the code branch, on which the active part already exists in the new perspective. This ensures the active part gains focus. Note that this change doesn't preserve focus of the active part in each perspective. I.e. if View A is active in perspective X and View B was active in perspective Y, upon switching to perspective Y, View A will become active if it exists. The change only ensures that focus is not lost altogether. Fixes: #2839 Signed-off-by: Simeon Andreev <[email protected]>
1 parent edbc549 commit 380a716

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/PartServiceImpl.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ private MPart toPart(MStackElement stackElement) {
192192

193193
private boolean constructed = false;
194194

195+
private boolean switchingPerspective = false;
196+
197+
private boolean focusingActivePart = false;
198+
195199
@Inject
196200
public PartServiceImpl(MApplication application, @Optional MWindow window) {
197201
// no need to track changes:
@@ -604,6 +608,15 @@ public boolean isPartOrPlaceholderInPerspective(String elementId, MPerspective p
604608

605609
@Override
606610
public void switchPerspective(MPerspective perspective) {
611+
try {
612+
switchingPerspective = true;
613+
switchPerspectiveInternal(perspective);
614+
} finally {
615+
switchingPerspective = false;
616+
}
617+
}
618+
619+
private void switchPerspectiveInternal(MPerspective perspective) {
607620
Assert.isNotNull(perspective);
608621
MWindow window = getWindow();
609622
if (window != null && isInContainer(window, perspective)) {
@@ -687,6 +700,11 @@ public void activate(MPart part, boolean requiresFocus) {
687700
}
688701

689702
private void activate(MPart part, boolean requiresFocus, boolean activateBranch) {
703+
if (focusingActivePart) {
704+
Activator.trace(Policy.DEBUG_FOCUS_FLAG,
705+
"Active part is being focused on perspective switch: " + activePart, null);//$NON-NLS-1$
706+
return;
707+
}
690708
if (part == null) {
691709
if (constructed && activePart != null) {
692710
if (Policy.DEBUG_FOCUS) {
@@ -727,12 +745,23 @@ private void activate(MPart part, boolean requiresFocus, boolean activateBranch)
727745
IEclipseContext windowContext = window.getContext();
728746
// check if the active part has changed or if we are no longer the active window
729747
if (windowContext.getParent().getActiveChild() == windowContext && part == activePart) {
730-
// insert it in the beginning of the activation history, it may not have been inserted
731-
// pending when this service was instantiated
732-
partActivationHistory.prepend(part);
733-
UIEvents.publishEvent(UIEvents.UILifeCycle.ACTIVATE, part);
734-
if (Policy.DEBUG_FOCUS) {
735-
Activator.trace(Policy.DEBUG_FOCUS_FLAG, "Trying to activate already active part: " + part, null);//$NON-NLS-1$
748+
if (!focusingActivePart) {
749+
// insert it in the beginning of the activation history, it may not have been
750+
// inserted
751+
// pending when this service was instantiated
752+
partActivationHistory.prepend(part);
753+
if (switchingPerspective && requiresFocus) {
754+
focusingActivePart = true;
755+
try {
756+
focusPart(part);
757+
} finally {
758+
focusingActivePart = false;
759+
}
760+
}
761+
UIEvents.publishEvent(UIEvents.UILifeCycle.ACTIVATE, part);
762+
if (Policy.DEBUG_FOCUS) {
763+
Activator.trace(Policy.DEBUG_FOCUS_FLAG, "Trying to activate already active part: " + part, null);//$NON-NLS-1$
764+
}
736765
}
737766
return;
738767
}
@@ -1510,7 +1539,7 @@ private MElementContainer<? extends MUIElement> getContainer() {
15101539
return outerContainer;
15111540
}
15121541

1513-
private static void focusPart(MPart part) {
1542+
private void focusPart(MPart part) {
15141543
IEclipseContext context = part.getContext();
15151544
if (context != null) {
15161545
IPresentationEngine pe = context.get(IPresentationEngine.class);

0 commit comments

Comments
 (0)