|
35 | 35 | import java.util.List; |
36 | 36 | import java.util.Map; |
37 | 37 | import org.eclipse.core.expressions.ExpressionInfo; |
| 38 | +import org.eclipse.e4.core.commands.ExpressionContext; |
38 | 39 | import org.eclipse.e4.core.contexts.ContextInjectionFactory; |
39 | 40 | import org.eclipse.e4.core.contexts.IContextFunction; |
40 | 41 | import org.eclipse.e4.core.contexts.IEclipseContext; |
@@ -516,15 +517,7 @@ public boolean changed(IEclipseContext context) { |
516 | 517 |
|
517 | 518 | record.updateVisibility(parentContext.getActiveLeaf()); |
518 | 519 | runExternalCode(() -> { |
519 | | - manager.update(false); |
520 | | - getUpdater().updateContributionItems(e -> { |
521 | | - if (e instanceof MToolBarElement) { |
522 | | - if (((MUIElement) ((MToolBarElement) e).getParent()) == toolbarModel) { |
523 | | - return true; |
524 | | - } |
525 | | - } |
526 | | - return false; |
527 | | - }); |
| 520 | + updateToolbar(toolbarModel, manager); |
528 | 521 | }); |
529 | 522 | return true; |
530 | 523 | } |
@@ -627,9 +620,118 @@ public void processContents(MElementContainer<MUIElement> container) { |
627 | 620 | } |
628 | 621 | } |
629 | 622 |
|
| 623 | + // Set up visibility tracking for direct toolbar items with visibleWhen expressions |
| 624 | + final MToolBar toolbarModel = (MToolBar) obj; |
| 625 | + setupVisibilityTracking(toolbarModel, parentManager); |
| 626 | + |
630 | 627 | updateWidget(parentManager); |
631 | 628 | } |
632 | 629 |
|
| 630 | + /** |
| 631 | + * Set up visibility tracking for direct toolbar items (not from contributions) |
| 632 | + * that have visibleWhen expressions. |
| 633 | + */ |
| 634 | + private void setupVisibilityTracking(final MToolBar toolbarModel, final ToolBarManager manager) { |
| 635 | + // Check if any direct children have visibleWhen expressions |
| 636 | + List<MToolBarElement> itemsWithVisibility = new ArrayList<>(); |
| 637 | + for (MUIElement child : toolbarModel.getChildren()) { |
| 638 | + if (child instanceof MToolBarElement element) { |
| 639 | + if (requiresVisibilityCheck(element)) { |
| 640 | + itemsWithVisibility.add(element); |
| 641 | + } |
| 642 | + } |
| 643 | + } |
| 644 | + |
| 645 | + if (itemsWithVisibility.isEmpty()) { |
| 646 | + return; |
| 647 | + } |
| 648 | + |
| 649 | + // Collect all variable names that need to be tracked |
| 650 | + ExpressionInfo info = new ExpressionInfo(); |
| 651 | + for (MToolBarElement element : itemsWithVisibility) { |
| 652 | + ContributionsAnalyzer.collectInfo(info, element.getVisibleWhen()); |
| 653 | + String visibilityId = element.getPersistedState().get(MenuManagerRenderer.VISIBILITY_IDENTIFIER); |
| 654 | + if (visibilityId != null) { |
| 655 | + info.addVariableNameAccess(visibilityId); |
| 656 | + } |
| 657 | + } |
| 658 | + updateVariables.addAll(Arrays.asList(info.getAccessedVariableNames())); |
| 659 | + |
| 660 | + final IEclipseContext parentContext = getContext(toolbarModel); |
| 661 | + if (parentContext == null) { |
| 662 | + return; |
| 663 | + } |
| 664 | + |
| 665 | + parentContext.runAndTrack(new RunAndTrack() { |
| 666 | + @Override |
| 667 | + public boolean changed(IEclipseContext context) { |
| 668 | + if (getManager(toolbarModel) == null) { |
| 669 | + // tool bar no longer being managed, ignore it |
| 670 | + return false; |
| 671 | + } |
| 672 | + |
| 673 | + // Update visibility for all items with visibleWhen expressions |
| 674 | + ExpressionContext exprContext = new ExpressionContext(parentContext.getActiveLeaf()); |
| 675 | + boolean visibilityChanged = false; |
| 676 | + for (MToolBarElement element : itemsWithVisibility) { |
| 677 | + boolean newVisibility = computeElementVisibility(element, exprContext); |
| 678 | + if (element.isVisible() != newVisibility) { |
| 679 | + element.setVisible(newVisibility); |
| 680 | + visibilityChanged = true; |
| 681 | + } |
| 682 | + } |
| 683 | + |
| 684 | + if (visibilityChanged) { |
| 685 | + runExternalCode(() -> updateToolbar(toolbarModel, manager)); |
| 686 | + } |
| 687 | + return true; |
| 688 | + } |
| 689 | + }); |
| 690 | + } |
| 691 | + |
| 692 | + /** |
| 693 | + * Check if a toolbar element requires visibility checking |
| 694 | + */ |
| 695 | + private boolean requiresVisibilityCheck(MToolBarElement element) { |
| 696 | + return element.getVisibleWhen() != null |
| 697 | + || element.getPersistedState().get(MenuManagerRenderer.VISIBILITY_IDENTIFIER) != null; |
| 698 | + } |
| 699 | + |
| 700 | + /** |
| 701 | + * Compute visibility for a single toolbar element |
| 702 | + */ |
| 703 | + private boolean computeElementVisibility(MToolBarElement element, ExpressionContext exprContext) { |
| 704 | + boolean visible = true; |
| 705 | + |
| 706 | + // Check persisted state visibility identifier |
| 707 | + String identifier = element.getPersistedState().get(MenuManagerRenderer.VISIBILITY_IDENTIFIER); |
| 708 | + if (identifier != null) { |
| 709 | + Object rc = exprContext.eclipseContext.get(identifier); |
| 710 | + if (rc instanceof Boolean) { |
| 711 | + visible = ((Boolean) rc).booleanValue(); |
| 712 | + } |
| 713 | + } |
| 714 | + |
| 715 | + // Check visibleWhen expression |
| 716 | + if (visible && element.getVisibleWhen() != null) { |
| 717 | + visible = ContributionsAnalyzer.isVisible(element.getVisibleWhen(), exprContext); |
| 718 | + } |
| 719 | + |
| 720 | + return visible; |
| 721 | + } |
| 722 | + |
| 723 | + private void updateToolbar(final MToolBar toolbarModel, final ToolBarManager manager) { |
| 724 | + manager.update(false); |
| 725 | + getUpdater().updateContributionItems(e -> { |
| 726 | + if (e instanceof MToolBarElement) { |
| 727 | + if (((MUIElement) ((MToolBarElement) e).getParent()) == toolbarModel) { |
| 728 | + return true; |
| 729 | + } |
| 730 | + } |
| 731 | + return false; |
| 732 | + }); |
| 733 | + } |
| 734 | + |
633 | 735 | private void updateWidget(ToolBarManager manager) { |
634 | 736 | manager.update(true); |
635 | 737 | ToolBar toolbar = manager.getControl(); |
|
0 commit comments