Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public class MenuManagerRenderer extends SWTPartRenderer {

private final Collection<IContributionManager> mgrToUpdate = new LinkedHashSet<>();

/** Managers to update once the outermost processContributions call ends, null outside of it. */
private Collection<IContributionManager> deferredUpdates;

@Inject
private Logger logger;

Expand Down Expand Up @@ -475,13 +478,33 @@ public void processContributions(MMenu menuModel, String elementId, boolean isMe
if (elementId == null) {
return;
}
if (deferredUpdates != null) {
doProcessContributions(menuModel, elementId, isMenuBar, isPopup);
return;
}
Collection<IContributionManager> managers = new LinkedHashSet<>();
deferredUpdates = managers;
try {
doProcessContributions(menuModel, elementId, isMenuBar, isPopup);
} finally {
deferredUpdates = null;
// menus created by these updates fill themselves when first shown
List<IContributionManager> existing = managers.stream()
.filter(m -> !(m instanceof MenuManager mm) || mm.getMenu() != null).toList();
for (IContributionManager mgr : existing) {
scheduleManagerUpdate(mgr);
}
}
}

private void doProcessContributions(MMenu menuModel, String elementId, boolean isMenuBar, boolean isPopup) {
final ArrayList<MMenuContribution> toContribute = new ArrayList<>();
ContributionsAnalyzer.XXXgatherMenuContributions(menuModel, application.getMenuContributions(), elementId,
toContribute, isPopup);
generateContributions(menuModel, toContribute, isMenuBar);
for (MMenuElement element : menuModel.getChildren()) {
if (element instanceof MMenu) {
processContributions((MMenu) element, element.getElementId(), false, isPopup);
if (element instanceof MMenu && element.getElementId() != null) {
doProcessContributions((MMenu) element, element.getElementId(), false, isPopup);
}
}
}
Expand Down Expand Up @@ -1142,6 +1165,10 @@ private void unlinkMenu(MMenu menu) {
}

private void scheduleManagerUpdate(IContributionManager mgr) {
if (deferredUpdates != null) {
deferredUpdates.add(mgr);
return;
}
// Bug 467000: Avoid repeatedly updating menu managers
// This workaround is opt-in for 4.5
boolean workaroundEnabled = Boolean.getBoolean("eclipse.workaround.bug467000"); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.e4.ui.model.application.ui.basic.MTrimmedWindow;
import org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem;
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuContribution;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
import org.eclipse.e4.ui.tests.rules.WorkbenchContextExtension;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
Expand Down Expand Up @@ -141,6 +142,46 @@ public void testMMenu_ToBeRendered() {
assertTrue(mm.getItems()[0].isVisible());
}

@Test
public void testMenuBarRebuildsDoNotGrowWithContributions() {
contextRule.createAndRunWorkbench(window);

assertEquals(renderMenuBarWithContributions(1), renderMenuBarWithContributions(20));
}

private int renderMenuBarWithContributions(int count) {
String menuId = "test.menubar." + count;
MTrimmedWindow window2 = ems.createModelElement(MTrimmedWindow.class);
MMenu menuBar = ems.createModelElement(MMenu.class);
menuBar.setElementId(menuId);
window2.setMainMenu(menuBar);
for (int i = 0; i < count; i++) {
MMenuContribution contribution = ems.createModelElement(MMenuContribution.class);
contribution.setParentId(menuId);
MMenuItem item = ems.createModelElement(MDirectMenuItem.class);
item.setElementId(menuId + ".item" + i);
item.setLabel("item" + i);
contribution.getChildren().add(item);
application.getMenuContributions().add(contribution);
}
int[] rebuilds = new int[1];
MenuManager manager = new MenuManager("", menuId) {
@Override
public void update(boolean force) {
if (force || isDirty()) {
rebuilds[0]++;
}
super.update(force);
}
};
getMenuManagerRenderer().linkModelToManager(menuBar, manager);
application.getChildren().add(window2);

assertEquals(count, manager.getSize());
assertTrue(rebuilds[0] > 0);
return rebuilds[0];
}

private MenuManagerRenderer getMenuManagerRenderer() {
Object renderer = menu.getRenderer();
assertEquals(MenuManagerRenderer.class, renderer.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
Bug264804Test.class,
MenuHelperTest.class,
Bug410426Test.class,
PopupMenuDynamicFillTest.class,
})
public class MenusTestSuite {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*******************************************************************************
* Copyright (c) 2026 vogella GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Lars Vogel - initial API and implementation
******************************************************************************/
package org.eclipse.ui.tests.menus;

import static org.eclipse.ui.tests.harness.util.UITestUtil.processEvents;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.actions.CompoundContributionItem;
import org.eclipse.ui.menus.AbstractContributionFactory;
import org.eclipse.ui.menus.IContributionRoot;
import org.eclipse.ui.menus.IMenuService;
import org.eclipse.ui.services.IServiceLocator;
import org.eclipse.ui.tests.api.ListView;
import org.eclipse.ui.tests.harness.util.CloseTestWindowsExtension;
import org.eclipse.ui.tests.harness.util.UITestUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* Tests that opening a context menu fills its dynamic items a fixed number of
* times, independent of the number of menu contributions (bug 467000).
*/
@ExtendWith(CloseTestWindowsExtension.class)
public class PopupMenuDynamicFillTest {

private static final String VIEW_ID = "org.eclipse.ui.tests.internal.ActionExpressionTest";

private static class CountingDynamicItem extends CompoundContributionItem {
int fills;

@Override
protected IContributionItem[] getContributionItems() {
fills++;
return new IContributionItem[] { new ActionContributionItem(new Action("dynamic " + fills) {
}) };
}
}

@Test
public void testDynamicFillsDoNotGrowWithContributions() throws Exception {
IWorkbenchWindow window = UITestUtil.openTestWindow();
assertEquals(fillsPerOpening(window, 1), fillsPerOpening(window, 20));
}

private int fillsPerOpening(IWorkbenchWindow window, int contributions) throws PartInitException {
IWorkbenchPage page = window.getActivePage();
IMenuService menuService = window.getService(IMenuService.class);
CountingDynamicItem dynamic = new CountingDynamicItem();
List<AbstractContributionFactory> factories = new ArrayList<>();
factories.add(factory(dynamic));
for (int i = 0; i < contributions; i++) {
factories.add(factory(new ActionContributionItem(new Action("static " + i) {
})));
}
factories.forEach(menuService::addContributionFactory);
try {
ListView view = (ListView) page.showView(VIEW_ID);
processEvents();
Menu menu = view.getMenuManager().getMenu();
int before = dynamic.fills;
menu.notifyListeners(SWT.Show, new Event());
processEvents();
int fills = dynamic.fills - before;
menu.notifyListeners(SWT.Hide, new Event());
processEvents();
page.hideView(view);
return fills;
} finally {
factories.forEach(menuService::removeContributionFactory);
}
}

private static AbstractContributionFactory factory(IContributionItem item) {
return new AbstractContributionFactory("popup:" + VIEW_ID, "org.eclipse.ui.tests") {
@Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
additions.addContributionItem(item, null);
}
};
}
}
Loading